in reply to Access variable names

There might be better ways to do this. Here is one way (not very clean tho)

use strict; use warnings; my $x = 10; my $y = undef; my $check = '$x,$y'; my @vars = split /,/,$check; for (@vars) { if (defined(eval($_))) { print "$_ = Defined\n"; } else { print "$_ = Not defined\n";} } __END__ $x = Defined $y = Not defined

There are risks associated with eval so you need to watch out

cheers

SK

Replies are listed 'Best First'.
Re^2: Access variable names
by andreas1234567 (Vicar) on Sep 01, 2005 at 12:09 UTC
    Thanks! That was a cool solution. One caveat though, the variables to be checked must be visible (scope-wise) to the eval:
    use strict; use warnings; my $foo = "1"; my $bar = "42"; my $defnz = sub { for (@_) { if (!defined(eval($_))) { print "$_ undef!"; return; } elsif (eval($_) eq '') { print STDERR "$_ empty!"; return; } } return 1; }; my $baz = "101"; print "ok1\n" if &$defnz(split /,/,q($foo,$bar)); print "ok2\n" if &$defnz(split /,/,q($foo,$bar,$baz)); __END__ ok1 $baz undef!