Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

read and sysread

by slloyd (Hermit)
on Mar 02, 2005 at 13:33 UTC ( [id://435814]=perlquestion: print w/replies, xml ) Need Help??

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

What is the real difference between 'read' and 'sysread'?

Replies are listed 'Best First'.
Re: read and sysread
by ambrus (Abbot) on Mar 02, 2005 at 13:37 UTC
    1. Sysread may return a parital result (that is, read less characters then requested) any time. Indeed, it must return exactly one packet with datagram sockets like udp. Read cannot do that except at the end of file.
    2. Sysread bypasses buffering, and also perlio layers to some extent.
    3. Update: sysread cannot be tied.
    4. Possibly more I don't know about.

    As a general rule, use read unless you have a reason to use sysread.

Re: read and sysread
by jbrugger (Parson) on Mar 02, 2005 at 13:44 UTC
    buffered or unbuffered, using or not using stdio read:
    Fixed-length buffered input from a filehandle

    read FILEHANDLE,SCALAR,LENGTH,OFFSET
    read FILEHANDLE,SCALAR,LENGTH

    Attempts to read LENGTH bytes of data into variable SCALAR from the specified FILEHANDLE. Returns the number of bytes actually read, 0 at end of file, or undef if there was an error. SCALAR will be grown or shrunk to the length actually read. An OFFSET may be specified to place the read data at some other place than the beginning of the string. This call is actually implemented in terms of stdio's fread call. To get a true read system call, see sysread().
    sysread:
    sysread function

    Fixed-length unbuffered input from a filehandle

    sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
    sysread FILEHANDLE,SCALAR,LENGTH

    Attempts to read LENGTH bytes of data into variable SCALAR from the specified FILEHANDLE, using the system call read. It bypasses stdio, so mixing this with other kinds of reads, print(), write(), seek(), or tell() can cause confusion because stdio usually buffers data. Returns the number of bytes actually read, 0 at end of file, or undef if there was an error. SCALAR will be grown or shrunk so that the last byte actually read is the last byte of the scalar after the read.

    An OFFSET may be specified to place the read data at some place in the string other than the beginning. A negative OFFSET specifies placement at that many bytes counting backwards from the end of the string. A positive OFFSET greater than the length of SCALAR results in the string being padded to the required size with "\0" bytes before the result of the read is appended.
Re: read and sysread
by manav (Scribe) on Mar 02, 2005 at 14:00 UTC
    from perldoc -f read

    The call is actually implemented in terms of stdio's fread(3) call. To get a true read(2) system call, see "sysread".

    Manav

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found