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

Hello wise monks,
This is my first question to the monastery, although I've been silently lurking here for a while...
Anyway. I am reading (from a configuration file) a list of expressions to evaluate.
I am reading these into a hash where the hash key may be one of the variable in the expression it associated to or may not.
The configuration file looks like this
x ++$x y int(((log10 ($x/100 + 1)) + 3)*100) z $x+$y+$z
and so the hash turns out like this:
$function_hash{"x"}="++$x"; $function_hash{"y"}="int(((log10 ($x/100 + 1)) + 3)*100)"; $function_hash{"z"}="$x+$y+$z";
Now, I am getting values for the variable through a proprietary communication protocol (which is,btw, the real complicated part of that program, but it's not relevant now).
So I'm ending up with another hash that have:
$value_hash{"x"} = 10; $value_hash{"z"} = 30;
It may or may not contain all the variables I have functions for.
I am using the module Alias to turn all the field names in the value hash into local variables (with attr) and then I am doing eval on the entries in the %function_hash to get what I need.
It works nice except for those warnings "Use of uninitialized value..." which happens since I use strict.
I cannot use vars with the variables I have in the value hash since I know them only at run time (from the config file).
I don't want to use something like:
$Alias::AttrPrefix = "main::";
since then the functions in the configuration will need to be written differently or go through some parsing I don't want to do...
Please help (and I want to go on using strict...)
Thanks in advanced!!

Replies are listed 'Best First'.
Re: A question about Alias module and use strict
by blakem (Monsignor) on May 15, 2002 at 05:20 UTC
    "Use of uninitialized...." is a warning, not a strictness violation. If you are using a recent perl (5.6 or above) you can lexically turn this warning off by putting this in the appropriate block:
    no warnings 'uninitialized';
    See Re: supress an error for a slightly longer example.

    -Blake

Re: A question about Alias module and use strict
by rinceWind (Monsignor) on May 15, 2002 at 09:54 UTC
    It may or may not contain all the variables I have functions for.
    Herein lieth the nub of your problem. To avoid the "Use of uninitialized ..." you need to provide a default value for your variables, instead of just letting them be undef.

    However, this is non-trivial in your case. I recommend detecting any variables used by your expression, and populating %value_hash with default values. Warning: untested code

    foreach ($function_hash{$foo} =~ /\$(\w+)/g) { $value_hash{$1} = 0; # default value }
    hth

    --rW