Hi,
to quote from perldoc -f select:
WARNING: Do not attempt to mix buffered I/O (like read() or <FHE>)
with select(). You have to use sysread() instead.
Thus, none of the examples will work reliably - just because socket is
ready-for-read does not mean that you can read whole line.
Using sysread means some extra work, but is reliable.
On Sunday 03 June 2001 00:23, Abdulaziz Ghuloum wrote:
> Hello,
>
> I'll try to explain what I think is happening, hoping it may be helpful.
>
> my @replies ;
> # here we're creating an IO::Select object, good
> my $sel = IO::Select->new();
> # and adding the socket handle, good also
> $sel->add($socket);
>
> # we wait until there are available data to be read.
> # This call will block until we have data on one of the added sockets.
> while (@ready = $sel->can_read())
> {
> foreach (@ready) # we iterate through all sockets with data
> {
> push @replies, <$_> ; # we read from this handle until we reach EOF
> # adding every line to @replies. If the server sends us one line only,
> we'll wait
> # until it finishes sending everything it has.
> }
> # here, we're done reading everything from every socket. why are we
> iterating again?
> }
>
> The problem I believe is caused by the push statement. push takes an array
> and a LIST and appends the list to the array. So, it evaluates the <$_> in
> list context.
>
> Here is what *might* work:
>
> #!/usr/bin/perl -w
> use IO::Socket;
> use IO::Select;
>
> my $socket = IO::Socket::INET->new( PeerAddr => 'www.yahoo.com:80') or die
> "Connect Error\n";
>
> print $socket "GET /\n\n";
> my @replies ;
> my $sel = IO::Select->new();
> $sel->add($socket);
>
> while (@ready = $sel->can_read()){
> foreach (@ready){
> my $l = <$_>;
> close $_ and next unless defined $l;
> push @replies, $l ;
> }
> }
>
> print "@replies\n";
>
> On Sat, 2 Jun 2001 18:05:47 -0700 (PDT), David Milne said:
> > Hi,
> > I am having problems with the IO::Select classes.
> > I am trying to read a long string from a pop server.
> > Accordingly, I have something along the lines of
> >
> > .....
> > my @replies ;
> > my $sel = IO::Select->new();
> > $sel->add($socket);
> >
> > while (@ready = $sel->can_read())
> > {
> > foreach (@ready)
> > {
> > push @replies, <$_> ;
> > }
> > }
> >
> > Problems:
> > print statements (not shown here) tell me that
> >
> > 1) Socket appears to block, although @ready should
> > only contain a list of readable filehandles.
> >
> > 2) If Server closes the connection, the while
> > condition evaluates to true always and it loops
> > forever.
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
--
Ondrej Par
Internet Securities
Software Engineer
e-mail: [EMAIL PROTECTED]
Phone: +420 2 222 543 45 ext. 112