Larry <> wrote:
> I have just read up on sysread and I was struck by the following:
>
> "*Attempts* to read LENGTH characters of data into variable SCALAR from
> the specified FILEHANDLE, using the system call read(2)."
>
> It made me wonder. What does that mean by "attemps" ?
If an error occurs, then it may not read the requested number of bytes.
If eof is reached, then it may not read the requested number of bytes.
If there is more than zero but less than the requested number of bytes
ready to be read, then the sysread will read just the number of bytes
that are currently ready, and not wait for the full requested number to
become ready. (If O_NONBLOCK is in effect, then strike "more than zero
but" from the above)
There maybe other instances where the attempt fails, like if the call gets
interrupted by a signal or something. It would be highly system dependent.
> I'm writing up a script to get binary data from <STDIN> (the script is
> run on a normal web server and the data is sent to it by using http's
> POST method)
>
> I have a binary header on top of the raw data so here's what I'm doing:
>
> # Get the header size: (4 bytes, int32)
> sysread(\*STDIN, $buf, 4);
It might be possible, but extremely unlikely, that this could return early
after reading less than 4 bytes, even in the absence of an error condition.
If I had to use sysread, I'd probably take that chance, myself. But why
not just use read? That will restart as needed until it gets 4 bytes,
unless there are errors.
>
> # Get the header:
> sysread(\*STDIN, $header, unpack("N", $buf) );
>
> # Get the raw data:
> while( sysread(\*STDIN, $raw, 204
)
> {
> ...raw data...
> }
>
> close STDIN;
>
> __END__;
>
> Suppose sysread was to read a long header data like 65000 bytes, will
> sysread actually be able to read all the data and store it on $header?
It will be able to, but it might not do so reliably.
> Do you think I should deal with that another way?
I'd probably just use "read"?
Xho
--
--------------------
http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.