develooper Front page | perl.perl5.porters | Postings from March 2000

Re: read(FH,$buffer,2**31 - 1)

Thread Previous
Tom Christiansen
March 15, 2000 15:56
Re: read(FH,$buffer,2**31 - 1)
Message ID:
>Hmm.  Why doesn't Perl like reading a lot from STDIN?

>  perl -Mdiagnostics -e 'read(FOO,$FOO,2 ** 31 - 1)'

>runs smoothly.

>  perl -Mdiagnostics -e 'read(STDIN,$STDIN,2 ** 31 - 1)'

>Out of memory during "large" request for -2147479552 bytes at -e line 1

>    (F) The malloc() function returned 0, indicating there was insufficient
>    remaining memory (or virtual memory) to satisfy the request. However,
>    the request was judged large enough (compile-time default is 64K), so
>    a possibility to shut down by trapping this error is granted.

Perhaps if you actually read from something you could read from 
you might get a different answer.

    % perl -Mdiagnostics -e '*FOO = *STDIN; read(FOO,$FOO,2 ** 31 - 1)'
    panic: realloc at -e line 1 (#1)
	(P) Something requested a negative number of bytes of realloc.
    Uncaught exception from user code:
	    panic: realloc at -e line 1.

Word to the wise: always read in only as much as is there, and
always check you got what you asked for.  And don't be trying to
go preallocating these (off_t (-1)) sized buffers.  That's a crackhead

In some cases, this is ok not to, in others, it isn't.  If you know
how much is there, certainly check.  If you don't know how much is
there, use a block at a time--very carefully.  You need to understand
about partial reads and writes.

    use Errno qw/EINTR/;
    $blksize = (stat FROM)[11] || 16384;  # preferred block size?
    while ($len = sysread FROM, $buf, $blksize) {
        if (!defined $len) {
            next if $! == EINTR;
            die "System read error: $!\n";
        $offset = 0;
        while ($len) {          # Handle partial writes.
            $written = syswrite TO, $buf, $len, $offset;
            die "System write error: $!\n" unless defined $written;
            $offset += $written;
            $len    -= $written;


Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About