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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.