Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(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"}
--Chris

e-mail jcwren

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://34252]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2018-07-18 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (383 votes). Check out past polls.

    Notices?