Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

(jcwren) RE: multiple keys - one value

by jcwren (Prior)
on Sep 27, 2000 at 21:56 UTC ( #34252=note: print w/replies, xml ) Need Help??

in reply to multiple keys - one value
in thread using a hash of functions

This is a little clumsy, but you could have  '?' => 'HELP', etc, then use a loop at startup that replaced non-code refs with the code ref of the hash indice the value side points to.

It doesn't completely solve the semantic problem, but if you rename a routine, you only have to touch it one place, and adding synonyms becomes trivial.
#!/usr/local/bin/perl -w use strict; my %hash = ('HELP' => \&do_help, '?' => 'HELP', 'LIST' => \&do_list, 'SHOW' => 'LIST', 'TEST' => \&do_test, ); { while (my ($k, $v) = each (%hash)) { $hash {$k} = $hash {$v} if (ref $hash {$k} ne 'CODE'); } &{$hash {'HELP'}}; &{$hash {'?'}}; &{$hash {'LIST'}}; &{$hash {'SHOW'}}; &{$hash {'TEST'}}; } sub do_help {print "help\n"} sub do_list {print "list\n"} sub do_test {print "test\n"}

e-mail jcwren

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-10-24 03:23 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (127 votes). Check out past polls.