open/seek vs IO::Open/setpos

Discussion in 'Perl Misc' started by news@roaima.freeserve.co.uk, Dec 8, 2003.

  1. Guest

    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
    --
    @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}
     
    , Dec 8, 2003
    #1
    1. Advertising

  2. 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.
     
    Joost Diepenmaat, Dec 8, 2003
    #2
    1. Advertising

  3. Guest

    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/:DEFAULT :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}
     
    , Dec 9, 2003
    #3
  4. 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/: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.
     
    James Willmore, Dec 9, 2003
    #4
  5. Guest

    James Willmore <> wrote:
    >> $h->setpos (0, SEEK_SET) # use Fcntl qw/:DEFAULT :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}
     
    , Dec 12, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maki

    seek trough files in vhdl

    Maki, Jan 24, 2005, in forum: VHDL
    Replies:
    0
    Views:
    992
  2. news.verizon.net

    Win32 ADO Seek method

    news.verizon.net, Sep 9, 2003, in forum: Perl
    Replies:
    0
    Views:
    859
    news.verizon.net
    Sep 9, 2003
  3. Mad Scientist Jr
    Replies:
    1
    Views:
    5,459
  4. Olaf Baeyens

    Seek advice for ASP/PHP

    Olaf Baeyens, Oct 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    448
    Olaf Baeyens
    Oct 6, 2004
  5. Replies:
    3
    Views:
    143
    Andreas Perstinger
    May 14, 2013
Loading...

Share This Page