in reply to Check for variable without leaving a trace

make your own solution:
use Data::Dumper; print Dumper \%hash; print safe_defined(\%hash,"test","test2"); print Dumper \%hash; sub safe_defined { my ($h,@keys) = @_; for(@keys) { return 0 unless defined $h->{$_}; $h = $h->{$_}; } return 1; }


UPDATE: On a second thought, you should really use exists to test if a key exists on a hash, just change the defined keyword on the sub.

Replies are listed 'Best First'.
Re^2: Check for variable without leaving a trace
by educated_foo (Vicar) on Dec 09, 2009 at 14:47 UTC
    This isn't what I'm trying to do. I want to check whether a program has used the variable %hash without creating it. You used to be able to do this with defined, but someone thought it would be a good idea to deprecate that. exists $::{hash} can tell you whether one or more of $h, &h, %h, @h is defined, but not which.
      hm, now I get it.
      but I think not even perl can tell that ( $h was used and %h was not, for example)
        It mostly can (or at least could before 5.11), using defined:
        main @> $test = 1 1 main @> defined $test 1 main @> defined %test '' main @> %test2 = 1,2 1 2 main @> defined $test2 '' main @> defined %test2 1 main @> %test3 = () undef main @> defined %test3 '' main @> defined $test3 ''
        It doesn't detect an empty hash, but that's good enough for me.