#!/usr/bin/perl use strict; use warnings; my $cmd = 'ping'; my $host = '127.0.0.1'; my $timeout = 3; my @cmd = (); eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; open my $pipe, '-|', "$cmd -c 5 $host" or die "Could not open pipe: $!"; while ( my $line = <$pipe> ) { push @cmd, $line; } close $pipe; alarm 0; }; if ($@) { print "Output: @cmd\n"; print "timed out - $@\n"; warn unless $@ eq "alarm\n"; } else { print "Output: @cmd\n"; print "No time out\n"; }