Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Referencing Subroutines through a package

by AnomalousMonk (Archbishop)
on Aug 16, 2009 at 01:28 UTC ( [id://788954]=note: print w/replies, xml ) Need Help??


in reply to Referencing Subroutines through a package

Also (and, I think, better):
ContentHandler => { '/' => $self->can('Event'}, },
See  can(METHOD) in the Default UNIVERSAL methods section of perlobj, also in the UNIVERSAL: The Root of All Objects section of perltoot.

Replies are listed 'Best First'.
Re^2: Referencing Subroutines through a package
by ikegami (Patriarch) on Aug 16, 2009 at 09:17 UTC

    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://788954]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found