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

Hi, currently, Im trying to run a shell script from perl and detecting for non-numeric return codes etc... how do i also detect if the file is not openable or executable or corrupted. Im a bit of a novice, started coding perl only last week and cant seem to find the exact answer in the perlopentut. the current syntax Im using is:
$processor = \( 0+ `/aa/bb/ccc.ksh` ); if (! $processor || $processor !~ /\d/){ ... }

Replies are listed 'Best First'.
Re: detecting file open failure
by JavaFan (Canon) on Jan 29, 2009 at 16:13 UTC
    Backticks give you the output of the program. That isn't what you want. Instead, you want system. You can retrieve the exit code from $?.
Re: detecting file open failure
by kyle (Abbot) on Jan 29, 2009 at 17:26 UTC

    That looks a lot like the code I wrote in Re: Converting from string to SCALAR when using strict "refs". If I assume that what I learned about the problem back then still applies today, then I think you're looking for a way to tell if setting $processor failed in some way.

    One way is to check $?, and that will tell you the exit status of the program you ran in backticks. I suspect you don't care about that, however, if the program gave you bogus output. What you really want is to check that you got a number from it.

    To check that it gave you a number, like you want, look at ${$processor}. You could use Scalar::Util::looks_like_number, or you could match against a simple regular expression like so:

    my $processor = \( 0+ `blah` ); if ( ${$processor} !~ m{ \A \d+ \z }xms ) { die 'oh noes'; }

    If that doesn't do what you want, you'll have to explain better what you're looking for.

Re: detecting file open failure
by jethro (Monsignor) on Jan 29, 2009 at 16:43 UTC

    Use 'perldoc -f -x' to get to the manual page of the filetest operators. To check if for example a file is executable, you would use if (-x $file) { ... }

    To check if a file is corrupted there is no easy or generally usable way. If the file is a perl script you could try to check it with 'perl -c'

      also, how can I test wether the value returned by the script is numeric or non-numeric?

        That depends on whether by numeric you mean a simple integer like 12 or a number like -12.15 or even 12E25. You might check that with a regex, similar to what kyle already posted

        chomp($processor); # removes \n # would check for whole numbers like 1, 12, 389 if (! $processor || $processor !~ /^\d+$/){ #would check for numbers with optional decimal point and + or minus si +gn if (! $processor || $processor !~ /^(+|-)?\d+(\.\d*)?$/){

        There is a cpan module that has all kinds of regexes predefined (don't rememer the name atm), if you want to check for more complex numbers, that module would be a good idea

        UPDATE: Note that in the example code you posted above you have a pointer to the result in $processor and not the result itself. If you just want the result of the ccc.ksh execution in $processor, better remove the \ in front of the left paren