in reply to Re^2: Using a sting with a variable name
in thread Using a sting with a variable name

I agree this is a bad idea, but this should work:
#!/usr/local/bin/perl -w use strict; my $var2 = 1; # may just be $var my $one = '$var'; my $two = '$var2'; if(eval("$one")){ print "I don't want to see this\n" } if(eval("$two")){ print "I want to see this\n" }

Replies are listed 'Best First'.
Re^4: Using a sting with a variable name
by ikegami (Patriarch) on May 11, 2009 at 15:05 UTC
    Let's hope you trust the template...
    my $one = '$var[ system qw( rm -rf / ) ]';
    Even the following would do with the method you used:
    my $one = 'system qw( rm -rf / )';
      Sometimes you really need to know whether a variable is defined; wiping the hard drive every time you run the script is just the price of that knowledge. ;-)

      --
      use JAPH;
      print JAPH::asString();

      I'm not going to use this method... I have actually implemented it using hashes. I mainly wondered if one wanted to do it how would they go abut doing it. Lots of replies! :-)