Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Reading partial lines/strings from a pipe

by philcrow (Priest)
on Oct 10, 2007 at 18:06 UTC ( #644049=note: print w/ replies, xml ) Need Help??


in reply to Reading partial lines/strings from a pipe

When working with raw bytes, it pays to use the read method. It will block until something is available, then read up to a specified number of bytes:

# from Programming Perl my $buffer; while ( read $your_handle, $buffer, 1024 ) { # process buffer here }
That will read up to 1K from the handle, blocking if nothing is there, but returning whatever is there (the first 1K of it anyway).

The Perl Cookbook is an excellent reference for doing this sort of work.

Phil

The Gantry Web Framework Book is now available.


Comment on Re: Reading partial lines/strings from a pipe
Download Code
Re^2: Reading partial lines/strings from a pipe
by slightly72 (Initiate) on Oct 10, 2007 at 18:24 UTC

    Phil, thank you very much for the pointer and the book reference, it got me in the right direction.

    The final solution is almost like the one you suggested:

    my $buffer = ''; $rdr->blocking(0); while ($buffer eq '') { $rdr->read($buffer,1024); }

    $rdr->read is just an OO wrapper for read.

    The key to making this work was to use $rdr->blocking(0), which makes the pipe reading non-blocking. Otherwise a deadlock occurs -- maybe because the read command blocks while waiting for 1k to be filled, but not sure.

    Thank you,
    Tibi

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-12-19 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (77 votes), past polls