dragonchild has asked for the wisdom of the Perl Monks concerning the following question:
So, I created a function called define_attributes(). At first, I had:
Ugly. Reading PM led me to the following import function:__PACKAGE__->define_attributes(qw/ foo bar /);
Better. This works with inheritance and looks clean. However, it clobbers import(). (My contract with anyone who uses me is that I will be clobbering define_attributes(), and that's ok.) Also, a co-worker of mine said that it also will not work with use base (which does not call import() on its own). So, he came up with the following:sub import { my $caller = (caller)[0]; no strict 'refs'; *{"${caller}::define_attributes"} = \&define_attributes; *{"${caller}::import"} = \&import; }
However, this doesn't work with two branches. (Foo and Bar both inherit from Base. Foo gets public(), but Bar doesn't.){ no strict 'refs'; my $i = 0; while(my $caller = (caller($i))[0]) { unless ($caller eq 'main' or $caller eq 'base') { *{"${caller}::define_attributes"} = \&define_attributes; } $i++; } }
Anyone have any suggestions? I'd really like to submit this module to CPAN so I can use it in other modules I'm working on, but this problem is causing serious issues for me.
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: importing a function
by adrianh (Chancellor) on Feb 12, 2003 at 16:44 UTC | |
|
Re: importing a function
by broquaint (Abbot) on Feb 12, 2003 at 17:25 UTC | |
by dragonchild (Archbishop) on Feb 12, 2003 at 17:44 UTC | |
by broquaint (Abbot) on Feb 12, 2003 at 18:00 UTC |