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

... because all values unequal 0 are TRUE in perl ...
  • Comment on Re: Re: using exit and number 1 at end of file

Replies are listed 'Best First'.
Re: Re: Re: using exit and number 1 at end of file
by sauoq (Abbot) on Jun 08, 2003 at 18:15 UTC

    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.";
    
Re: Re: Re: using exit and number 1 at end of file
by zarniwoop (Initiate) on Jun 08, 2003 at 16:21 UTC
    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
Re: Re: Re: using exit and number 1 at end of file
by The Mad Hatter (Priest) on Jun 08, 2003 at 16:12 UTC
    ...except for undef.
Re: Re: Re: using exit and number 1 at end of file
by Arguile (Hermit) on Jun 08, 2003 at 19:18 UTC