http://www.perlmonks.org?node_id=985342


in reply to Re: Stumped with select->can_read (buffered)
in thread Stumped with select->can_read

That makes sense. Would I have to use read() in this case?

I tried doing:

while(@ready = $sel->can_read) { foreach my $fh(@ready) { my $line = ""; my $buf = ""; while(read($fh, $buf, 1024)) { $line .= $buf; } } }

Didn't work for me, maybe because LENGTH (=1024) is too big? Sorry, I'm not familiar with read(), as you can see :/

EDIT: nvm. I'm reading the link you posted now. Will report back if I still can't figure it out. Thanks!

Replies are listed 'Best First'.
Re^3: Stumped with select->can_read (buffered)
by zentara (Archbishop) on Aug 03, 2012 at 20:43 UTC
    It's best to use sysread, but you will have to figure out a way to detect line endings. Maybe after concating the sysread data into a temp buffer then pull off lines with a regex or split. Read perldoc -f sysread. It will read as much as it can in a non-blocking manner. This will try to read in 1024 byte chunks, but will read less if that is all that is there.
    while(@ready = $sel->can_read) { foreach my $fh(@ready) { my $line = ""; my $buf = ""; # while(read($fh, $buf, 1024)) while( my $bytes_read = sysread( $fh, $buf, 1024 ) > 0 ) ) { print "$bytes_read\n"; { $line .= $buf; } } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      I tried doing that, but it didn't seem to work. My guess is that because one particular $fh is very busy, the while loop never ends?

      This *seems* to work at the moment:

      while(@ready = $sel->can_read) { foreach my $fh(@ready) { my $line = ""; sysread( $fh, $line, 1024 ); } }

      My concern now is whether 1024 is too small of a buffer that I could miss something (if that's possible), or missing a newline character because of the buffer size. Would it be horrible (performance wise) to read one byte at a time until I reach a newline character, thus forming a real $line?

      Thanks again!

      Pedro

        My concern now is whether 1024 is too small of a buffer that I could miss something (if that's possible)

        If there are more than 1024 bytes to be read, then the next call to can_read() will tell you about them.

        - tye        

        Would it be horrible (performance wise) to read one byte at a time until I reach a newline character, thus forming a real $line?

        It's been done before. :-) It certainly is inefficient. What I would do, is sysread as big of chunks as you can, then concat everything into a temp buffer, then split the buffer on newlines into an @array. If the scalar @array is greater than 1, then you have a line. Return the last @array element to your $temp_buffer, and process the rest of the @array elements as lines. It's called a sliding buffer, and you can google for "perl sliding buffer" for examples.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh