Discussion:
Timeout & Saisie
(trop ancien pour répondre)
ebrnospam
2013-02-09 17:57:28 UTC
Permalink
Bonjour à tous,

j'essaye d'écrire ce petit algorithme en PERL mais sans succès :

comptearebours = 10secondes;

faire
afficher un calcul // Exemple 3*6
lire le résultat
tant que ( résultat non correct && comptearebour != 0 )

j'ai tenté d'utiliser alarm, AnyEvent mais sans grand succès jusqu'alors ...

Pourriez vous m'aider ?

Merci

Eric
Jean-Louis Morel
2013-02-10 13:45:07 UTC
Permalink
Post by ebrnospam
comptearebours = 10secondes;
faire
afficher un calcul // Exemple 3*6
lire le résultat
tant que ( résultat non correct && comptearebour != 0 )
j'ai tenté d'utiliser alarm, AnyEvent mais sans grand succès jusqu'alors ...
Pourriez vous m'aider ?
Avec la fonction alarm, la construction basique est du genre :

#!/usr/bin/perl
use strict;
use warnings;
$|++;

my $timeout = 10;

eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $timeout;
job();
alarm 0;
};

if ($@) {
die unless $@ eq "alarm\n";
print "\nTrop tard !\n";
}

sub job {
print "3 * 6 = ";
while ( <STDIN> != 18 ) {
print "incorrect\n3 * 6 = ";
}
print "ok\n";
}

__END__

Script testé avec Perl 5.14 sous Ubuntu.
Ne fonctionne pas sous Windows (la fonction alarm n'est pas implémentée).

HTH.

--
J-L
http://www.bribes.org/perl

Loading...