bernie_simon has asked for the wisdom of the Perl Monks concerning the following question:

I would like to evaluate an expression where the values of the variables in the expression are taken from a hash. I can't figure out how. Can anyone help?

Replies are listed 'Best First'.
Re: eval using hash values
by robartes (Priest) on Nov 05, 2002 at 15:05 UTC
    What exactly do you want to do? Is something like this what you are looking for?
    use strict; my %hash=qw/camel desert dolphin water/; eval("print qq{A camel lives in the $hash{'camel'}, as opposed to a do +lphin, who lives in the $hash{'dolphin'}\n};");

    CU
    Robartes-

Re: eval using hash values
by swiftone (Curate) on Nov 05, 2002 at 15:18 UTC
    It may be a bit of an overkill, but assuming you are trying to have:
    $text = 'my $foo has gone completely $bar'; %hash = (foo => 'dog', bar => 'crazy'); $final_text = unknown_method($text); #$final_text is now 'my dog has gone completely crazy'
    I think Text::Template will do exactly what you want. If that's not what you want, please clarify.