FWIW, there is another eval trick:

use strict; use warnings; my %cs101 = ("joe"=> "80" ); print "Choose which hash\n"; chomp (my $input = <STDIN>); #my $whichhash=\%cs101; my $whichhash="$input"; sub hashvalues { while( (my $key, my $value) = each (%$whichhash) ) { print "$key $value\n"; } } die "Illegal hash name: $input\n" unless $input =~ /^\w\w+$/; eval '$whichhash = \%'.$input; die "Input: <$input> is not a hash name: $@\n" if $@; &hashvalues;
... however, executing unchecked user input is no good idea. Consider somebody inputs cs101; unlink $0...
So, the approach suggested by toolic above is more secure and also allows for better user interaction, i.e. by presenting the user a menu list created from the HoH.

Update (16:05 CEST): From my point of view, when giving an answer to a SoPW-question, the answer should also include a note about possible drawbacks and limitations. Knowing the pros/cons, helps the reader to decide upon the usefulness of the answer(s) under her/his given circumstances.
Here, unlink $0 is just an example that is not intended to be a useful exploit but as one that should create awareness of a potential drawback. Creating a usefull exploit is left as an exercise for the reader ;-)
Seriously, one should not rely on the pure assumption, that the program will always be executed under the same privileges of the user and without malicious intend. I've seen scrips that were designed to be used as CLI tools and later wrapped and executed by a webserver, however vulnerable to injection attacks. This might be far away from the OPs intention, but who knows? Maybe, I should have made this explicit earlier...


In reply to Re: Dereference array via STDIN by Perlbotics
in thread Dereference array via STDIN by gctaylor1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.