line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::StringArray; |
2
|
1
|
|
|
1
|
|
596
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
741
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.103'; |
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 module is a demonstration from I
|
25
|
|
|
|
|
|
|
Perl>. It stores integers between 0 and 255 as a single character |
26
|
|
|
|
|
|
|
in a string that acts like an array through C. Behind the C, |
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
|
|
|
0
|
|
|
sub _null { "\x00" } |
39
|
0
|
|
|
0
|
|
|
sub _last () { $_[0]->FETCHSIZE - 1 } |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _normalize_index { |
42
|
0
|
0
|
|
0
|
|
|
$_[1] == abs $_[1] ? $_[1] : $_[0]->_last + 1 - abs $_[1] |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
0
|
|
|
sub _store { chr $_[1] } |
46
|
0
|
|
|
0
|
|
|
sub _show { ord $_[1] } |
47
|
0
|
|
|
0
|
|
|
sub _string { ${ $_[0] } } |
|
0
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub TIEARRAY { |
50
|
0
|
|
|
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
|
|
|
|
|
|
|
sub FETCH { |
63
|
0
|
|
|
0
|
|
|
my $index = $_[0]->_normalize_index( $_[1] ); |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
$index > $_[0]->_last ? () : $_[0]->_show( |
66
|
|
|
|
|
|
|
substr( $_[0]->_string, $index, 1 ) |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
0
|
|
|
sub FETCHSIZE { length $_[0]->_string } |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub STORESIZE { |
73
|
0
|
|
|
0
|
|
|
my $self = shift; |
74
|
0
|
|
|
|
|
|
my $new_size = shift; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my $size = $self->FETCHSIZE; |
77
|
|
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
|
if( $size > $new_size ) { # truncate |
|
|
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
|
|
|
|
|
|
|
sub STORE { |
87
|
0
|
|
|
0
|
|
|
my $self = shift; |
88
|
0
|
|
|
|
|
|
my $index = shift; |
89
|
0
|
|
|
|
|
|
my $value = shift; |
90
|
|
|
|
|
|
|
|
91
|
0
|
0
|
0
|
|
|
|
croak( "The magnitude of [$value] exceeds the allowed limit [255]" ) |
92
|
|
|
|
|
|
|
if( int($value) != $value || $value > 255 ); |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
$self->_extend( $index ) if $index > $self->_last; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
substr( $$self, $index, 1, chr $value ); |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
$value; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub _extend { |
102
|
0
|
|
|
0
|
|
|
my $self = shift; |
103
|
0
|
|
|
|
|
|
my $index = shift; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
$self->STORE( 0, 1 + $self->_last ) |
106
|
|
|
|
|
|
|
while( $self->_last >= $index ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
0
|
|
|
sub EXISTS { $_[0]->_last >= $_[1] ? 1 : 0 } |
110
|
0
|
|
|
0
|
|
|
sub CLEAR { ${ $_[0] } = '' } |
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
0
|
|
|
sub SHIFT { $_[0]->_show( substr ${ $_[0] }, 0, 1, '' ) } |
|
0
|
|
|
|
|
|
|
113
|
0
|
|
|
0
|
|
|
sub POP { $_[0]->_show( chop ${ $_[0] } ) } |
|
0
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub UNSHIFT { |
116
|
0
|
|
|
0
|
|
|
my $self = shift; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
foreach ( reverse @_ ) { |
119
|
0
|
|
|
|
|
|
substr ${ $self }, 0, 0, $self->_store( $_ ) |
|
0
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub PUSH { |
124
|
0
|
|
|
0
|
|
|
my $self = shift; |
125
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
$self->STORE( 1 + $self->_last, $_ ) foreach ( @_ ) |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub SPLICE { |
130
|
0
|
|
|
0
|
|
|
my $self = shift; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my $arg_count = @_; |
133
|
0
|
|
|
|
|
|
my( $offset, $length, @list ) = @_; |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
if( 0 == $arg_count ) { |
|
|
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
|
1
|
|
|
1
|
|
6
|
no warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
180
|
|
143
|
0
|
|
|
|
|
|
( $self->_normalize_index( $offset ), do { |
144
|
0
|
0
|
|
|
|
|
if( $length < 0 ) { $self->_last - $length } |
|
0
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
else { $offset + $length - 1 } |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
) |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
my $replacement = join '', map { chr } @list; |
|
0
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
my @removed = |
153
|
0
|
|
|
|
|
|
map { ord } |
|
0
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
split //, |
155
|
|
|
|
|
|
|
substr $$self, $offset, $length; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
substr $$self, $offset, $length, $replacement; |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
if( wantarray ) { |
160
|
0
|
|
|
|
|
|
@removed; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
else { |
163
|
0
|
0
|
|
|
|
|
defined $removed[-1] ? $removed[-1] : undef; |
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<< >> |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Copyright © 2005-2022, brian d foy . All rights reserved. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
You may redistribute this under the terms of the Artistic License 2.0. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
1; |