Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Now, I'd never thought I'd be stooping this low as to ask for example code, but IO::Select really has got me puzzled. I've fooled around with it, but to no avail. How would I look for UDP packets on three ports and simply print messages like:
"Port $port got packet $packet"
? Thanks

Replies are listed 'Best First'.
Re: IO::Select Difficulties
by mitd (Curate) on Jul 25, 2001 at 00:21 UTC

    use strict; use IO::Socket; use IO::Select; #set up some handles my $h1 = IO::Socket::INET->( LocalPort => $port_1, Proto => 'udp' ) or die "Couldn't start upd listen on $port_1: $@\n"; my $h2 = IO::Socket::INET->( LocalPort => $port_2, Proto => 'udp' ) or die "Couldn't start upd listen on $port_2: $@\n"; my $readable_h = new IO::Select(); $readable_h->add($h1); $readable_h->add($h2); while (1) { my @readable = $readable_h->can_read($timeout); foreach my $sock (@readable) { $buf = <$sock> # ## do something with buf # } }

    Coded quickly mising detail but should give you the right idea.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'