File Coverage

blib/lib/Set/String.pm
Criterion Covered Total %
statement 71 200 35.5
branch 13 98 13.2
condition 13 71 18.3
subroutine 16 26 61.5
pod 1 2 50.0
total 114 397 28.7


line stmt bran cond sub pod time code
1             ##################################################################
2             # Set::String
3             #
4             # See POD
5             ##################################################################
6             package Set::String;
7 1     1   22100 use strict;
  1         2  
  1         241  
8 1     1   7 use Carp qw/croak cluck/;
  1         3  
  1         84  
9 1     1   1900 use Want;
  1         3332  
  1         69  
10 1     1   2406 use Set::Array;
  1         31935  
  1         41  
11 1     1   12 use attributes qw(reftype);
  1         2  
  1         9  
12              
13 1     1   80 use subs qw(chop chomp crypt defined eval index lc lcfirst ord);
  1         2  
  1         152  
14 1     1   90 use subs qw(pack pos substr uc ucfirst unpack);
  1         1  
  1         4  
15              
16             # Subclass of Set::Array
17             BEGIN{
18 1     1   61 use vars qw(@ISA $VERSION);
  1         2  
  1         161  
19 1     1   18 @ISA = qw(Set::Array);
20 1         5420 $VERSION = '0.03';
21             }
22              
23             #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24             # Used to decrypt an encrypted string. Given that there's no
25             # way to access this variably directly (that I know of), I think
26             # this is a fairly safe implementation.
27             #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28             my $decrypted;
29              
30             sub new{
31 5     5 1 30 my($class,$string) = @_;
32 5 50 33     14 $string = @$class if !$string && ref($class);
33 5         23 my @array = CORE::split('',$string);
34 5         8 undef $string;
35 5   33     26 return bless \@array, ref($class) || $class;
36             }
37              
38             sub chop{
39 1     1   267 my($self,$num) = @_;
40              
41 1   50     4 $num ||= 1;
42              
43 1 50 33     5 if(want('OBJECT') or !(CORE::defined wantarray)){
44 1         44 for(1..$num){ $self->SUPER::pop }
  3         87  
45 1         40 return $self;
46             }
47              
48 0         0 my $copy = CORE::join('',@$self);
49 0         0 my @chopped;
50 0         0 for(1..$num){
51 0         0 push(@chopped,CORE::chop $copy);
52             }
53              
54 0 0       0 return reverse @chopped if wantarray;
55 0 0       0 return scalar @chopped if defined wantarray;
56             }
57              
58             sub chomp{
59 1     1   65 my($self,$num) = @_;
60              
61 1   50     7 $num ||= 1;
62              
63 1 50 33     19 if(want('OBJECT') or !(CORE::defined wantarray)){
64 1         48 my $string = join '',@$self;
65 1         2 for(1..$num){
66 1         6 CORE::chomp $string;
67             }
68 1         26 @$self = split '',$string;
69 1         4 undef $string;
70 1         5 return $self;
71             }
72              
73 0         0 my $copy = CORE::join('',@$self);
74 0         0 my @chomped;
75 0         0 for(1..$num){
76 0         0 push(@chomped,CORE::chop $copy);
77             }
78              
79 0 0       0 return reverse @chomped if wantarray;
80 0 0       0 return scalar @chomped if defined wantarray;
81             }
82              
83             sub crypt{
84 0     0   0 my($self,$salt) = @_;
85              
86 0 0       0 croak("No salt provided to 'crypt()' method") unless $salt;
87              
88 0         0 $decrypted = CORE::join('',@$self);
89 0         0 my $temp = CORE::crypt($decrypted,$salt);
90              
91 0 0 0     0 if(want('OBJECT') or !(defined wantarray)){
92 0         0 @$self = split('',$temp);
93 0         0 undef $temp;
94 0         0 return $self;
95             }
96              
97 0         0 return $temp;
98             }
99              
100             sub decrypt{
101 0     0 0 0 my($self) = @_;
102              
103 0 0       0 unless(defined $decrypted){
104 0         0 cluck("Pointless to decrypt an unencrypted string. Ignoring");
105 0 0 0     0 if(want('OBJECT') or !(defined wantarray)){ return $self }
  0         0  
106 0 0       0 return @$self if wantarray;
107 0 0       0 return join('',@$self) if defined wantarray;
108             }
109              
110 0 0 0     0 if(want('OBJECT') or !(defined wantarray)){
111 0         0 @$self = split('',$decrypted);
112 0         0 return $self;
113             }
114              
115 0         0 return $decrypted;
116             }
117              
118             # Returns 1 or 0
119             sub defined{
120 1     1   75 my($self) = @_;
121 1 50       5 return 0 unless CORE::defined($self->[0]);
122 1         5 return 1;
123             }
124              
125             ############################################################################
126             # Eval the string as is. The object becomes the eval'd string, replacing
127             # the original content (or assigning them to an lvalue).
128             #
129             # I'm considering allowing an alternate string to be eval'd, but how do I
130             # handle it exactly?
131             ############################################################################
132             sub eval{
133 1     1   2 my($self) = @_;
134 1         91 my $result = CORE::eval CORE::join('',@$self);
135 1 50 33     6 if(want('OBJECT') or !(CORE::defined wantarray)){
136 0         0 @$self = CORE::split('',$result);
137 0         0 undef $result;
138 0         0 return $self;
139             }
140 1         49 return $result;
141             }
142              
143             sub index{
144 0     0   0 my($self,$substr,$start) = @_;
145              
146 0 0       0 croak("No substring provided to 'index()' method") unless $substr;
147              
148 0         0 my $pos;
149 0 0       0 if(defined $start){
150 0         0 $pos = CORE::index(CORE::join('',@$self),$substr,$start);
151             }
152             else{
153 0         0 $pos = CORE::index(CORE::join('',@$self),$substr);
154             }
155              
156 0 0 0     0 if(want('OBJECT') or !(defined wantarray)){
157 0         0 return bless \$pos;
158             }
159              
160 0 0       0 return $pos if $pos;
161 0         0 return -1;
162             }
163              
164             ###############################################################
165             # Slightly different from standard lc in that you can specify
166             # the number of characters you want to lc (left to right).
167             ###############################################################
168             sub lc{
169 1     1   3 my($self,$n) = @_;
170              
171 1 50 33     5 if( (CORE::defined $n) && ($n > scalar(@$self)) ){
172 0         0 croak("Argument to method 'lc()' exceeds length of string");
173             }
174              
175 1   50     7 $n ||= scalar(@$self);
176 1         2 $n -= 1;
177 1 50       3 if($n < 0){ $n = 0 }
  0         0  
178              
179 1 50       7 if($n !~ /\d+/){
180 0         0 croak("Invalid argument to 'lc()' method");
181             }
182              
183 1 50 33     4 if(want('OBJECT') || !(CORE::defined wantarray)){
184 0         0 for(my $m = 0; $m <= $n; $m++){
185 0         0 $self->[$m] = CORE::lc($self->[$m]);
186             }
187 0         0 undef $n;
188 0         0 return $self;
189             }
190              
191 1         47 my $copy = CORE::join('',@$self);
192 1         5 return CORE::lc($copy);
193             }
194              
195             sub lcfirst{
196 0     0   0 my($self) = @_;
197              
198 0 0 0     0 if(want('OBJECT') || !(CORE::defined wantarray)){
199 0         0 $self->[0] = CORE::lc($self->[0]);
200 0         0 return $self;
201             }
202              
203 0         0 my $copy = CORE::join('',@$self);
204 0         0 return CORE::lcfirst($copy);
205             }
206              
207             ###############################################################################
208             # Slightly different than standard 'ord' in that the programmer may
209             # specify a range of characters to get ord vals on. Alternatively, a single
210             # index may be specified.
211             #
212             # By default, this method will return all ord values unless an
213             # index is specified. Returns a list or list ref.
214             ###############################################################################
215             sub ord{
216 0     0   0 my($self,$num) = @_;
217              
218 0 0       0 cluck("Calling 'ord()' on empty array") if scalar(@$self) == 0;
219              
220 0 0       0 $num = scalar(@$self) unless CORE::defined $num;
221              
222 0         0 $num--;
223              
224 0 0 0     0 if(want('OBJECT') or !(defined wantarray)){
225 0         0 for(0..$num){ $self->[$_] = CORE::ord($self->[$_]) }
  0         0  
226 0         0 return $self;
227             }
228              
229 0         0 my @copy = @$self;
230 0         0 for(0..$num){ $copy[$_] = CORE::ord($copy[$_]) }
  0         0  
231              
232 0 0       0 return @copy if wantarray;
233 0 0       0 return \@copy if defined wantarray;
234             }
235              
236             # Overload the Set::Array version of pack (and unpack)
237             sub pack{
238 0     0   0 my($self,$template) = @_;
239              
240 0 0       0 croak("No template provided to 'pack()' method") unless $template;
241              
242 0 0 0     0 if(want('OBJECT') || !(defined wantarray)){
243 0         0 @$self = join('',@$self);
244 0         0 @$self = CORE::pack($template,@$self);
245 0         0 return $self;
246             }
247              
248 0         0 my @temp = join('',@$self);
249 0         0 return CORE::pack($template,@temp);
250             }
251              
252             ###########################################################################
253             # Returns an index or array of indices
254             #
255             # e.g. if string is "fee fie foe foo" and 'e' is the pattern, 2,3,7 and 11
256             # would be returned.
257             ###########################################################################
258             sub pos{
259 0     0   0 my($self,$pattern) = @_;
260              
261 0 0       0 croak("No pattern supplied to 'pos()' method") unless $pattern;
262              
263 0         0 my @indices;
264 0         0 my $string = CORE::join('',@$self);
265              
266 0         0 while($string =~ /$pattern/g){
267 0         0 my $pos = CORE::pos $string;
268 0         0 push @indices, $pos;
269             }
270            
271 0 0 0     0 if(want('OBJECT') or !(defined wantarray)){
272 0         0 @$self = @indices;
273 0         0 undef @indices;
274 0         0 return $self;
275             }
276              
277 0 0       0 return @indices if wantarray;
278 0 0       0 return \@indices if defined wantarray;
279             }
280              
281             sub substr{
282 0     0   0 my($self,$offset,$length) = @_;
283            
284 0 0       0 croak("No offset specified for 'substr()' method") unless defined $offset;
285              
286 0 0 0     0 if( (defined $length) && ($length <= 0) ){
287 0         0 croak("Nonsensical value used as length for 'substr()' method");
288             }
289              
290 0         0 my $string = CORE::join('',@$self);
291              
292 0         0 my $substr;
293              
294 0 0       0 if($length){
295 0         0 $substr = CORE::substr($string,$offset,$length);
296             }
297             else{
298 0         0 $substr = CORE::substr($string,$offset);
299             }
300              
301 0 0 0     0 if( want('OBJECT') or !(defined wantarray) ){
302 0         0 @$self = CORE::split('',$substr);
303 0         0 undef $string;
304 0         0 return $self;
305             }
306              
307 0         0 return $substr;
308             }
309              
310             # Not yet implemented
311             sub unpack{
312 0     0   0 my($self,$template) = @_;
313            
314 0 0       0 croak("No template provided to 'unpack()' method") unless $template;
315              
316 0 0 0     0 if( want('OBJECT') or !(defined wantarray) ){
317 0         0 @$self = CORE::unpack($template,join('',@$self));
318 0         0 return $self;
319             }
320              
321 0         0 my $temp = join('',@$self);
322 0 0       0 if(wantarray){ return CORE::unpack($template,$temp) }
  0         0  
323 0 0       0 if(defined wantarray){ return CORE::unpack($template,$temp) }
  0         0  
324             }
325              
326             # Not yet implemented
327             #sub quotemeta{}
328              
329             # Not yet implemented
330             #sub split{}
331              
332             # May not be implemented
333             #sub study{}
334              
335             ###############################################################
336             # Slightly different from standard uc in that you can specify
337             # the number of characters you want to uc (left to right).
338             ###############################################################
339             sub uc{
340 1     1   2 my($self,$n) = @_;
341              
342 1 50 33     6 if( (CORE::defined $n) && ($n > scalar(@$self)) ){
343 0         0 croak("Argument to method 'uc()' exceeds length of string");
344             }
345              
346 1   50     31 $n ||= scalar(@$self);
347 1         2 $n -= 1;
348 1 50       4 if($n < 0){ $n = 0 }
  0         0  
349              
350 1 50       8 if($n !~ /\d+/){
351 0         0 croak("Invalid argument to 'uc()' method");
352             }
353              
354 1 50 33     4 if(want('OBJECT') || !(CORE::defined wantarray)){
355 0         0 for(my $m = 0; $m <= $n; $m++){
356 0         0 $self->[$m] = CORE::uc($self->[$m]);
357             }
358 0         0 undef $n;
359 0         0 return $self;
360             }
361              
362 1         52 my $copy = CORE::join('',@$self);
363 1         6 return CORE::uc($copy);
364             }
365              
366             sub ucfirst{
367 0     0     my($self) = @_;
368              
369 0 0 0       if(want('OBJECT') || !(CORE::defined wantarray)){
370 0           $self->[0] = CORE::uc($self->[0]);
371 0           return $self;
372             }
373              
374 0           my $copy = CORE::join('',@$self);
375 0           return CORE::ucfirst($copy);
376             }
377              
378             # Not yet implemented
379             #sub vec {}
380             1;
381             __END__