Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sysread

Reply
Thread Tools

sysread

 
 
Larry
Guest
Posts: n/a
 
      10-05-2008
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" ?

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);

# 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?
Do you think I should deal with that another way?

any help will be apreciated,

thanks
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      10-05-2008
Larry <(E-Mail Removed)> 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" ?



It may fail for the reasons given later in that same paragraph of the docs.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-05-2008
Larry <(E-Mail Removed)> 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.
 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      10-06-2008
In article <20081005192912.762$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)
wrote:

> 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.


this is something I forgot to tell you about:

"sysread bypasses buffered IO, so mixing this with other kinds of reads,
print, write, seek, tell, or eof can cause confusion because the perlio
or stdio layers usually buffers data."

what is buffered IO ?? how size is this buffer ??

May I just use read() for getting important data (like the header) then
use sysread to get the raw data??

thanks
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      10-06-2008
Larry wrote:
) this is something I forgot to tell you about:
)
) "sysread bypasses buffered IO, so mixing this with other kinds of reads,
) print, write, seek, tell, or eof can cause confusion because the perlio
) or stdio layers usually buffers data."
)
) what is buffered IO ?? how size is this buffer ??
)
) May I just use read() for getting important data (like the header) then
) use sysread to get the raw data??

Why do you not want to use read() to get the raw data ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      10-06-2008
In article <(E-Mail Removed)>,
Larry <(E-Mail Removed)> wrote:

> "sysread bypasses buffered IO, so mixing this with other kinds of reads,
> print, write, seek, tell, or eof can cause confusion because the perlio
> or stdio layers usually buffers data."
>
> what is buffered IO ?? how size is this buffer ??


Ok, I have found this:

use IO::Handle '_IOLBF';

$io->setvbuf($buffer_var, _IOLBF, 1024);

(still I don't know I need to set _IOLBF or _IOFBF)

Will it set input buffer also? I would like to keep it as small as
possible.

And yes, I'll be reading from STDIN by using read, not sysread:
(hopefully the following will work, untested yet)

use IO::Handle;

$io = new IO::Handle;
$io->setvbuf($buffer_var, _IOLBF, 1024);

if ( $io->fdopen(fileno(STDIN),"r") )
{
$io->read($buf, 4);
$io->read($header, unpack("N", $buf));

while( $io->read($raw, 1024) )
{
... raw data
}

}
 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      10-06-2008
In article <(E-Mail Removed)>,
Larry <(E-Mail Removed)> wrote:

> And yes, I'll be reading from STDIN by using read, not sysread:
> (hopefully the following will work, untested yet)
>


I tried the following and it worked great (although I don't know how
much '_IONBF' affected the script)

Do you think I should read the header in small chunks or I can read it
all on the fly with just one read() call ??

If I set the internal buffer to be as small as possible, will I be able
to read the header all at once with just one reda() call ??

Still, I don't know what size the internal buffer is...

#!/usr/bin/perl

use strict;
use warnings;
use IO::Handle '_IONBF';

my $io = new IO::Handle;
my $buf;
my $len;
my $header;

if( $ENV{"REQUEST_METHOD"} eq 'GET' || $ENV{"REQUEST_METHOD"} eq 'HEAD' )
{
close(STDIN);
print "Content-type: text/plain\n\n";
print "Hello World!\n";
exit;
}

if( $ENV{"REQUEST_METHOD"} ne 'POST' )
{
close(STDIN);
print "Content-type: text/plain\n\n";
print "What are you trying to do?\n";
exit;
}

open my $fh1, ">", "rawdata.txt" or die "$!\n";
binmode $fh1;

if ( $io->fdopen(fileno(STDIN),"r") )
{

$io->read($len, 4);
$io->read($header, unpack("N", $len));

{open my $fh2, ">", "header.txt" or die "$!\n";
binmode $fh2;
print $fh2 $header;
close $fh2;}

while( $io->read($buf, 1024) )
{
print $fh1 $buf;
}

$io->close;
}

close $fh1;

print "Content-type: text/plain\n\n";
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      10-06-2008
Larry <(E-Mail Removed)> wrote:

> what is buffered IO ??



Suffering from Buffering?

http://perl.plover.com/FAQs/Buffering.html


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-06-2008
Larry <(E-Mail Removed)> wrote:
> In article <20081005192912.762$(E-Mail Removed)>, (E-Mail Removed)
> wrote:
>
> > 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.

>
> this is something I forgot to tell you about:
>
> "sysread bypasses buffered IO, so mixing this with other kinds of reads,
> print, write, seek, tell, or eof can cause confusion because the perlio
> or stdio layers usually buffers data."
>
> what is buffered IO ??


IO that is buffered by perl or by C on behalf of perl.

> how size is this buffer ??


That depends on your system. Why do you care? The point of using
high-level languages is that you usually don't need to worry about such
things.

> May I just use read() for getting important data (like the header) then
> use sysread to get the raw data??


That would be mixing sysread with "other kinds of reads", which as you
just quoted, is a bad idea.

Why are you bound and determined to shoot yourself in the foot?

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.
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-06-2008

Larry wrote:
> I have just read up on sysread and I was struck by the following:

....
> 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)

....
> any help will be apreciated,


If I was having trouble reinventing a wheel, I'd consider using the free
wheel.

According to the docs, CGI.pm handles binary uploads of this type
without requiring you to use sysread in your code.

Just my 0.02 worth.
--
RGB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`sysread' end of file reached `rbuf_fill' problem Mento Ruby Ruby 0 12-11-2006 10:21 AM
IO#sysread on windows pihentagy Ruby 3 06-14-2006 10:33 PM
Timeout Error or sysread': end of file reached (EOFError) Muazzam Mushtaq Ruby 0 03-28-2006 05:05 AM
invalid arg to sysread deep within protocol.rb William E. Rubin Ruby 3 12-08-2005 09:53 PM
sysread and buffered I/O Hal Fulton Ruby 39 07-24-2004 03:45 AM



Advertisments