In an ideal world, these user agent classes would have been written from the ground up as Moose roles, so could be combined however you like.
Something like this should work though...
use 5.010; use Class::Load qw/load_class/; sub compose { load_class($_) foreach @_; # Note "state" is Perl 5.10 mojo. For back compat, use # "my $count = 0" OUTSIDE the sub definition. That is, # create a closure over it. state $count = 0; my $package = sprintf('%s::__COMPOSED__::X%04x', __PACKAGE__, ++$c +ount); { no strict 'refs'; @{"$package\::ISA"} = @_; } $package; } my $class = compose(qw/LWP::UserAgent Data::Dumper/); say $class; my $obj = $class->new; say $obj; say $obj->can('agent'); say $obj->can('Dumper'); # Or even... my $obj2 = compose(qw/LWP::UserAgent Data::Dumper HTTP::Request/)->new +;
The problems creep in when the many classes you're inheriting from all override the same method. For example, LWP::UserAgent::Proxified, LWP::UserAgent::Determined and LWP::UserAgent::Cached might all override LWP::UserAgent's "request" method in different ways.
If LWP::UserAgent::Proxified inherits from all of them, then although Perl's method resolution order is well-defined, it will only call the overridden method from a single one of its child classes.
This is where Moose roles - and in particular, method modifiers - really shine. The base class provides the default "request" implementation, and he Proxified, Determined and Cached roles provide modifiers for the default implementation. The roles can usually be combined safely, with the various modifiers all getting applied to the "request" method.
In reply to Re: base class manipulation: Is there something similar on CPAN?
by tobyink
in thread base class manipulation: Is there something similar on CPAN?
by OlegG
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |