开发者

How can I implement a timeout for a qx(command)?

How could I implement in this piece of code a timeout: if the "hwinfo --usb"-command didn't return anything after a certain amount of time, ( stop the command and ) do a return or die from the sub _usb_device.

#!/usr/bin/env perl
use warnings; 开发者_Python百科
use strict;


sub _usb_device {
    my @array;
    {
    local $/ = "";
    @array = qx( hwinfo --usb );
    }
    ...
    ...
}


Timeouts are usually done with alarms.


 sub _usb_device 
 {
    # Scope array
    my @array;

    # Try shell command
    eval
    {
        local $SIG{ALRM} = sub { die "timeout\n" };
        local $/ = "";
        alarm 10;
        @array = qx( hwinfo --usb );
        alarm 0;
    };

    # Catch and rethrow non timout errors
    die $@ if $@ && $@ ne "timeout\n";

    # Done
    return @array;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜