Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: converting libraries into modules

by dragonchild (Archbishop)
on Mar 05, 2006 at 02:30 UTC ( [id://534550]=note: print w/replies, xml ) Need Help??


in reply to converting libraries into modules

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:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: converting libraries into modules
by Courage (Parson) on Mar 05, 2006 at 18:00 UTC
    although using Exporter is suggested by, say, h2xs, I think using Exporter isn't a good habbit.
    Its not good to pollute namespace.

    Using package::utilname(...); $utlobject->utl_method(...); is flexible enough and no real need to save few keystrokes by not pointing package name, and risking to have naming conflict later....

    Also, extra dependency on Exporter, and many extra lines in your template to introduce Exporter, do not worth the effort.

    Best regards,
    Courage, the Cowardly Dog

      While I agree with you from a purity perspective, Exporter exists specifically to make the transition between P4 repositories and P5 libraries easier. In other words, Exporter is the exact right tool in this situation.

      Now, if the OP wants, we can educate him on better software practices later. But, for now, this solves the OP's problem to a 't'.


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        I agree, although I do not know what ...solves the OP's problem to a 't'. means :):)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://534550]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found