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