system("cmdtorun,@args >error.txt 2>&1"); #### #!/usr/bin/perl #by ChemBoy of perlmonks # Ever gotten annoyed at how different from every other subroutine # call system is? Ever screwed up your error reporting because you # couldn't remember what to do with $?, $@ and $!? I sure have... # and I got sick of rewriting this over and over, so I wrapped it # in a utility subroutine, and here it is. It assumes the # system LIST style is being used, because system SCALAR is # somewhat hazardous and (IMO) to be discouraged, but obviously # it's readily modified to be more tolerant. # Usage: # wrap_system(qw(perl -u -e 1)) or die "$@\n" # perl exited with status 0 after receiving signal 6 (core dumped) wrap_system(qw(perl -u -e 1)) or die "$@\n"; sub wrap_system { if ( 0 == system { $_[0] } @_ ) { return 1 } if ( -1 == $? ) { $@ = "Unable to launch $_[0]: $!" } else { $@ = "$_[0] exited with status " . ($? >> 8); if (my $sig = $? & 127) { $@ .= " after receiving signal $sig" } if ($? & 128) {$@ .= " (core dumped)" } } return; }