Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: List Wrapper for Object Methods.

by Arunbear (Parson)
on Dec 03, 2004 at 10:06 UTC ( #412078=note: print w/ replies, xml ) Need Help??


in reply to List Wrapper for Object Methods.

You can use AUTOLOAD to do that

our $AUTOLOAD; sub AUTOLOAD { my $self = shift; my $method = $AUTOLOAD; $method =~ s/.*://; # strip out Package:: foreach my $anything (@{$self}) { die "method $method not supported" unless $anything->can($method); $anything->$method(@_); } }
Though this does smell like it would benefit from the composite design pattern.


Comment on Re: List Wrapper for Object Methods.
Download Code
Replies are listed 'Best First'.
Re^2: List Wrapper for Object Methods.
by pelagic (Curate) on Dec 03, 2004 at 11:25 UTC
    Thanks a lot Arunbear this works nicely.
    There's just one thing left for me:
    I see that my AUTOLOAD method now also "catches" calls for DESTROY and I don't know exactly what's happening then. Isn't the AUTOLOAD method a litte too generic?

    pelagic
      To get around that you can add the line
      return if $AUTOLOAD =~ /::DESTROY$/;
      at the beginning of the AUTOLOAD sub, or define a do-nothing DESTROY method (assuming you really don't have any cleanup to do). Perl calls the DESTROY method when it does garbage collection, even if you haven't defined a DESTROY method.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (17)
As of 2015-07-30 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls