(I hope I'm replying to this correctly -- if not let me know. Thanks!)
Do you want something like this:?
#!C:\Perl\bin\perl.exe -w
# Strict
use strict;
use warnings;
# Globals
my %varHash;
my %valHash;
# Main program
$varHash{'test1'} = "\$hostnamevar";
print $varHash{'test1'} ."\n";
eval ("my $varHash{'test1'}");
$valHash{$varHash{'test1'}} = "Hello World\n";
my $var = $varHash{'test1'};
printf "Variable associated with 'test1' = '%s'\n", $var;
my $val = $valHash{$var};
printf "Value associated with '%s' = '%s'\n", $var, $val;
exit 0
That way, you've got a separate hash /valHash/, which maps the variable
to a value.
Voila ... no more undefined variable references. Or am I missing
something else ...
John
Tal Cohen wrote:
>Original Message -----------------------
>On Sun, Sep 18, 2005 at 11:11:46AM -0400, Tal Cohen wrote:
>
>
>
>>I?m having a problem doing something that I thought would be simple, but
>>is proving frustrating: I have a hash whose resulting values are variable
>>names (including the $ sign). I want to use these variables in my
>>software, but am unable to declare them. Here is a simplified sample code
>>that tries to perform the action that I need, but errors out:
>>
>> #!C:\Perl\bin\perl.exe -w
>> use strict;
>> my %varHash;
>> $varHash{'test1'} = "\$hostnamevar";
>> print $varHash{'test1'} ."\n";
>> eval ("my $varHash{'test1'}");
>> $hostnamevar = "Hello World\n";
>> print "$hostnamevar\n";
>> exit 0;
>>
>>Here is the error message:
>> Global symbol "$hostnamevar" requires explicit package name at C:\Documents
>> and Settings\TCohen\Desktop\junk.pl line 7.
>> Global symbol "$hostnamevar" requires explicit package name at C:\Documents
>> and Settings\TCohen\Desktop\junk.pl line 8.
>> Execution of C:\Documents and Settings\TCohen\Desktop\junk.pl aborted due
>> to compilation errors.
>>
>>If I remove the use strict, then it works (but then I break good coding
>>principals).
>>
>>Does anyone have a solution that I can use?
>>
>>
>
>You need to declare your variables at compile time, but the eval isn't
>executed until runtime.
>
>Anyway, I'm not sure why you want to do this. If you're already using the
>variable in the code, why can't you just declare it in the code?
>
>Otherwise you should just leave the variables in the hash and use them that
>way.
>
>Ronald
>
>Hi Ronald,
> The sample code is only a simplification of the specific functionality
> that I am trying to achieve. It is a drastically stripped down (emphasis on
> drastically) version of my code and only an example of what I am trying to
> achieve functionally (i.e. only for error demonstration purposes). The code
> that I am developing will load variable names dynamically from an XML file,
> and then perform operations on them based on their respective values. I don't
> know in advance the variable names or how many there will be.
>
>Thanks,
>Tal
>
>
>
>------------------------------------------------------------------------
>
>
>_______________________________________________
>Boston-pm mailing list
>[email protected]
>http://mail.pm.org/mailman/listinfo/boston-pm
>
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm