Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

STDIN inheritance during exec and difference between getc and sysread

by bdimych (Monk)
on Feb 25, 2013 at 19:48 UTC ( #1020557=perlquestion: print w/replies, xml ) Need Help??

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

Hi all!

I've accidentally found that "getc" and "sysread" behave differently after "exec":
getc does not get remaining data from the STDIN but sysread does it

echo -n ab | perl -e "print getc; exec 'perl', '-e', 'print getc'"
prints only "a", but
echo -n ab | perl -e "sysread(STDIN, \$b, 1); print \$b; exec 'perl', +'-e', 'sysread(STDIN, \$b, 1); print \$b'"
prints "ab"

why so? where is the "b" in the first case?

Replies are listed 'Best First'.
Re: STDIN inheritance during exec and difference between getc and sysread
by BrowserUk (Patriarch) on Feb 25, 2013 at 20:14 UTC

    getc is buffered IO. The first call to a buffered IO call causes a full buffer of data -- 4096 bytes or as much as is available -- to be read from the device into the buffer. Then, in the case of getc(), just the first character of that input is returned to the caller. Subsequent calls to getc() return their data from that buffer.

    Sysread on the other hand only reads as much as is requested from the device. Subsequent sysreads need to go back to the device for more.

    The implication of your findings is that whilst the filehandle is shared across exec; the buffers used by buffered IO are not. Which is probably as it should be.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1020557]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2023-12-08 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (35 votes). Check out past polls.

    Notices?