37

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?

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
miniml
  • 1,489
  • 2
  • 17
  • 27

4 Answers4

41

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.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 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? – Chris Lutz Mar 13 '11 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) – Matteo Italia Mar 13 '11 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. – Chris Lutz Mar 13 '11 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. `:)` – Matteo Italia Mar 14 '11 at 00:09
  • The module fails to initialize if you forget to return a true value anyway. Also, when was the last time you returned a false value from your production module and allowed it to generate a run time error? – miniml Mar 14 '11 at 00:33
  • @Minimalist: see my first comment directed @Chris: the fact that you don't like it won't change the way the (current) language works. I can rant for a day about how I think PHP is awful, yet this won't change it of a iota. – Matteo Italia Mar 14 '11 at 00:41
  • I actually find this kind of pointless. If you want the load to fail, just call `die`. Seriously, the only time I have had a module end with a false value, it was because I forgot to put a `1;` at the end. – Brad Gilbert Mar 14 '11 at 01:00
  • 4
    It 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 Foo` from 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 at `true.pm` and `perl5i` both eliminate this misfeature. It's also possible this could come in as a `use 5.16` feature. – Schwern Mar 14 '11 at 08:23
5

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.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • 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. – ikegami Mar 14 '11 at 19:08
4

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76
4

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).
Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47