[Omaha.pm] Debugger (or Carp?): How can I detect the creation of a key in a hash object?

Jay Hannah jhannah at omnihotels.com
Thu Jul 6 09:28:17 PDT 2006


> From: Andy Lester <andy at petdance.com>
> Why are you trying to detect the creation of a key?  Is it so 
> you can set a breakpoint on it?

That specific key should never be set. My program is correctly puking
because it exists. For the life of me, though, I can't find where that
key is springing into existence. 

So, I want the debugger to stop wherever that key is being set, or I
want some Carp noise (w/ stack backtrace) to print out when it just got
set. Either way I'd then now where it happened. 

Right now I just know that it did happen somewhere, which doesn't
help...

------------

God save the queen, I'm subclassing Tie::Hash:

$ cat j.pl
package JayDebug;
use Carp qw( cluck );
use Tie::Hash;
our @ISA = ("Tie::StdHash");
sub STORE {
   my ($self, $key, $value) = @_;
   cluck "key [$key] is being set to [$value]";
   $self->{$key} = $value;
}

package main;
my %j;
tie %j, "JayDebug";
$j{a} = 1;
$j{b} = 2;
$j{a} = 3;
print join "|", keys %j;
print "\n";

$ perl j.pl
key [a] is being set to [1] at j.pl line 8
        JayDebug::STORE('JayDebug=HASH(0x81599c8)', 'a', 1) called at
j.pl line 15
key [b] is being set to [2] at j.pl line 8
        JayDebug::STORE('JayDebug=HASH(0x81599c8)', 'b', 2) called at
j.pl line 16
key [a] is being set to [3] at j.pl line 8
        JayDebug::STORE('JayDebug=HASH(0x81599c8)', 'a', 3) called at
j.pl line 17
a|b


Wow. I'm deep in it now...

j
gathering more deep black magic 



More information about the Omaha-pm mailing list