Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Package-specific Attribute Handling and Multiple Inheritence

by cees (Curate)
on Sep 03, 2005 at 18:25 UTC ( [id://488957]=note: print w/replies, xml ) Need Help??


in reply to Package-specific Attribute Handling and Multiple Inheritence

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://488957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found