in reply to Calling Subroutines of package from another program?
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.
When you run test_module.pl, you should see:#!/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.
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 | |
by Marshall (Canon) on Mar 11, 2009 at 18:36 UTC |