Приглашаем посетить
Прутков (prutkov.lit-info.ru)

Section 13.4.  Systemic Failure

Previous
Table of Contents
Next

13.4. Systemic Failure

Be careful when testing for failure of the system builtin.

The system command is a particularly nasty case. Unlike most other Perl builtins, it returns false on success and true on failure. Fatal doesn't work on it either, so most people give up and write something like:

    system $cmd
        and croak "Couldn't run: $cmd ($OS_ERROR)";

The flow-of-control there is highly counterintuitive unless you're familiar with system's unusual failure return value.

A cleaner approach is to use the WIFEXITED ("if-exited") subroutine from the standard POSIX module:


    use POSIX qw( WIFEXITED );

    
# And later...
WIFEXITED(system $cmd) or croak "Couldn't run: $cmd ($OS_ERROR)";

Note that this particular return value anomaly will be fixed in Perl 6. The revised system function will still return an integer status value as in Perl 5, but the boolean value of that status will be "reversed": true if the status is zero and false otherwise. Those new semantics are already available in Perl 5, via the Perl6::Builtins CPAN module:


    use Perl6::Builtins qw( system );

    
# and later...
system $cmd or croak "Couldn't run: $cmd ($OS_ERROR)";

    Previous
    Table of Contents
    Next