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

I have a Perl script which is below:

________________________________________________
use strict; use vars qw ( %data $digit ); $data{value} = 'abcd.$digit.xyz'; sub process_value { $digit = 1000; eval { $data{value} }; print "\$data{value} $data{value} \n"; } &process_value;
________________________________________________

"$data{value} abcd.$digit.xyz "
Am I missing something?

thanks,
swaroop

Code tags added by davido per consideration.

Replies are listed 'Best First'.
Re: eval function
by graff (Chancellor) on Aug 18, 2005 at 05:47 UTC
    You're missing a couple of things:
    • <code> and </code> around your script, so we can read it more easily as code (you've been here long enough now that you should know this)
    • a simple, concise statement about what you actually want, and how that's different from what you're getting.

    Oh, and maybe you're missing the difference between single quotes and double quotes? I'm not sure, but if you wanted to get "$data{value} abcd.1000.xyz", then you should use double quotes instead of single quotes in the 3rd line.

    Apart from that, I don't understand what you expect to accomplish with that eval statement.

    Sorry -- I think I get it now. You want to put a variable name into a string assignment, then later on, you want to assign a value to a variable having that name, and have it interpolated into the string.

    There's a better way to do that:

    $data{value} = 'abc.VALUE.xyz'; sub process_value { my $digit = 1000; $data{value} =~ s/VALUE/$digit/; print "\$data{value} $data{value}\n" } process_value();
Re: eval function
by Anonymous Monk on Aug 18, 2005 at 10:19 UTC
    eval { }, that is, the block form of eval, just executes the code in the block, trapping any dies. An eval block where the code just contains a variable (as in your case), only makes sense if the variable is tied - with it isn't in your case.

    What you probably want is:

    $data{value} = eval "qq{$data{value}}";
    Note the double double quoting of the variable. And note that the double double in neither the previous sentence, nor the current one are typos.
Re: eval function
by anonymized user 468275 (Curate) on Aug 18, 2005 at 09:31 UTC
    Rather than find an alternative with s/, it seems more appropriate to address the actual mistake, which was to use single quotes (causing the contents to be interpreted literally) where double quotes were required i.e.
    $data{value} = "abcd.$digit.xyz";
    in which perl will indeed apply the required substitution.

    One world, one people

      Hi,

      If we give double quotes , The output will be "abcd..xyz".
      Its because of perl assignes the value of $digit to $data{value} in the begining itself.
      Anyways , thanks for the reply.

      - swaroop
        Perhaps so, but there's no quick fix then, because $data{ value } is trying to do too many jobs -- it seems to contain a formula for setting itself which then destroys the formula if needed for subsequent iterations. I think at this point a functional description of the requirement is needed before diving into any solution.

        One world, one people

Re: eval function
by Tanktalus (Canon) on Aug 18, 2005 at 20:41 UTC

    It's not entirely clear to me what you're trying to accomplish, nor to anyone else, it seems. Perhaps it's this:

    use strict; use vars qw ( %data $digit ); $data{value} = 'abcd.$digit.xyz'; sub process_value { $digit = 1000; $data{value} = eval qq["$data{value}"]; print "\$data{value} $data{value} \n"; } &process_value;
    Output is:
    $data{value} abcd.1000.xyz
Re: eval function
by japhy (Canon) on Aug 18, 2005 at 13:35 UTC
    You can't just eval() like you did. First, you can't use the BLOCK form of eval. Secondly, you'll need multiple levels of evaluation. You could use my DynScalar module on CPAN if you'd like to do this seamlessly.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart