I have code that executes system commands:
use strict;
use warnings FATAL => 'all';
use autodie ':default'; # i.e. no "system"
use Devel::Confess 'color';
use Capture::Tiny 'capture';
sub execute {
my $cmd = shift;
my ($stdout, $stderr, $exit) = capture {
system( $cmd )
};
if ($exit != 0) {
say "exit = $exit";
say "STDOUT = $stdout";
say "STDERR = $stderr";
die "$cmd failed";
}
return 0
}
Based on previous advice that I've received from
How to get Capture::Tiny to print stderr and stdout upon failure?
However, this subroutine doesn't work very well when the output of a command gives some sort of prompt or warning, and just hangs forever instead of printing the error message/prompt.
The hanging normally happens with various LaTeX commands, but because LaTeX doesn't provide for very good minimal working examples, similar hanging with no printed/displayed error is also produced if a file without writing permission is attempted to be deleted,
e.g. chmod a-w tmp
and then calling the script execute('rm tmp')
which would ordinarily give this question at the CLI: rm: remove write-protected regular file 'tmp'?
but just hangs with nothing printed at output.
I've tried changing system( $cmd )
to eval { system( $cmd ) }
but I still can't see the error messages.
How can I alter the subroutine above so that I get the prompts and errors like when trying to delete a file without write permission?