Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Stumped with select->can_read (buffered)

by w1r3d (Initiate)
on Aug 03, 2012 at 21:07 UTC ( #985354=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^4: Stumped with select->can_read (buffered)
Download Code
Re^5: Stumped with select->can_read (buffered)
by tye (Cardinal) on Aug 03, 2012 at 21:24 UTC
    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        

Re^5: Stumped with select->can_read (buffered)
by zentara (Archbishop) on Aug 04, 2012 at 08:00 UTC
    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
      sysread as big of chunks as you can, then concat everything into a temp buffer

      sysread can read onto the end of the buffer, avoiding the need to copy the data again:

      sysread( $fh, $buf, 16*1024, length($buf) );

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985354]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-07-11 12:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls