in reply to Re: simple tie?
in thread simple tie?

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

Replies are listed 'Best First'.
•Re: Re: Re: simple tie?
by merlyn (Sage) on May 27, 2002 at 13:37 UTC
    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(); }
        Look, a couple of things here.

        Why have you stuck a package in the middle of another? Thats a recipe for madness. Also as merlyn stated using 'tie::config' is going to get you burned. It should be Tie::Config or something (i think that name is taken (it is: Tie::Config and a bunch more as well)).

        Second if you want this problem solved why dont you just download Config::Inifile. Even if you are on a unix platform it does eactly what you are looking for.

        If you want to have two packages in the same file then at least save yourself the headache and put them in contiguous blocks.

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

Re: Re: Re: simple tie?
by Beatnik (Parson) on May 27, 2002 at 13:24 UTC
    Well I didn't really know what he wanted :) but a ++ ! :)

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