| 1 | package List::ToHumanString; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 4 | our $VERSION = '1.003002'; # VERSION | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 4 | require Exporter; | |||||||||||||||||||
| 10 | 4 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 11 | 4 | our @EXPORT = qw(to_human_string); | |||||||||||||||||||
| 12 | 4 | our $Extra_Comma = 1; | |||||||||||||||||||
| 13 | 4 | our $Separator = '|'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 8 | ▶ | sub _combine_list_to_string { | ||||||||||||||||||
|
Subroutine
_combine_list_to_string: called CC=6 SCAR=18.0
| |||||||||||||||||||||
| 16 | 8 | my @list = @_; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 8 | ▶ | @list = grep defined && /\S/, @list; | ||||||||||||||||||
|
Condition33%1/3
defined $_ && /\S/
MC/DC0%0/2
defined $_ && /\S/
defined $_
/\S/
Truth table33%1/3
A && B
A= defined $_B= /\S/
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 8 | ▶ | if ( not @list ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 21 | 1 | return ''; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | elsif ( @list == 1 ) { | ||||||||||||||||||||
| 24 | 1 | return "$list[0]"; | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | elsif ( @list == 2 ) { | ||||||||||||||||||||
| 27 | 3 | return "$list[0]" . ' and ' . "$list[1]"; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | else { | ||||||||||||||||||||
| 30 | 3 | ▶ | return join ', ', map "$_", | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | $Extra_Comma | ||||||||||||||||||||
| 32 | ? ( @list[0..$#list-1], 'and ' . $list[-1] ) | ||||||||||||||||||||
| 33 | : ( @list[0..$#list-2], $list[-2] . ' and ' . $list[-1] ) | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 20 | ▶ | sub to_human_string { | ||||||||||||||||||
|
Subroutine
to_human_string: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 38 | 20 | my ( $string, @list ) = @_; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 20 | ▶ | @list = grep defined && /\S/, @list; | ||||||||||||||||||
|
Condition100%3/3
defined $_ && /\S/
MC/DC100%2/2
defined $_ && /\S/
defined $_
/\S/
Truth table100%3/3
A && B
A= defined $_B= /\S/
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 20 | ▶ | my $replacement_index = ( @list == 1 ? 0 : 1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 20 | $string=~ | |||||||||||||||||||
| 44 | s% | ||||||||||||||||||||
| 45 | { ( [^}]* \Q$Separator\E [^}]* ) } | ||||||||||||||||||||
| 46 | % | ||||||||||||||||||||
| 47 | 14 | ( split /\Q$Separator\E/, $1, 2 )[ $replacement_index ]; | |||||||||||||||||||
| 48 | %gex; | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 20 | my $re = qr/\Q$Separator\Elist\Q$Separator\E/; | |||||||||||||||||||
| 51 | 20 | ▶ | if ( $string =~ /$re/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 8 | my $human_list = _combine_list_to_string( @list ); | |||||||||||||||||||
| 53 | 8 | $string =~ s/$re/$human_list/g; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 20 | return $string; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 4 | 1; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||