Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: Referencing globals clobbers data

by Joost (Canon)
on Apr 08, 2002 at 15:54 UTC ( [id://157465]=note: print w/replies, xml ) Need Help??


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

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;

Replies are listed 'Best First'.
Re: Re: Re: Referencing globals clobbers data
by demerphq (Chancellor) on Apr 08, 2002 at 16:24 UTC
    Yup. Thats much better....

    ;-)

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found