#!/usr/bin/perl use strict; use warnings; package MyPkg; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(myfunc); sub myfunc{ print "bar"; } 1;
In this case, the file containing this code should be named MyPkg.pm, to match the name of the package. A bare-bones caller of this code could be as simple as:
#!/usr/bin/perl use strict; use warnings; use MyPkg qw{myfunc}; myfunc();
Some additional notes:
- In the module, require Exporter; is not always necessary (not yet clear the situation for its necessity)
- If your package is something like MyGroup::MySubGroup, two things to observe: (1) the name of your .pm file should be MySubGroup.pm and (2) the file should be placed in a subdirectory relative to @INC paths named MyGroup. For example, if you want to locate your re-usable module togehter with your Perl script, you would put it in a subdirectory of the directory with the Perl script named MyGroup
No comments:
Post a Comment