Why do we return 1 in the perl script?

beer9
beer9 used Ask the Experts™
on
In the perl script I have seen the last line of the code is usually '1;' I would like to know why it is like that. Incase I run this script from the bash then bash expect '0' if command successfully executed.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I'm not sure of the reasoning, but I believe perl "modules" need to do that, so they will work.
Are you talking about modules (extension .pm) or scripts?
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
perldoc -f require
...
               Otherwise, "require" demands that a library file be included if
               it hasn't already been included.  The file is included via the
               do-FILE mechanism, which is essentially just a variety of
               "eval" with the caveat that lexical variables in the invoking
               script will be invisible to the included code.  Has semantics
               similar to the following subroutine:
...
               The file must return true as the last statement to indicate
               successful execution of any initialization code, so it's
               customary to end such a file with "1;" unless you're sure it'll
               return true otherwise.  But it's better just to put the "1;",
               in case you add more statements.
Commented:
It is not about the return value, it is about the question. ;)
Think of bash like: Did you encounter an error?
Think of perl like: Were you successful?
ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
the 1; at the end of a perl module is not returned to bash if it is run from bash
a value can be returned to bash by passing it to the exit function, or in $! with the die function

Commented:
I'm not sure I agree that you "should" return a 1.  I have always been taught to return a zero to indicate a successful run.  Where are you being told to return a 1, is it a specific module or function?

Commented:
You mix up "1;" at the end of a perl script and "return 1;".
Your can find a good explanation for it here:
http://lists.netisland.net/archives/phlpm/phlpm-2001/msg00426.html
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
I think you meant mix up "1;" with "exit 1;"

Commented:
ozo, you are right :) It's been a tough day at work today :)

Author

Commented:
Thank  you! :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial