Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: OO - problem with inheritance

by frag (Hermit)
on Jan 14, 2002 at 20:27 UTC ( [id://138601]=note: print w/replies, xml ) Need Help??


in reply to OO - problem with inheritance

It sounds like you're wrestling with the question of how to create class data so that the accessors are inherited but not the data itself.

I think you'll find perltootc - Tom's OO Tutorial for Class Data in Perl very useful, in particular the section Inheritance Concerns. Borrowing code from that, you can use something like this:

package base_class; my %parameter_config = {# the same as before...}; # accessor for class-specific hash named %parameter_config: sub parameter_config { my $obclass = shift; my $class = ref($obclass) || $obclass; my $varname = $class . "::parameter_config"; no strict "refs"; # to access package data symbolically %$varname = {foo=>'bar'}; # this is the package-local hash %parameter_config. # Just refering to '%parameter_config' should refer to # %base_class::parameter_config. }

To initialize the package-specific %parameter_config, throw in similar magic into &base_class::new.

-- Frag.
--
"It's beat time, it's hop time, it's monk time!"

Replies are listed 'Best First'.
Re: Re: OO - problem with inheritance
by uwevoelker (Pilgrim) on Jan 14, 2002 at 20:47 UTC
    > It sounds like you're wrestling with the question of how to create class data so that the accessors are inherited but not the data itself.
    I would like the accessors _and_ data inherit.

    In base_class is the big $parameter_config hashref. And there is a constructor new() which execlusively deals with this hashref. He gets his values by $parameter_config->{..}{..} and so on. But the other modules, which inherit from base_class only submit their changes to that huge config-hashref. Because this can't be done directly, I wrote two subroutines in base_class: valid_parameter and config_parameter. I want them to store the additional information in this hashref. But - and here is my problem - this hashref should be unique for each datatype/class. So I would like to access the hashref in base_class but the contents should be specific to the calling class.

    I agree, that this is very naive. Now I'm searching for an other approach. Have you written my reply to Ilya's answer? I would like to use the last method: copy the hash and change it a little bit. But because it's an hashref, I think this won't work...

    To your suggestion:
    I need the %parameter_config hash before new() is called, because new() needs this information to figure out, if the parameters given to new() are correct. So I need code I can run when the class is loaded.

    Thank you, good bye, Uwe
      So I would like to access the hashref in base_class but the contents should be specific to the calling class.

      I'm a little confused about what you're doing. You want each change submitted to the base_class's %parameter_config for confirmation; after that, the actual values get stored in each subclass's own %parameter_config, right? If so, just change your accessors so that before fiddling with %sub_class::parameter_config it first messes with %base_class::parameter_config, to do whatever checking/storing you want done there, and then store the data in each individual subclass's hash. (Or, if you follow your new approach, replace "fiddling with %base_class::parameter_config" with "call validation methods from ValidateNewParameter". Same principle.)

      I need the %parameter_config hash before new() is called, because new() needs this information to figure out, if the parameters given to new() are correct. So I need code I can run when the class is loaded.

      OK, so put something like this at the start of each subclass:

      use vars qw(%parameter_config); # or 'our' but NOT 'my', not if you # use the approach I gave before %parameter_config = &base_class::parameter_config;
      And now each subclass has its own copy of %parameter_config. Just make sure that &base_class::parameter_config returns a copy of the hash.

      -- Frag.
      --
      "It's beat time, it's hop time, it's monk time!"

        Hello Frag,
        thank you for your answer. I agree with you that I could solve my problem by copying the hash in ¶meter_config. But instead I go for ValidateNewParameter.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-03-28 18:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found