Agree fully. I've also posted execute() which uses an exec-like mechanism, bypassing shell issues for arguments. Here it is again:
sub execute { # execute a command without shell but with timeout
my ($cmd,@args)=@_;
my $timeout=15; # seconds
my ($result,$pid,$i,$time);
if ($args[$#args]=~/^--?timeout=(\d+)$/i) { # or pass as last arg
$timeout=$1;
pop @args;
}
die "execute($cmd,".join(',',@args)."): null arguments" # SSF 08
+0708 eliminate null arguments, they are always erroneous
if grep { length($_)==0 } @args;
$i=index($cmd,' '); # args appended to command?
if ($i>-1) {
unshift @args,split(/\s+/,substr($cmd,$i+1)); # could be more
+than one
$cmd=substr($cmd,0,$i);
}
eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
local($/)=undef;
alarm $timeout;
$time=time if debug_has(EXEC);
$pid=open(CMD,'-|',$cmd,@args); # run without shell overhead
if ($pid) {
$result=<CMD>;
close CMD;
debug sprintf("execute(%s%s%s):\n%s%s [%.3f s]",$cmd,@args?
+',':'',join(',',@args),substr($result,0,500),length($result)>500?'...
+':'',time-$time) if debug_has(EXEC);
} else {
alarm 0;
die "execute($cmd ".join(' ',@args)."): $!" unless $pid;
}
alarm 0;
};
if ($@) {
die $@ unless $@ eq "alarm\n"; # propagate unexpected errors
}
$result;
}
SSF |