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

Really? That's not what I get (nor expect):
use 5.010; @var = qw (a b c); $var2 = undef; $one = '$var'; $two = '$var2'; if (defined ${"${\substr $one, 1}"}) { say '$var exists'; } else { say '$var does not exist'; } __END__ $var does not exist

Replies are listed 'Best First'.
Re^6: Using a sting with a variable name
by ikegami (Patriarch) on May 11, 2009 at 15:47 UTC
    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.
      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.