in reply to Referencing globals clobbers data
in thread Tie-ing hashes clobbers data

The quickest way to solve this is to change the offending line with:
return {%data};
Hmm, so now he makes a copy of a copy of a copy.... I dont think this is the best call. Do away with the glob semantics and this all becomes very straightforward...

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.

Replies are listed 'Best First'.
Re: Re: Referencing globals clobbers data
by Joost (Canon) on Apr 08, 2002 at 15:54 UTC
    That's why it's called the quick fix :-)

    How about...

    package FileHash; use strict; use warnings; use Tie::Hash; use Data::Dumper; our @ISA = ("Tie::Hash"); sub TIEHASH { my ($class, $file) = @_; my $s = bless { FILE => $file, DATA => {}, },$class; $s->_read(); return $s; } sub STORE { $_[0]->{DATA}->{$_[1]} = $_[2] } sub CLEAR { $_[0]->{DATA} = () } sub FETCH { $_[0]->{DATA}->{$_[1]} } sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]->{DATA}} } sub NEXTKEY { each %{$_[0]->{DATA}} } sub EXISTS { exists $_[0]->{DATA}->{$_[1]} } sub DELETE { delete $_[0]->{DATA}->{$_[1]} } sub DESTROY { $_[0]->_write } # ----- private subs ----- sub _write { # still need to deal with race conditions my $file = $_[0]->{FILE} ; open (FH, "> $file") or die "Can't open $file for FileHash _write: +$!"; print FH Dumper($_[0]->{DATA}); # writes out as $VAR1 = { ..... } close FH or die "Can't close $file for FileHash _write: $!"; } sub _read { # still need to deal with race conditions my $self = shift; return unless -f $self->{FILE}; $self->{DATA} = do $self->{FILE}; } 1;
      Yup. Thats much better....

      ;-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.