Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Perl Module ending without 1;

by prantikd (Novice)
on Jul 18, 2009 at 12:37 UTC ( #781320=perlquestion: print w/replies, xml ) Need Help??
prantikd has asked for the wisdom of the Perl Monks concerning the following question:

What happens if I write a Perl module which does not end with 1;?
I tried importing the module by use MyModule and require MyModule but did not get any errors.

It would be very helpful if someone have a good answer.

- Prantik

Replies are listed 'Best First'.
Re: Perl Module ending without 1;
by ig (Vicar) on Jul 18, 2009 at 14:56 UTC

    When you use your module, use require's your module. This is explained in the documentation for use.

    require includes your module by evaluating it with do. This is explained in the documentation for require.

    If your module compiles successfully, do returns the value of the last expression evaluated. When you end your module with 1;, this value is 1. This is explained in the documentation for do.

    require checks the value returned by do and if it is false it dies with the error $filename did not return true value, where $filename is the full path name of the file being loaded (your module).

    So, as others have pointed out, it is not necessary that your module end with 1; but if you want to include it with use or require then the last expression evaluated must have a true value. Ending modules with 1; is merely a convention to ensure they return a true value.

    Note that you can include a module that ends with 0; (or any other expression that evaluates to a false value. You can't include it using use or require, but you can use do and ignore the return value or load the content into a scalar and use eval to evaluate the scalar and, again, ignore the return value.

Re: Perl Module ending without 1;
by linuxer (Curate) on Jul 18, 2009 at 12:48 UTC

    A module file must end return with a true value. And adding the 1; at the end is the easiest way to accomplish this.

    If you don't do it, you have to hope, that your module code will always end with return a true return value... (Please don't hope... and please don't think, "my module will always do this...")

    update: s/end/return/; sorry, used the wrong word for this... Thanks JavaFan

      A module file must end with a true value.
      No, a module must return a true value. Many people don't realize that when a module is loaded with use, its code is actually run. Of course, if the module just contains subs, little code is run; but most modules assign a version number, set @ISA or @EXPORT/@EXPORT_OK. All these assignments wouldn't happen if the code in the module wasn't run.
Re: Perl Module ending without 1;
by toolic (Bishop) on Jul 18, 2009 at 17:42 UTC
    What happens if I write a Perl module which does not end with 1;?
    You could get an error.

    To satisfy my curiosity, I created a contrived example to prove that an error would result with use. Since I had never experienced this in practice, it is probably difficult to achieve, but it is possible. Here is a legal (but probably unlikely) scenario. Notice that I have commented out the '1;' at the end of the package. If you uncomment it, there will be no error:

    C:\Perl>type package MyFoo; use strict; use warnings; sub bar {print "bar\n"} #1; C:\Perl>type use warnings; use strict; use MyFoo; print "hello\n"; MyFoo::bar(); C:\Perl>perl did not return a true value at line 3. BEGIN failed--compilation aborted at line 3.

    Since it is simple enough to do, and it is a well-known convention, it is a good practice to add '1;'.

Re: Perl Module ending without 1;
by jrsimmon (Hermit) on Jul 18, 2009 at 12:45 UTC
    There is no requirement for 1;, or any other exact line, at the end of a module or script. All that does is ensure that the module returns 1 rather than whatever the previously most recent value was.
Re: Perl Module ending without 1;
by grizzley (Chaplain) on Jul 20, 2009 at 07:10 UTC

    On last year's YAPC, there was a presentation about what can be found at the end of CPAN modules. Few possibilities, which I remember:

    42; 8; "true"; "false"; "this is the end of the module"; "I was there 05/07/04";
    Simply - it must be something, that doesn't evaluate to 0.
        ++ for links I couldn't find :)
Re: Perl Module ending without 1;
by vinoth.ree (Monsignor) on Aug 17, 2009 at 12:08 UTC

    saranperl also discussing the same question, get some thing useful If you need.

    module return

    Forget to mention the node link

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://781320]
Approved by linuxer
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2017-04-28 18:50 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (528 votes). Check out past polls.