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

What would be a good way to evaluate a variable in a text
string and store it into another variable? Couldn't get 'eval' to work.

I am trying to do something like -

#!/usr/local/bin/perl $val = '123'; open (FILE), "input") || die "Can't open for reading: $!\n"; while ( defined ( $_ = <FILE> ) ) { $string = eval { $val }; print "<$val>\n"; }
The input file has the following text string

input:

test_string_$val

Expecting $string to be - test_string_123

Any suggestion?

janitored by ybiC: Balanced <code> tags around code snippet

Replies are listed 'Best First'.
Re: How to evaluate a variable in a text string
by Roy Johnson (Monsignor) on Mar 28, 2005 at 21:01 UTC
    The non-strict (using symbolic references -- not recommended) way:
    $val = '123'; while (<DATA>) { s/\$(\w+)/$$1/g; print; } __DATA__ test_string_$val
    The strict-compliant (recommended) way:
    use strict; use warnings; my %subs = (val => '123'); while (<DATA>) { s/\$(\w+)/$subs{$1}/g; print; } __DATA__ test_string_$val
    Please read Why it's stupid to use a variable as a variable name

    Caution: Contents may have been coded under pressure.
      Much appreciated!!!
Re: How to evaluate a variable in a text string
by RazorbladeBidet (Friar) on Mar 28, 2005 at 20:52 UTC
    You are eval'ing $val instead of eval'ing $_

    I'm assuming the rest isn't really your code (or it would be failing) so I won't say anything about that.

    HTH!
    RBB
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      Thanks for your reply
      My mistake in typing up the code snippet
      It is actually -

      $string = eval { $_ };
      print "<$string>\n";

        It looks like you are confusing the "string form" of eval with the "block form". Try perldoc -f eval to get the full story, and then merlyn's comment may be clearer. To do what you want to do, you need something like

        $string = eval qq("$_");
        where $_ contains your 'test_string_$val'. But as Roy already pointed out, this may not be a very wise thing to do in the first place.

        the lowliest monk

Re: How to evaluate a variable in a text string
by dragonchild (Archbishop) on Mar 28, 2005 at 20:58 UTC
    You want to learn about hashes - they will make your life a lot easier. Plus, this smells of a config file. Why not look at the many config options around. They will also make your life even easier than that. :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.