http://qs321.pair.com?node_id=340168

Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
I was looking on another Perl forum when I noticed an interesting question. Basically, is there a module that simplifies the same method call on a list of objects:
$obj_multi->new( $obj_1, $obj_2 ); $obj_multi->foo(); # $obj_1->foo(); $obj_2->foo();
I whipped this up real quick:
package MultiMethod; use strict; use warnings; use vars '$AUTOLOAD'; sub new { my $class = shift; my $self = \@_; return bless $self , $class; } sub DESTROY { return; } sub AUTOLOAD { my $self = shift; if ( $AUTOLOAD =~ /::([^:]+)$/ ) { $_->$1(@_) for @$self; } } 42;
Which I knew wasn't perfect. There is no error checking and there is also likely issues with ref counting and what not. Then it hit me, there is no need for a module:
MultiMethod( 'methodname', [], $file, $file2 ); sub MultiMethod { my $method = shift; my $args = shift; $_->$method(@$args) for @_; }
Finally, I promised I would ask around at the Monastery and update with any useful feedback. So how would you do it? Keeping in mind that you want it to be re-useable and as user friendly as possible.

Cheers - L~R