Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with perl script..... 2

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
0
0
US
Hi,

I have perl script which contains all functions. Now how can i call these functions from another script. Do i need to install the first script as a package. If so can anyone tell me how i can do it.

Thanks in advance
 
Have a look at the require statement.

To memory, haven't looked at it in a while, if you put a 1 at the end of the (2 B included) file, and require the file, the functions and variables should be accessible.

The package idea is proabably well worthwhile looking at

--Paul
 
firstly make a package

Code:
package package1;

BEGIN { }

sub subroutine1 {print "Hello!\n"}
return 1;

END { }

then you need to reference it from your Perl script

Code:
require "package1.pl";

package1::subroutine1();


Kind Regards
Duncan
 
Sorry if i am asking a simple question. What is the command for installing the below script as package in perl.

package package1;

BEGIN { }

sub subroutine1 {print "Hello!\n"}
sub subroutne2 {subroutine1()}
return 1;

END { }

 
Or use the following solution:

- Construct the package. The package file (named my_package.pm):

use Exporter;

package my_package;
@ISA = qw(Exporter);

# export the subroutines hello1 and hello2

@EXPORT = qw(hello1 hello2);
sub hello1 {
...
}

sub hello2 {
...
}
....
1;

- Use the package. This is your script where you use the subs hello1 and hello2 defined in my_package package (script.pl)

use my_package;
hello1();
hello2();

Hope it makes sense.

Regards,
Adrian
 
Simply call the file package.pm and put it in a path where the script can access it.

Could be the same path as the perl script for example.
 
How to install the package you have constructed ?
The better way when constructing a new package is to use h2xs tool, because it creates the schelet for you. It also create a Makefile.PL template script, which you can use to install the package.

Let say you want to construct a package called my_package.

From the shell (or command promp) issue the following:

%>h2xs -AXn my_package

This will create a directory called my_package. Inside it will create the following files:

Changes MANIFEST Makefile.PL README my_package.pm test.pl

Edit the my_package.pm file, to your package. Follow comments inside the package. Edit the Makefile.PL file as you need.
From the command prompt issue:
%> perl Makefile.PL
A makefile will be generated for your platform.

NOW HOW TO INSTALL THE PACKAGE?

Like any package from CPAN

make
make test
make install

Now you really have installed the package. From now, you do not have to worry about the path to the package file, when using the package from your scripts.

Adrian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top