jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

Due to some reason I cannot retrieve the current possition in the file that I'm reading. Here is a snippet:
use Fcntl 'SEEK_SET'; use IO::File ; my $fh = IO::File->new($file) ; $fh->binmode() ; $fh->read($binrec, 15) ; $pos = $fh->getpos() ; print "pos = $pos\n" ; ...... $fh->seek($pos,SEEK_SET) ; # go back
The variable $pos remains empty.... ?
What goes wrong here ?

Thanks
Luca

Replies are listed 'Best First'.
Re: HowTo retrieve the position in a file from the FH
by reasonablekeith (Deacon) on Mar 13, 2006 at 16:41 UTC
    You just want to use setpos, instead of seek.
    $fh->setpos($pos) ; # go back
    getpos returns an "opaque value" that represents the position in the file, it isn't just a numeric offset.
    ---
    my name's not Keith, and I'm not reasonable.
Re: HowTo retrieve the position in a file from the FH
by BrowserUk (Patriarch) on Mar 13, 2006 at 16:58 UTC

    You'd have a lot less trouble if you just used standard filehandles and related functions docuemented in perlfunc, rather than faffing around with those pseudo-object modules that each have their own and slightly different view of things and how things "should work".

    open my $fh, '<', $file or die $!; binmode $fh; my $binrec; read $fh, $binrec, 15; my $pos = tell $fh; print "pos = $pos\n"; ## prints 'pos = 15' .... seek $fh, $pos, SEEK_SET; ## go back

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Right. And we should get rid of the entire idea of filehandles-as-objects from perl6. Or is that the way we're going? I can't keep up.

      Instead, as reasonablekeith reasonably pointed out, the solution really is to RTFM. Looking at IO::File shows that it derives from IO::Seekable, among others. Since that seemed like a logical next place to look, I went there, found the documentation on getpos, and saw that setpos was right there as well. I tried it out, and it worked wonderfully. I went to post, and noticed that reasonablekeith already had posted exactly that, so I left it alone.

      (Alternate ways of doing things ++. Snide remarks about using a standard module --. Extending the argument ad absurdum, you get the idea to parse XML with built-in regular expressions in perlre because all the XML modules have their own and slightly different view of things and how things should work, when RTFM'ing will still get the job done quickly and reliably.)

        And we should get rid of the entire idea of filehandles-as-objects from perl6.

        Why would you/we/they? Won't they be the 'standard interface'? But that's a ways down the road yet.

        Snide remarks about using a standard module

        Sorry. It wasn't meant, nor do I consider it to be, a snide remark. Simply sage advice for someone starting out with perl whom appears to be rather confused by the profusion of ways of doing things. Start simple and progress.

        Pointing out that IO::File also had methods obj->tell() and $obj->seek(), that actually do what the OP is trying to do, might have been more useful to the OP than obscure references to opaque objects.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.