If you forget the 1 at the end of a package, Perl tells you "The package didn't return a true value". Well, if it knows you forgot it, why not just put it there for you?
4 Answers
Because Perl modules are required to return a value to signal if the require directive must succeed (true value returned) or fail (false value returned; this can make sense if the module failed to initialize for some reason).
If you don't return anything, the interpreter cannot know if the require must succeed or fail; at the same time, since it's easy to forget to put the true value at the end of the package, it suggests the "common fix" for this error: add a true value as a return.
For some other info/folklore about the modules return value have a look at this question.
-
If you forgot to put it there, wouldn't that mean you probably didn't need to signal any kind failed initialization? Since it seems like it would be a very specialized case, can't we safely assume that it'll succeed unless we're telling it that there's a chance it won't succeed? Mar 13, 2011 at 23:38
-
3@Chris: we can argue about what would be better or more logical, but the language has been designed in this way, talking about it won't change this behavior.
:)(personally, I think that it's better like it is now: if a return value is requested, then you have to specify it) Mar 13, 2011 at 23:41 -
1@Matteo - Since Perl 6 still very much in the "indefinite future" stage of development, I think talking about the language can (and almost certainly will) change it's behavior. Mar 13, 2011 at 23:58
-
@Chris: sure, I'm talking about the current Perl; still, I don't think they will change it, first of all for the "if a return value is requested, then you have to specify it" thing, and then because the whole "return strange true values" has become part of the Perl folklore.
:)Mar 14, 2011 at 0:09 -
4It doesn't have a practical use any more, not compared to the continuous annoyance it provides. And removing the requirement doesn't stop
$ret = require Foofrom continuing to work. As for there being no point about ranting and complaining and wanting the language to be different... this is Perl. We bring our own change. Have a look attrue.pmandperl5iboth eliminate this misfeature. It's also possible this could come in as ause 5.16feature.– SchwernMar 14, 2011 at 8:23
A package can return a false value if it fails to initialize, for example if it couldn't find a required data file or external library. This way it fails cleanly at load time (and this failure can even be tested for) rather than unpredictably later.
-
A package can return a false value if it fails to initialize, but it shouldn't. It would be better off dieing with a useful message.– ikegamiMar 14, 2011 at 19:08
From wikipedia Perl module:
A Perl module must end with a true value or else it is considered not to
have loaded. By convention this value is usually 1 though it can be
any true value. A module can end with false to indicate failure but
this is rarely used and it would instead die() (exit with an error).
1;
When a module is loaded (via use) the compiler will complain unless the last statement executed when it is loaded is true. This line ensures that this is the case (as long as you don't place any code after this line). It's Perl's way of making sure that it successfully parsed all the way to the end of the file.
See Link
You can use any statement that evaluates as true. 1 just happened to a become a perl idiom.