Function system(), « moi c’est comme toi mais en mieux »

  1. an adapted version of the system() function
# returns the return code of $command
# if given $stdout a reference to a string, will put the standard output
# there, idem for $stderr

sub system {
my ($command,$stdout,$stderr)=@_;

my $tmpname=TMPDIR."/perl_ctk_system$$";

my $toexec = "$command 1>". ( $stdout ? "$tmpname.stdout":'/dev/null') . " 2>". ( $stderr ? "$tmpname.stderr":'/dev/null');

$rc = 0xffff & system $toexec;

if ($stdout) {
$$stdout=&readfile("$tmpname.stdout");
unlink "$tmpname.stdout";
}

if ($stderr) {
$$stderr=&readfile("$tmpname.stderr");
unlink "$tmpname.stderr";
}

return $rc;
}

3 juillet 2000

On note au passage, l’utilisation de 2 fichiers temporaires pour recupérer STDOUT et STDERR… très optimal.

Leave a Reply

You must be logged in to post a comment.