in reply to Re: backtick iterpolation issue
in thread backtick iterpolation issue
I was using backticks as I needed to get the stderr and stdout.
But using backticks as you did doesn't capture the stderr of your system executable!
#!/usr/bin/env perl -w use strict; my $foo = qx{ ls }; # alternate syntax for backticks open my $fh, '>', 'bar.out' or die $!; print $fh $foo; close $fh; __END__
$ cat bar.out foo.pl $
#!/usr/bin/env perl -w use strict; my $foo = qx{ ls baz }; # doesn't exist open my $fh, '>', 'bar.out' or die $!; print $fh $foo; close $fh; __END__
$ cat bar.out $
You may have seen an error message spit out to your screen by your system executable, but it was not captured in your Perl program because you used backticks. You can combine the output streams, depending on your OS, with something like:
#!/usr/bin/env perl -w use strict; my $foo = qx{ ls baz 2>&1 }; open my $fh, '>', 'bar.out' or die $!; print $fh $foo; close $fh; __END__
$ cat bar.out ls: baz: No such file or directory $
But then you'll have to parse what you get to see if matches an error string. This all begs the question of why you didn't follow advice from others as well as myself to use a module that is built for the task ... because, among other benefits they usually handle errors gracefully.
|
|---|