in reply to Handling weird return values with or die

I obviously didn't read the entry for system in the Camel Book well enough, its code sample is:
@args = ("command", "arg1", "arg2"); system(@args) == 0 or die "system @args failed: $?"

Thanks, though... I've got it working now.

Just out of curiosity, is there some interesting piece of lore that explains why perl functions and shell functions have opposing standards for return values? The return values for other perl functions such as open, unlink, etc. as cited above seem to be the more intuitive, but I guess confusion only comes from having multiple standards.

Replies are listed 'Best First'.
Re: Re: Handling weird return values with or die
by Zaxo (Archbishop) on Jul 05, 2001 at 17:02 UTC

    Perl functions as it does to be a good fit to intuition and natural language.</p?

    A shell process's return value is the only way (aside from whatever it may have written to stderr) that status can be checked. Since there are many interesting ways for a process to fail, the ability to return different error codes is useful. Hence 0 == success, 0 != error_code.

    It takes some getting used to.

    After Compline,
    Zaxo

      I see; since boolean true would refer to any value above 0 and false only refers to to 0, and the fact that multiple error codes are much more useful than multiple success codes :-), zero has to be the success code. Thanks!
Re: Handling weird return values with or die
by Abigail (Deacon) on Jul 05, 2001 at 19:57 UTC
    It is not a matter of opposing standards. There are two different things. First, there are the function calls, whose behaviour is strictly defined (or else they would be unusuable). At the C level, failure is often indicated by a return value of -1, or NULL, and the reason of failure is passed in a global variable, errno. Not very intuitive (IMO), and Perl, being the thin layer it is above Unix/C, only makes it slightly more convenient. It returns 0 on failure. The reason of failure is still passed in a global variable, $! (which explains that variable).

    The return values of spawned process have nothing to do with the shell. It's the other way around. Processes typically return 0 indicating success, and anything else for failure. There's no global variable to set, all they can do is return a single integer. "0 on success" is a mere convention. It's just because this convention is followed so often, typical shell Boolean logic is "reversed"; 0 is true, other integers are false.

    -- Abigail

      Thank you; that was quite a good explanation. I had never considered the semantic relationship between errno and $!.

      I always find it helpful to learn context around the tools I use. I believe that if I can think a little more like the designer of the tool when I'm using it, I will be that much more effective.

Re: Re: Handling weird return values with or die
by Excalibor (Pilgrim) on Jul 05, 2001 at 18:30 UTC

    I'd say the truthness values come from C.

    As C language does not have a bool type, thruthness is expressed with anything different from 0, and 0 is false.

    The shell, (and certainly most programs in most OS's) return 0 when everything went OK, and an error code when not...

    Notice there'll be a my $rc = 0 is true;, which basically says the result of your sub is a 'zero' and that it's true (not an error, for example...). Only available in Perl6 properties (I wish it arrives soon :-)

    laters, david

    sub foo { return join '~', @_ }