tags:

views:

68

answers:

2

I am converting some jpg images to png w/ imagemagick & have the following in perl:

system("convert $jpg $png");
print "$?\n";

Is there a way to capture the actual errors from imagemagick (rather than just whether or not it executed successfully as I have in the code above)?

note: I use imagemagick solely as an example....this is more a general question on how to capture errors from whatever program that system() executes.

thx!

A: 

For best results see: How-can-I-capture-STDERR-from-an-external-command?

Also read the previous one:

Why can't I get the output of a command with system()?

nc3b
Your URL needed the '?': http://perldoc.perl.org/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command%3f
MkV
@MkV Thank you :)
nc3b
+2  A: 

cribbed from the IPC::Run manpage:

use IPC::Run qw{run timeout};
my ($in, $out, $err);

run [convert => ($jpg, $png)], \$in, \$out, \$err, timeout( 10 ) or die "$err (error $?)"

You could also use PerlMagick like this:

use Image::Magick;

my $p = new Image::Magick;
$p->Read($jpg);
$p->Write($png);
MkV
using IPC::Run seems to work somewhat but I can't figure out how to pass a more complicated command....ie like --quiet..... run [convert --quiet => ($jpg, $png)], \$in, \$out, \$err, timeout( 10 ) or die "convert: $?";
ginius
The `--quiet` should just be another entry in the array you pass to run: `run ['convert', '--quiet', $jpg, $png],` etc. *(This is why fat commas are bad; they can confuse people into thinking there's magic syntax at work.)*
Porculus
I assumed a certain level of Perl knowledge, given that this isn't just written using a shell script. Anyway, you can do this *run [convert => -quiet => ($jpg, $png)], \$in, \$out, \$err, timeout( 10 ) or die "$err (error $?)"* as you can see, the parameter is -quiet not --quiet
MkV
Or for Porculus: *run ['convert', '-quiet', $jpg, $png], \$in, \$out, \$err, timeout( 10 ) or die "$err (error $?)"*, as you can see the fat comma (*=>*) works like a regular comma except it causes its left operand to be interpreted as a string (as long as its a 'word', no whitespace, or sigils, see perlop for more details) which aids in comprehending the relationship between parameters in calls like system: i.e. better *system( cmd => ('param1', 'param2') );* than *system('cmd', 'param1', 'param2');*
MkV