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
|