correct • elegant • free

△ comp.lang.perl △

◅ perl does not complain about missing ';'

How to execute UNIX commands ▻

Help creating usable modules

In article <>,
Lorna McLaughlin  <> wrote:
>I have written perl "packages" with containing functions, some of
>which return values.  I would like to make them into reusable modules,
>but do not understand how to use the export etc etc. to make a pm file
>into a module.  Here is a sample package.

Your sample "package" is already a perfectly good "module"; you
don't need to do anything to it.  All your subroutines are object
methods[1], so you shouldn't be exporting them anyway.

[1] In Perl, "a method is simply a subroutine that expects an object
reference (or a package name, for static methods) as the first

The Exporter comes into play when you have subroutines that *aren't*
methods which you want to make available to users of the module.
A good example is the Cwd module (part of the standard distribution),
which contains this incantation.

    require Exporter;
    @ISA = qw(Exporter);
    @EXPORT = qw(cwd getcwd fastcwd fastgetcwd);
    @EXPORT_OK = qw(chdir);

This is all boilerplate (i.e. it's a standard bit of code that
crops up again and again), except the names put into @EXPORT and

The names in @EXPORT will automatically be exported to any caller
that does not specify a list.  So if you say, in another module or
a Perl program,

    use Cwd;

then the names `cwd', `getcwd', `fastcwd', and `fastgetcwd' will
all be imported into your namespace, so they can be used without
a package qualifier.

    $ perl -e 'use Cwd; print cwd, "\n"'

The names in @EXPORT_OK are available for export, but only if the
caller specifically requests them.  So if you say,

    use Cwd 'cwd', 'chdir';

then the names `cwd' and `chdir' will be imported into your namespace;
the other names will not be imported (but you can still use them
by saying, for example, `Cwd::fastcwd').

The names of methods should never be exported, because static
methods (`new', typically) will specify the class name, and object
methods have an object from which they can discover the appropriate
class name.

    $p = new Patient \%data;
    # Could be written
    #     $p = Patient::new(Patient, \%data);
    # but never is.

    # Could be written
    #     Patient::Display($p, 'output');
    # but never is.

(By the way, it's unusual to capitalize subroutine / method names.)

Did any of that help?  There's a lot more information available by
typing `man perlmod'.

Tim Goodwin   | "continuous witter about large metal wanking
Cambridge, UK | machines" -- Robin Fairbairns, about uk.transport.

Original headers:

From: (Tim Goodwin)
Newsgroups: comp.lang.perl.misc
Subject: Re: Help creating usable modules
Date: 29 Mar 1996 17:04:12 GMT
Organization: Unipalm PIPEX
Message-ID: <4jh56c$>
References: <>

△ comp.lang.perl △

◅ perl does not complain about missing ';'

How to execute UNIX commands ▻