in reply to Calling Subroutines of package from another program?

I will attempt to explain how to put some generally useful function into a "module" so that you can call it from other programs.

Best way, is by example:
Make a file called TEST_GIZMO.pm and stick this code into it:

#!/usr/bin/perl -w use strict; package TEST_GIZMO; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.00; our @ISA = qw(Exporter); our @EXPORT = qw(test1 test2); our @EXPORT_OK = qw(); #some avanced stuff but it is possible #to only export a subset of functions by #default and allow others to be imported #"by request". sub test1 { print "test1 in TEST GIZMO worked!\n"; } sub test2 { print "test2 in TEST GIZMO worked!\n"; } 1; ### this is obscure stuff, but you need this line !!!! ### by default Perl returns the value of the last line in a ### sub or package. In this case, it means that this package ### inclusion "worked". In the C world or command line, ### shell world, 0, zero means "it worked", non-zero is ### an error code, but not in THIS case of Perl. "1" is the ### right return value here! Any way trust me, you need a ### 1 return value at the end of a .pm module.
Now make a file called test_module.pl and stick this code into it:
#!/usr/bin/perl -w use strict; use TEST_GIZMO; test1; test2; TEST_GIZMO::test1; #this is "fully qualified name", a way to #invoke test1 even if that name wasn't #explicitly exported.
When you run test_module.pl, you should see:
test1 in TEST GIZMO worked!
test2 in TEST GIZMO worked!
test1 in TEST GIZMO worked!

I hope that this is enough "boiler plate" for you to create your own library functions. There is of course a lot to this subject, but I hope I got you started!

Replies are listed 'Best First'.
Re^2: Calling Subroutines of package from another program?
by anbutechie (Sexton) on Mar 11, 2009 at 05:45 UTC
    Thank you very much... It is working. But, should the script name (TEST_GIZMO.pm) and package name (TEST_GIZMO) be same?
    Regards,
    Anbarasu
      Glad to hear that you got test code working. You are now well on your way!

      Yes, putting package TEST_GIZMO inside of file TEST_GIZMO.pm would be the normal way. It is possible to have multiple packages within one file. The package is a name space. For what you are doing, that isn't necessary and can get confusing.

      I would recommend that you put some sort of test function within the package. In one of my libraries, I just put sub test{} in every .pm file and of course didn't export the name "test" as every module has a test sub and the names would conflict. Then to test my library, I just call with the fully qualified name TEST_GIZMO::test; ModuleB::test;, etc. on all the .pm files. It is really handy to have the name of the file the same as package inside.

      You will see that the .pm file can be run just like a .pl file. So you can have some sort of thing like "test if DEBUG"; DEBUG is a const flag. Anyway the idea is to get the package working with its own test routine before you start calling it from other programs.

      You should consider your naming convention for your packages. You don't want to use names that might conflict with CPAN packages, etc. Say you've got some project TLA, then you could use TLA_Parse for say the parsing routines for project TLA. You'll have to decide what makes sense for whatever it is that you are doing.