Why the 1 at the end of each perl package?

miniml picture miniml · Mar 14, 2011 · Viewed 17.2k times · Source

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?

Answer

Matteo Italia picture Matteo Italia · Mar 14, 2011

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.