mjd-perl-pm on Thu, 17 May 2001 16:41:58 -0400

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: why is the '1;' necessary at the end of perl modules

> I am still unable to find the (any) documentation that states
> clearly why it's necessary.
> Is the reasoning behind this documented in one of the manpages?

Yes, but perhaps not clearly enough.  It's a feature that turned out
to be a mistake.

Typically, a module contains a bunch of subroutine definitions.  A
module may also contain code which is not part of a subroutine.  This
code is executed at the time the module is loaded, so it can be used
to initialize the module.  For example, the module might open
adatatbase connection or initialize some time at the time it is

Such code might be successful, or it might fail.  Perl allows the
module to indicate the result by returning a true or false value back
to its caller.  If the value is false, Perl aborts the compilation
with an error message.

Unfortunately, the default return value is *false*, so a module which
just defines a bunch of subroutines, and which has no initialization
code, accidentally returns a false value back to the caller, and Perl
aborts.  So we insert "1;" at the end to suppress this misbehavior.


discusses this in some more detail.

For documentation, see perldiag:

       %s did not return a true value
	   (F) A required (or used) file must return a true value
	   to indicate that it compiled correctly and ran its
	   initialization code correctly.  It's traditional to
	   end such a file with a "1;", though any true value
	   would do.  See the require entry in the perlfunc man-

and the relevant part of 'perlfunc':

            The file must return true as the last statement
	    to indicate successful execution of any initialization code, so
	    it's customary to end such a file with "1;" unless you're sure
	    it'll return true otherwise. But it's better just to put the
	    "1;", in case you add more statements.

**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**