Comment puis-je mettre en œuvre un délai d'attente pour une qx (commande)?
Question
Comment pourrais-je mettre en œuvre ce morceau de code un délai d'attente: si le « hwinfo --usb » -command n'a retourné quoi que ce soit après un certain laps de temps, (arrêt de la commande et) faire un retour ou mourir de la sous _usb_device.
#!/usr/bin/env perl
use warnings;
use strict;
sub _usb_device {
my @array;
{
local $/ = "";
@array = qx( hwinfo --usb );
}
...
...
}
La solution
Timeouts sont généralement fait avec .
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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow