local $SIG {ALRM} = sub {die "alarm"}; my $fh; eval { alarm (2); open $fh, "-|", "$vmdiscover ..." or die "Couldn't open $vmdiscover: $!"; while (<$fh>) {}; alarm (0); }; if ($@ && $@ =~ /^alarm/) { print "Command has timed out"; } elsif ($@) { print "$@\n"; } close $fh;