Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > open/seek vs IO::Open/setpos

Reply
Thread Tools

open/seek vs IO::Open/setpos

 
 
news@roaima.freeserve.co.uk
Guest
Posts: n/a
 
      12-08-2003
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/EFAULT :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
--
@s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      12-08-2003
On Mon, 08 Dec 2003 16:48:35 +0000, new wrote:

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


 
Reply With Quote
 
 
 
 
news@roaima.freeserve.co.uk
Guest
Posts: n/a
 
      12-09-2003
Joost Diepenmaat <> wrote:
>> $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.


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/EFAULT :seek/

Thoughts?
Chris
--
@s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}
 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      12-09-2003
On Tue, 9 Dec 2003 09:11:50 +0000
wrote:
<snip>
> 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/EFAULT :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.
 
Reply With Quote
 
news@roaima.freeserve.co.uk
Guest
Posts: n/a
 
      12-12-2003
James Willmore <> wrote:
>> $h->setpos (0, SEEK_SET) # use Fcntl qw/EFAULT :seek/


> 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
--
@s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}
 
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




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57