Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl: catch error system

Status
Not open for further replies.

julie25

Programmer
Aug 7, 2003
22
FR
Hie,

I'm doing a system call system(); and I'd like to capture the error system to put it in my interface..
I tried :
|| &ErrorMsg()
or || die ""
but my problem is that when the system call is finished, the error message displays.

Thanks.

Julie25
 
instead of
$rc=system("mycommand"); # this will return the success code
$rc = `mycommand`; $rc will now contain the output from command

HTH
--Paul
 
Thank you for your answer Paul.

I'm not sure to understand well.

My problem is not to catch the message but to catch the error event.

I wrote:
system(....) || die "error..";
And every execution (with succesful execution too!) I've got the error message.

I would like that my error message appears only when the system call didn' t function but it appears after each execution...

Thanks for all.

Julie
 
Hi Julie

i'm not quite sure buy try

system(....) or die "error..";

... rather than || - i'm sure there is a difference and it just might work!

Regards
Duncan
 
Whew, you guys are way off.

When system() finishes normally, it returns 0. Thusly, checking to see if it fails by saying:

system(@args) || die "error";

is wrong. In the documentation ("perldoc -f system"), they show the proper way to do this:

system(@args) == 0 or die "system @args failed: $?";

The documentation goes on to show different ways to look at the return value (which is stored in $?).

Hope this helps,

--jim
 
Why not use the System2 module whcih lets you catch STDERR, STDOUT, Exit Code etc.

Here is how I used it in a program I wrote:

use System2;

my ($out, $err) = system2("/opt/OV/bin/OpC/opccfgdwn", "-silent", "/tmp/spec1", "/OV_backup/cfgdwnback$day$month$year");
my ($exit_value, $signal_num, $dumped_core) = System2::exit_status($?);

if ($exit_value == 0) {
`$OV_BACK_PATH/opcmsg a=OpC o=opc_backup s=normal msg_g=OpC msg_t="Configuration download on venus.hphc.org completed succesfully, Message = $out"`;
} elsif ($exit_value == 1) {
`$OV_BACK_PATH/opcmsg a=OpC o=opc_backup s=major msg_g=OpC msg_t="Configuration download on venus.hphc.org failed, Message = $err, $out"`;
} elsif ($exit_value == 2) {
`$OV_BACK_PATH/opcmsg a=OpC o=opc_backup s=minor msg_g=OpC msg_t="Configuration download on venus.hphc.org completed with warnings, Message = $err, $out"`;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top