Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Referencing Subroutines through a package

by ikegami (Patriarch)
on Aug 16, 2009 at 09:17 UTC ( [id://788994]=note: print w/replies, xml ) Need Help??


in reply to Re: Referencing Subroutines through a package
in thread Referencing Subroutines through a package

Also (and, I think, better)

I disagree. I think it's worse. I'll explain, but let's complete the code first.

The solution from the first post:

my %handlers = ( ContentHandler => { '/' => sub { $self->{object}->Event() }, #'/' => sub { $self->{object}->Event(@_) }, }, ); my $cb = $handlers{ContentHandler}; ... = $cb->(); #... = $cb->(...);

The solution you propose:

my %handlers = ( ContentHandler => { '/' => $self->{object}->can('Event'), }, ); my $method = $handlers{ContentHandler}; ... = $self->{object}->$method(...);

The problem is that you're getting the method of one object and you're calling it with a different object. Bad! If they aren't different object, then you have redundant code. Bad! The following would be a better version of your solution:

my %handlers = ( ContentHandler => { '/' => 'Event', }, ); my $method = $handlers{ContentHandler}; ... = $self->{object}->$method(...);

This revised solution and the one from the first post are equally good.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found