A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Timing out a system call

as seen in perldoc perlipc
 
#!/usr/bin/perl

use warnings;

my @cmd = @ARGV;
my @ls = ();
my $t = 10;

    eval {
        local $SIG{ALRM} = sub { die "alarm went off" };
        alarm $t;
            @ls = `@cmd`;
        alarm 0;
    };

if (! @ls) {
        print "No response after $t seconds\n";
}
else {
        foreach (@ls) { print $_; }
}

# actually never gets to here
if ($@ and $@ !~ /alarm went off/) { die "Command timed out after $t seconds\n"; };