in reply to using exit and number 1 at end of file

In Perl you don't need to explicitly exit as it will clean up everything for you. The "scripts" you see end with 1; are probably modules. All modules, so that use/require-ing them works, should return a true value. The easiest way to ensure this is by putting a true value at the very end of the file. 42;, for example, would also have the same effect.

Replies are listed 'Best First'.
Re: Re: using exit and number 1 at end of file
by tos (Deacon) on Jun 08, 2003 at 15:48 UTC
    ... because all values unequal 0 are TRUE in perl ...

      If you neglect overloading, all values other than "0", "", (), or undef are true. Equality to zero has nothing to do with it. Consider these:

      my @zeroes = qw( zero 0ButTrue 00 ); for my $z (@zeroes) { print "'$z' ", $z == 0 ? 'is ' : 'is not ', "equal to zero.\n"; print "'$z' ", $z ? 'is ' : 'is not ', "true.\n\n"; }
      With overloading, it is even possible to get an object which is both not equal to zero and false.
      package C; use overload 'bool' => sub { 0 }; use overload '==' => sub { 0 }; sub new { bless {} } package main; my $c = C->new; print "\$c is not equal to zero.\n" unless $c == 0; print "\$c is not true.\n" unless $c;

      -sauoq
      "My two cents aren't worth a dime.";
      
      Non-zero return values are useful when you're using perl in a shell script, where a zero exitcode means 'true' and non-zero exitcodes generally signal various kinds of error condition.

      For example, in Bash you might want to do:

        if findfile.pl; then (something) fi

      and make findfile.pl return >0 if it doesn't find a file.

      Zarni-oh-no-this-is-PERLmonks-isn't-it-woop
      ...except for undef.