--- Aaron Craig <[EMAIL PROTECTED]> wrote:
> I always that that modules were the domain of real Perl gurus, so I
> did a lot of requiring in my first Perl scripts.  After running into
> all sorts of problems with namespace, especially for global variable
> names, I decided to check out modules.  Surprise, surprise, they're
> simple!!

Once you get the idea behind them, they really can be.
 
[snip]

> The file name of the module is MyModule.pm, and it lives in the lib 
> directory off your root Perl directory.
> _______________________________________
> package MyModule;
> 
> # this tells perl that it is the MyModule module
> 
> # all modules must return a value when they compile, so stick this
> line 
> somewhere in your module -- I usually put it at the end of all my 
> declarations and before the main code.
> return 1;

This is why, when you look at someone else's module, the last line of
"code" is usually
 1;

(since the return is implied)
 
> # this subroutine is required to make a module a module.  I won't go
> into exactly what it does -- suffice to say that it creates an object
> # in memory that your script can have access to.  Copy and past this
> into your own module.

new() is NOT required to make a module.
Some sort of constructor is a good idea if you're making an object
class, but you don't have to do objects to make a module.
Admittedly, most of the modules I write *are* OO, but it's not
*necessary*.

> sub new()
>    {
>    my ($proto) = @_;
>    my $class = ref($proto) || $proto;
>    my $self = {};
>    bless ($self, $class);
>    return $self;
>    }

A module can be as simple as myFoo.pm:

  package myFoo;
  sub foo { print "bar!\n" }
  1;

which can be used by foo.pl:

  use myFoo;
  myFoo::foo;

now say 
  perl foo.pl
and it should respond with
  bar!

That's the simplest gist of it, though you can always use Exporter and
objects and bootstrap some XS if you like. It's just not *required*.
=o) 



__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to