in reply to Convert string to variable

See Why it's stupid to `use a variable as a variable name'.

The better way is to use a hash. You already know how to do it, just create another hash alongside %var_list (which could be better named %questions):

my %questions = (date_in => 'Start Date', date_out => 'End Date', ); my %answers; for my $key (keys %questions) { $answers{$key} = prompt($questions{$key} . ': '); }

Alternatively, you can store all the information in one hash:

my %questions = (date_in => { q => 'Start Date' }, date_out => { q => 'End Date' }, ); for my $key (keys %questions) { $questions{$key}{a} = prompt($questions{$key}{q} . ': '); }

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Convert string to variable
by stevieb (Canon) on Feb 16, 2018 at 22:53 UTC

    MJD's "varvarname" post is a classic. I grin and smile every single time I see it, and read it through its entirety every time :) (Obviously, I use it where necessary and nobody else has posted it)

    Every single Perl hacker has experienced this through my experience. Not one of them (including me) hasn't run into wanting to do these things that I've found over the years.

    For the more experienced, it's a bit of entertainment. For the new people, it should point out the detrimental effects of how you can literally burn yourself (using safe-for-work language here).

    Thank you for raising that newsgroup thread one more time. I needed a good laugh :)

    OP... we've all been thrown that thread one time or another, so take with a grain of salt. choroba's advice is good advice.