[kw-pm] potentially dumb perl question regarding packages

Richard Dice rdice at pobox.com
Thu Oct 27 14:28:29 PDT 2005


Quoting Quantum Mechanic <quantum.mechanic.1964 at gmail.com>:

> To broach a pun, the author may have been "packaging" a number of modules
> into a single file, simply by concatenating the files together. That would
> also help explain the "1;" in your example.

That's a thought, but I figure it's probably more a mis-understanding of the
nature of packages / modules / classes (at least, what it takes for them to
work) on the part of the original author.

The reason why .pm files end (most often) in "1;" is because it is needed by the
"use" (or "require") command.  That is, if the terminal executable line of the
.pm file isn't a true value then "use" considers this to mean that the attempt
to use the file failed, and therefore use dies and kills the program at
compile-time.

But in this case, there should be no "use" statement in the program, i.e.
fred.pl doesn't have a "use fred;" line in it.   And yet, assuming that "package
fred" is set up to implement a class, 

   my $newfred = fred->new();

should still work even though no 'use fred;' was previously called.  This is
because "package fred" further down in the file creates the "fred" namespace, in
which the stuff which semantically implements the class is founds.

Cheers,
Richard

> On 10/27/05, Robert P. J. Day <rpjday at mindspring.com> wrote:
> >
> >
> > some perl code i've inherited has a number of ".pl" files with the
> > following structure -- consider a sample file fred.pl:
> >
> > =============================
> >
> > #!/usr/bin/perl
> > ...
> >
> > package fred;
> >
> > ... executable code ...
> >
> > 1;
> >
> > ==============================
> >
> > i understand the concept of defining a package name in a .pm file,
> > but this is just a regular executable file and it doesn't appear to
> > need to be part of any package.
> >
> > what is the rationale for defining a package for a top-level
> > executable file?
> >
> > rday
> >
> > p.s. this file *does* "use" a number of module files, but they have
> > no relation to the above package name in any way.
> > _______________________________________________
> > kw-pm mailing list
> > kw-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/kw-pm
> >
> 




-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/


More information about the kw-pm mailing list