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 dont 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