| 1 | package Devel::TrackGlobalScalar; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | eval "use Carp::Heavy;"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | require Tie::Scalar; | |||||||||||
| 7 | 1 | our @ISA = qw(Tie::StdScalar); | |||||||||||
| 8 | |||||||||||||
| 9 | 1 | our $VERSION = '0.03'; | |||||||||||
| 10 | |||||||||||||
| 11 | 1 | our %opt = ( | |||||||||||
| 12 | track_source => 1, | ||||||||||||
| 13 | report_write_access => 0, | ||||||||||||
| 14 | ); | ||||||||||||
| 15 | |||||||||||||
| 16 | 1 | our %globals; | |||||||||||
| 17 | |||||||||||||
| 18 | 1 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=6 SCAR=18.2
| |||||||||||||
| 19 | 1 | my $class = shift; | |||||||||||
| 20 | 1 | my $globals = shift; | |||||||||||
| 21 | 1 | ▶ | die if not defined $globals; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 1 | ▶ | $globals = [$globals] if not ref $globals; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | |||||||||||||
| 24 | 1 | my %args = @_; | |||||||||||
| 25 | 1 | $opt{$_} = $args{$_} for keys %args; | |||||||||||
| 26 | |||||||||||||
| 27 | 1 | foreach my $global (@$globals) { | |||||||||||
| 28 | 1 | my $global_esc = $global; | |||||||||||
| 29 | 1 | $global_esc =~ s/\\/\\\\/g; | |||||||||||
| 30 | 1 | $global_esc =~ s/'/\\'/g; | |||||||||||
| 31 | |||||||||||||
| 32 | 1 | my $rv; | |||||||||||
| 33 | 1 | my $code = "\$rv = tie($global, __PACKAGE__, \\$global, '$global_esc'); 1"; | |||||||||||
| 34 | 1 | ▶ | eval $code or die "Failed to tie global '$global': $@"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | } | ||||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | 1 | ▶ | sub TIESCALAR { | ||||||||||
|
Subroutine
TIESCALAR: called CC=2 SCAR=7.0
| |||||||||||||
| 41 | 1 | my $class = shift; | |||||||||||
| 42 | 1 | ▶ | my $instance = shift || undef; | ||||||||||
|
Condition50%1/2
shift() || undef
MC/DC0%0/1
shift() || undef
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 43 | 1 | my $name = shift; | |||||||||||
| 44 | |||||||||||||
| 45 | 1 | my $o = bless [\$instance, $name] => $class; | |||||||||||
| 46 | 1 | $globals{$name} = $o; | |||||||||||
| 47 | 1 | return $o; | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | sub FETCH { | ||||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||||
| 51 | #_report($_[0], 'FETCH'); | ||||||||||||
| 52 | 0 | return ${$_[0][0]}; | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 0 | ▶ | sub STORE { | ||||||||||
|
Subroutine
STORE: not called CC=1 SCAR=6.9
| |||||||||||||
| 56 | 0 | _report($_[0], 'STORE'); | |||||||||||
| 57 | 0 | ${$_[0][0]} = $_[1]; | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 0 | ▶ | sub DESTROY { | ||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||
| 61 | 0 | _report($_[0], 'DESTROY'); | |||||||||||
| 62 | 0 | undef ${$_[0][0]}; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 0 | ▶ | sub _report { | ||||||||||
|
Subroutine
_report: not called CC=3 SCAR=24.8
| |||||||||||||
| 66 | 0 | my $obj = shift; | |||||||||||
| 67 | 0 | my $action = shift; | |||||||||||
| 68 | |||||||||||||
| 69 | 0 | my $global_name = $obj->[1]; | |||||||||||
| 70 | |||||||||||||
| 71 | 0 | my $msg = Carp::longmess("${action}ing global '$global_name' at"); | |||||||||||
| 72 | 0 | ▶ | if ($opt{track_source}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 0 | $obj->[2] = $msg; | |||||||||||
| 74 | } | ||||||||||||
| 75 | 0 | ▶ | if ($opt{report_write_access}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 0 | print STDERR $msg . "\n"; | |||||||||||
| 77 | } | ||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | sub get_source { | ||||||||||
|
Subroutine
get_source: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||
| 82 | 0 | my $key = shift; | |||||||||||
| 83 | print STDERR "Source tracking not enabled. Pass the track_source => 1 option when loading TrackGlobalScalar to enable\n" | ||||||||||||
| 84 | 0 | ▶ | if not $opt{track_source}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | ▶ | return '' if not @{$self} > 2; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | return $self->[2]; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 0 | ▶ | sub dump_all_sources { | ||||||||||
|
Subroutine
dump_all_sources: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 90 | 0 | my $class = shift; | |||||||||||
| 91 | print STDERR "Source tracking not enabled. Pass the track_source => 1 option when loading TrackGlobalScalar to enable\n" | ||||||||||||
| 92 | 0 | ▶ | if not $opt{track_source}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | |||||||||||||
| 94 | 0 | my $sources = \%globals; | |||||||||||
| 95 | 0 | foreach my $key (keys %$sources) { | |||||||||||
| 96 | 0 | ▶ | if (defined ($sources->{$key}->[2])) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 0 | print STDERR "$key was last set at:\n" . $sources->{$key}->[2] . "\n\n"; | |||||||||||
| 98 | } | ||||||||||||
| 99 | } | ||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 1 | 1; | |||||||||||
| 103 | |||||||||||||
| 104 | __END__ | ||||||||||||