Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl read()

Does it empty it

         

Robber

3:49 pm on Oct 8, 2002 (gmt 0)

10+ Year Member



Does something such as the following empty STDIN so that you wouldnt be able to read to it later and would have to access the value through $buffer?

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

jatar_k

7:34 am on Oct 9, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



perl anyone?

Robber

8:28 am on Oct 9, 2002 (gmt 0)

10+ Year Member



Thanks for the bump jk,

Just a thought, could it be that the filehandle (STDIN) used in the read function needs to be closed before it is used elsewhere in the script - just like you would close something opened with open(), so perhaps my script should look like this:

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
xyz
.
.
.
close STDIN;

Cheers

volatilegx

6:56 pm on Oct 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the close STDIN is not necessary.

I believe I have made multiple reads of STDIN in one of my more boneheaded attempts at scripting. I don't think the handle is cleared when read.

andreasfriedrich

8:21 pm on Oct 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



STDIN is not cleared indeed, but to read again what you´ve already read you would need to seek [perldoc.com] to a previous position in STDIN.

A simple test case to check whether STDIN is cleared or not.

Assuming a little file ac.txt with the rather informative text 'Aaron Carter' in it the following code

[af@server Ecotur]$ perl -e 'open IN, "<ac.txt";<IN>; 
print tell(IN), "\n"; seek IN, 0, 0;
print tell(IN), "\n"; print read(IN, $x, 20), "\n";
print "$x\n";'

produces the following output:

12 
0
12
Aaron Carter

A filehandle is opened, the first record is read, the position in the file is 12, we seek to the beginning, the position is 0, we try to read 20 bytes but get only 12, read put "Aaron Carter" into $x.

If STDIN is not cleared, this code should have the same output:

[af@server Ecotur]$ perl -e '<STDIN>; 
print tell(STDIN), "\n"; seek STDIN, 0, 0;
print tell(STDIN), "\n"; print read(STDIN, $x, 20), "\n";
print "$x\n";' < ac.txt

It does. QED. :)

Andreas