File Coverage

blib/lib/String/Substrings.pm
Criterion Covered Total %
statement 28 28 100.0
branch 10 10 100.0
condition 5 6 83.3
subroutine 5 5 100.0
pod 0 1 0.0
total 48 50 96.0


line stmt bran cond sub pod time code
1             package String::Substrings;
2              
3 3     3   8655 use 5.006;
  3         11  
  3         120  
4 3     3   301 use strict;
  3         7  
  3         242  
5 3     3   17 use warnings;
  3         17  
  3         121  
6              
7 3     3   204 use Carp;
  3         4  
  3         4256  
8              
9             require Exporter;
10              
11             our @ISA = qw(Exporter);
12              
13             our %EXPORT_TAGS = ( 'all' => [ qw(
14             substrings
15             ) ] );
16              
17             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18              
19             our @EXPORT = qw(
20             substrings
21             );
22              
23             our $VERSION = '0.02';
24              
25             sub substrings($;$) {
26 255     255 0 551338 my ($string, $length) = @_;
27 255 100       793 return undef unless defined $string;
28            
29             # paramter validation
30 254 100       772 if (my $r = ref($string)) {
31 2         45 croak "Please call me `substrings STRING [,LENGTH]`
32             but not with substrings $r, LENGTH";
33             }
34 252 100 100     2092 if (defined($length) and (my $r = ref($length) or $length !~ /^\d+/)) {
      66        
35 4         36 croak "Please call me `substrings STRING [,LENGTH]`
36             but not with substrings '$string', $r";
37             }
38            
39            
40 248         394 my $strlength = length($string);
41 248         403 my @s = ();
42 248 100       549 if (defined $length) {
43 121 100       414 return @s if $length == 0;
44 115         594 push @s, map {substr $string, $_, $length} (0 .. $strlength-$length);
  5063         8488  
45             } else {
46 127         268 foreach my $length (1 .. $strlength) {
47 10240         35251 push @s, map {substr $string, $_, $length}
  515165         894522  
48             (0 .. $strlength - $length);
49             }
50             }
51 242         90084 return @s;
52             }
53              
54             1;
55             __END__