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

You're still only checking whether the variable is defined or not. The data I gave was for after you made the change you said would check for existence.
  • Comment on Re^6: Using a sting with a variable name

Replies are listed 'Best First'.
Re^7: Using a sting with a variable name
by JavaFan (Canon) on May 11, 2009 at 16:36 UTC
    You really think the answer will change after adding a test for defined $one? Last time I looked, '$var' is a defined value.

      Sorry, I used the wrong var name. What I meant is that fixing the failing case below and will cause the third to fail.

      uuse strict; use warnings; no strict 'vars'; use Test::More tests => 3; sub var_exists { my ($var) = @_; return defined ${substr $var, 1}; } { no warnings 'once'; $var1 = 'a'; $var2 = undef; @var3 = qw( a b c ); } ok(var_exists('$var1'), 'var1'); ok(var_exists('$var2'), 'var2'); ok(!var_exists('$var3'), 'var3');
      1..3 ok 1 - var1 not ok 2 - var2 # Failed test 'var2' # at a.pl line 20. ok 3 - var3 # Looks like you failed 1 test of 3.
        From a Perl (language!) point of view, there's no difference between a package variable that hasn't been used, and a package variable which is undefined. The following, after all, is a valid Perl program; it compiles and runs:
        print $foo;
        it may warn, but it's valid.

        It only matters in the implementation, and in the few edge cases where you can poke into the underlaying implementation.