List/Objects/WithUtils/Role/Array/Immutable.pm

filter: Statementstmt 100% 30 / 30 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 82.3% 42 / 51 SCAR 11.1
CC
3
Cov
85%
__ANON__
3.5
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
« List/Objects/WithUtils/Role/Hash/Immutable.pm ↑ Summary List/Objects/WithUtils/Autobox.pm »
1 package List::Objects::WithUtils::Role::Array::Immutable;
2 93 $List::Objects::WithUtils::Role::Array::Immutable::VERSION = '2.028002';
3 93 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 93 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 93 ▶use Tie::Array ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub _make_unimp {
8 1023 ▶ my ($method) = @_;
Subroutine _make_unimp: called CC=1 SCAR=0.0
9 sub {
10 11 ▶ local $Carp::CarpLevel = 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 11 Carp::croak "Method '$method' not implemented on immutable arrays"
12 }
13 1023 }
14  
15 93 our @ImmutableMethods = qw/
16 clear
17 delete delete_when
18 insert
19 pop push
20 rotate_in_place
21 set
22 shift unshift
23 splice
24 /;
25  
26 93 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 93 requires 'new', @ImmutableMethods;
28  
29 2 ▶around is_mutable => sub { () };
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 around new => sub {
32 7 ▶ my ($orig, $class) = splice @_, 0, 2;
Subroutine __ANON__: called CC=3 SCAR=12.6
33 7 my $self = $class->$orig(@_);
34  
35 # SvREADONLY behavior is not very reliable.
36 # Remove mutable behavior from our backing tied array instead:
37  
38 # If we're already tied, something else is going on,
39 # like we're a typed array.
40 # Otherwise, tie a StdArray & push items.
41 7 ▶ tie @$self, 'Tie::StdArray' and push @$self, @_
Branch: 1 branch
BranchTrueFalseTotal
unless tied @$self 7 0 7
Condition33%2/6
tied @$self || tie(@$self, 'Tie::StdArray') && push(@$self, @_)
l!l&&r!l&&!r
070
tie @$self, "Tie::StdArray" and push @$self, @_
!ll&&!rl&&r
070
MC/DC0%0/3
tied @$self || tie(@$self, 'Tie::StdArray') && push(@$self, @_)
tied @$self tie @$self, "Tie::StdArray" push @$self, @_
Truth table0%0/4
A || B and C
A= tied @$selfB= tie @$self, "Tie::StdArray"C= push @$self, @_
ABCresult
1XX1
0111
00X0
0100
42 unless tied @$self;
43  
44 7 Role::Tiny->apply_roles_to_object( tied(@$self),
45 'List::Objects::WithUtils::Role::Array::TiedRO'
46 );
47  
48 7 $self
49 93 };
50  
51 93 around $_ => _make_unimp($_) for @ImmutableMethods;
52  
53 93 ▶print
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 93 93
54 qq[<LeoNerd> Coroutines are not magic pixiedust\n],
55 qq[<DrForr> LeoNerd: Any sufficiently advanced technology.\n],
56 qq[<LeoNerd> DrForr: ... probably corrupts the C stack during XS calls? ;)\n],
57 unless caller;
58 93 1;
59  
60 =pod
61  
62 =head1 NAME
63  
64 List::Objects::WithUtils::Role::Array::Immutable - Immutable array behavior
65  
66 =head1 SYNOPSIS
67  
68 # Via List::Objects::WithUtils::Array::Immutable ->
69 use List::Objects::WithUtils 'immarray';
70 my $array = immarray(qw/ a b c /);
71 $array->push('d'); # dies
72  
73 =head1 DESCRIPTION
74  
75 This role adds immutable behavior to L<List::Objects::WithUtils::Role::Array>
76 consumers.
77  
78 The following methods are not available and will throw an exception:
79  
80 clear
81 set
82 pop push
83 shift unshift
84 delete delete_when
85 insert
86 rotate_in_place
87 splice
88  
89 (The backing array is also marked read-only.)
90  
91 See L<List::Objects::WithUtils::Array::Immutable> for a consumer
92 implementation that also pulls in L<List::Objects::WithUtils::Role::Array> &
93 L<List::Objects::WithUtils::Role::Array::WithJunctions>.
94  
95 =head1 AUTHOR
96  
97 Jon Portnoy <avenj@cobaltirc.org>
98  
99 Licensed under the same terms as Perl.
100  
101 =cut
« List/Objects/WithUtils/Role/Hash/Immutable.pm ↑ Summary List/Objects/WithUtils/Autobox.pm »