Sysread in perl

T

Tony

Hi,

I am trying to read the last 500 bytes of a file. I'm trying to use
sysread command, I have read the docs for it on cpan and it seems the
right thing to use.
My only problem is when i spcify a negative offset it fails with the
following error "Offset outside string at test.pl line 30.".

here is the line it is complaing about

sysread(F,$string,500,-500);

I have tried escaping the minus sign , putting it into a var and using
eval and they all come back with the same error.

Heres the paragraph from cpans docs

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

should this work or is it just me reading the distructions wrong.


thanks


Tony
 
G

gnari

Tony said:
Hi,

I am trying to read the last 500 bytes of a file. I'm trying to use
sysread command, I have read the docs for it on cpan and it seems the
right thing to use.
My only problem is when i spcify a negative offset it fails with the
following error "Offset outside string at test.pl line 30.".

this error message should have been a hint
Heres the paragraph from cpans docs

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

notice how it says 'string' but not 'file' everywhere
should this work or is it just me reading the distructions wrong.

a) if you expect sysread with OFFSET -500 to read last 500
bytes of file, then you are reading the 'distructions' wrong,
and should lookup sysseek

b) if you expect it to read into the last 500 bytes of your
string, then you need to make sure the string is at lead 500
bytes to start with, as the \0 padding only applies to
positive OFFSETS

gnari
 
T

Thomas Kratz

Tony said:
Hi,

I am trying to read the last 500 bytes of a file. I'm trying to use
sysread command, I have read the docs for it on cpan and it seems the
right thing to use.
My only problem is when i spcify a negative offset it fails with the
following error "Offset outside string at test.pl line 30.".

here is the line it is complaing about

sysread(F,$string,500,-500);

I have tried escaping the minus sign , putting it into a var and using
eval and they all come back with the same error.

Heres the paragraph from cpans docs

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

should this work or is it just me reading the distructions wrong.

It's you ;-) You are *reading* from the filehandle ( called F in your
example) and *writing* to $string. The offset specifies where in $string
you want to put the number of bytes specified in the 3rd parameter, not
where you start reading from the file.

If you want to read from the end of a file look at

perldoc -f seek
perldoc -q tail

or the modules

File::Tail
File::ReadBackwards

on CPAN

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top