Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Method Calls on multiple objects

by bart (Canon)
on Mar 27, 2004 at 00:13 UTC ( [id://340169]=note: print w/replies, xml ) Need Help??


in reply to Method Calls on multiple objects

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 @_.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found