Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here are two other ways of dealing with it:

First, in your base module, create a MODIFY_CODE_ATTRIBUTES method that manually walks up the inheritance tree calling MODIFY_CODE_ATTRIBUTES on all of the super classes.

use strict; use warnings; package One; sub MODIFY_CODE_ATTRIBUTES { my ( $pkg, $ref, @attr ) = @_; print "Package One handler given (@attr) and handles 'Eins'\n"; return grep { $_ ne 'Eins' } @attr; } package Two; use base qw(One); sub MODIFY_CODE_ATTRIBUTES { my ( $pkg, $ref, @attr ) = @_; print "Package Two handler given (@attr) and handles 'Zwei'\n"; return grep { $_ ne 'Zwei' } @attr; } package Three; sub MODIFY_CODE_ATTRIBUTES { my ( $pkg, $ref, @attr ) = @_; print "Package Three handler given (@attr) and handles 'Drei'\n"; return grep { $_ ne 'Drei' } @attr; } package Four; sub empty {} package Five; use base qw[ Two Three Four ]; use Class::ISA; sub MODIFY_CODE_ATTRIBUTES { my ( $pkg, $ref, @attr ) = @_; foreach my $class (Class::ISA::super_path($pkg)) { @attr = $class->MODIFY_CODE_ATTRIBUTES( $ref, @attr ) if $class->can('MODIFY_CODE_ATTRIBUTES'); } print "These attributes are left over (@attr) \n" if @attr; return @attr; } sub foo : Eins Zwei Drei { } __END__

Second, use NEXT to make sure all classes have a chance to run their MODIFY_CODE_ATTRIBUTES method. This depends on all classes working together to make sure they all use and call NEXT.

use strict; use warnings; package One; use NEXT; sub MODIFY_CODE_ATTRIBUTES { my ( $pkg, $ref, @attr ) = @_; print "Package One handler given (@attr) and handles 'Eins'\n"; @attr = grep { $_ ne 'Eins' } @attr; return $pkg->NEXT::MODIFY_CODE_ATTRIBUTES($ref, @attr); } package Two; use base qw(One); use NEXT; sub MODIFY_CODE_ATTRIBUTES { my ( $pkg, $ref, @attr ) = @_; print "Package Two handler given (@attr) and handles 'Zwei'\n"; @attr = grep { $_ ne 'Zwei' } @attr; return $pkg->NEXT::MODIFY_CODE_ATTRIBUTES($ref, @attr); } package Three; use NEXT; sub MODIFY_CODE_ATTRIBUTES { my ( $pkg, $ref, @attr ) = @_; print "Package Three handler given (@attr) and handles 'Drei'\n"; @attr = grep { $_ ne 'Drei' } @attr; return $pkg->NEXT::MODIFY_CODE_ATTRIBUTES($ref, @attr); } package Four; sub empty {} package Five; use base qw[ Two Three Four ]; sub foo : Eins Zwei Drei { } __END__

Update: I should have noted that neither of these options are ideal... The first could potentially call the same MODIFY_CODE_ATTRIBUTES method in one class two times, if one of the modules already calls SUPER::MODIFY_CODE_ATTRIBUTES (not sure if that would break things, probably not). Also, a module will not have the ability to override the MODIFY_CODE_ATTRIBUTES method of it's parent class, since the parent class version will still be called... The second version requires you to trust that all the modules you inherit from that have a MODIFY_CODE_ATTRIBUTES method also use the NEXT method otherwise something might get missed.


In reply to Re: Package-specific Attribute Handling and Multiple Inheritence by cees
in thread Package-specific Attribute Handling and Multiple Inheritence by Thilosophy

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-03-29 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found