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

Yes, I know it is silly. Can you do it or is this just a massive conspiracy because no one can actually do it? :-)
  • Comment on Re^2: Using a sting with a variable name

Replies are listed 'Best First'.
Re^3: Using a sting with a variable name
by ikegami (Patriarch) on May 11, 2009 at 14:56 UTC

    Yes and no.

    It's not possible to get a reliable answer for scalar package variables, but you can get a close enough answer. (If pkg var @foo exists, so does $foo even if it's never used.)

    It's not possible with lexical variables unless you're ok with actually executing the literal (and thus arbitrary code) to find out.

    There are template systems that use the syntax of Perl's interpolating literals.

      Why is it not possible for scalar package variables, but possible for array package variables?

      I'll answer that myself:

      *foo{THING} returns undef if that particular THING hasn't been used yet, except in the case of scalars. *foo{SCALAR} returns a reference to an anonymous scalar if $foo hasn't been used yet. This might change in a future release.
      And let me point out that this is from the perlref man page. It shows how to access "global" (package-based) variables through the symbol table.

      For the OP and others, pardon me while I give some references to things in various posts on the thread. Follow up the presentation with the documentation of those concepts.

      Farther down on that page is "symbolic references", which are also useful in this situation. That is, given a string $s that contains "var1" and use $$s to refer to $var1. You can't tell the difference between the variable existing but holding undef, or not previously existing. That's the general problem here. But, if your variables aren't undef, you might not have to worry about that.

      It also shows ${"${pack}::$name"} = 5; which doesn't use eval. However, the use of eval in another post wasn't really to access it, but to get a compile-time error if a variable was used without being defined.

      But... I agree that you should not do that. These tricks are for meta-programming, to write such things as package importers. For normal code, don't use a namespace as a hash.

      —John update: picky nomenclature.

        Why is it not possible for scalar package variables, but possible for list package variables?

        Lists aren't variables, and CODE, IO, etc aren't even close to lists. Fixed:

        Why is it not possible for scalar package variables, but possible for other types of package variables?

Re^3: Using a sting with a variable name
by bangers (Pilgrim) on May 11, 2009 at 15:02 UTC
    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" }
      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! :-)