File Coverage

blib/lib/Tie/Hash/Blame.pm
Criterion Covered Total %
statement 40 40 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 51 51 100.0


line stmt bran cond sub pod time code
1             ## no critic (RequireUseStrict)
2             package Tie::Hash::Blame;
3             {
4             $Tie::Hash::Blame::VERSION = '0.01';
5             }
6              
7             ## use critic (RequireUseStrict)
8 1     1   596 use strict;
  1         1  
  1         24  
9 1     1   4 use warnings;
  1         2  
  1         29  
10             require Tie::Hash;
11 1     1   752 use parent '-norequire', 'Tie::ExtraHash';
  1         250  
  1         4  
12              
13             sub TIEHASH {
14 1     1   16 my ( $class ) = @_;
15              
16 1         8 return bless [
17             {}, # key/value storage
18             {}, # history storage
19             ], $class;
20             }
21              
22             sub _storage {
23 8     8   12 my ( $self ) = @_;
24              
25 8         17 return $self->[0];
26             }
27              
28             sub _history {
29 15     15   24 my ( $self ) = @_;
30              
31 15         31 return $self->[1];
32             }
33              
34             sub STORE {
35 6     6   3808 my ( $self, $key, $value ) = @_;
36              
37 6         18 my $storage = $self->_storage;
38 6         13 my $history = $self->_history;
39              
40 6         15 $storage->{$key} = $value;
41              
42 6         16 my ( undef, $filename, $line_no ) = caller;
43 6         23 $history->{$key} = {
44             filename => $filename,
45             line_no => $line_no,
46             };
47              
48 6         37 return $value;
49             }
50              
51             sub DELETE {
52 1     1   1512 my ( $self, $key ) = @_;
53              
54 1         6 my $storage = $self->_storage;
55 1         4 my $history = $self->_history;
56              
57 1         4 delete $history->{$key};
58 1         5 return delete $storage->{$key};
59             }
60              
61             sub CLEAR {
62 1     1   1110 my ( $self ) = @_;
63              
64 1         13 my $storage = $self->_storage;
65 1         4 my $history = $self->_history;
66              
67 1         4 %$storage = ();
68 1         4 %$history = ();
69              
70 1         6 return;
71             }
72              
73             sub blame {
74 7     7 1 68 my ( $self ) = @_;
75              
76 7         16 my $history = $self->_history;
77 7         11 my %copy;
78              
79 7         22 foreach my $k (keys %$history) {
80 9         12 my $v = $history->{$k};
81              
82 9         42 $copy{$k} = { %$v };
83             }
84              
85 7         23 return \%copy;
86             }
87              
88             1;
89              
90              
91              
92             =pod
93              
94             =head1 NAME
95              
96             Tie::Hash::Blame - A hash that remembers where its keys were set
97              
98             =head1 VERSION
99              
100             version 0.01
101              
102             =head1 SYNOPSIS
103              
104             use Tie::Hash::Blame;
105              
106             my %hash;
107             tie %hash, 'Tie::Hash::Blame';
108              
109             =head1 DESCRIPTION
110              
111             Have you ever tried to track changes to a hash throughout a large program?
112             It's hard, isn't it? This module makes things a little easier. Its intended
113             use is for debugging, because ties are magic, and magic is evil.
114              
115             =head1 METHODS
116              
117             =head2 tied(%hash)->blame
118              
119             Returns a hash reference containing the location of the last assignment to
120             each hash key. The keys in the returned hash reference are the same as in the
121             underlying hash; the values, however, are all hash references with two keys:
122             'filename' and 'line_no'.
123              
124             =head1 AUTHOR
125              
126             Rob Hoelz
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is copyright (c) 2012 by Rob Hoelz.
131              
132             This is free software; you can redistribute it and/or modify it under
133             the same terms as the Perl 5 programming language system itself.
134              
135             =head1 BUGS
136              
137             Please report any bugs or feature requests on the bugtracker website
138             https://github.com/hoelzro/tie-hash-blame/issues
139              
140             When submitting a bug or request, please include a test-file or a
141             patch to an existing test-file that illustrates the bug or desired
142             feature.
143              
144             =cut
145              
146              
147             __END__