| 1 | package String::Substrings; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||||||||||||||||
| 14 | substrings | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ) ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||||||||||||
| 20 | substrings | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 255 | ▶ | sub substrings($;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
substrings: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 255 | my ($string, $length) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 255 | ▶ | return undef unless defined $string; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # paramter validation | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 254 | ▶ | if (my $r = ref($string)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | croak "Please call me `substrings STRING [,LENGTH]` | |||||||||||||||||||||||||||||||||||||||||
| 32 | but not with substrings $r, LENGTH"; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 252 | ▶ | if (defined($length) and (my $r = ref($length) or $length !~ /^\d+/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $length and (my $r = ref $length) || !($length =~ /^\d+/)
my $r = ref $length or not $length =~ /^\d+/
MC/DC100%3/3
defined $length and (my $r = ref $length) || !($length =~ /^\d+/)
defined $length
my $r = ref $length
not $length =~ /^\d+/
Truth table100%4/4
A and B or not(C)
A= defined $lengthB= my $r = ref $lengthC= not $length =~ /^\d+/
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | croak "Please call me `substrings STRING [,LENGTH]` | |||||||||||||||||||||||||||||||||||||||||
| 36 | but not with substrings '$string', $r"; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 248 | my $strlength = length($string); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 248 | my @s = (); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 248 | ▶ | if (defined $length) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 121 | ▶ | return @s if $length == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 115 | push @s, map {substr $string, $_, $length} (0 .. $strlength-$length); | |||||||||||||||||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 127 | foreach my $length (1 .. $strlength) { | |||||||||||||||||||||||||||||||||||||||||
| 47 | 10240 | push @s, map {substr $string, $_, $length} | |||||||||||||||||||||||||||||||||||||||||
| 48 | (0 .. $strlength - $length); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 242 | return @s; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 55 | __END__ | ||||||||||||||||||||||||||||||||||||||||||