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.
|