[tpm] IO::Handle not catching errors

Madison Kelly linux at alteeve.com
Tue May 27 05:42:42 PDT 2008


Cees Hek wrote:
> On Tue, May 27, 2008 at 5:45 AM, Madison Kelly <linux at alteeve.com> wrote:
>> Hi all,
>>
>>   I use IO::Handle for shell calls, and then check 'error' to see if
>> there was an error. However, this isn't catching errors... Can someone
>> clue me in?
>>
>> sub do_shell_call
>> {
>>        my ($shell_call)=@_;
>>
>>        my $sc=IO::Handle->new();
>>        print "  Shell call: [$shell_call]\n";
>>        open ($sc, $shell_call) or die "Shell call: [$shell_call] failed with
>> error: $!";
>>        my $output=0;
>>        while (<$sc>)
>>        {
>>                if (!$output) { print "/----\n"; $output=1; }
>>                print "| $_";
>>        }
>>        if ($output) { print "\\----\n"; }
>>        if ($sc->error)
>>        {
>>                die "Shell call: [$shell_call] returned an error: $!";
>>        }
>>        $sc->close;
>>
>>        return 0;
>> }
> 
> Which errors are you trying to catch?  An error with IO::Handle, or
> errors with the script or program you are launching?  I think
> IO::Handle will only return errors regarding things like 'unable to
> open file', or 'could not write to file' (those are paraphrased
> errors).  If you want to catch errors in the script you are running,
> you will have to look at the return value of the script to see if it
> exitted cleanly.
> 
> When running external programs, you might be better off using
> something like IPC::Run which is better at error handling, or possibly
> IPC::Open2 or IPC::Open3.
> 
> Cheers,
> 
> Cees

Thanks for the reply! I love this list, so many helpful people. :)

Please see my reply to James sent *just* before this. I went into better 
detail what I am trying to do. In short though; I wanted to use 
'IO::Handle' to avoid dependency issues as it's a "core" module. I am 
trying to trap non-zero exit status' from the called shell command. The 
first '... or die ...' catches problems will calling the shell call 
itself (bad permissions, etc), and the second '$sc->error' check I want 
to trap shell calls that succeeded but exited with non-zero codes.

Madi


More information about the toronto-pm mailing list