| 1 | package Devel::WatchVars; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | =cut | ||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | no overloading; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use Scalar::Util qw(weaken readonly reftype); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||
| 17 | ########################################################### | ||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | 15 | our $VERSION = "v1.0.5"; | |||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 15 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | 15 | our @EXPORT = qw(watch unwatch); | |||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | ########################################################### | ||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | 15 | my $TIE_PACKAGE = __PACKAGE__ . "::Tie::Scalar"; | |||||||||||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||||||||||||
| 28 | 15 | local $@; | |||||||||||||||||||||||||||||||||||||
| 29 | 15 | ▶ | die unless eval "require $TIE_PACKAGE; 1"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | ########################################################### | ||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | 13 | ▶ | sub watch(\$;$) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
watch: called CC=6 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my($sref, $name) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
watch: not called CC=6 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $reftype = ref($sref) && reftype($sref); | ||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
ref $sref && builtin::reftype($sref)
ref $sref && builtin::reftype($sref)
MC/DC50%1/2
ref $sref && builtin::reftype($sref)
ref $sref
builtin::reftype($sref)
MC/DC0%0/2
ref $sref && builtin::reftype($sref)
ref $sref
builtin::reftype($sref)
Truth table66%2/3
A && B
A= ref $srefB= builtin::reftype($sref)
Truth table0%0/3
A && B
A= ref $srefB= builtin::reftype($sref)
| |||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $reftype =~ /^(?:SCALAR|REF)\z/ || | ||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||
| 39 | croak "You didn't pass a SCALAR (by reference), you passed a ", | ||||||||||||||||||||||||||||||||||||||
| 40 | $reftype ? "$reftype (by reference)" | ||||||||||||||||||||||||||||||||||||||
| 41 | : defined($sref) ? "non-reference" | ||||||||||||||||||||||||||||||||||||||
| 42 | : "undef"; | ||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | !readonly($$sref) || croak "Can't watch a readonly scalar"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my $value = $$sref; | |||||||||||||||||||||||||||||||||||||
| 47 | 0 | my $self = tie $$sref, $TIE_PACKAGE, $name, $value; | |||||||||||||||||||||||||||||||||||||
| 48 | 0 | weaken($$self{sref} = $sref); | |||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | ########################################################### | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | 9 | ▶ | sub unwatch(\$) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
unwatch: called CC=5 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my($tref) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
unwatch: not called CC=5 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | ref($tref) eq "SCALAR" || croak "You didn't pass a scalar (by reference)"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $self = tied $$tref || croak "Can't unwatch something that isn't tied"; | ||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
tied $$tref || croak(q[Can't unwatch something that isn't tied])
tied $$tref || croak(q[Can't unwatch something that isn't tied])
MC/DC100%1/1
tied $$tref || croak(q[Can't unwatch something that isn't tied])
tied $$tref
MC/DC0%0/1
tied $$tref || croak(q[Can't unwatch something that isn't tied])
tied $$tref
Truth table100%2/2
A
A= tied $$tref
Truth table0%0/2
A
A= tied $$tref
| |||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $self->isa($TIE_PACKAGE) || croak "Can't unwatch something that isn't watched"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my($sref, $value) = @$self{sref => "value"}; | |||||||||||||||||||||||||||||||||||||
| 59 | 0 | undef $self; | |||||||||||||||||||||||||||||||||||||
| 60 | 0 | untie $$tref; | |||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $$sref = $value if ref($sref) eq "SCALAR"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | ########################################################### | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | 15 | 1; | |||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | __END__ | ||||||||||||||||||||||||||||||||||||||