in reply to Dynamically Building Variable Names

I agree with Fletch that you probably don't want it.

I can see some vague utility if you want to access global variables. In this case, you can use a plain hash to get a quick hack that works even with strict and warnings turned on:

#!/usr/bin/perl use strict; # Use good practices use warnings; my %globals = (barbaz => "something here"); foo("bar", "baz"); sub foo { my $name = join('', @_); # Build varname print $globals{$name}, "\n"; # use it at will.... }
If you want to go the dark-side way (but remember that it's difficult to get back!), you have to indirect the variable name you're building:
#!/usr/bin/perl $barbaz = "something here"; foo("bar", "baz"); sub foo { $name = join('', @_); # Build varname print $$name, "\n"; # use it AT YOUR RISK! }
But, at the risk of repeating myself and Fletch, you don't want to do that.

Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: Dynamically Building Variable Names
by dragonchild (Archbishop) on Apr 22, 2005 at 17:33 UTC
    Just to hammer the point home even more - your second example is identical to
    #!/usr/bin/perl $barbaz = "something here"; foo("bar", "baz"); sub foo { $name = join('', @_); # Build varname print $main::{$name}, "\n"; }

    In other words, you're using a hash - the package's symbol table. However, if you don't want to clobber something, use your own hash.