The 1 return value is a way for the module to say "it is ok to use me" to the code that calls require or use. There are some modules that actually return false on purpose!

Perl has a two step process for requiring a file: loading it and checking the return value.First, require and use (which calls require) parse and compile the file using eval (or possibly a C routine equivalent to it). This "loads" the file and any statements in the file will have been executed, just as if you had written a script instead of a module. Perl has to evaluate all of the statements because each statement has the potential to control the compiler's next step. For further details on why Perl can't compile a file without running it, see Perl Cannot Be Parsed: A Formal Proof.

If the file fails to load, then require and use throw an exception. Otherwise, they move onto step two and check the return value of eval. Since the module file is executed like a script, this is always the last executed statement in the module file. If it is true, require and use return the value of the last statement. If it is false, they throw an exception, even though the file is loaded!

By convention, the last executed statement of modules is "1;" all by itself on a line, since that always evaluates to true. Some people like other values (e.g. 42 or a JAPH). The choice is up to you.

Some modules have a false final statement on purpose. This is their way of telling Perl to ignore them if they detect an execution environment that is unfriendly to the file. So it isn't always true, that a module file returns true.

Best, beth


In reply to Re: module return by ELISHEVA
in thread module return by saranperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.