Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Re: desire to pass file handles by value

by mandog (Curate)
on Nov 07, 2003 at 03:47 UTC ( [id://305225]=note: print w/replies, xml ) Need Help??


in reply to Re: desire to pass file handles by value
in thread desire to pass file handles by value

Thanks for the help. For now, I'm going to record the orignal file position on entry to the sub & restore it on exit.

#... my $pos=tell(FH); #... seek(FH,$pos,0); return

The file handle duplicating thing looks interesting though...

update: fixed dumb semantic error

Replies are listed 'Best First'.
Re: Re: Re: desire to pass file handles by value
by jdporter (Paladin) on Nov 07, 2003 at 05:44 UTC
    Now if only you could localize a variable that represents the file position, you wouldn't have to manually save/restore its value. Hmmm....
    package Tie::Scalar::FHPos; use FileHandle; sub TIESCALAR { my( $pkg, $fh ) = @_; bless [$fh], $pkg; } sub FETCH { my( $self ) = @_; $self->[0]->tell } sub STORE { my( $self, $pos ) = @_; $self->[0]->seek($pos,0); }
    Test:
    open F, "< foo.dat"; our $pos; tie $pos, 'Tie::Scalar::FHPos', \*F; { local $pos = 20; print "$pos: ", scalar(<F>); } print "$pos: ", scalar(<F>);
    Cool!

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

      Nice. It does have negative one side effect, though. If you are reading from some other file (say <STDIN>), and you reference $pos, the tell or seek will change perl's idea of the most recent input file to F. This will mess up the "<STDIN> line n" in any error/warning message, and also change what handle the $. var is looked up in.

      To fix this, put a local $.; in STORE and FETCH. (For some reason, this then triggers some warnings from STORE when $pos is localized. Fix by saying seek($pos||0,0)).

      If you are downvoting this for other than lack of interest, thanks for sending me a message or reply to let me know why so I can do better (or just stay quiet) next time.

        Ah, thanks for debugging that. When I tried it on STDIN, I got the fubar, and simply dismissed it with a "What do you expect, seeking back and forth on a non-file stream?" :-)
        But Perl is smarter than I am... as usual.

        jdporter
        The 6th Rule of Perl Club is -- There is no Rule #6.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://305225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-03-28 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found