I want to get both the output and status of a pipe. Here is a working example:
#!/usr/bin/perl use strict; open my $fh, '-|', 'bash -c "echo Hello; exit 1"'; print <$fh>; close $fh; my $status = $? >> 8; print "$status\n";
Fine. It outputs
Hello 1
Now let's try to use autodie:
#!/usr/bin/perl use strict; use autodie; open my $fh, '-|', 'bash -c "echo Hello; exit 1"'; print <$fh>; close $fh; my $status = $? >> 8; print "$status\n";
No good, that breaks it:
Hello Can't close(GLOB(0x10082a098)) filehandle: '' at test.pl line 7
It is the "close" statement. So we can take that out.
use strict; use autodie; open my $fh, '-|', 'bash -c "echo Hello; exit 1"'; print <$fh>; #close $fh; my $status = $? >> 8; print "$status\n";
Now it is broken differently---it has lost the exit status:
Hello 0
I need to call close in order to get the exit status of a pipe.
So now I have settled on this ugly thing:
use strict; use autodie; open my $fh, '-|', 'bash -c "echo Hello; exit 1"'; print <$fh>; {no autodie; close $fh;} my $status = $? >> 8; print "$status\n";
That seems to work, but I'm not doing any error checking on the close statement:
Can I use close with autodie when my pipe fails? If not, how should I check for errors?Hello 1
In reply to close and autodie on pipes by apomatix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |