No, it seems to me that what the original did, was allow all methods these object can do, on them all. In fact it sound a lot like tee — which is what I'd base the name of this module on.
Anyway, you original approach looks good to me, except I'd cache the sub, so AUTOLOAD isn't repeatedly called for each method.
sub AUTOLOAD {
my $self = shift;
if (my($method) = $AUTOLOAD =~ /::([^:]+)$/ ) {
no strict 'refs';
*$AUTOLOAD = sub {
my $self = shift;
$_->$method(@_) for @$self;
};
$self->$AUTOLOAD(@_);
}
}
That appears to work, at first sight.
I'm not sure it's actually better/faster.
p.s. Your original calls don't look right, it should be
my $obj_multi = MulitiObject->new( $obj_1, $obj_2 );
$obj_multi->foo($x, $y); # $obj_1->foo($x, $y); $obj_2->foo(
+$x, $y);
Oh, and make to constructor
sub new {
my $class = shift;
return bless [ @_ ], $class;
}
I don't trust takling references to
@_.