You could achieve the desired effect by wrapping a sub around accessing the values of the hash:
my $sr=sub{print shift() . qq(\n)}; # Subref for Hash
my %h=(a=>$sr, b=>$sr);
sub gh{ # Sub to access the hash value and call the sub..
my $p=shift; # Get the KEY requested
# In real life, you would first check to see if it was a coderef..
&{$h{$p}}($p); # Call the anon sub, passing it the key
# Returns the result of the call..
};
gh($_) for qw(a b b a);
### Prints
a
b
b
a
..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|