open/seek vs IO::Open/setpos

N

news

I wouldn't be surprised if I'm doing something stupid here, but for the
life of me I can't see it.

Compare the following two snippets, which write and then rewrite a
sample file.

1. IO::Open and setpos

use IO;
use Fcntl;
use strict;

unlink "/tmp/x"; # Testing
my $h = new IO::File "+>/tmp/x" or die "IO::File: $!\n";
$h->print ("hello, world\n") or warn "hello: $!\n";
$h->setpos (0) or warn "setpos: $!\n";
$h->print ("goodbye\n") or warn "goodbye: $!\n";
$h->close or warn "close: $!\n";

2. open and seek

unlink "/tmp/x"; # Testing
open H, "+>/tmp/x" or die "open: $!\n";
print H "hello, world\n" or warn "hello: $!\n";
seek H, 0, 0 or warn "seek: $!\n";
print H "goodbye\n" or warn "goodbye: $!\n";
close H or warn "close: $!\n";

In case (1) I get "setpos: Invalid argument". Case (2) works.

Originally I had read the documentation for setpos to require the
following line, but that just generated a runtime usage error.

$h->setpos (0, SEEK_SET) # with Fcntl qw/:DEFAULT :seek/

Any suggestions, please? I'd prefer to use IO::File syntax, since it
gives me a little more flexibility and convenience. I've perl version
5.6.1 here.

Cheers,
Chris
 
J

Joost Diepenmaat

I wouldn't be surprised if I'm doing something stupid here, but for the
life of me I can't see it.
....

$h->setpos (0) or warn "setpos: $!\n";

setpos does not do wat you think it does; from the IO::Seekable
manpage:

$io->setpos
Uses the value of a previous getpos call to return to
a previously visited position. Returns "0 but true" on
success, "undef" on failure.

HTH,
Joost.
 
N

news

setpos does not do wat you think it does; from the IO::Seekable
manpage:
$io->setpos
Uses the value of a previous getpos call to return to
a previously visited position. Returns "0 but true" on
success, "undef" on failure.

Hmm. Yes had I read this - but assumed that getpos() would return a (perl)
integer offset into the file like its C library counterpart. Unfortunately
it transpires that it doesn't - it's an opaque (string/binary) value.

Thanks for (re-)pointing this out!

Now, do you have any suggestions for the next paragraph in that same page:

$io->setpos ( POS, WHENCE )
Seek the IO::File to position POS, relative to WHENCE:

WHENCE=0 (SEEK_SET)
POS is absolute position. (Seek relative to the start
of the file)

I can't get this to work at all - I get a runtime usage error,
"Usage: IO::Seekable::setpos(handle, pos) at ..."

e.g.
$h = new IO::File ... # O_RDWR or "+> file", etc.
$h->print ...
$h->setpos (0, SEEK_SET) # use Fcntl qw/:DEFAULT :seek/

Thoughts?
Chris
 
J

James Willmore

On Tue, 9 Dec 2003 09:11:50 +0000
I can't get this to work at all - I get a runtime usage error,
"Usage: IO::Seekable::setpos(handle, pos) at ..."

e.g.
$h = new IO::File ... # O_RDWR or "+> file", etc.
$h->print ...

my $pos = $h->getpos();

Then ....
$h->setpos (0, SEEK_SET) # use Fcntl qw/:DEFAULT :seek/


You have to 'getpos' first - if I read the documentation properly.
Then, 'setpos'.

From IO::Seekable ....
$io->setpos
Uses the value of a previous getpos call to return to
a previously visited position. Returns "0 but true" on
success, "undef" on failure.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Malek's Law: Any simple idea will be worded in the most
complicated way.
 
N

news

You have to 'getpos' first - if I read the documentation properly.
Then, 'setpos'.

Given that getpos returns an opaque value, it seems to me that providing
a WHENCE value (i.e. SEEK_SET in my example) is meaningless. Can anyone
provide a counter-example?

Cheers,
Chris
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top