line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Devel::WarnGlobal::Scalar; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Track down and eliminate scalar globals |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.09'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
109
|
|
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
|
|
|
|
|
|
|
sub TIESCALAR { |
20
|
5
|
|
|
5
|
|
579
|
my $type = shift; |
21
|
5
|
|
|
|
|
7
|
my ($in) = @_; |
22
|
|
|
|
|
|
|
|
23
|
5
|
50
|
|
|
|
13
|
exists $in->{'get'} or croak "Improper use of 'tie' on $type: Field 'get' required; stopped"; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
596
|
|
26
|
5
|
|
|
|
|
13
|
my Devel::WarnGlobal::Scalar $self = bless {}, $type; |
27
|
5
|
|
|
|
|
16
|
$self->{'get'} = $in->{'get'}; |
28
|
5
|
100
|
|
|
|
14
|
$self->{'set'} = $in->{'set'} if defined $in->{'set'}; |
29
|
5
|
100
|
|
|
|
16
|
$self->{'name'} = $in->{'name'} if defined $in->{'name'}; |
30
|
5
|
|
|
|
|
17
|
$self->{'die_on_write'} = $type->_get_boolean($in, 'die_on_write', 0); |
31
|
5
|
|
|
|
|
12
|
$self->{'warn'} = $type->_get_boolean($in, 'warn', 1); |
32
|
|
|
|
|
|
|
|
33
|
5
|
|
|
|
|
23
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _get_boolean { |
37
|
10
|
|
|
10
|
|
13
|
my $type = shift; |
38
|
10
|
|
|
|
|
12
|
my ($hash, $member, $default) = @_; |
39
|
|
|
|
|
|
|
|
40
|
10
|
100
|
|
|
|
24
|
if ( defined $hash->{$member} ) { |
41
|
1
|
|
|
|
|
4
|
return $hash->{$member}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
9
|
|
|
|
|
21
|
return $default; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub FETCH { |
49
|
9
|
|
|
9
|
|
1747
|
my Devel::WarnGlobal::Scalar $self = shift; |
50
|
|
|
|
|
|
|
|
51
|
9
|
100
|
|
|
|
28
|
$self->{'warn'} and do { |
52
|
8
|
|
|
|
|
20
|
warn(ucfirst($self->_get_identifier()), " was read-accessed ", $self->_get_caller_info()); |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
|
55
|
9
|
|
|
|
|
56
|
return $self->{'get'}->(); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _get_caller_info { |
59
|
14
|
|
|
14
|
|
17
|
my Devel::WarnGlobal::Scalar $self = shift; |
60
|
|
|
|
|
|
|
|
61
|
14
|
|
|
|
|
66
|
my ($package, $filename, $line, $subroutine) = caller(1); |
62
|
14
|
|
|
|
|
113
|
return "at $filename line $line.\n"; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _get_identifier { |
68
|
14
|
|
|
14
|
|
20
|
my Devel::WarnGlobal::Scalar $self = shift; |
69
|
|
|
|
|
|
|
|
70
|
14
|
100
|
|
|
|
35
|
if (defined $self->{'name'}) { |
71
|
8
|
|
|
|
|
45
|
return "global '$self->{'name'}'"; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
else { |
74
|
6
|
|
|
|
|
26
|
return "a global"; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub STORE { |
79
|
6
|
|
|
6
|
|
1391
|
my Devel::WarnGlobal::Scalar $self = shift; |
80
|
6
|
|
|
|
|
10
|
my ($new_value) = @_; |
81
|
|
|
|
|
|
|
|
82
|
6
|
100
|
66
|
|
|
39
|
if ( $self->{'warn'} && (! $self->{'die_on_write'} ) ) { |
83
|
4
|
|
|
|
|
12
|
warn(ucfirst( $self->_get_identifier() ), " was write-accessed ", $self->_get_caller_info()); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
6
|
100
|
|
|
|
31
|
if (! defined($self->{'set'}) ) { |
87
|
|
|
|
|
|
|
|
88
|
4
|
100
|
66
|
|
|
30
|
if ( defined($self->{'die_on_write'}) && $self->{'die_on_write'} ) { |
89
|
2
|
|
|
|
|
6
|
die "Attempt to write-access ", $self->_get_identifier(), "(read-only) ", $self->_get_caller_info(); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
else { |
93
|
2
|
|
|
|
|
8
|
$self->{'set'}->($new_value); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
0
|
|
0
|
sub DESTROY { } |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub warn { |
100
|
6
|
|
|
6
|
0
|
881
|
my Devel::WarnGlobal::Scalar $self = shift; |
101
|
6
|
|
|
|
|
12
|
my ($warn_val) = @_; |
102
|
|
|
|
|
|
|
|
103
|
6
|
100
|
|
|
|
18
|
defined $warn_val or return $self->{'warn'}; |
104
|
|
|
|
|
|
|
|
105
|
4
|
|
|
|
|
11
|
$self->{'warn'} = $warn_val; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub die_on_write { |
109
|
6
|
|
|
6
|
0
|
858
|
my Devel::WarnGlobal::Scalar $self = shift; |
110
|
6
|
|
|
|
|
9
|
my ($die_val) = @_; |
111
|
|
|
|
|
|
|
|
112
|
6
|
100
|
|
|
|
22
|
defined $die_val or return $self->{'die_on_write'}; |
113
|
|
|
|
|
|
|
|
114
|
4
|
|
|
|
|
13
|
$self->{'die_on_write'} = $die_val; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
1; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
__END__ |