Last active
December 24, 2015 08:59
-
-
Save nanusdad/6773918 to your computer and use it in GitHub Desktop.
Creating a simple Perl module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use a module. Check out perldoc perlmod and Exporter. | |
In file Foo.pm | |
package Foo; | |
use strict; | |
use warnings; | |
use Exporter; | |
our @ISA= qw( Exporter ); | |
# these CAN be exported. | |
our @EXPORT_OK = qw( export_me export_me_too ); | |
# these are exported by default. | |
our @EXPORT = qw( export_me ); | |
sub export_me { | |
# stuff | |
} | |
sub export_me_too { | |
# stuff | |
} | |
1; | |
In your main program: | |
use strict; | |
use warnings; | |
use Foo; # import default list of items. | |
export_me( 1 ); | |
Or to get both functions: | |
use strict; | |
use warnings; | |
use Foo qw( export_me export_me_too ); # import listed items | |
export_me( 1 ); | |
export_me_too( 1 ); | |
You can also import package variables, but the practice is strongly discouraged. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment