| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | blessed($o) eq "B::PVMG" or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $m = $o->MAGIC; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | while ($m) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $m->TYPE eq "r" and return 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | or croak "readonly needs a reference"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 64 | ▶ | exists $o->{skip}{$x} and return $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 64 | $o->{skip}{$x} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 64 | ▶ | !$ro && $immortal{$x} and return $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $ro and $immortal{$x}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 61 | ▶ | blessed $r && !$o->{peek} and return $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($r) and not $o->{"peek"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition77%7/9
$t eq "CODE" or $t eq "IO" or $t eq "FORMAT" or $t eq "REGEXP"
$t eq "CODE" or $t eq "IO" or $t eq "FORMAT"
$t eq "CODE" or $t eq "IO"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 56 | ▶ | if ($t eq "REF") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | _recurse $ro, $$r, $o; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 56 | ▶ | if ($t eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 11 | _recurse $ro, \$_, $o for @$r; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 56 | ▶ | if ($t eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | *$r{$_} and _recurse $ro, *$r{$_}, $o | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 3 | ▶ | for qw/SCALAR ARRAY HASH/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | or croak "Readonly needs a reference"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 3 | ▶ | if ($t eq "SCALAR" or $t eq "REF") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$t eq "SCALAR" or $t eq "REF"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | $$r = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 3 | ▶ | if ($t eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | @$r = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 3 | ▶ | if ($t eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | %$r = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | ▶ | if ($t eq "GLOB") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||