I recently wrote some code that spent an awful lot of time making decisions and taking branches based on the contents of various entries in a hash-ref. A constant irritating source of bugs was me making typos and trying to read hash entries that didn't exist. After I'd tracked down the first three or four of these hard-to-find bugs I decided to see if I could make it easier on myself.
I found Tie::Hash::Vivify which lets you hook into auto-vivification. The author's intention was apparently to let you automatically populate hash entries when you access them. I subverted it like this:
use Tie::Hash::Vivify; use Carp qw(confess); use Data::Dumper; my $hashref = Tie::Hash::Vivify->new(sub { confess("No auto-vivifying! +\n".Dumper(\@_)) });
so now whenever I try to read a hash entry that doesn't exist, my code dies with a stack trace (so I can tell where the error is) and shows me the parameters that were passed to FETCH (telling me what the error is).
Hurrah!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Death to auto-vivification
by clinton (Priest) on Mar 03, 2008 at 12:25 UTC | |
|
Re: Death to auto-vivification
by bobf (Monsignor) on Mar 03, 2008 at 19:21 UTC | |
by doom (Deacon) on Mar 04, 2008 at 06:38 UTC |