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!!

Without going into all of the theory behind them (which I didn't know the 
first thing about when I started using them), here is a module.

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;

# now we can start our subroutines

# 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.

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

# the rest of your subroutines go here.
# let's put in a dummy one, just to make sure everything is working
sub GetValue()
         {
         my($self) = @_;
         # what's this $self stuff?  Don't worry about it for now.  But the 
first parameter of any subroutine that you want visible to
         # the outside world should be $self.
         return 1;
         }

sub EchoInput()
         {
         my($self, $input) = @_;
         return $input;
         }
_________________________________________________________________________________________

Now, you'll want to use your new module.

Let's say we have a script called MyScript.pl.  Here it is:

___________________________________________________________________________________________
#! perl
# above is the shebang line, and you'll all notice I'm on a M$ OS :)

# let's get access to our module
# this command tells perl to grab the module and stick into memory
use MyModule;

# we still can't use it though -- we have to assign it to a variable:
my $mod = MyModule->new();

# now, $mod contains all of our subroutines in the MyModule module, which 
we call like this
my $value = $mod->GetValue();

# the arrow (->) means that referring to a pointer, to use C++ terminology. 
Without going into great detail, the line above in plain English
# means "set the variable $value to whatever you get back from the 
subroutine GetValue inside the $mod object"

print "The value is $value\n";

my $echo = $mod->EchoInput("hello world");
print "The echo is $echo\n";
______________________________________________________________________________

See how easy modules are?

At 06:33 23.05.2001 +1000, you wrote:


>Hello All,
>
>I am new to Perl and putting together a script that needs about 20 reasonably
>complex subroutines.
>
>To keep the code as modular and managable as possible I would prefer not to
>define all the routines in the main script.
>
>It seems like using 'require' is the way to go (since I don't have the time or
>experience to do modules at the moment).

Aaron Craig
Programming
iSoftitler.com

Reply via email to