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