Click to See Complete Forum and Search --> : Perl: read function


StuPeas
09-05-2006, 01:28 PM
In all the books I have read it states that "you can do a read INTO THE MIDDLE OF A STRING" using the syntax:

read (FILEHANDLE, $buf, LENGTH, OFFSET);

I wrote a short script to test this and it doesnt appear to work (see below)

open (READFILE, "reading.txt") || die "$!";
#see below for text file "reading.txt".

$written = "This text is already here.";

read (READFILE, $written, 23, 10);

print $written;



The text file is below (stored in same directory as the script!!):

This is the first line
This is the second line
This is the third line

If you could do a read INTO THE MIDDLE OF A STRING (In this case, $written), then I would have expected an output of:

This text This is the first line is already here

Instead, all the text after the ofset position (In our case "is already here") is overwritten giving the output below:

This text This is the first line

So it appears that you cannot do a read INTO the middle of a string, only overwrite the text FROM any position in a string.

Can antbody advise me if I have wrongly interpreted the books, or indeed if the books are wrong (i doubt it though)

dragle
09-05-2006, 03:52 PM
Hi,

Later versions of perlfunc drop the "into the middle of the string" logo. Try parsing perldoc -f read, perldoc perlfunc, or

http://perldoc.perl.org/functions/read.html

for an updated description.

Cheers!

StuPeas
09-05-2006, 06:12 PM
Nice one Dan, I thought it may be a wording problem