Skip to content

Instantly share code, notes, and snippets.

@phluks
Created Oct 23, 2012
Embed
What would you like to do?
Perl AnyEvent: How to make async calls in a loop?
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use AnyEvent;
use AnyEvent::Util;
# OBS: This doesn't work!
for my $n (1..100) {
my $rand = int(rand(10));
my $hdl;
$hdl = AnyEvent::io (
fh => \*STDIN,
poll => 'r',
cb => sub {
my $str = `sleep $rand; echo $n`;
output($str);
}
);
}
# Hmm - args
sub output {
my $n = shift;
say "Got: $n";
}
__END__
perl testAny.pl
illegal file descriptor or filehandle (either no attached file descriptor or illegal value): at /usr/local/lib64/perl5/AnyEvent/Impl/EV.pm line 55.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment