| 1 | package Devel::WarnGlobal::Scalar; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Track down and eliminate scalar globals | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.09'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 14 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 15 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | ################################# Methods ############################### | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | sub TIESCALAR { | ||||||||||||||||||||||||||
|
Subroutine
TIESCALAR: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 20 | 5 | my $type = shift; | |||||||||||||||||||||||||||
| 21 | 5 | my ($in) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | exists $in->{'get'} or croak "Improper use of 'tie' on $type: Field 'get' required; stopped"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 5 | my Devel::WarnGlobal::Scalar $self = bless {}, $type; | |||||||||||||||||||||||||||
| 27 | 5 | $self->{'get'} = $in->{'get'}; | |||||||||||||||||||||||||||
| 28 | 5 | ▶ | $self->{'set'} = $in->{'set'} if defined $in->{'set'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | $self->{'name'} = $in->{'name'} if defined $in->{'name'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 5 | $self->{'die_on_write'} = $type->_get_boolean($in, 'die_on_write', 0); | |||||||||||||||||||||||||||
| 31 | 5 | $self->{'warn'} = $type->_get_boolean($in, 'warn', 1); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 5 | return $self; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 10 | ▶ | sub _get_boolean { | ||||||||||||||||||||||||||
|
Subroutine
_get_boolean: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 10 | my $type = shift; | |||||||||||||||||||||||||||
| 38 | 10 | my ($hash, $member, $default) = @_; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 10 | ▶ | if ( defined $hash->{$member} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 1 | return $hash->{$member}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 9 | return $default; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 9 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 9 | my Devel::WarnGlobal::Scalar $self = shift; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 9 | ▶ | $self->{'warn'} and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 8 | warn(ucfirst($self->_get_identifier()), " was read-accessed ", $self->_get_caller_info()); | |||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | return $self->{'get'}->(); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 14 | ▶ | sub _get_caller_info { | ||||||||||||||||||||||||||
|
Subroutine
_get_caller_info: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 14 | my Devel::WarnGlobal::Scalar $self = shift; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 14 | my ($package, $filename, $line, $subroutine) = caller(1); | |||||||||||||||||||||||||||
| 62 | 14 | return "at $filename line $line.\n"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 14 | ▶ | sub _get_identifier { | ||||||||||||||||||||||||||
|
Subroutine
_get_identifier: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 14 | my Devel::WarnGlobal::Scalar $self = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 14 | ▶ | if (defined $self->{'name'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 8 | return "global '$self->{'name'}'"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 6 | return "a global"; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 6 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 79 | 6 | my Devel::WarnGlobal::Scalar $self = shift; | |||||||||||||||||||||||||||
| 80 | 6 | my ($new_value) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | if ( $self->{'warn'} && (! $self->{'die_on_write'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"warn"} and not $self->{"die_on_write"}
MC/DC50%1/2
$self->{"warn"} and not $self->{"die_on_write"}
$self->{"warn"}
not $self->{"die_on_write"}
Truth table66%2/3
A and not(B)
A= $self->{"warn"}B= not $self->{"die_on_write"}
| |||||||||||||||||||||||||||||
| 83 | 4 | warn(ucfirst( $self->_get_identifier() ), " was write-accessed ", $self->_get_caller_info()); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 6 | ▶ | if (! defined($self->{'set'}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | if ( defined($self->{'die_on_write'}) && $self->{'die_on_write'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"die_on_write"} and $self->{"die_on_write"}
MC/DC50%1/2
defined $self->{"die_on_write"} and $self->{"die_on_write"}
defined $self->{"die_on_write"}
$self->{"die_on_write"}
Truth table66%2/3
A and B
A= defined $self->{"die_on_write"}B= $self->{"die_on_write"}
| |||||||||||||||||||||||||||||
| 89 | 2 | die "Attempt to write-access ", $self->_get_identifier(), "(read-only) ", $self->_get_caller_info(); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||
| 93 | 2 | $self->{'set'}->($new_value); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub DESTROY { } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | ▶ | sub warn { | ||||||||||||||||||||||||||
|
Subroutine
warn: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 100 | 6 | my Devel::WarnGlobal::Scalar $self = shift; | |||||||||||||||||||||||||||
| 101 | 6 | my ($warn_val) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | ▶ | defined $warn_val or return $self->{'warn'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | $self->{'warn'} = $warn_val; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 6 | ▶ | sub die_on_write { | ||||||||||||||||||||||||||
|
Subroutine
die_on_write: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 6 | my Devel::WarnGlobal::Scalar $self = shift; | |||||||||||||||||||||||||||
| 110 | 6 | my ($die_val) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | defined $die_val or return $self->{'die_on_write'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 4 | $self->{'die_on_write'} = $die_val; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||