Thilosophy has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monks,
I have a module ( CGI::Application::Plugin::AutoRunmode ) that installs subroutine attribute handlers into the caller's namespace:
This works fine, unless some other module also tries to do the same thing (we would be overwriting each-others handlers). So I am now trying to find another way to implement these subroutine attributes, that is less risky to collide with other modules. My current idea is outlined below, but it does not work, raising my first question: Why does it not work? The second question would be for alternative approaches to the problem.package MyApp; use base 'CGI::Application'; use CGI::Application::Plugin::AutoRunmode; sub my_run_mode : StartRunmode { # (the StartRunmode attribute is provided by the plugin) }
I thought I could avoid putting the attribute handlers into the caller's namespace by instead adding a package that contains these handlers to @ISA. Perldoc says:
When an attribute list is present in a declaration, a check is made to see whether an attribute 'modify' handler is present in the appropriate package (or its @ISA inheritance tree).However, this sample code does not work: Package Three has two parent packages, One and Two. The attribute handler in package Two is never accessed. Am I missing something?
Thanks for your time,use strict; use warnings; { package One; sub MODIFY_CODE_ATTRIBUTES{ my ($pkg, $ref, @attr) = @_; print "Package A handler: @attr \n"; return @attr; } } { package Two; sub MODIFY_CODE_ATTRIBUTES{ my ($pkg, $ref, @attr) = @_; print "Package B handler: @attr\n"; return (); } } package Three; use base qw[ One Two]; sub foo : Eins Zwei Drei { } =========== $ perl /tmp/attr.pl Package A handler: Eins Zwei Drei Invalid CODE attributes: Eins : Zwei : Drei at /tmp/attr.pl line 30 BEGIN failed--compilation aborted at /tmp/attr.pl line 30.
Thilo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Package-specific Attribute Handling and Multiple Inheritence
by Roger (Parson) on Sep 03, 2005 at 14:12 UTC | |
Re: Package-specific Attribute Handling and Multiple Inheritence
by cees (Curate) on Sep 03, 2005 at 18:25 UTC | |
Re: Package-specific Attribute Handling and Multiple Inheritence
by xdg (Monsignor) on Sep 06, 2005 at 11:57 UTC | |
by Thilosophy (Curate) on Sep 07, 2005 at 01:51 UTC | |
by xdg (Monsignor) on Sep 07, 2005 at 02:05 UTC |
Back to
Seekers of Perl Wisdom