blib/lib/Readonly/Tiny.pm

filter: Statementstmt 88.8% 64 / 72 Branchbran 65.0% 26 / 40 Conditioncond 83.3% 15 / 18 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 85.7% 12 / 14 Podpod 75.0% 3 / 4 totaltotal 80.3% 127 / 158 CC 30 SCAR 26.7
CC
30
Cov
81%
__ANON__
30.0
_recurse
20.4
Readonly
7.2
CRAP 14.4
↑ Summary
1 package Readonly::Tiny;
2  
3 =head1 NAME
4  
5 Readonly::Tiny - Simple, correct readonly values
6  
7 =head1 SYNOPSIS
8  
9 use Readonly::Tiny;
10  
11 my $x = readonly [1, 2, 3];
12 # $x is not readonly, but the array it points to is.
13
14 my @y = (4, 5, 6);
15 readonly \@y;
16 # @y is readonly, as well as its contents.
17  
18 =head1 DESCRIPTION
19  
20 Readonly::Tiny provides a simple and correct way of making values
21 readonly. Unlike L<Readonly> it does not cause arrays and hashes to be
22 tied, it just uses the core C<SvREADONLY> flag.
23  
24 =head1 FUNCTIONS
25  
26 =cut
27  
28 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 our $VERSION = "4";
33  
34 4 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 our @EXPORT = qw/readonly/;
36 4 our @EXPORT_OK = qw/readonly readwrite Readonly/;
37  
38 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Scalar::Util qw/reftype refaddr blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Hash::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 #use Data::Dump qw/pp/;
42  
43 4 ▶use constant RX_MAGIC => (reftype(qr/x/) ne "REGEXP");
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 4 if (RX_MAGIC) {
46 require B;
47  
48 0 ▶ *is_regexp = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
49 0 ▶ my $o = B::svref_2object($_[0]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $o = B::svref_2object($_[0]) or return 0 0 0
50 0 ▶ blessed($o) eq "B::PVMG" or return;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($o) eq "B::PVMG" or return 0 0 0
51  
52 0 my $m = $o->MAGIC;
53 0 ▶ while ($m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m) 0 0 0
54 0 ▶ $m->TYPE eq "r" and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$m->TYPE eq "r" and return 1 0 0 0
55 0 $m = $m->MOREMAGIC;
56 }
57  
58 0 return;
59 };
60 }
61  
62 0 ▶sub debug {
Subroutine debug: not called CC=1 SCAR=0.0
Pod undocumented
63 #warn sprintf "%s [%x] %s\n", @_;
64 }
65  
66 =head2 readonly
67  
68 my $ro = readonly $ref, \%opts;
69  
70 Make a data structure readonly. C<$ref> must be a reference; the
71 referenced value, and any values referenced recursively, will be made
72 readonly. C<$ref> is returned, but it will not itself be readonly; it is
73 possible to make a variable readonly by passing a reference to it, as in
74 the L</SYNOPSIS>.
75  
76 C<%opts> is a hashref of options:
77  
78 =over 4
79  
80 =item peek
81  
82 Normally blessed references will not be looked through. The scalar
83 holding the reference will be made readonly (so a different object
84 cannot be assigned) but the contents of the object itself will be left
85 alone. Supplying C<< peek => 1 >> allows blessed refs to be looked
86 through.
87  
88 =item skip
89  
90 This should be a hashref keyed by refaddr. Any object whose refaddr is
91 in the hash will be skipped.
92  
93 =back
94  
95 Note that making a hash readonly has the same effect as calling
96 L<C<Hash::Util::lock_hash>|Hash::Util/lock_hash>; in particular, it
97 causes restricted hashes to be re-restricted to their current set of
98 keys.
99  
100 =head2 readwrite
101  
102 my $rw = readwrite $ref, \%opts;
103  
104 Undo the effects of C<readonly>. C<%opts> is the same. Note that making
105 a hash readwrite will undo any restrictions put in place using
106 L<Hash::Util>.
107  
108 B<BE VERY CAREFUL> calling this on values you have not made readonly
109 yourself. It will silently ignore attempts to make the core values
110 C<PL_sv_undef>, C<PL_sv_yes> and C<PL_sv_no> readwrite, but there are
111 many other values the core makes readonly, usually with good reason.
112 Recent versions of perl will not allow you to make readwrite a value the
113 core has set readonly, but you should probably not rely on this.
114  
115 =cut
116  
117 sub _recurse;
118  
119 20 ▶sub readonly { _recurse 1, @_; $_[0] }
Subroutine readonly: called CC=1 SCAR=0.0
Pod documented
120 10 ▶sub readwrite { _recurse 0, @_; $_[0] }
Subroutine readwrite: called CC=1 SCAR=0.0
Pod documented
121  
122 4 my %immortal =
123 map +(refaddr $_, 1),
124 \undef, \!1, \!0;
125  
126 64 ▶sub _recurse {
Subroutine _recurse: called CC=20 SCAR=30.2
127 64 my ($ro, $r, $o) = @_;
128  
129 64 ▶ my $x = refaddr $r
Branch: 1 branch
BranchTrueFalseTotal
my $x = builtin::refaddr($r) or croak("readonly needs a reference") 64 0 64
130 or croak "readonly needs a reference";
131  
132 64 ▶ exists $o->{skip}{$x} and return $r;
Branch: 1 branch
BranchTrueFalseTotal
exists $o->{"skip"}{$x} and return $r 0 64 64
133 64 $o->{skip}{$x} = 1;
134  
135 64 ▶ !$ro && $immortal{$x} and return $r;
Branch: 1 branch
BranchTrueFalseTotal
not $ro and $immortal{$x} and return $r 3 61 64
Condition100%3/3
not $ro and $immortal{$x}
!ll&&!rl&&r
44173
MC/DC100%2/2
not $ro and $immortal{$x}
not $ro $immortal{$x}
Truth table100%3/3
not(A) and B
A= not $roB= $immortal{$x}
ABresult
0X0
100
111
136 61 ▶ blessed $r && !$o->{peek} and return $r;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($r) and not $o->{"peek"} and return $r 3 58 61
Condition100%3/3
builtin::blessed($r) and not $o->{"peek"}
!ll&&!rl&&r
5353
MC/DC100%2/2
builtin::blessed($r) and not $o->{"peek"}
builtin::blessed($r) not $o->{"peek"}
Truth table100%3/3
A and not(B)
A= builtin::blessed($r)B= not $o->{"peek"}
ABresult
0X0
100
111
137  
138 58 my $t = reftype $r;
139 #debug $t, $x, pp $r;
140  
141 # It's not clear it's meaningful to SvREADONLY these types. A qr//
142 # is a ref to a REGEXP, so a scalar holding one can be made
143 # readonly; the REGEXP itself would normally be skipped anyway
144 # because it's blessed.
145 58 ▶ $t eq "CODE" || $t eq "IO" || $t eq "FORMAT" || $t eq "REGEXP"
Branch: 1 branch
BranchTrueFalseTotal
$t eq "CODE" || $t eq "IO" || $t eq "FORMAT" || $t eq "REGEXP" and return $r 2 56 58
Condition77%7/9
$t eq "CODE" or $t eq "IO" or $t eq "FORMAT" or $t eq "REGEXP"
l!l&&r!l&&!r
1156
$t eq "CODE" or $t eq "IO" or $t eq "FORMAT"
l!l&&r!l&&!r
1057
$t eq "CODE" or $t eq "IO"
l!l&&r!l&&!r
1057
MC/DC50%2/4
$t eq "CODE" or $t eq "IO" or $t eq "FORMAT" or $t eq "REGEXP"
$t eq "CODE" $t eq "IO" $t eq "FORMAT" $t eq "REGEXP"
Truth table60%3/5
A or B or C or D
A= $t eq "CODE"B= $t eq "IO"C= $t eq "FORMAT"D= $t eq "REGEXP"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
146 and return $r;
147  
148 # Look for r magic pre-5.12
149 56 RX_MAGIC and is_regexp($r) and return $r;
150  
151 56 ▶ unless ($o->{shallow}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o->{"shallow"}) 56 0 56
152 56 ▶ if ($t eq "REF") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "REF") 3 53 56
153 3 _recurse $ro, $$r, $o;
154 }
155 56 ▶ if ($t eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "ARRAY") 11 45 56
156 11 _recurse $ro, \$_, $o for @$r;
157 }
158 56 ▶ if ($t eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "HASH") 7 49 56
159 7 &Internals::SvREADONLY($r, 0);
160 7 _recurse $ro, \$_, $o for values %$r;
161 7 Hash::Util::lock_keys(%$r);
162 }
163 56 ▶ if ($t eq "GLOB") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "GLOB") 3 53 56
164 *$r{$_} and _recurse $ro, *$r{$_}, $o
165 3 ▶ for qw/SCALAR ARRAY HASH/;
Branch: 1 branch
BranchTrueFalseTotal
*$r{$_} and _recurse($ro, scalar *$r{$_}, $o) 9 0 9
166 }
167 }
168  
169 # bleeding prototypes...
170 56 &Internals::SvREADONLY($r, $ro);
171 #debug "READONLY", $r, &Internals::SvREADONLY($r);
172 }
173  
174 =head2 Readonly
175  
176 Readonly my $x, 1;
177 Readonly my @y, 2, 3, 4;
178 Readonly my %z, foo => 5;
179  
180 This is a compatibility shim for L<Readonly>. It is prototyped to take a
181 reference to its first argument, and assigns the rest of the argument
182 list to that argument before making the whole thing readonly.
183  
184 =cut
185  
186 3 ▶sub Readonly (\[$@%]@) {
Subroutine Readonly: called CC=7 SCAR=19.8
Pod documented
187 3 my $r = shift;
188 3 ▶ my $t = reftype $r
Branch: 1 branch
BranchTrueFalseTotal
my $t = builtin::reftype($r) or croak("Readonly needs a reference") 3 0 3
189 or croak "Readonly needs a reference";
190  
191 3 ▶ if ($t eq "SCALAR" or $t eq "REF") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "SCALAR" or $t eq "REF") 1 2 3
Condition66%2/3
$t eq "SCALAR" or $t eq "REF"
l!l&&r!l&&!r
102
MC/DC50%1/2
$t eq "SCALAR" or $t eq "REF"
$t eq "SCALAR" $t eq "REF"
Truth table66%2/3
A or B
A= $t eq "SCALAR"B= $t eq "REF"
ABresult
1X1
011
000
192 1 $$r = $_[0];
193 }
194 3 ▶ if ($t eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "ARRAY") 1 2 3
195 1 @$r = @_;
196 }
197 3 ▶ if ($t eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "HASH") 1 2 3
198 1 %$r = @_;
199 }
200 3 ▶ if ($t eq "GLOB") {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "GLOB") 0 3 3
201 0 *$r = $_[0];
202 }
203  
204 3 readonly $r;
205 }
206  
207 4 1;
208  
209 =head1 EXPORTS
210  
211 C<readonly> is exported by default. C<readwrite> and C<Readonly> are
212 exported on request.
213  
214 =head1 SEE ALSO
215  
216 L<Readonly> was the first module to supply readonly values. It was
217 written for Perl 5.6, and as a result the interface and implementation
218 are both rather clunky. With L<Readonly::XS> the performance is improved
219 for scalar varuables, but arrays and hashes still use a tied
220 implementation which is very slow.
221  
222 L<Const::Fast> is a greatly improved reaoonly module which uses perl's
223 internal C<SvREADONLY> flag instead of ties. The differences between
224 this module and L<Const::Fast> are:
225  
226 =over 4
227  
228 =item *
229  
230 The C<readonly> function does not insist on performing an assignment, it
231 just returns a readonly value. This is, IMHO, more useful, since it
232 means a readonly value can be returned from a function. In particular,
233 it is often useful to return a readonly value from a builder method.
234  
235 =item *
236  
237 It does not attempt to clone deep structures. If C<Clone::Fast> is
238 applied to a structure with cross-links it will clone the whole thing,
239 on the principle that parts of the graph may be shared with something
240 else which should not be readonly. This module takes the approach that
241 if you asked for something to be made readonly you meant it, and if it
242 points to something it shouldn't that's your mistake.
243  
244 =back
245  
246 =head1 BUGS
247  
248 Please report bugs to <L<bug-Readonly-Tiny@rt.cpan.org>>.
249  
250 =head1 AUTHOR
251  
252 Ben Morrow <ben@morrow.me.uk>
253  
254 =head1 COPYRIGHT
255  
256 Copyright 2015 Ben Morrow.
257  
258 Released under the 2-clause BSD licence.
259  
↑ Summary