saranperl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: module return
by grizzley (Chaplain) on Aug 17, 2009 at 10:33 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: module return
by ELISHEVA (Prior) on Aug 17, 2009 at 12:56 UTC

    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

Re: module return
by Bloodnok (Vicar) on Aug 17, 2009 at 11:18 UTC
    See require - 5th paragraph...which explains, most succinctly, why the 1 is needed.

    A user level that continues to overstate my experience :-))
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: module return
by Anonymous Monk on Aug 17, 2009 at 10:36 UTC
      ya, very good notes for creating new module.
      "We need the 1; at the end because when a module loads Perl checks to +see that the module returns a true value to ensure it loaded OK. You +could put any true value at the end (see Code::Police) but 1 is the c +onvention."
      The above lines have bean given for that cause. according to my understanding. "if you give 1 at end of the module, perl loads the module, otherwise not loaded". is it correct????
        No. 1 indicates it loaded successfully, otherwise it failed to load successfully. use/require die when it fails to load successfully, but its still loaded.
        D:\>echo package asdf; $VERSION = 5; 1; >asdf.pm D:\>perl -Masdf -e 1 D:\>echo package asdf; $VERSION = 5; 0; >asdf.pm D:\>perl -Masdf -e 1 asdf.pm did not return a true value. BEGIN failed--compilation aborted. D:\>perl -e " eval { require asdf; 1 } or warn $@; die $asdf::VERSION +" asdf.pm did not return a true value at -e line 1. 5 at -e line 1.