http://qs321.pair.com?node_id=305233


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

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.