Perl: How to break on write-to-variable in Eclipse

124 views Asked by At

I have a script that is writing entries into a hash. However, at a certain point entries exist in the hash that I think should not. So, obviously I've cocked it up somewhere, but there is only one place where I think I add elements in to the hash and I've tested that to make sure that these "rogue" elements aren't being added at this location.

What I would like to do is break on a write to the hash, something like this, but in a "global" kinda way because I don't know where this stray write is in the code - I can't see it...

So what are my options? Can I set a watch point in the EPIC debugger and if so how? (I've had a play but can;t find anything relevant).

Or could I perhaps create a extended hash that can intercept writes somehow?

Any ideas on an "easy" debugging method. Otherwise I think I'll be back to brute force debug :S Thanks in davance...

3

There are 3 answers

0
mob On BEST ANSWER

Not an EPIC-specific answer, but check out Tie::Watch. You can setup a variable (like a hash) to be watched, and your program can output something every time the variable is updated.

updated: Tie::Trace does pretty much the same thing, with a simpler interface.

1
amon On

Here is a DIY-version of mobs answer: Make that hash a tied hash to a class that outputs a stack trace on every write access:

package MyHash {
  use Tie::Hash;
  use parent -norequire, "Tie::StdHash";
  use Carp "cluck";

  sub STORE {
    my ($self, $k, $v) = @_;
    cluck "hash write to $self: $k => $v";
    $self->SUPER::STORE($k => $v);
  }
}

tie my %hash => "MyHash";
$hash{foo} = 42;
print "$hash{foo}\n";

Output:

hash write to MyHash=HASH(0x91be87c): foo => 42 at -e line 1.
        MyHash::STORE('MyHash=HASH(0x91be87c)', 'foo', 42) called at -e line 1
42
0
mpapec On
  use Hash::Util qw(
     lock_keys unlock_keys
     lock_hash unlock_hash
  );

  my %hash = (foo => 42, bar => 23);

  # no keys change
  lock_keys(%hash);

  # no keys/values change
  lock_hash(%hash);