in reply to simple tie?

Try something like :
Package Tie::Hash::SaveConfig; use strict; use vars qw(@ISA); @ISA = qw(Tie::StdHash); sub TIEHASH { my $class = shift; my %hash; bless \%hash,$class; } sub STORE { my ($self,$key,$value) = @_; $self->SaveConfig($key,$value,$self->{$key}); $self->{$key} = $value; #Redefine value return $value; } sub DELETE { my ($self,$key) = @_; $self->SaveConfig($key,$self->{$key}); return $self->{$key}; } sub SaveConfig { my $self = shift @_; #Do something here } } 1; __END__
Then tie it as follows...
use Tie::Hash::SaveConfig; my %hash = (); tie %hash,'Tie::Hash::SaveConfig'; %hash = (Foo=>"Bar");
Check davorgs perl.com article

Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.

Replies are listed 'Best First'.
Re: Re: simple tie?
by Hofmator (Curate) on May 27, 2002 at 13:21 UTC

    Good answer, Beatnik! Allow me to suggest a simplification to your solution. Use SUPER;) and you don't need the TIEHASH routine.

    Package Tie::Hash::SaveConfig; @ISA = qw(Tie::StdHash); use strict; sub STORE { my $self = shift; SaveConfig(); $self->SUPER::STORE(@_); # or (probably better) here: # SaveConfig(); } sub SaveConfig { # do something ... print "Config saved\n"; } 1;

    Update: Note merlyn's comment below. I put an additional comment into the code. From the original post, though, it's not clear what is actually wanted.

    -- Hofmator

      SaveConfig(); $self->SUPER::STORE(@_);
      I'd swap those two lines: that way the hash is in the new state before the config is saved, unless the point was to save the old config.

      -- Randal L. Schwartz, Perl hacker

        Error: Variable "%userdata" is not imported at library.pl line 242. Here is the code:
        sub some_sub { require 'wpconfig.cgi'; tie(%config, 'tie::config'); tie(%userdata, 'tie::config'); } package tie::config; @ISA = qw(Tie::StdHash); use strict; sub STORE { my $self = shift; $self->SUPER::STORE(@_); saveConfig(); } # saveConfig: This is what actually saves wpconfig.cgi. sub saveConfig { my $fh = main::open '>wpconfig.cgi'; print $fh "# DO NOT MODIFY THIS FILE\n# IT IS GENERATED DYNAMICALL +Y!!\n\n"; print {$fh} main::genHash(config); # genHash just takes the hash a +nd generated perl code for it. print {$fh} main::genHash(userdata); print $fh "\n1;"; close $fh; } package main; # exit: Exits and unties %config and %userdata. sub exit { untie(%config) if %config; untie(%userdata) if %userdata; # <- line 242 CORE::exit(); }
        This works fine (not errors) but it won't update the file.
        sub some_sub { require 'wpconfig.cgi'; tie(%config, 'tie::config'); tie(%userdata, 'tie::config'); } package tie::config; sub TIEHASH {} sub STORE { my $self = shift; $self->SUPER::STORE(@_); saveConfig(); } # saveConfig: This is what actually saves wpconfig.cgi. sub saveConfig { my $fh = main::open '>wpconfig.cgi'; print $fh "# DO NOT MODIFY THIS FILE\n# IT IS GENERATED DYNAMICALL +Y!!\n\n"; print {$fh} main::genHash(config); print {$fh} main::genHash(userdata); print $fh "\n1;"; close $fh; } package main; # exit: Exits and unties %config and %userdata. sub exit { untie(%config) if %config; untie(%userdata) if %userdata; CORE::exit(); }
      Well I didn't really know what he wanted :) but a ++ ! :)

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.