File Coverage

blib/lib/Data/Trace.pm
Criterion Covered Total %
statement 33 33 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 46 46 100.0


line stmt bran cond sub pod time code
1             package Data::Trace;
2              
3 2     2   97843 use 5.006;
  2         13  
4 2     2   6 use strict;
  2         2  
  2         42  
5 2     2   7 use warnings;
  2         2  
  2         45  
6              
7 2     2   733 use FindBin();
  2         1501  
  2         41  
8 2     2   665 use lib $FindBin::RealBin;
  2         965  
  2         7  
9              
10 2     2   984 use Data::Tie::Watch; # Tie::Watch copy.
  2         4  
  2         44  
11 2     2   779 use Data::DPath; # All refs in a struct.
  2         171979  
  2         12  
12 2     2   304 use Carp qw(longmess); # Stack trace.
  2         3  
  2         304  
13              
14             =head1 NAME
15              
16             Data::Trace - Trace when a data structure gets updated.
17              
18             =head1 VERSION
19              
20             Version 0.10
21              
22             =cut
23              
24             our $VERSION = '0.10';
25              
26             =head1 SYNOPSIS
27              
28             use Data::Trace;
29              
30             my $data = {a => [0, {complex => 1}]};
31             sub BadCall{ $data->{a}[0] = 1 }
32             Data::Trace->Trace($data);
33             BadCall(); # Shows strack trace of where data was changed.
34              
35             =head1 DESCRIPTION
36              
37             This module provides a convienient way to find out
38             when a data structure has been updated.
39              
40             It is a debugging/tracing aid for complex systems to identify unintentional
41             alteration to data structures which should be treated as read-only.
42              
43             Probably can also create a variable as read-only in Moose and see where
44             its been changed, but this module is without Moose support.
45              
46             =head1 SUBROUTINES/METHODS
47              
48             =head2 Trace
49              
50             Data::Trace->Trace( \$scalar );
51             Data::Trace->Trace( \@array );
52             Data::Trace->Trace( \@hash );
53             Data::Trace->Trace( $complex_data );
54              
55             =cut
56              
57             sub Trace {
58 16     16 1 8247 my ( $self, $data ) = @_;
59              
60 16 100       36 if ( not ref $data ) {
61 3         16 die "Error: data must be a reference!";
62             }
63              
64 13         45 my @nodes = grep { ref } Data::DPath->match( $data, "//" );
  47         3819  
65              
66 13         41 for my $node ( @nodes ) {
67              
68             # print "Tying: $node\n";
69             Data::Tie::Watch->new(
70             -variable => $node,
71             -store => sub {
72 10     10   18 my ( $self, $v ) = @_;
73 10         23 $self->Store( $v );
74 10         560 print "Storing here:" . longmess();
75             }
76 17         95 );
77             }
78             }
79              
80             =head1 AUTHOR
81              
82             Tim Potapov, C<< >>
83              
84             =head1 BUGS
85              
86             Please report any bugs or feature requests to L.
87              
88             Currently only detect C operations.
89             Expand this to also detect C, C, C, etc.
90              
91             =head1 TODO
92              
93             Consider adding an option to have a warn message anytime a structure is FETCHed.
94              
95             =head1 SUPPORT
96              
97             You can find documentation for this module with the perldoc command.
98              
99             perldoc Data::Trace
100              
101              
102             =head1 LICENSE AND COPYRIGHT
103              
104             This software is Copyright (c) 2022 by Tim Potapov.
105              
106             This is free software, licensed under:
107              
108             The Artistic License 2.0 (GPL Compatible)
109              
110              
111             =cut
112              
113             1; # End of Data::Trace