blib/lib/Sort/Key/Types.pm

filter: Statementstmt 95.7% 89 / 93 Branchbran 62.0% 31 / 50 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 11 / 11 Podpod 33.3% 1 / 3 totaltotal 81.2% 134 / 165 CC 29 SCAR 24.3
CC
29
Cov
82%
combine_sub
20.6
register_type
5.6
_mks2n
4.2
CRAP 11.3
« blib/lib/Sort/Key/Natural.pm ↑ Summary blib/lib/Sort/Key/Multi.pm »
1 package Sort::Key::Types;
2  
3 5 our $VERSION = '1.30';
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 require Exporter;
10 5 our @ISA = qw(Exporter);
11 5 our @EXPORT_OK = qw(register_type);
12  
13 5 our $DEBUG;
14 5 ▶$DEBUG ||= 0;
Condition50%1/2
$Sort::Key::Types::DEBUG ||= 0
l!l
05
MC/DC0%0/1
$Sort::Key::Types::DEBUG ||= 0
$Sort::Key::Types::DEBUG
Truth table50%1/2
A
A= $Sort::Key::Types::DEBUG
Aresult
11
00
15  
16 # this hash is also used from Sort::Key::Multi to find out which
17 # letters can be used as types:
18  
19 5 our %mktypes = ( s => 0,
20 l => 1,
21 n => 2,
22 i => 3,
23 u => 4 );
24  
25 33 ▶sub _mks2n {
Subroutine _mks2n: called CC=4 SCAR=14.5
26 33 ▶ if (my ($rev, $key)=$_[0]=~/^([-+]?)(.)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $_[0] =~ /^([-+]?)(.)$/) 33 0 33
27 33 ▶ exists $mktypes{$key}
Branch: 1 branch
BranchTrueFalseTotal
exists $Sort::Key::Types::mktypes{$key} or croak("invalid multi-key type '$_[0]'") 33 0 33
28 or croak "invalid multi-key type '$_[0]'";
29 33 my $n = $mktypes{$key};
30 33 ▶ $n+=128 if $rev eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $rev eq "-" 11 22 33
31 33 return $n
32 }
33 0 die "internal error, bad key '$_[0]'";
34 }
35  
36 5 our %mkmap = qw(str s
37 string s
38 locale l
39 loc l
40 lstr l
41 int i
42 integer i
43 uint u
44 unsigned_integer u
45 number n
46 num n);
47  
48 5 $_ = [$_] for (values %mkmap);
49 5 our %mksub = map { $_ => undef } keys %mkmap;
50  
51 50 ▶sub _get_map {
Subroutine _get_map: called CC=4 SCAR=14.0
52 50 my ($rev, $name) = $_[0]=~/^([+-]?)(.*)$/;
53 50 ▶ exists $mkmap{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $Sort::Key::Types::mkmap{$name} or croak("unknown key type '${name}'\n") 50 0 50
54 or croak "unknown key type '$name'\n";
55 50 ▶ if ($rev eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev eq "-") 15 35 50
56 15 ▶ return map { /^-(.*)$/ ? $1 : "-$_" } @{$mkmap{$name}}
Branch: 1 branch
BranchTrueFalseTotal
/^-(.*)$/ ? : 0 15 15
57 }
58 35 @{$mkmap{$name}}
59 }
60  
61 36 ▶sub _get_sub {
Subroutine _get_sub: called CC=2 SCAR=7.1
62 36 $_[0]=~/^[+-]?(.*)$/;
63 36 ▶ exists $mksub{$1}
Branch: 1 branch
BranchTrueFalseTotal
exists $Sort::Key::Types::mksub{$1} or croak("unknown key type '$1'\n") 36 0 36
64 or croak "unknown key type '$1'\n";
65 36 return $mksub{$1}
66 }
67  
68 44 ▶sub _combine_map { map { _get_map $_ } @_ }
Subroutine _combine_map: called CC=1 SCAR=0.0
69  
70 5 ▶use constant _nl => "\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 24 ▶sub combine_types { pack('C*', (map { _mks2n $_ } _combine_map(@_))) }
Subroutine combine_types: called CC=1 SCAR=0.0
Pod undocumented
73  
74 31 ▶sub combine_sub {
Subroutine combine_sub: called CC=18 SCAR=30.2
Pod undocumented
75 31 my $sub = shift;
76 31 my $for = shift;
77 31 ▶ $for = defined $for ? " for $for" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $for ? : 7 24 31
78  
79 31 my @subs = map { _get_sub $_ } @_;
80  
81 31 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) { } 31 0 31
82 31 my $code = 'sub { '._nl;
83 31 ▶ if (ref $sub eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub eq 'CODE') { } 19 12 31
84 19 ▶ unless (grep { defined $_ } @subs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {defined $_;} @subs) 16 3 19
85 16 return $sub
86 }
87 3 $code.= 'my @keys = &{$sub};'._nl;
88 }
89 else {
90 12 ▶ if ($sub eq '@_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq "\@_") 12 0 12
91 12 ▶ return undef unless grep {defined $_} @subs;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @subs 2 10 12
92 }
93 10 $code.= 'my @keys = '.$sub.';'._nl;
94 }
95 13 ▶ $code.= 'print "in: |@keys|\n";'._nl if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Sort::Key::Types::DEBUG 0 13 13
96  
97 13 $code.= '@keys == '.scalar(@_)
98 . ' or croak "wrong number of keys generated$for '
99 . '(expected '.scalar(@_).', returned ".scalar(@keys).")";'._nl;
100  
101 { # new scope so @map doesn't get captured
102 13 my @map = _combine_map @_;
103 13 ▶ if (@map==@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@map == @_) { } 10 3 13
104 10 for my $i (0..$#_) {
105 10 ▶ if (defined $subs[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subs[$i]) 10 0 10
106 10 $code.= '{ local $_ = $keys['.$i.']; ($keys['.$i.']) = &{$subs['.$i.']}() }'._nl;
107 }
108 }
109 10 ▶ $code.='print "out: |@keys|\n";'._nl if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Sort::Key::Types::DEBUG 0 10 10
110 10 $code.='return @keys'._nl;
111 }
112 else {
113 3 $code.='my @keys1;'._nl;
114 3 for my $i (0..$#_) {
115 4 ▶ if (defined $subs[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subs[$i]) { } 4 0 4
116 4 $code.= '{ local $_ = shift @keys; push @keys1, &{$subs['.$i.']}() }'._nl;
117 }
118 else {
119 0 $code.= 'push @keys1, shift @keys;'._nl;
120 }
121 }
122 3 ▶ $code.='print "out: |@keys1|\n";'._nl if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Sort::Key::Types::DEBUG 0 3 3
123 3 $code.='return @keys1'._nl;
124 }
125 }
126 13 $code.='}'._nl;
127 13 ▶ print "CODE$for:\n$code----\n" if $DEBUG >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $Sort::Key::Types::DEBUG >= 2 0 13 13
128 13 my $map = eval $code;
129 13 ▶ $@ and die "internal error: code generation failed ($@)";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "internal error: code generation failed ($@)" 0 13 13
130 13 return $map;
131 }
132 else {
133 0 ▶ @_==1 or croak "too many keys or keygen subroutine undefined$for";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("too many keys or keygen subroutine undefined$for") 0 0 0
134 0 return @subs;
135 }
136 }
137  
138 7 ▶sub register_type {
Subroutine register_type: called CC=5 SCAR=17.3
Pod documented
139 7 my $name = shift;
140 7 my $sub = shift;
141 7 ▶ $name=~/^\w+(?:::\w+)*$/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^\w+(?:::\w+)*$/ or croak("invalid type name '${name}'") 7 0 7
142 or croak "invalid type name '$name'";
143 7 ▶ @_ or
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("too few keys") 7 0 7
144 croak "too few keys";
145 7 ▶ (exists $mkmap{$name} or exists $mktypes{$name})
Branch: 1 branch
BranchTrueFalseTotal
exists $Sort::Key::Types::mkmap{$name} || exists $Sort::Key::Types::mktypes{$name} and croak("type '${name}' already registered or reserved in " . 'Sort::Key::Types') 0 7 7
Condition33%1/3
exists $Sort::Key::Types::mkmap{$name} or exists $Sort::Key::Types::mktypes{$name}
l!l&&r!l&&!r
007
MC/DC0%0/2
exists $Sort::Key::Types::mkmap{$name} or exists $Sort::Key::Types::mktypes{$name}
exists $Sort::Key::Types::mkmap{$name} exists $Sort::Key::Types::mktypes{$name}
Truth table33%1/3
A or B
A= exists $Sort::Key::Types::mkmap{$name}B= exists $Sort::Key::Types::mktypes{$name}
ABresult
1X1
011
000
146 and croak "type '$name' already registered or reserved in ".__PACKAGE__;
147 7 $mkmap{$name} = [ _combine_map @_ ];
148 7 $mksub{$name} = combine_sub $sub, $name, @_;
149 ()
150 7 }
151  
152  
153 5 1;
154  
155 __END__
« blib/lib/Sort/Key/Natural.pm ↑ Summary blib/lib/Sort/Key/Multi.pm »