In Perl5, libraries and classes are implemented using the same mechanism (packages, usually in their own .pm files), but they're completely different. If you're converting P4-style libraries to P5-style, all you need to do is follow this template.
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.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.