in reply to Re: Class attribute handling
in thread Class attribute handling
Thank you all so much. So many cool answers and that so quick. I'm impressed.
It took me a while to understand everything. Now I understood it completely and your code is working successfully.
Because my code is also using the _get_attr_data as object and not only as package, I had to rewrite the beginning of _get_attr_data. And I added another cache to this class method, so that not each time the attribute data of a inherited class has to be recomputed again.
... # Encapsulated class data my %_attr_data; # for each package: stores reference to its attrib +ute data only my %_mro_cache; my %_attr_data_cache; # for each package stores reference to its a +ttribute data + inherited attribute data # Class methods to operate on encapsulated class data sub _get_attr_data { my $caller = shift; my $package = ref($caller) || $caller; return %{$_attr_data_cache{$package}} if defined $_attr_data_c +ache{$package}; my @parents = @{ $_mro_cache{$package} ||= mro::get_linear_isa +($package) }; my %return; for my $parent (@parents) { # assure that a reference is inside $_attr_data{$parent} ref $_attr_data{$parent} or next; for my $attr (keys %{$_attr_data{$parent}}) { $return{$attr} ||= $_attr_data{$parent}{$attr}; } } $_attr_data_cache{$package} = {%return}; return %return; } ...
In this case the %_mro_cache should not be needed anymore. Because this method returns if it already handled the attribute data of an inherited package before. Is this method rewritten in a good manner?
Of course you convinced me. I'll learn Moo or Moose and stop using my concept.
Ah, and one other question. In the documentation of ref they write that a direct truth test could be wrong, because 0 which is false could be returned for a reference. Could this happen in my case? When would ref return a 0 for a reference? Would you always recommend to test against the empty string?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Class attribute handling
by tobyink (Canon) on Dec 12, 2019 at 23:14 UTC | |
by Dirk80 (Pilgrim) on Dec 13, 2019 at 17:03 UTC |