in reply to Re (tilly) 5: Inheriting object data
in thread Inheriting object data

Well, I modified it to use a has-a relationship (I believe) but now I am violating my original desire to only have to pass the hash once, i.e. I will have to pass it for each class I create.

As far as your first suggestion, I am turned off by the use of global variables that look like they ought to be object properties. (= Do you think this is still a better way than my original design?

  • Comment on Re: Re (tilly) 5: Inheriting object data

Replies are listed 'Best First'.
Re: Re: Re (tilly) 5: Inheriting object data
by chromatic (Archbishop) on Feb 17, 2001 at 06:07 UTC
    How about something like this? It's encapsulated due to lexical variables and block scope, but you have access to it with accessor methods:
    package Parent; { my ($name, $pass); sub new { my $class = shift; my $self = {}; if (@_) { ($name, $pass) = @_; } @$self{'name', 'pass'} = ($name, $pass); bless($self, $class); } }
    You could add another method in there to reset the name and password. You could store references to $name and $pass in the hash, so updating the parent data is reflected in the children.

      I think this is the way to go, archon. See the section "Class Data" in the perltoot manpage for an example.

      ooh! i think i'll go with this approach. much thanks!