in reply to Executing an application in Perl/CGI

Use and/&& to test againsts system since it returns 0 for successful and true value on failure.
system('some_command') && die 'cannot execute some_command'; # or system 'some_command' and die 'cannot execute some_command';

Update: Fixed precedence and added another example with and. Thanks, kyle and blazar :-)


Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Replies are listed 'Best First'.
Re^2: Executing an application in Perl/CGI
by blazar (Canon) on May 22, 2007 at 19:34 UTC
    system 'some_command' && die 'cannot execute some_command';

    Really, both this and the OP's attempt get it wrong due to precedency issues. In fact I find myself repeating once again that as a rule of thumb, high precedence logical operators are for values and low precedence ones are for flow control. In particular, the OP's code appears to work because the die is never actually reached, while yours is the same as

    system(die('cannot execute some_command'));

    So the program dies before calling system.

      Even with the fix the program still dies on the system call. It works perfectly fine but run in a web browser, it dies.
        Even with the fix the program still dies on the system call. It works perfectly fine but run in a web browser, it dies.

        Didn't $! and $? help?

Re^2: Executing an application in Perl/CGI
by kyle (Abbot) on May 22, 2007 at 19:33 UTC

    system 'some_command' && die 'cannot execute some_command';

    This will always die because it's interpreted as:

    system( 'some_command' && die 'cannot execute some_command' );

    Since the string 'some_command' is a true value, the die always executes. To fix this, use and, which has a lower precedence:

    system 'some_command' and die 'cannot execute some_command';