blib/lib/Readonly.pm

filter: Statementstmt 85.1% 217 / 255 Branchbran 67.3% 97 / 144 Conditioncond 72.2% 13 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 90.9% 40 / 44 Podpod 77.7% 7 / 9 totaltotal 79.0% 380 / 481 CC 82 SCAR 19.9
CC
82
Cov
78%
Readonly
18.3
Scalar
13.1
Scalar1
11.1
CRAP 7.3
↑ Summary
1 package Readonly;
2 14 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #use warnings;
6 #no warnings 'uninitialized';
7 package Readonly;
8 14 our $VERSION = '2.05';
9 14 $VERSION = eval $VERSION;
10  
11 # Autocroak (Thanks, MJD)
12 # Only load Carp.pm if module is croaking.
13 50 ▶sub croak {
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
14 50 require Carp;
15 50 goto &Carp::croak;
16 }
17  
18 # These functions may be overridden by Readonly::XS, if installed.
19 14 ▶use vars qw/$XSokay/; # Set to true in Readonly::XS, if available
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Predeclare the following, so we can use them recursively
22 sub _ARRAY (\@);
23 sub _HASH (\%);
24  
25 # For perl 5.8.x or higher
26 # These functions are exposed in perl 5.8.x (Thanks, Leon!)
27 # They may be overridden by Readonly::XS, if installed on old perl versions
28 14 ▶if ($] < 5.008) { # 'Classic' perl
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.008) { } 0 14 14
29 0 ▶ *is_sv_readonly = sub ($) {0};
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 *make_sv_readonly
31 0 ▶ = sub ($) { die "make_sv_readonly called but not overridden" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
32  
33 # See if we can use the XS stuff.
34 0 $Readonly::XS::MAGIC_COOKIE = $Readonly::XS::MAGIC_COOKIE
35 = "Do NOT use or require Readonly::XS unless you're me.";
36 0 eval 'use Readonly::XS';
37 }
38 else { # Modern perl doesn't need Readonly::XS
39 30 ▶ *is_sv_readonly = sub ($) { Internals::SvREADONLY($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
40 *make_sv_readonly
41 17 ▶ = sub ($) { Internals::SvREADONLY($_[0], 1) };
Subroutine __ANON__: called CC=1 SCAR=0.0
42 14 $XSokay = 1; # We're using the new built-ins so this is a white lie
43 }
44  
45 # Undo setting readonly
46 1 ▶sub _SCALAR ($) {
Subroutine _SCALAR: called CC=4 SCAR=22.2
47 1 my ($r_var) = @_;
48 1 ▶ if ($XSokay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Readonly::XSokay) { } 1 0 1
49 1 ▶ Internals::SvREADONLY($r_var, 0) if is_sv_readonly($r_var);
Branch: 1 branch
BranchTrueFalseTotal
if &is_sv_readonly($r_var) 0 1 1
50 }
51 else {
52 0 ▶ return if tied($r_var) !~ 'Readonly::Scalar';
Branch: 1 branch
BranchTrueFalseTotal
unless tied($r_var) =~ /Readonly::Scalar/ 0 0 0
53 0 my $r_scalar;
54 {
55 0 my $obj = tied $$r_var;
56 0 $r_scalar = $obj;
57 }
58 0 untie $r_var;
59 0 $r_var = $r_scalar;
60 }
61 }
62  
63 9 ▶sub _ARRAY (\@) {
Subroutine _ARRAY: called CC=7 SCAR=20.0
64 9 my ($r_var) = @_;
65 9 ▶ return if !tied(@$r_var);
Branch: 1 branch
BranchTrueFalseTotal
unless tied @$r_var 4 5 9
66 5 ▶ return if tied(@$r_var) !~ 'Readonly::Array';
Branch: 1 branch
BranchTrueFalseTotal
unless tied(@$r_var) =~ /Readonly::Array/ 0 5 5
67 5 my $r_array;
68 {
69 5 my $obj = tied @$r_var;
70 5 $r_array = $obj;
71 }
72 5 untie @$r_var;
73 5 @$r_var = @$r_array;
74  
75 # Recursively check child elements for references; clean if Readonly
76 5 foreach (@$r_var) {
77 12 my $_reftype = ref $_;
78 0 ▶ if ($_reftype eq 'SCALAR') { _SCALAR($_) }
Branch: 3 branches
BranchTrueFalseTotal
if ($_reftype eq 'SCALAR') { } 0 12 12
elsif ($_reftype eq 'ARRAY') { } 1 11 12
elsif ($_reftype eq 'HASH') { } 0 11 11
79 elsif ($_reftype eq 'ARRAY') {
80 1 _ARRAY(@$_);
81 }
82 elsif ($_reftype eq 'HASH') {
83 0 _HASH(%$_);
84 }
85 }
86 }
87  
88 8 ▶sub _HASH (\%) {
Subroutine _HASH: called CC=7 SCAR=20.0
89 8 my ($r_var) = @_;
90 8 ▶ return if !tied(%$r_var);
Branch: 1 branch
BranchTrueFalseTotal
unless tied %$r_var 4 4 8
91 4 ▶ return if tied(%$r_var) !~ 'Readonly::Hash';
Branch: 1 branch
BranchTrueFalseTotal
unless tied(%$r_var) =~ /Readonly::Hash/ 0 4 4
92 4 my $r_hash;
93 {
94 4 my $obj = tied %$r_var;
95 4 $r_hash = $obj;
96 }
97 4 untie %$r_var;
98 4 %$r_var = %$r_hash;
99  
100 # Recursively check child elements for references; clean if Readonly
101 4 foreach (values %$r_var) {
102 4 my $_reftype = ref $_;
103 0 ▶ if ($_reftype eq 'SCALAR') { _SCALAR($_) }
Branch: 3 branches
BranchTrueFalseTotal
if ($_reftype eq 'SCALAR') { } 0 4 4
elsif ($_reftype eq 'ARRAY') { } 1 3 4
elsif ($_reftype eq 'HASH') { } 0 3 3
104 elsif ($_reftype eq 'ARRAY') {
105 1 _ARRAY(@$_);
106 }
107 elsif ($_reftype eq 'HASH') {
108 0 _HASH(%$_);
109 }
110 }
111 }
112  
113 # Common error messages, or portions thereof
114 14 ▶use vars qw/$MODIFY $REASSIGN $ODDHASH/;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 14 $MODIFY = 'Modification of a read-only value attempted';
116 14 $REASSIGN = 'Attempt to reassign a readonly';
117 14 $ODDHASH = 'May not store an odd number of values in a hash';
118  
119 # ----------------
120 # Read-only scalars
121 # ----------------
122 package Readonly::Scalar;
123  
124 0 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: not called CC=2 SCAR=17.9
125 0 my ($self, $cloning) = @_;
126 0 ▶ Readonly::_SCALAR($$self) if $cloning;
Branch: 1 branch
BranchTrueFalseTotal
if $cloning 0 0 0
127 }
128  
129 5 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=5 SCAR=16.4
130 5 my $whence
131 = (caller 2)[3]; # Check if naughty user is trying to tie directly.
132 5 ▶ Readonly::croak "Invalid tie"
Branch: 1 branch
BranchTrueFalseTotal
unless $whence and $whence =~ /^Readonly::(?:Scalar1?|Readonly)$/ 1 4 5
Condition66%2/3
$whence and $whence =~ /^Readonly::(?:Scalar1?|Readonly)$/
!ll&&!rl&&r
104
MC/DC50%1/2
$whence and $whence =~ /^Readonly::(?:Scalar1?|Readonly)$/
$whence $whence =~ /^Readonly::(?:Scalar1?|Readonly)$/
Truth table66%2/3
A and B
A= $whenceB= $whence =~ /^Readonly::(?:Scalar1?|Readonly)$/
ABresult
0X0
100
111
133 unless $whence && $whence =~ /^Readonly::(?:Scalar1?|Readonly)$/;
134 4 my $class = shift;
135 4 ▶ Readonly::croak "No value specified for readonly scalar" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
136 4 ▶ Readonly::croak "Too many values specified for readonly scalar"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 4 4
137 unless @_ == 1;
138 4 my $value = shift;
139 4 return bless \$value, $class;
140 }
141  
142 1 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
143 1 my $self = shift;
144 1 return $$self;
145 }
146 1 ▶*STORE = *STORE = sub { Readonly::croak $Readonly::MODIFY };
Subroutine __ANON__: called CC=1 SCAR=0.0
147 *UNTIE = *UNTIE
148 0 ▶ = sub { Readonly::croak $Readonly::MODIFY if caller() ne 'Readonly' };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
if caller() ne "Readonly" 0 0 0
149  
150 # ----------------
151 # Read-only arrays
152 # ----------------
153 package Readonly::Array;
154  
155 4 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=2 SCAR=7.2
156 4 my ($self, $cloning) = @_;
157 4 ▶ Readonly::_ARRAY(@$self) if $cloning;
Branch: 1 branch
BranchTrueFalseTotal
if $cloning 4 0 4
158 }
159  
160 28 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=2 SCAR=6.9
161 28 my $whence
162 = (caller 1)[3]; # Check if naughty user is trying to tie directly.
163 28 ▶ Readonly::croak "Invalid tie" unless $whence =~ /^Readonly::Array1?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $whence =~ /^Readonly::Array1?$/ 1 27 28
164 27 my $class = shift;
165 27 my @self = @_;
166 27 return bless \@self, $class;
167 }
168  
169 32 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
170 32 my $self = shift;
171 32 my $index = shift;
172 32 return $self->[$index];
173 }
174  
175 14 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
176 14 my $self = shift;
177 14 return scalar @$self;
178 }
179  
180 14 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
181 14 ▶ eval q{
Subroutine EXISTS: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.006" 14 0 14
182 sub EXISTS
183 {
184 my $self = shift;
185 my $index = shift;
186 return exists $self->[$index];
187 }
188 } if $] >= 5.006; # couldn't do "exists" on arrays before then
189 }
190 *STORE = *STORESIZE = *EXTEND = *PUSH = *POP = *UNSHIFT = *SHIFT = *SPLICE
191 = *CLEAR = *STORE = *STORESIZE = *EXTEND = *PUSH = *POP = *UNSHIFT
192 19 ▶ = *SHIFT = *SPLICE = *CLEAR = sub { Readonly::croak $Readonly::MODIFY};
Subroutine __ANON__: called CC=1 SCAR=0.0
193 *UNTIE = *UNTIE
194 6 ▶ = sub { Readonly::croak $Readonly::MODIFY if caller() ne 'Readonly' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if caller() ne "Readonly" 1 5 6
195  
196 # ----------------
197 # Read-only hashes
198 # ----------------
199 package Readonly::Hash;
200  
201 4 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=2 SCAR=7.2
202 4 my ($self, $cloning) = @_;
203 4 ▶ Readonly::_HASH(%$self) if $cloning;
Branch: 1 branch
BranchTrueFalseTotal
if $cloning 4 0 4
204 }
205  
206 25 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=3 SCAR=11.0
207 25 my $whence
208 = (caller 1)[3]; # Check if naughty user is trying to tie directly.
209 25 ▶ Readonly::croak "Invalid tie" unless $whence =~ /^Readonly::Hash1?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $whence =~ /^Readonly::Hash1?$/ 1 24 25
210 24 my $class = shift;
211  
212 # must have an even number of values
213 24 ▶ Readonly::croak $Readonly::ODDHASH unless (@_ % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 24 24
214 24 my %self = @_;
215 24 return bless \%self, $class;
216 }
217  
218 25 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
219 25 my $self = shift;
220 25 my $key = shift;
221 25 return $self->{$key};
222 }
223  
224 8 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
225 8 my $self = shift;
226 8 my $key = shift;
227 8 return exists $self->{$key};
228 }
229  
230 5 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
231 5 my $self = shift;
232 5 my $dummy = keys %$self;
233 5 return scalar each %$self;
234 }
235  
236 16 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
237 16 my $self = shift;
238 16 return scalar each %$self;
239 }
240 *STORE = *DELETE = *CLEAR = *STORE = *DELETE = *CLEAR
241 10 ▶ = sub { Readonly::croak $Readonly::MODIFY};
Subroutine __ANON__: called CC=1 SCAR=0.0
242 *UNTIE = *UNTIE
243 5 ▶ = sub { Readonly::croak $Readonly::MODIFY if caller() ne 'Readonly'; };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if caller() ne "Readonly" 1 4 5
244  
245 # ----------------------------------------------------------------
246 # Main package, containing convenience functions (so callers won't
247 # have to explicitly tie the variables themselves).
248 # ----------------------------------------------------------------
249 package Readonly;
250 14 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 14 ▶use vars qw/@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
252 14 push @ISA, 'Exporter';
253 14 push @EXPORT, qw/Readonly/;
254 14 push @EXPORT_OK, qw/Scalar Array Hash Scalar1 Array1 Hash1/;
255  
256 # Predeclare the following, so we can use them recursively
257 sub Scalar ($$);
258 sub Array (\@;@);
259 sub Hash (\%;@);
260  
261 # Returns true if a string begins with "Readonly::"
262 # Used to prevent reassignment of Readonly variables.
263 83 ▶sub _is_badtype {
Subroutine _is_badtype: called CC=2 SCAR=6.9
264 83 my $type = $_[0];
265 83 ▶ return lc $type if $type =~ s/^Readonly:://;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ s/^Readonly::// 5 78 83
266 78 return;
267 }
268  
269 # Shallow Readonly scalar
270 3 ▶sub Scalar1 ($$) {
Subroutine Scalar1: called CC=6 SCAR=24.1
Pod documented
271 3 ▶ croak "$REASSIGN scalar" if is_sv_readonly($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &is_sv_readonly($_[0]) 1 2 3
272 2 my $badtype = _is_badtype(ref tied $_[0]);
273 2 ▶ croak "$REASSIGN $badtype" if $badtype;
Branch: 1 branch
BranchTrueFalseTotal
if $badtype 0 2 2
274  
275 # xs method: flag scalar as readonly
276 2 ▶ if ($XSokay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Readonly::XSokay) 2 0 2
277 2 $_[0] = $_[1];
278 2 make_sv_readonly($_[0]);
279 2 return;
280 }
281  
282 # pure-perl method: tied scalar
283 0 my $tieobj = eval { tie $_[0], 'Readonly::Scalar', $_[1] };
284 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
285 0 ▶ croak "$REASSIGN scalar" if substr($@, 0, 43) eq $MODIFY;
Branch: 1 branch
BranchTrueFalseTotal
if substr($@, 0, 43) eq $Readonly::MODIFY 0 0 0
286 0 die $@; # some other error?
287 }
288 0 return $tieobj;
289 }
290  
291 # Shallow Readonly array
292 2 ▶sub Array1 (\@;@) {
Subroutine Array1: called CC=2 SCAR=7.0
Pod documented
293 2 my $badtype = _is_badtype(ref tied $_[0]);
294 2 ▶ croak "$REASSIGN $badtype" if $badtype;
Branch: 1 branch
BranchTrueFalseTotal
if $badtype 0 2 2
295 2 my $aref = shift;
296 2 return tie @$aref, 'Readonly::Array', @_;
297 }
298  
299 # Shallow Readonly hash
300 1 ▶sub Hash1 (\%;@) {
Subroutine Hash1: called CC=5 SCAR=19.1
Pod documented
301 1 my $badtype = _is_badtype(ref tied $_[0]);
302 1 ▶ croak "$REASSIGN $badtype" if $badtype;
Branch: 1 branch
BranchTrueFalseTotal
if $badtype 0 1 1
303 1 my $href = shift;
304  
305 # If only one value, and it's a hashref, expand it
306 1 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq "HASH") 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
307 0 return tie %$href, 'Readonly::Hash', %{$_[0]};
308 }
309  
310 # otherwise, must have an even number of values
311 1 ▶ croak $ODDHASH unless (@_ % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 1 1
312 1 return tie %$href, 'Readonly::Hash', @_;
313 }
314  
315 # Deep Readonly scalar
316 17 ▶sub Scalar ($$) {
Subroutine Scalar: called CC=10 SCAR=25.8
Pod documented
317 17 ▶ croak "$REASSIGN scalar" if is_sv_readonly($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &is_sv_readonly($_[0]) 2 15 17
318 15 my $badtype = _is_badtype(ref tied $_[0]);
319 15 ▶ croak "$REASSIGN $badtype" if $badtype;
Branch: 1 branch
BranchTrueFalseTotal
if $badtype 0 15 15
320 15 my $value = $_[1];
321  
322 # Recursively check passed element for references; if any, make them Readonly
323 15 foreach ($value) {
324 15 ▶ if (ref eq 'SCALAR') { Scalar my $v => $$_; $_ = \$v }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'SCALAR') { } 1 14 15
elsif (ref $_ eq 'ARRAY') { } 2 12 14
elsif (ref $_ eq 'HASH') { } 2 10 12
325 2 elsif (ref eq 'ARRAY') { Array my @v => @$_; $_ = \@v }
326 2 elsif (ref eq 'HASH') { Hash my %v => $_; $_ = \%v }
327 }
328  
329 # xs method: flag scalar as readonly
330 15 ▶ if ($XSokay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Readonly::XSokay) 15 0 15
331 15 $_[0] = $value;
332 15 make_sv_readonly($_[0]);
333 15 return;
334 }
335  
336 # pure-perl method: tied scalar
337 0 my $tieobj = eval { tie $_[0], 'Readonly::Scalar', $value };
338 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
339 0 ▶ croak "$REASSIGN scalar" if substr($@, 0, 43) eq $MODIFY;
Branch: 1 branch
BranchTrueFalseTotal
if substr($@, 0, 43) eq $Readonly::MODIFY 0 0 0
340 0 die $@; # some other error?
341 }
342 0 return $tieobj;
343 }
344  
345 # Deep Readonly array
346 27 ▶sub Array (\@;@) {
Subroutine Array: called CC=6 SCAR=17.9
Pod documented
347 27 my $badtype = _is_badtype(ref tied @{$_[0]});
348 27 ▶ croak "$REASSIGN $badtype" if $badtype;
Branch: 1 branch
BranchTrueFalseTotal
if $badtype 2 25 27
349 25 my $aref = shift;
350 25 my @values = @_;
351  
352 # Recursively check passed elements for references; if any, make them Readonly
353 25 foreach (@values) {
354 67 ▶ if (ref eq 'SCALAR') { Scalar my $v => $$_; $_ = \$v }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'SCALAR') { } 1 66 67
elsif (ref $_ eq 'ARRAY') { } 1 65 66
elsif (ref $_ eq 'HASH') { } 3 62 65
355 1 elsif (ref eq 'ARRAY') { Array my @v => @$_; $_ = \@v }
356 3 elsif (ref eq 'HASH') { Hash my %v => $_; $_ = \%v }
357 }
358  
359 # Lastly, tie the passed reference
360 25 return tie @$aref, 'Readonly::Array', @values;
361 }
362  
363 # Deep Readonly hash
364 27 ▶sub Hash (\%;@) {
Subroutine Hash: called CC=9 SCAR=22.0
Pod documented
365 27 my $badtype = _is_badtype(ref tied %{$_[0]});
366 27 ▶ croak "$REASSIGN $badtype" if $badtype;
Branch: 1 branch
BranchTrueFalseTotal
if $badtype 2 25 27
367 25 my $href = shift;
368 25 my @values = @_;
369  
370 # If only one value, and it's a hashref, expand it
371 25 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq "HASH") 11 14 25
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
14011
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
372 11 @values = %{$_[0]};
373 }
374  
375 # otherwise, must have an even number of values
376 25 ▶ croak $ODDHASH unless (@values % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @values % 2 == 0 2 23 25
377  
378 # Recursively check passed elements for references; if any, make them Readonly
379 23 foreach (@values) {
380 78 ▶ if (ref eq 'SCALAR') { Scalar my $v => $$_; $_ = \$v }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'SCALAR') { } 1 77 78
elsif (ref $_ eq 'ARRAY') { } 3 74 77
elsif (ref $_ eq 'HASH') { } 1 73 74
381 3 elsif (ref eq 'ARRAY') { Array my @v => @$_; $_ = \@v }
382 1 elsif (ref eq 'HASH') { Hash my %v => $_; $_ = \%v }
383 }
384 23 return tie %$href, 'Readonly::Hash', @values;
385 }
386  
387 8 ▶sub Clone(\[$@%]) {
Subroutine Clone: called CC=6 SCAR=17.9
Pod undocumented
388 8 require Storable;
389 8 my $retval = Storable::dclone($_[0]);
390 8 ▶ $retval = $$retval if ref $retval eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if ref $retval eq "REF" 3 5 8
391 8 my $reftype = ref $retval;
392 8 ▶ if ($reftype eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'SCALAR') { } 1 7 8
elsif ($reftype eq 'ARRAY') { } 3 4 7
elsif ($reftype eq 'HASH') { } 4 0 4
393 1 _SCALAR($retval);
394 1 return $$retval;
395 }
396 elsif ($reftype eq 'ARRAY') {
397 3 _ARRAY(@$retval);
398 }
399 elsif ($reftype eq 'HASH') {
400 4 _HASH(%$retval);
401 4 ▶ return %$retval if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 1 4
402 }
403 4 return $retval;
404 }
405  
406 # Common entry-point for all supported data types
407 0 ▶eval q{sub Readonly} . ($] < 5.008 ? '' : '(\[$@%]@)') . <<'SUB_READONLY';
Subroutine Readonly: called CC=15 SCAR=29.1
Pod documented
Branch: 12 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'SCALAR') { } 9 15 24
elsif (ref $_[0] eq 'ARRAY') { } 6 9 15
elsif (ref $_[0] eq 'HASH') { } 9 0 9
elsif (ref $_[0]) { } 0 0 0
if &is_sv_readonly(${$_[0];}) 0 9 9
if $badtype 1 8 9
if @_ > 2 0 8 8
if @_ == 1 and defined ${$_[0];} 4 4 8
if ($@) 0 4 4
if $@ =~ /^$Readonly::MODIFY at/ 0 0 0
if @_ % 2 != 0 and not @_ == 1 && ref $_[0] eq "HASH" 1 8 9
$] < 5.008 ? : 0 14 14
Condition88%8/9
@_ == 1 and defined ${$_[0];}
!ll&&!rl&&r
314
@_ % 2 != 0 and not @_ == 1 && ref $_[0] eq "HASH"
!ll&&!rl&&r
441
@_ == 1 && ref $_[0] eq 'HASH'
!ll&&!rl&&r
104
MC/DC100%2/2
@_ == 1 and defined ${$_[0];}
@_ == 1 defined ${$_[0];}
MC/DC66%2/3
@_ % 2 != 0 and not @_ == 1 && ref $_[0] eq "HASH"
@_ % 2 != 0 @_ == 1 ref $_[0] eq 'HASH'
Truth table100%3/3
A and B
A= @_ == 1B= defined ${$_[0];}
ABresult
0X0
100
111
Truth table75%3/4
A and not(B && C)
A= @_ % 2 != 0B= @_ == 1C= ref $_[0] eq 'HASH'
ABCresult
0XX0
1110
10X1
1101
408 {
409 if (ref $_[0] eq 'SCALAR')
410 {
411 croak $MODIFY if is_sv_readonly ${$_[0]};
412 my $badtype = _is_badtype (ref tied ${$_[0]});
413 croak "$REASSIGN $badtype" if $badtype;
414 croak "Readonly scalar must have only one value" if @_ > 2;
415  
416 # Because of problems with handling \$ prototypes declarations like
417 # Readonly my @a = ... and Readonly my %h = ... are also caught here
418 croak 'Invalid initialization by assignment'
419 if @_ == 1 && defined ${$_[0]};
420  
421 my $tieobj = eval {tie ${$_[0]}, 'Readonly::Scalar', $_[1]};
422 # Tie may have failed because user tried to tie a constant, or we screwed up somehow.
423 if ($@)
424 {
425 croak $MODIFY if $@ =~ /^$MODIFY at/; # Point the finger at the user.
426 die "$@\n"; # Not a modify read-only message; must be our fault.
427 }
428 return $tieobj;
429 }
430 elsif (ref $_[0] eq 'ARRAY')
431 {
432 my $aref = shift;
433 return Array @$aref, @_;
434 }
435 elsif (ref $_[0] eq 'HASH')
436 {
437 my $href = shift;
438 croak $ODDHASH if @_%2 != 0 && !(@_ == 1 && ref $_[0] eq 'HASH');
439 return Hash %$href, @_;
440 }
441 elsif (ref $_[0])
442 {
443 croak "Readonly only supports scalar, array, and hash variables.";
444 }
445 else
446 {
447 croak "First argument to Readonly must be a reference.";
448 }
449 }
450 SUB_READONLY
451 14 1;
452  
453 =head1 NAME
454  
455 Readonly - Facility for creating read-only scalars, arrays, hashes
456  
457 =head1 Synopsis
458  
459 use Readonly;
460  
461 # Deep Read-only scalar
462 Readonly::Scalar $sca => $initial_value;
463 Readonly::Scalar my $sca => $initial_value;
464  
465 # Deep Read-only array
466 Readonly::Array @arr => @values;
467 Readonly::Array my @arr => @values;
468  
469 # Deep Read-only hash
470 Readonly::Hash %has => (key => value, key => value, ...);
471 Readonly::Hash my %has => (key => value, key => value, ...);
472 # or:
473 Readonly::Hash %has => {key => value, key => value, ...};
474  
475 # You can use the read-only variables like any regular variables:
476 print $sca;
477 $something = $sca + $arr[2];
478 next if $has{$some_key};
479  
480 # But if you try to modify a value, your program will die:
481 $sca = 7;
482 push @arr, 'seven';
483 delete $has{key};
484 # The error message is "Modification of a read-only value attempted"
485  
486 # Alternate form (Perl 5.8 and later)
487 Readonly $sca => $initial_value;
488 Readonly my $sca => $initial_value;
489 Readonly @arr => @values;
490 Readonly my @arr => @values;
491 Readonly %has => (key => value, key => value, ...);
492 Readonly my %has => (key => value, key => value, ...);
493 Readonly my $sca; # Implicit undef, readonly value
494  
495 # Alternate form (for Perls earlier than v5.8)
496 Readonly \$sca => $initial_value;
497 Readonly \my $sca => $initial_value;
498 Readonly \@arr => @values;
499 Readonly \my @arr => @values;
500 Readonly \%has => (key => value, key => value, ...);
501 Readonly \my %has => (key => value, key => value, ...);
502  
503 =head1 Description
504  
505 This is a facility for creating non-modifiable variables. This is useful for
506 configuration files, headers, etc. It can also be useful as a development and
507 debugging tool for catching updates to variables that should not be changed.
508  
509 =head1 Variable Depth
510  
511 Readonly has the ability to create both deep and shallow readonly variables.
512  
513 If you pass a C<$ref>, an C<@array> or a C<%hash> to corresponding functions
514 C<::Scalar()>, C<::Array()> and C<::Hash()>, then those functions recurse over
515 the data structure, marking everything as readonly. The entire structure is
516 then non-modifiable. This is normally what you want.
517  
518 If you want only the top level to be readonly, use the alternate (and poorly
519 named) C<::Scalar1()>, C<::Array1()>, and C<::Hash1()> functions.
520  
521 Plain C<Readonly()> creates what the original author calls a "shallow"
522 readonly variable, which is great if you don't plan to use it on anything but
523 only one dimensional scalar values.
524  
525 C<Readonly::Scalar()> makes the variable 'deeply' readonly, so the following
526 snippet kills over as you expect:
527  
528 use Readonly;
529  
530 Readonly::Scalar my $ref => { 1 => 'a' };
531 $ref->{1} = 'b';
532 $ref->{2} = 'b';
533  
534 While the following snippet does B<not> make your structure 'deeply' readonly:
535  
536 use Readonly;
537  
538 Readonly my $ref => { 1 => 'a' };
539 $ref->{1} = 'b';
540 $ref->{2} = 'b';
541  
542 =head1
543  
544 =head1 The Past
545  
546 The following sections are updated versions of the previous authors
547 documentation.
548  
549 =head2 Comparison with "use constant"
550  
551 Perl provides a facility for creating constant values, via the L<constant>
552 pragma. There are several problems with this pragma.
553  
554 =over 2
555  
556 =item * The constants created have no leading sigils.
557  
558 =item * These constants cannot be interpolated into strings.
559  
560 =item * Syntax can get dicey sometimes. For example:
561  
562 use constant CARRAY => (2, 3, 5, 7, 11, 13);
563 $a_prime = CARRAY[2]; # wrong!
564 $a_prime = (CARRAY)[2]; # right -- MUST use parentheses
565  
566 =item * You have to be very careful in places where barewords are allowed.
567  
568 For example:
569  
570 use constant SOME_KEY => 'key';
571 %hash = (key => 'value', other_key => 'other_value');
572 $some_value = $hash{SOME_KEY}; # wrong!
573 $some_value = $hash{+SOME_KEY}; # right
574  
575 (who thinks to use a unary plus when using a hash to scalarize the key?)
576  
577 =item * C<use constant> works for scalars and arrays, not hashes.
578  
579 =item * These constants are global to the package in which they're declared;
580 cannot be lexically scoped.
581  
582 =item * Works only at compile time.
583  
584 =item * Can be overridden:
585  
586 use constant PI => 3.14159;
587 ...
588 use constant PI => 2.71828;
589  
590 (this does generate a warning, however, if you have warnings enabled).
591  
592 =item * It is very difficult to make and use deep structures (complex data
593 structures) with C<use constant>.
594  
595 =back
596  
597 =head1 Comparison with typeglob constants
598  
599 Another popular way to create read-only scalars is to modify the symbol table
600 entry for the variable by using a typeglob:
601  
602 *a = \'value';
603  
604 This works fine, but it only works for global variables ("my" variables have
605 no symbol table entry). Also, the following similar constructs do B<not> work:
606  
607 *a = [1, 2, 3]; # Does NOT create a read-only array
608 *a = { a => 'A'}; # Does NOT create a read-only hash
609  
610 =head2 Pros
611  
612 Readonly.pm, on the other hand, will work with global variables and with
613 lexical ("my") variables. It will create scalars, arrays, or hashes, all of
614 which look and work like normal, read-write Perl variables. You can use them
615 in scalar context, in list context; you can take references to them, pass them
616 to functions, anything.
617  
618 Readonly.pm also works well with complex data structures, allowing you to tag
619 the whole structure as nonmodifiable, or just the top level.
620  
621 Also, Readonly variables may not be reassigned. The following code will die:
622  
623 Readonly::Scalar $pi => 3.14159;
624 ...
625 Readonly::Scalar $pi => 2.71828;
626  
627 =head2 Cons
628  
629 Readonly.pm used to impose a performance penalty. It was pretty slow. How
630 slow? Run the C<eg/benchmark.pl> script that comes with Readonly. On my test
631 system, "use constant" (const), typeglob constants (tglob), regular read/write
632 Perl variables (normal/literal), and the new Readonly (ro/ro_simple) are all
633 about the same speed, the old, tie based Readonly.pm constants were about 1/22
634 the speed.
635  
636 However, there is relief. There is a companion module available, Readonly::XS.
637 You won't need this if you're using Perl 5.8.x or higher.
638  
639 I repeat, you do not need Readonly::XS if your environment has perl 5.8.x or
640 higher. Please see section entitled L<Internals|/"Internals"> for more.
641  
642 =head1 Functions
643  
644 =over 4
645  
646 =item Readonly::Scalar $var => $value;
647  
648 Creates a nonmodifiable scalar, C<$var>, and assigns a value of C<$value> to
649 it. Thereafter, its value may not be changed. Any attempt to modify the value
650 will cause your program to die.
651  
652 A value I<must> be supplied. If you want the variable to have C<undef> as its
653 value, you must specify C<undef>.
654  
655 If C<$value> is a reference to a scalar, array, or hash, then this function
656 will mark the scalar, array, or hash it points to as being Readonly as well,
657 and it will recursively traverse the structure, marking the whole thing as
658 Readonly. Usually, this is what you want. However, if you want only the
659 C<$value> marked as Readonly, use C<Scalar1>.
660  
661 If $var is already a Readonly variable, the program will die with an error
662 about reassigning Readonly variables.
663  
664 =item Readonly::Array @arr => (value, value, ...);
665  
666 Creates a nonmodifiable array, C<@arr>, and assigns the specified list of
667 values to it. Thereafter, none of its values may be changed; the array may not
668 be lengthened or shortened or spliced. Any attempt to do so will cause your
669 program to die.
670  
671 If any of the values passed is a reference to a scalar, array, or hash, then
672 this function will mark the scalar, array, or hash it points to as being
673 Readonly as well, and it will recursively traverse the structure, marking the
674 whole thing as Readonly. Usually, this is what you want. However, if you want
675 only the hash C<%@arr> itself marked as Readonly, use C<Array1>.
676  
677 If C<@arr> is already a Readonly variable, the program will die with an error
678 about reassigning Readonly variables.
679  
680 =item Readonly::Hash %h => (key => value, key => value, ...);
681  
682 =item Readonly::Hash %h => {key => value, key => value, ...};
683  
684 Creates a nonmodifiable hash, C<%h>, and assigns the specified keys and values
685 to it. Thereafter, its keys or values may not be changed. Any attempt to do so
686 will cause your program to die.
687  
688 A list of keys and values may be specified (with parentheses in the synopsis
689 above), or a hash reference may be specified (curly braces in the synopsis
690 above). If a list is specified, it must have an even number of elements, or
691 the function will die.
692  
693 If any of the values is a reference to a scalar, array, or hash, then this
694 function will mark the scalar, array, or hash it points to as being Readonly
695 as well, and it will recursively traverse the structure, marking the whole
696 thing as Readonly. Usually, this is what you want. However, if you want only
697 the hash C<%h> itself marked as Readonly, use C<Hash1>.
698  
699 If C<%h> is already a Readonly variable, the program will die with an error
700 about reassigning Readonly variables.
701  
702 =item Readonly $var => $value;
703  
704 =item Readonly @arr => (value, value, ...);
705  
706 =item Readonly %h => (key => value, ...);
707  
708 =item Readonly %h => {key => value, ...};
709  
710 =item Readonly $var;
711  
712 The C<Readonly> function is an alternate to the C<Scalar>, C<Array>, and
713 C<Hash> functions. It has the advantage (if you consider it an advantage) of
714 being one function. That may make your program look neater, if you're
715 initializing a whole bunch of constants at once. You may or may not prefer
716 this uniform style.
717  
718 It has the disadvantage of having a slightly different syntax for versions of
719 Perl prior to 5.8. For earlier versions, you must supply a backslash, because
720 it requires a reference as the first parameter.
721  
722 Readonly \$var => $value;
723 Readonly \@arr => (value, value, ...);
724 Readonly \%h => (key => value, ...);
725 Readonly \%h => {key => value, ...};
726  
727 You may or may not consider this ugly.
728  
729 Note that you can create implicit undefined variables with this function like
730 so C<Readonly my $var;> while a verbose undefined value must be passed to the
731 standard C<Scalar>, C<Array>, and C<Hash> functions.
732  
733 =item Readonly::Scalar1 $var => $value;
734  
735 =item Readonly::Array1 @arr => (value, value, ...);
736  
737 =item Readonly::Hash1 %h => (key => value, key => value, ...);
738  
739 =item Readonly::Hash1 %h => {key => value, key => value, ...};
740  
741 These alternate functions create shallow Readonly variables, instead of deep
742 ones. For example:
743  
744 Readonly::Array1 @shal => (1, 2, {perl=>'Rules', java=>'Bites'}, 4, 5);
745 Readonly::Array @deep => (1, 2, {perl=>'Rules', java=>'Bites'}, 4, 5);
746  
747 $shal[1] = 7; # error
748 $shal[2]{APL}='Weird'; # Allowed! since the hash isn't Readonly
749 $deep[1] = 7; # error
750 $deep[2]{APL}='Weird'; # error, since the hash is Readonly
751  
752 =back
753  
754 =head1 Cloning
755  
756 When cloning using L<Storable> or L<Clone> you will notice that the value stays
757 readonly, which is correct. If you want to clone the value without copying the
758 readonly flag, use the C<Clone> function:
759  
760 Readonly::Scalar my $scalar => {qw[this that]};
761 # $scalar->{'eh'} = 'foo'; # Modification of a read-only value attempted
762 my $scalar_clone = Readonly::Clone $scalar;
763 $scalar_clone->{'eh'} = 'foo';
764 # $scalar_clone is now {this => 'that', eh => 'foo'};
765  
766 The new variable (C<$scalar_clone>) is a mutable clone of the original
767 C<$scalar>.
768  
769 =head1 Examples
770  
771 These are a few very simple examples:
772  
773 =head2 Scalars
774  
775 A plain old read-only value
776  
777 Readonly::Scalar $a => "A string value";
778  
779 The value need not be a compile-time constant:
780  
781 Readonly::Scalar $a => $computed_value;
782  
783 =head2 Arrays/Lists
784  
785 A read-only array:
786  
787 Readonly::Array @a => (1, 2, 3, 4);
788  
789 The parentheses are optional:
790  
791 Readonly::Array @a => 1, 2, 3, 4;
792  
793 You can use Perl's built-in array quoting syntax:
794  
795 Readonly::Array @a => qw/1 2 3 4/;
796  
797 You can initialize a read-only array from a variable one:
798  
799 Readonly::Array @a => @computed_values;
800  
801 A read-only array can be empty, too:
802  
803 Readonly::Array @a => ();
804 Readonly::Array @a; # equivalent
805  
806 =head2 Hashes
807  
808 Typical usage:
809  
810 Readonly::Hash %a => (key1 => 'value1', key2 => 'value2');
811  
812 A read-only hash can be initialized from a variable one:
813  
814 Readonly::Hash %a => %computed_values;
815  
816 A read-only hash can be empty:
817  
818 Readonly::Hash %a => ();
819 Readonly::Hash %a; # equivalent
820  
821 If you pass an odd number of values, the program will die:
822  
823 Readonly::Hash %a => (key1 => 'value1', "value2");
824 # This dies with "May not store an odd number of values in a hash"
825  
826 =head1 Exports
827  
828 Historically, this module exports the C<Readonly> symbol into the calling
829 program's namespace by default. The following symbols are also available for
830 import into your program, if you like: C<Scalar>, C<Scalar1>, C<Array>,
831 C<Array1>, C<Hash>, and C<Hash1>.
832  
833 =head1 Internals
834  
835 Some people simply do not understand the relationship between this module and
836 Readonly::XS so I'm adding this section. Odds are, they still won't understand
837 but I like to write so...
838  
839 In the past, Readonly's "magic" was performed by C<tie()>-ing variables to the
840 C<Readonly::Scalar>, C<Readonly::Array>, and C<Readonly::Hash> packages (not
841 to be confused with the functions of the same names) and acting on C<WRITE>,
842 C<READ>, et. al. While this worked well, it was slow. Very slow. Like 20-30
843 times slower than accessing variables directly or using one of the other
844 const-related modules that have cropped up since Readonly was released in
845 2003.
846  
847 To 'fix' this, Readonly::XS was written. If installed, Readonly::XS used the
848 internal methods C<SvREADONLY> and C<SvREADONLY_on> to lock simple scalars. On
849 the surface, everything was peachy but things weren't the same behind the
850 scenes. In edge cases, code performed very differently if Readonly::XS was
851 installed and because it wasn't a required dependency in most code, it made
852 downstream bugs very hard to track.
853  
854 In the years since Readonly::XS was released, the then private internal
855 methods have been exposed and can be used in pure perl. Similar modules were
856 written to take advantage of this and a patch to Readonly was created. We no
857 longer need to build and install another module to make Readonly useful on
858 modern builds of perl.
859  
860 =over
861  
862 =item * You do not need to install Readonly::XS.
863  
864 =item * You should stop listing Readonly::XS as a dependency or expect it to
865 be installed.
866  
867 =item * Stop testing the C<$Readonly::XSokay> variable!
868  
869 =back
870  
871 =head1 Requirements
872  
873 Please note that most users of Readonly no longer need to install the
874 companion module Readonly::XS which is recommended but not required for perl
875 5.6.x and under. Please do not force it as a requirement in new code and do
876 not use the package variable C<$Readonly::XSokay> in code/tests. For more, see
877 L<the section on Readonly's new internals/Internals>.
878  
879 There are no non-core requirements.
880  
881 =head1 Bug Reports
882  
883 If email is better for you, L<my address is mentioned below|/"Author"> but I
884 would rather have bugs sent through the issue tracker found at
885 http://github.com/sanko/readonly/issues.
886  
887 =head1 Acknowledgements
888  
889 Thanks to Slaven Rezic for the idea of one common function (Readonly) for all
890 three types of variables (13 April 2002).
891  
892 Thanks to Ernest Lergon for the idea (and initial code) for deeply-Readonly
893 data structures (21 May 2002).
894  
895 Thanks to Damian Conway for the idea (and code) for making the Readonly
896 function work a lot smoother under perl 5.8+.
897  
898 =head1 Author
899  
900 Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
901  
902 CPAN ID: SANKO
903  
904 Original author: Eric J. Roode, roode@cpan.org
905  
906 =head1 License and Legal
907  
908 Copyright (C) 2013-2016 by Sanko Robinson <sanko@cpan.org>
909  
910 Copyright (c) 2001-2004 by Eric J. Roode. All Rights Reserved.
911  
912 This module is free software; you can redistribute it and/or modify it under
913 the same terms as Perl itself.
914  
915 =cut
↑ Summary