in reply to Preventing autovivification of object attributes

I'm not sure I see why your code is needed. It appears that the assignment of attributes is done on a "only assign attributes that have been noted as existing."

To paracode, I mean:

sub new { # ... for (@valid_keys) { if (exists $arg{$_}) { $self->{$_} = $arg{$_} } } }
is being done already. There doesn't appear to be a need to put a layer on top of that.

_____________________________________________________
Jeff japhy Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-Q)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: Preventing autovivification of object attributes
by nysus (Parson) on Jul 15, 2001 at 22:56 UTC
    Well, when I run Conway's code without my additions, the line below with a misspelled attribute in it, "arist", lets the object be blessed by the constructor with no problem.
    my $cd = Music->new(name => 'Four Seasons', arist => 'Vivaldi', rating + => 7,);
    With my code, I at least get an error. So it looks like an improvement but not in the way I thought.

    Looking again at his code, I see you are correct: if the argument given doesn't exist as an attribute, it is getting ignored rather than autovivified as I mistakenly assumed. What confused me I didn't read close enough is Conway's second paragraph of page 119:

    If you have a reference to a hash-based object, say, $objref, and you' +re using an attribute such as $objref->{_weirdness_factor}, then chan +ces are that somewhere in the heat of coding, you'll accidentally wri +te something like $objref->{_wierdness_factor}++.
    I'm beginning to think he is talking about autovivification that occurs from code located within the class like what happens on page 129 with his Transceiver class and not autovivication during object construction. Am I right on this? Thanks for the help.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

      Yep, no problem. I see where you were coming from now.

      _____________________________________________________
      Jeff japhy Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-Q)}y js++=;shajsj<++y(p-q)}?print:??;