in reply to '100 elsif's vs hash of coderef's
or skip the dispatch hash altogether and instead dispatch by name:
use strict; use warnings; for my $name (qw(Title Description Object Properties Value Wibble)) { my $code = main->can("do$name"); if (!$code) { print "!!! Missing handler for '$name'\n"; next; } $code->(); } sub doTitle { print "Title handler\n" } sub doDescription { print "Description handler\n" } sub doObject { print "Object handler\n" } sub doProperties { print "Properties handler\n" } sub doValue { print "Value handler\n" }
Prints:
Title handler Description handler Object handler Properties handler Value handler !!! Missing handler for 'Wibble'
Now if you need a new handler all you do is add the sub.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: '100 elsif's vs hash of coderef's
by AnomalousMonk (Archbishop) on Dec 10, 2015 at 23:03 UTC | |
by GrandFather (Saint) on Dec 10, 2015 at 23:52 UTC | |
by AnomalousMonk (Archbishop) on Dec 11, 2015 at 08:35 UTC |