On Mon, Mar 23, 2009 at 19:41, Chap Harrison <c...@pobox.com> wrote:
snip
> I suppose what I want is the "module" (I mean, it just *sounds* right), but
> from what I've read so far, Perl modules seem to be very advanced,
> general-purpose animals - found mainly in CPAN, or in object-oriented
> programming.
snip

Modules are the way to go.  They are not as complex as they seem to
you right now.  Here is a simple module:

    package SomeModule;

    use strict;
    use warnings;

    sub func {
        print "I am in func in SomeModule\n";
    }

    1;

At first look it looks like normal Perl code but for two things: the
package statement[1] and the odd 1; at the end.  The package statement
creates a new namespace.  This means that the func subroutine declared
here won't conflict with a func declared in the main program (or other
modules).  This module should be named SomeModule.pm.  The 1; is there
because modules must end with a true value; the easiest way to make
sure it ends with a true value.  We will get to why it must end with a
true value in a minute.

That is all fine and good, but how do you make that code available to
the program you are writing?  Well, you have a bunch of options, but
the standard method is the use function[2]:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use SomeModule;

    print "I am in the main package\n";
    SomeModule::func();

The use is the reason you need the true value at the end of the
file[3].  You are evaluating the code in the module, and many things
can go wrong.  Making sure that you reached the end of the module
successfully is one of use's jobs[4].  Now, in order to get at the
functions and variables declared in the SomeModule namespace you must
prefix them with SomeModule::.  This can be a pain and often times the
purpose of a package is to supply functions you don't want to write
over and over again.  In this case it would get annoying to always
have to prefix those functions with their package names.  Now, this
being Perl, TIMTOWTDI[5], but the standard choice is the Exporter
module[6]:

    package SomeModule;

    use strict;
    use warnings;

    use Exporter;

    our @ISA       = qw/Exporter/; #don't worry about this line right now
    our @EXPORT_OK = qw/func/;     #this is the list of things
available for export
    our $VERSION   = "0.0.1";      #the module's version number

    sub func {
        print "I am in func in SomeModule\n";
    }

    1;

Now, you can import the func function in you code by saying

    #!/usr/bin/perl

    use strict;
    use warnings;

    use SomeModule qw/func/;

    print "I am in the main package\n";
    func();

And there you go, that is the basics of non-OO modules.  For more
information see the perlmod[7] docs in perldoc and the other docs I
mentioned.

1. http://perldoc.perl.org/functions/package.html
2. http://perldoc.perl.org/functions/use.html
3. well, really it is the require[8] function that use calls
4. again, it is really require's job
5. There Is More Than One Way To Do It
6. http://perldoc.perl.org/Exporter.html
7. http://perldoc.perl.org/perlmod.html
8. http://perldoc.perl.org/functions/require.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to