usrlocal has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am modifying a perl script to call a shell script. the shell script usually returns a numeric return code, so i need to check for cases where the file may not be present, may not be executable or may return a non-numeric return code . Ive been toying around with various options and the current code looks like this:
$proc = \( 0+ `/aa/bb/cc.ksh arg1` ); if (! $proc || $proc !~ /\d/){ ....
any idea how this should be modified. if you could point me to a concise tutorial also that would help. all the tutorials i find seem to be rather large to dig for a relatively small piece of info.thanks
  • Comment on How to check for existence,executabilituy and return code when executing a shell script from perl
  • Download Code

Replies are listed 'Best First'.
Re: How to check for existence,executabilituy and return code when executing a shell script from perl
by kennethk (Abbot) on Feb 11, 2009 at 16:48 UTC
    It seems like the more appropriate way of performing your task would be to first test if the file exists, perhaps using the -X functions. They can also check file permissions. Lastly, why in the world are you taking a reference to your return after being forced into numeric context and then testing the value? For what you want to test, you should be calling your backticks as $proc =`/aa/bb/cc.ksh arg1`;.

    Update: And, to boot, if you want to check your return value for non-decimal characters, your test should be $proc =~ /\D/ - your regex syntax does not do what you think. Please read perlretut.

    Update 2:You should also get in the habit of using or in place of || - otherwise you may get Burned by precedence rules (operator precedence).

Re: How to check for existence,executabilituy and return code when executing a shell script from perl
by ikegami (Patriarch) on Feb 11, 2009 at 16:44 UTC
    $?, for starters