March 13, 2004

Code test

Get the return value of a system call in perl

my $ls = '/bin/ls -l';
open( LS, "$ls |" );
while (<LS> ){
     print "OUTPUT $_";	
}
close LS;
print "EXIT $? \n"

Neat huh?

Posted by Steven at March 13, 2004 03:03 PM
Comments

UGH, what an ugly POS (piece of software). And it spews stderr into your terminal, instead of providing some control over it). And you can't test the darn thing interactively.

Suppose your process gets killed, e.g. run this and pkill sleep:

my $ls = 'sleep 1000';
open( LS, "$ls |" );
while ( ){
print "OUTPUT $_";
}
close LS;
print "EXIT $? \n"

All you get from the above is pitiful, grossly inadequate:

EXIT 15

Now try this in Tcl:

set pipe [open "| sleep 1000"]
while {[gets $pipe line] >= 0} {puts "OUT $line"}
if {[catch {close $pipe} msgFromStderr]} then {
puts $msgFromStderr
puts "Process [lindex $errorCode 1] exited with status [lindex $errorCode 2] (code [lindex $errorCode 0])"
} else {
puts "Exit code 0"
}

and pkill sleep again. You get a nice, informative message:

child killed: software termination signal
Process 19144 exited with status SIGTERM (code CHILDKILLED)


KK

Posted by: KK at April 29, 2004 01:01 AM
Post a comment









Remember personal info?




You must enter the security code to post!