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?


In reply to Re^2: Class attribute handling by Dirk80
in thread Class attribute handling by Dirk80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.