Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Automating dispatch tables

by demerphq (Chancellor)
on Apr 16, 2004 at 14:19 UTC ( [id://345772]=note: print w/replies, xml ) Need Help??


in reply to Automating dispatch tables

Actually Chip recently pointed out on p5p that the second one is not the right way to handle this. In reality you are much better off avoiding can() and doing:

eval { $ref->$method() }; # updated: forgot the $method :-( die $@ if $@ and $@!~/^Undefined subroutine/;

This is because for all kinds of reasons can() is not 100% safe. For instance if the method needs to be autoloaded can() will return false, but calling the sub wont.

IMO the first one is also not the greatest way to write that.

$dispatch{$method}->() if $dispatch{$method};

---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found