If all you have is a bunch of subroutines you would like to put in a
subroutine library module, it's easy.

* The module needs to return TRUE when it's loaded to show that it loaded
correctly.  The easiest way to guarantee this is to put

        1;

as the last line in the module.

* Give the module file a .pm extension.

*  Put the module file in a directory that is in the @INC search path.  The
easiest way to do that is to put it where other modules are kept such as
site/lib.

*  In your main script, add the line

        use myModule;
or
        require myModule;

where, of course, "myModule" is the name you gave your module file less the
.pm file extension.

* If the code in your module needs some initialization BEFORE it is ever
called by the main script, then put that initialization code as top-level
code (i.e., not in any subroutine) in the module.  It will be executed after
the module is loaded but before control returns to the script that issued
the use or require statement.  It code should return true if the
initialization worked or false if it failed.

* If your module defines some variables that need to be referenced by the
calling script, then do NOT declare those variables as 'my' variables.
Instead declare them as 'our' variables if you are using Perl 5.6 (a 6xx
build) or declare them as 

        use vars qw(var1 var2 var3 ...);

if you are using Perl 5.5.  Have fun.

Merrill

> -----Original Message-----
> From: Purcell, Scott [SMTP:[EMAIL PROTECTED]]
> Sent: Monday, March 12, 2001 7:32 AM
> To:   '[EMAIL PROTECTED]'
> Subject:      require or module
> 
> Hello,
> I have some code that I am always copying and pasting. It is rather large,
> but basically opens a socket to a certain port/IP and makes some
> transactions. I am basically getting tired of copying/pasting this into
> documents because the maintenance is starting to take its toll.
> 
> I really would like to make it a module, but I don't know where to begin.
> I
> have all the Perl books, and was wondering if there is any good examples
> of
> doing this, or if it is too complicated, should I just create a file with
> the code, and require it in each of my code programs?
> 
> I would like to try a  module, as I have been doing perl for a while now,
> and feel it is the next step.
> 
> Thanks,
> 
> Scott Purcell
> 
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to