sub cmd_wrapper { my $cmd = shift; # CASE: Called in list context if( wantarray() ) { my @out; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 15; # Give the command 15 seconds to return @out = `$cmd`; alarm 0; }; return @out; } # CASE: Called in scalar context else { my $out; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 15; # Give the command 15 seconds to return $out = `$cmd`; alarm 0; }; return $out; } }