in reply to converting libraries into modules
Old:
[mylibrary1.pl] sub half { my $number = shift; return $number / 2; }
New:
[mylibrary1.pm] package mylibrary1; use base 'Exporter'; use vars qw( @EXPORT @EXPORT_OK ); @EXPORT = qw( half ); # These are the ones that are imported if nothin +g is specified. @EXPORT_OK = qw( half ); # These are the ones you may specify, if you +want. sub half { my $number = shift; return $number / 2; } 1; # All .pm files must end with a true value __END__
Now, when you use them, you'll do it like this:
Old way:
[myscript.pl] require 'mylibrary1.pl'; print half( 10 ), "\n";
New way:
[myscript.pl] use mylibrary1 qw( half ); print half( 10 ), "\n";
Now, the different between @EXPORT and @EXPORT_OK is how you construct the 'use' line. Here's a few examples:
use Foo; # Imports everything in @EXPORT, nothing in @EXPORT_OK use Foo 'bar'; # Imports 'bar' if it's in @EXPORT_OK. Imports nothing +from @EXPORT use Foo (); # Imports NOTHING from either.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: converting libraries into modules
by Courage (Parson) on Mar 05, 2006 at 18:00 UTC | |
by dragonchild (Archbishop) on Mar 05, 2006 at 18:47 UTC | |
by Courage (Parson) on Mar 06, 2006 at 08:39 UTC |