Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,
I have written a code "auto_runner.pl" which invokes another script "embed.pl".
embed.pl script in turn opens an xterm and sends messages on this xterm. I want to catch the error thrown in this xterm. Zenity tags are used to highlight error when embed.pl comes across one.
Also that I have no control of changing the code in embed.pl since it is someone's else's code I am using...
Please suggest which direction should I proceed.
Looks clueless to me.

sub CIrc_check{ my %SETUP=%{$_[0]}; print "\033[33m Embed.pl invoked\033[0m\n"; my $link=$SETUP{SVN}; my $output_path=$SETUP{LOCATION_OF_OUTPUT}; my $model_name=$SETUP{MODEL_NAME}; $output_path=$output_path."/"."EMBED_RESULT"; `mkdir $output_path`; chdir $output_path or die "Can't cd to $output_path : $!\n"; `xterm -e embed.pl -link $link &`; #When embed.pl strikes with an error in link or something it evaluated + it throws zenity tag sayin error in model name or so }

Replies are listed 'Best First'.
Re: Catching errors from embedded script in parent script
by zentara (Cardinal) on Jun 09, 2012 at 10:06 UTC
    You could use IPC::Open3, here is a basic example of collecting stdout separate from stderr. To test it's functioning, try entering "2 + 2" then try "2 / 0".
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh