Help - Search - Member List - Calendar
Full Version: 0; and 1;
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Beast
I coudn't find any reference (yet) the meaning of 0; or 1; in the end of
perl program.

It means exit or return value? when I explicitly type "return 0;" it
gives error, but when I give "exit 100;" the value of $$ isn't 100 anyway.


--

--beast

Chris Devers
On Thu, 14 Jul 2005, Beast wrote:

QUOTE
I coudn't find any reference (yet) the meaning of 0; or 1; in the end
of perl program.

In the absense of an explicit return statement, a block of Perl code
will return the value of the last statement to any calling code.

For an ordinary script, this doesn't really matter -- it will return
what it will return. For code that you expect to be called though, like
a package or module, you have to keep the return value -- and more to
the point, the truth of the return value -- in mind.

The simplest way to do this is the "1;" end-of-module idiom. I've also
seen people use random strings like "'true';" or "'pony pony pony';" --
but either way has the same result: as long as the truth status of the
final statement is guaranteed to unconditionally be "true", then the
module can be used without throwing any errors (or at least, not errors
because of the return status).

The important bit to keep in mind is the truth value of the last line.
Using '1;' is a short, simple, guaranteed way to get this truth value.


--
Chris Devers

Adriano Ferreira
On 7/14/05, Beast <[Email Removed]> wrote:
QUOTE

I coudn't find any reference (yet) the meaning of 0; or 1; in the end of
perl program.

In the examples in "perldoc perlmod" you will find

1; # don't forget to return a true value from the file

This is needed when you do a 'require' over a Perl module (which is a
Perl package in a file). The same applies to 'use' which does a
'require' as its first step. Perl programs or packages within the same
file don't need this. Or else a program simple as 'print("Hello,
World"); undef" would not work, but it does. As Chris Devers has said
it has to do with the return value of the last statement. This is
interpreted by 'require' as meaning everything was fine during the
initialization of the Perl module. Most of the time, we finish the
module just with "1;", but more deeper things could be done. An
example is a Perl module attached to a configuration file: it that
configuration could not be read, it is better to die than to continue.
(This is not the perfect example: something sensible could be done
with defaults rather than blowing in user's face).

For references, take a look at "perldoc -f require".


QUOTE
It means exit or return value? when I explicitly type "return 0;" it
gives error,

You return only from a sub.

QUOTE
but when I give "exit 100;" the value of $$ isn't 100 anyway.

'exit' finishes the program immediately. $? (if in shell) catches the exit code:
$ perl -e "exit(100)"
$ echo $?
100

Regards,
Adriano.


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.