blib/lib/Tie/StringArray.pm

filter: Statementstmt 18.1% 14 / 77 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 17.3% 4 / 23 Podpod n/a totaltotal 13.5% 18 / 133 CC 19 SCAR 25.6
CC
19
Cov
13%
SPLICE
43.8
STORE
20.0
STORESIZE
12.0
CRAP 12.9
↑ Summary
1 package Tie::StringArray;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '1.105';
8  
9  
10 =encoding utf8
11  
12 =head1 NAME
13  
14 Tie::StringArray - use a tied string as an array of chars
15  
16 =head1 SYNOPSIS
17  
18 use Tie::StringArray;
19  
20 tie my @array, 'Tie::StringArray', qw(137 88 54);
21  
22 =head1 DESCRIPTION
23  
24 The C<Tie::StringArray> module is a demonstration from I<Mastering
25 Perl>. It stores integers between 0 and 255 as a single character
26 in a string that acts like an array through C<tie>. Behind the C<tie>,
27 the array is a single string, so there's only one scalar to store.
28  
29 I don't think this is particularly useful for anything real.
30  
31 =over 4
32  
33 =item new
34  
35 =cut
36  
37  
38 0 ▶sub _null { "\x00" }
Subroutine _null: not called CC=1 SCAR=6.9
39 0 ▶sub _last () { $_[0]->FETCHSIZE - 1 }
Subroutine _last: not called CC=1 SCAR=6.9
40  
41 0 ▶sub _normalize_index {
Subroutine _normalize_index: not called CC=2 SCAR=17.9
42 0 ▶ $_[1] == abs $_[1] ? $_[1] : $_[0]->_last + 1 - abs $_[1]
Branch: 1 branch
BranchTrueFalseTotal
$_[1] == abs $_[1] ? : 0 0 0
43 }
44  
45 0 ▶sub _store { chr $_[1] }
Subroutine _store: not called CC=1 SCAR=6.9
46 0 ▶sub _show { ord $_[1] }
Subroutine _show: not called CC=1 SCAR=6.9
47 0 ▶sub _string { ${ $_[0] } }
Subroutine _string: not called CC=1 SCAR=6.9
48  
49 0 ▶sub TIEARRAY {
Subroutine TIEARRAY: not called CC=2 SCAR=17.9
50 0 my( $class, @values ) = @_;
51  
52 0 my $string = '';
53 0 my $self = bless \$string, $class;
54  
55 0 my $index = 0;
56  
57 0 $self->STORE( $index++, $_ ) foreach ( @values );
58  
59 0 $self;
60 }
61  
62 0 ▶sub FETCH {
Subroutine FETCH: not called CC=2 SCAR=17.9
63 0 my $index = $_[0]->_normalize_index( $_[1] );
64  
65 0 ▶ $index > $_[0]->_last ? () : $_[0]->_show(
Branch: 1 branch
BranchTrueFalseTotal
$index > $_[0]->_last ? : 0 0 0
66 substr( $_[0]->_string, $index, 1 )
67 );
68 }
69  
70 0 ▶sub FETCHSIZE { length $_[0]->_string }
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
71  
72 0 ▶sub STORESIZE {
Subroutine STORESIZE: not called CC=3 SCAR=24.8
73 0 my $self = shift;
74 0 my $new_size = shift;
75  
76 0 my $size = $self->FETCHSIZE;
77  
78 0 ▶ if( $size > $new_size ) { # truncate
Branch: 2 branches
BranchTrueFalseTotal
if ($size > $new_size) { } 0 0 0
elsif ($size < $new_size) { } 0 0 0
79 0 $$self = substr( $$self, 0, $size );
80 }
81 elsif( $size < $new_size ) { # extend
82 0 $$self .= join '', ($self->_null) x ( $new_size - $size );
83 }
84 }
85  
86 0 ▶sub STORE {
Subroutine STORE: not called CC=4 SCAR=30.0
87 0 my $self = shift;
88 0 my $index = shift;
89 0 my $value = shift;
90  
91 0 ▶ croak( "The magnitude of [$value] exceeds the allowed limit [255]" )
Branch: 1 branch
BranchTrueFalseTotal
if int $value != $value or $value > 255 0 0 0
Condition0%0/3
int $value != $value or $value > 255
l!l&&r!l&&!r
000
MC/DC0%0/2
int $value != $value or $value > 255
int $value != $value $value > 255
Truth table0%0/3
A or B
A= int $value != $valueB= $value > 255
ABresult
1X1
011
000
92 if( int($value) != $value || $value > 255 );
93  
94 0 ▶ $self->_extend( $index ) if $index > $self->_last;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $self->_last 0 0 0
95  
96 0 substr( $$self, $index, 1, chr $value );
97  
98 0 $value;
99 }
100  
101 0 ▶sub _extend {
Subroutine _extend: not called CC=2 SCAR=17.9
102 0 my $self = shift;
103 0 my $index = shift;
104  
105 0 ▶ $self->STORE( 0, 1 + $self->_last )
Branch: 1 branch
BranchTrueFalseTotal
$self->_last >= $index and do { $self->STORE(0, 1 + $self->_last) } 0 0 0
106 while( $self->_last >= $index );
107 }
108  
109 0 ▶sub EXISTS { $_[0]->_last >= $_[1] ? 1 : 0 }
Subroutine EXISTS: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_last >= $_[1] ? : 0 0 0
110 0 ▶sub CLEAR { ${ $_[0] } = '' }
Subroutine CLEAR: not called CC=1 SCAR=6.9
111  
112 0 ▶sub SHIFT { $_[0]->_show( substr ${ $_[0] }, 0, 1, '' ) }
Subroutine SHIFT: not called CC=1 SCAR=6.9
113 0 ▶sub POP { $_[0]->_show( chop ${ $_[0] } ) }
Subroutine POP: not called CC=1 SCAR=6.9
114  
115 0 ▶sub UNSHIFT {
Subroutine UNSHIFT: not called CC=2 SCAR=17.9
116 0 my $self = shift;
117  
118 0 foreach ( reverse @_ ) {
119 0 substr ${ $self }, 0, 0, $self->_store( $_ )
120 }
121 }
122  
123 0 ▶sub PUSH {
Subroutine PUSH: not called CC=2 SCAR=17.9
124 0 my $self = shift;
125  
126 0 $self->STORE( 1 + $self->_last, $_ ) foreach ( @_ )
127 }
128  
129 0 ▶sub SPLICE {
Subroutine SPLICE: not called CC=7 SCAR=37.8
130 0 my $self = shift;
131  
132 0 my $arg_count = @_;
133 0 my( $offset, $length, @list ) = @_;
134  
135 0 ▶ if( 0 == $arg_count ) {
Branch: 3 branches
BranchTrueFalseTotal
if (0 == $arg_count) { } 0 0 0
elsif (1 == $arg_count) { } 0 0 0
elsif (2 <= $arg_count) { } 0 0 0
136 0 ( 0, $self->_last )
137 }
138 elsif( 1 == $arg_count ) {
139 0 ( $self->_normalize_index( $offset ), $self->_last )
140 }
141 elsif( 2 <= $arg_count ) { # offset and length only
142 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 0 ( $self->_normalize_index( $offset ), do {
144 0 ▶ if( $length < 0 ) { $self->_last - $length }
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 0) { } 0 0 0
145 0 else { $offset + $length - 1 }
146 }
147 )
148 }
149  
150 0 my $replacement = join '', map { chr } @list;
151  
152 my @removed =
153 0 map { ord }
154 split //,
155 substr $$self, $offset, $length;
156  
157 0 substr $$self, $offset, $length, $replacement;
158  
159 0 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
160 0 @removed;
161 }
162 else {
163 0 ▶ defined $removed[-1] ? $removed[-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $removed[-1] ? : 0 0 0
164 }
165  
166 }
167  
168 =back
169  
170 =head1 TO DO
171  
172  
173 =head1 SEE ALSO
174  
175  
176 =head1 SOURCE AVAILABILITY
177  
178 This source is in Github:
179  
180 http://github.com/briandfoy/tie-stringarray/
181  
182 =head1 AUTHOR
183  
184 brian d foy, C<< <briandfoy@pobox.com> >>
185  
186 =head1 COPYRIGHT AND LICENSE
187  
188 Copyright © 2005-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
189  
190 You may redistribute this under the terms of the Artistic License 2.0.
191  
192 =cut
193  
194 2 1;
↑ Summary