
What goes in the module itself doesn't require much explanation. I'll just give a complete example of a simple NameOfTheProject::SomeModule:.use strict; package NameOfTheProject::SomeModule; sub some_function { # put sane code here return 123; } 1;
You can have the module export its sub(s) to the caller's namespace (that is: the package the use statement is in. By default, this is main). For this, put after the package statement:
Then, when useing the module, you can request that some_function be imported to your namespace with:Then, when useing the module, you can request that some_function be imported to your namespace with:use base 'Exporter'; our @EXPORT_OK = ('some_function');
To have it exported automatically, use @EXPORT instead of @EXPORT_OK. This will eventually bite you if the function names are generic. For example, many people get bitten by LWP::Simple's get function. It is not unlikely that you already have one.To have it exported automatically, use @EXPORT instead of @EXPORT_OK. This will eventually bite you if the function names are generic. For example, many people get bitten by LWP::Simple's get function. It is not unlikely that you already have one.use NameOfTheProject::SomeModule ('some_function'); print some_function(); # prints 123:
(The last , is optional, but it's included to make adding a line easier.)(The last , is optional, but it's included to make adding a line easier.)# This is config.pl mirror => '', path => '/var/www/example', skip_files => [ 'png', 'gif', 'jpg' ],
Error checking is left as an excercise.Error checking is left as an excercise.# This is script.pl use strict; my %config = do 'config.pl'; chdir $config{path}; ...:
Documentation for the #line directive is in perlsyn.Documentation for the #line directive is in perlsyn;
To test this new module, save it as Acme/Include.pm and create:
and:and:# This is foo.pl use strict; use Acme::Include; my $lexical = 'set in foo.pl'; include 'bar.pl'; print $lexical; # Should print: set in bar.pl
and then run perl foo.pl.and then run perl foo.pl.# This is bar.pl use strict; $lexical = 'set in bar.pl'; # There is no "my" here, because that would create a *new* lexical # variable, hiding the existing one. :).