File Coverage

lib/Devel/WatchVars.pm
Criterion Covered Total %
statement 39 39 100.0
branch 10 14 71.4
condition 4 6 66.6
subroutine 10 10 100.0
pod 2 2 100.0
total 65 71 91.5


line stmt bran cond sub pod time code
1             package Devel::WatchVars;
2              
3             =encoding utf8
4              
5             =cut
6              
7 8     8   1503029 use utf8;
  8         71  
  8         69  
8 8     8   294 use strict;
  8         20  
  8         168  
9 8     8   42 use warnings;
  8         18  
  8         261  
10 8     8   46 no overloading;
  8         16  
  8         278  
11              
12 8     8   46 use Carp;
  8         17  
  8         533  
13 8     8   50 use Scalar::Util qw(weaken readonly reftype);
  8         14  
  8         533  
14              
15 8     8   4223 use namespace::clean;
  8         116415  
  8         54  
16              
17             ###########################################################
18              
19             our $VERSION = "v1.0.5";
20              
21 8     8   2390 use Exporter qw(import);
  8         19  
  8         3366  
22             our @EXPORT = qw(watch unwatch);
23              
24             ###########################################################
25              
26             my $TIE_PACKAGE = __PACKAGE__ . "::Tie::Scalar";
27             {
28             local $@;
29             die unless eval "require $TIE_PACKAGE; 1";
30             }
31              
32             ###########################################################
33              
34             sub watch(\$;$) {
35 6     6 1 12759 my($sref, $name) = @_;
36 6   66     60 my $reftype = ref($sref) && reftype($sref);
37              
38 6 50       70 $reftype =~ /^(?:SCALAR|REF)\z/ ||
    50          
    100          
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 5 100       42 !readonly($$sref) || croak "Can't watch a readonly scalar";
45              
46 3         8 my $value = $$sref;
47 3         42 my $self = tie $$sref, $TIE_PACKAGE, $name, $value;
48 3         25 weaken($$self{sref} = $sref);
49             }
50              
51             ###########################################################
52              
53             sub unwatch(\$) {
54 4     4 1 1889 my($tref) = @_;
55 4 50       24 ref($tref) eq "SCALAR" || croak "You didn't pass a scalar (by reference)";
56 4   66     30 my $self = tied $$tref || croak "Can't unwatch something that isn't tied";
57 3 100       41 $self->isa($TIE_PACKAGE) || croak "Can't unwatch something that isn't watched";
58 2         14 my($sref, $value) = @$self{sref => "value"};
59 2         5 undef $self;
60 2         17 untie $$tref;
61 2 50       21 $$sref = $value if ref($sref) eq "SCALAR";
62             }
63              
64             ###########################################################
65              
66             1;
67              
68             __END__