Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Method Calls on multiple objects

by Kanji (Parson)
on Mar 27, 2004 at 02:57 UTC ( [id://340203]=note: print w/replies, xml ) Need Help??


in reply to Method Calls on multiple objects

On the error checking front, you may want to throw can into the mix, so methodname is only called if the object implements it.

sub MultiMethod { my $method = shift; my $args = shift; foreach my $object (@_) { $object->$method(@$args) if $object->can($method); } }

    --k.


Replies are listed 'Best First'.
Re: Re: Method Calls on multiple objects
by diotalevi (Canon) on Mar 27, 2004 at 06:17 UTC
    ->can isn't effective for checking methods handled by AUTOLOAD.

      It certainly is, if you program correctly. Either predeclare or override can().

        We've discussed this before, but not properly. Ie unfortunately don't have time to give it justice now, either.

        Suffice it to say for now that I continue to disagree with you on what correct programming is. Furthermore I'd suggest avoiding setting up expectations that the majority will do things right (for virtually any values of right).

      sub generate_method { my $self = shift; my ($name) = @_; my $method = <generate method and put into namespace>; return $method; } sub can { my $self = shift; my ($name) = @_; if ( <some rule that determines when a method is handled by AUTOLO +AD> ) { return $self->generate_method($name); } $self->SUPER::can($name); } sub AUTOLOAD { my $name = our $AUTOLOAD; $name =~ s/.*::([^:]+)$/$1/; my $self = shift; if ( <some rule that determines when a method is handled by AUTOLO +AD> ) { return $self->generate_method( $name )->(@_); } $self->SUPER::AUTOLOAD( @_ ); }

      Now it is ... kinda. It's slower and annoying, but can be done.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-29 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found