| 1 | ## no critic (RequireUseStrict) | ||
| 2 | package Tie::Hash::Blame; | ||
| 3 | { | ||
| 4 | 2 | $Tie::Hash::Blame::VERSION = '0.01'; | |
| 5 | } | ||
| 6 | |||
| 7 | ## use critic (RequireUseStrict) | ||
| 8 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | require Tie::Hash; | |
| 11 | 2 | ▶ | use parent '-norequire', 'Tie::ExtraHash'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 1 | ▶ | sub TIEHASH { |
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||
| 14 | 1 | my ( $class ) = @_; | |
| 15 | |||
| 16 | 1 | return bless [ | |
| 17 | {}, # key/value storage | ||
| 18 | {}, # history storage | ||
| 19 | ], $class; | ||
| 20 | } | ||
| 21 | |||
| 22 | 8 | ▶ | sub _storage { |
|
Subroutine
_storage: called CC=1 SCAR=0.0
| |||
| 23 | 8 | my ( $self ) = @_; | |
| 24 | |||
| 25 | 8 | return $self->[0]; | |
| 26 | } | ||
| 27 | |||
| 28 | 15 | ▶ | sub _history { |
|
Subroutine
_history: called CC=1 SCAR=0.0
| |||
| 29 | 15 | my ( $self ) = @_; | |
| 30 | |||
| 31 | 15 | return $self->[1]; | |
| 32 | } | ||
| 33 | |||
| 34 | 6 | ▶ | sub STORE { |
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||
| 35 | 6 | my ( $self, $key, $value ) = @_; | |
| 36 | |||
| 37 | 6 | my $storage = $self->_storage; | |
| 38 | 6 | my $history = $self->_history; | |
| 39 | |||
| 40 | 6 | $storage->{$key} = $value; | |
| 41 | |||
| 42 | 6 | my ( undef, $filename, $line_no ) = caller; | |
| 43 | 6 | $history->{$key} = { | |
| 44 | filename => $filename, | ||
| 45 | line_no => $line_no, | ||
| 46 | }; | ||
| 47 | |||
| 48 | 6 | return $value; | |
| 49 | } | ||
| 50 | |||
| 51 | 1 | ▶ | sub DELETE { |
|
Subroutine
DELETE: called CC=1 SCAR=0.0
| |||
| 52 | 1 | my ( $self, $key ) = @_; | |
| 53 | |||
| 54 | 1 | my $storage = $self->_storage; | |
| 55 | 1 | my $history = $self->_history; | |
| 56 | |||
| 57 | 1 | delete $history->{$key}; | |
| 58 | 1 | return delete $storage->{$key}; | |
| 59 | } | ||
| 60 | |||
| 61 | 1 | ▶ | sub CLEAR { |
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||
| 62 | 1 | my ( $self ) = @_; | |
| 63 | |||
| 64 | 1 | my $storage = $self->_storage; | |
| 65 | 1 | my $history = $self->_history; | |
| 66 | |||
| 67 | 1 | %$storage = (); | |
| 68 | 1 | %$history = (); | |
| 69 | |||
| 70 | 1 | return; | |
| 71 | } | ||
| 72 | |||
| 73 | 7 | ▶ | sub blame { |
|
Subroutine
blame: called CC=2 SCAR=6.9
Pod
documented
| |||
| 74 | 7 | my ( $self ) = @_; | |
| 75 | |||
| 76 | 7 | my $history = $self->_history; | |
| 77 | 7 | my %copy; | |
| 78 | |||
| 79 | 7 | foreach my $k (keys %$history) { | |
| 80 | 9 | my $v = $history->{$k}; | |
| 81 | |||
| 82 | 9 | $copy{$k} = { %$v }; | |
| 83 | } | ||
| 84 | |||
| 85 | 7 | return \%copy; | |
| 86 | } | ||
| 87 | |||
| 88 | 2 | 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 <rob@hoelz.ro> | ||
| 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__ | ||