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

Hello there,

Arriving at STDIN is some bytes :

123456789012345678901234567890123456789012
|<--reads-->|<--what happens to this ?-->|

$rv = read(STDIN, $data, 12);

What happens to the data in the STDIN buffer, if that data is greater than the 12 bytes ?

1...the unthinkable.
2...it just evaporates.
3...it stays where it is.
4...it gets sent directly to /dev/null without passing go.
5...<--fill in you're answer here-->


thanks very much indeed,

abachus.

  • Comment on What happens to the bytes not read by read() ?

Replies are listed 'Best First'.
Re: What happens to the bytes not read by read() ?
by bobf (Monsignor) on Dec 11, 2005 at 07:47 UTC

    This test indicates the data is still available for additional reading.

    use strict; use warnings; my $data; my $bytes = read( DATA, $data, 10 ); print "read $bytes bytes: [$data]\n"; # read 10 bytes: [1234567890] $bytes = read( DATA, $data, 5 ); print "read $bytes bytes: [$data]\n"; # read 5 bytes: [abcde] __DATA__ 1234567890abcdefg

    Update: As mentioned in the docs, read returns the number of bytes actually read, 0 at end of file, or undef if there was an error. This means you can loop until read returns false:

    use strict; use warnings; my $data; while( read( DATA, $data, 5 ) ) { print "[$data]\n"; } __DATA__ Just another Perl hacker,

    This prints

    [Just ] [anoth] [er Pe] [rl ha] [cker,]

Re: What happens to the bytes not read by read() ?
by GrandFather (Saint) on Dec 11, 2005 at 10:22 UTC

    If you close the file handle before they are read the garbage collector will come along and take them to the recycling centre for you. You may end up using them later in the program, but they will have been cleaned and sanitised so you probably won't recognise them.

    If they remain unread by the time the program exits the system takes all the bytes back. You don't get a refund.


    DWIM is Perl's answer to Gödel