blib/lib/DDC/Any.pm

filter: Statementstmt 89.9% 89 / 99 Branchbran 65.5% 38 / 58 Conditioncond 42.3% 11 / 26 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 4 totaltotal 71.1% 153 / 215 CC 41 SCAR 28.4
CC
41
Cov
75%
import
30.2
have_xs
15.1
mapstash
13.5
CRAP 17.2
« blib/lib/DDC/PP/CQCount.pm ↑ Summary blib/lib/DDC/PP/CQueryOptions.pm »
1 #-*- Mode: CPerl -*-
2  
3 ## File: DDC::Any.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description:
6 ## + DDC Query utilities: wrap DDC::XS or DDC::PP
7 ##======================================================================
8  
9 package DDC::Any;
10 17 ▶use DDC::Concordance;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Carp qw(carp confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 our @ISA = qw();
15 17 our $VERSION = $DDC::Concordance::VERSION;
16  
17 ##======================================================================
18 ## Globals
19  
20 17 our $WHICH = undef;
21 17 our ($COMPILER);
22  
23 ##======================================================================
24 ## Overrides
25  
26 ## $CQuery = DDC::Any->parse($qstr)
27 ## + convenience wrapper, re-implemented here b/c it uses the __PACKAGE__ keyword
28 103 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.1
Pod undocumented
29 103 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 103 0 103
30 103 ▶ $COMPILER = DDC::Any::CQueryCompiler->new() if (!$COMPILER);
Branch: 1 branch
BranchTrueFalseTotal
unless $DDC::Any::COMPILER 5 98 103
31 103 return $COMPILER->ParseQuery(@_);
32 }
33  
34 ## $version = DDC::Any->library_version()
35 ## + returns extended version string
36 2 ▶sub library_version {
Subroutine library_version: called CC=2 SCAR=7.2
Pod undocumented
37 2 ▶ return undef if (!defined($WHICH));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $DDC::Any::WHICH 0 2 2
38 2 return "$WHICH / " . $WHICH->can('library_version')->();
39 }
40  
41  
42 ## $obj = DDC::Any::Object->new(@args)
43 ## + override calls "real" subclass new() method
44 package DDC::Any::Object;
45 31 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
46 31 my $that = shift;
47 31 ▶ my $class = ref($that)||$that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
0310
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
48 31 $class =~ s/^DDC::Any::/${DDC::Any::WHICH}::/;
49 31 return $class->new(@_);
50 };
51  
52 ##======================================================================
53 ## Import
54 package DDC::Any;
55  
56 ##--------------------------------------------------------------
57 ## $bool = PACKAGE->have_xs()
58 ## + attempts to load DDC::XS, and returns true if it is available in a suitable version
59 17 our $MIN_XS_VERSION = 0.21;
60 12 ▶sub have_xs {
Subroutine have_xs: called CC=6 SCAR=27.1
Pod undocumented
61 12 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 12 0 12
62 0 ▶ eval "use DDC::XS;" if (!$INC{'DDC/XS.pm'});
Subroutine BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"DDC/XS.pm"} 12 0 12
63 12 ▶ return 0 if (!$INC{'DDC/XS.pm'});
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"DDC/XS.pm"} 12 0 12
64 0 ▶ (my $xs_version = ($DDC::XS::VERSION||0)) =~ s/[^0-9\.]//g;
Condition0%0/2
$DDC::XS::VERSION || 0
l!l
00
MC/DC0%0/1
$DDC::XS::VERSION || 0
$DDC::XS::VERSION
Truth table0%0/2
A
A= $DDC::XS::VERSION
Aresult
11
00
65 0 ▶ return ($xs_version && $xs_version >= $MIN_XS_VERSION);
Condition0%0/3
$xs_version && $xs_version >= $DDC::Any::MIN_XS_VERSION
!ll&&!rl&&r
000
MC/DC0%0/2
$xs_version && $xs_version >= $DDC::Any::MIN_XS_VERSION
$xs_version $xs_version >= $DDC::Any::MIN_XS_VERSION
Truth table0%0/3
A && B
A= $xs_versionB= $xs_version >= $DDC::Any::MIN_XS_VERSION
ABresult
0X0
100
111
66 }
67  
68 ##--------------------------------------------------------------
69 ## \%dst_stash = mapstash($src,$dst,%opts)
70 ## + %opts:
71 ## inherit => $which ##-- tweak inheritance; (0:don't, >0:$dst ISA $src, <0:$src ISA $dst)
72 ## deep => $bool, ##-- walk package tree? (default: true)
73 ## ignore => $re, ##-- ignore fully qualified source-symbols matching $re (default:none)
74 11 ▶sub mapstash {
Subroutine mapstash: called CC=13 SCAR=26.0
Pod undocumented
75 11 my ($src0,$dst0,%opts) = @_;
76 11 ▶ my $inherit = $opts{inherit} || 0;
Condition50%1/2
$opts{'inherit'} || 0
l!l
110
MC/DC0%0/1
$opts{'inherit'} || 0
$opts{'inherit'}
Truth table50%1/2
A
A= $opts{'inherit'}
Aresult
11
00
77 11 ▶ my $deep = exists($opts{deep}) ? $opts{deep} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'deep'} ? : 11 0 11
78 11 my $ignore = $opts{ignore};
79 11 ▶ $ignore = qr{$ignore} if (!ref($ignore));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ignore 0 11 11
80 11 my @queue = ([$src0,$dst0]);
81 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 11 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 748 11 759
83 748 my ($src,$dst) = @{shift @queue};
84 #print STDERR "mapping $src -> $dst\n";
85 748 my $src_stash = \%{"${src}::"};
86 748 my $dst_stash = \%{"${dst}::"};
87 748 ▶ while (my ($src_sym,$src_glob)=each %$src_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$src_stash) 8668 748 9416
88 8668 ▶ if ($ignore && "${src}::${src_sym}" =~ $ignore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore and ("${src}::$src_sym") =~ /$ignore/) 649 8019 8668
Condition66%2/3
$ignore and ("${src}::$src_sym") =~ /$ignore/
!ll&&!rl&&r
08019649
MC/DC50%1/2
$ignore and ("${src}::$src_sym") =~ /$ignore/
$ignore ("${src}::$src_sym") =~ /$ignore/
Truth table66%2/3
A and B
A= $ignoreB= ("${src}::$src_sym") =~ /$ignore/
ABresult
0X0
100
111
89 ##-- ignored
90 649 next;
91 }
92 8019 ▶ if ($deep && $src_sym =~ /::$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($deep and $src_sym =~ /::$/) { } 737 7282 8019
elsif ($src_sym eq 'ISA') { } 726 6556 7282
Condition66%2/3
$deep and $src_sym =~ /::$/
!ll&&!rl&&r
07282737
MC/DC50%1/2
$deep and $src_sym =~ /::$/
$deep $src_sym =~ /::$/
Truth table66%2/3
A and B
A= $deepB= $src_sym =~ /::$/
ABresult
0X0
100
111
93 ##-- sub-package
94 737 $src_sym =~ s/::$//;
95 737 $dst_stash->{"${src_sym}::"} = *{"${dst}::${src_sym}::"};
96 737 push(@queue, ["${src}::${src_sym}","${dst}::${src_sym}"]);
97 }
98 elsif ($src_sym eq 'ISA') {
99 ##-- copy inheritance
100 726 @{"${dst}::ISA"} = map {(my $isa=$_)=~s/^\Q${src0}\E\b/${dst0}/; $isa} @{"${src}::ISA"};
101 }
102 else {
103 ##-- anything else: copy
104 6556 $dst_stash->{$src_sym} = $src_glob;
105 }
106 }
107  
108 748 ▶ if ($inherit > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($inherit > 0) { } 0 748 748
elsif ($inherit < 0) { } 748 0 748
109 0 push(@{"${dst}::ISA"}, $src); ##-- tweak inheritance: $dst ISA $src
110 } elsif ($inherit < 0) {
111 748 push(@{"${src}::ISA"}, $dst); ##-- tweak inheritance: $src ISA $dst
112 }
113 }
114  
115 11 return \%{"${dst0}::"};
116 }
117  
118  
119 ##--------------------------------------------------------------
120 ## import guts
121  
122 ## $WHICH = PACKAGE->import(@requests)
123 18 ▶sub import {
Subroutine import: called CC=20 SCAR=34.1
124 18 my $that = shift;
125  
126 ##-- parse user request
127 18 my $which = $WHICH;
128 18 my %alias = ('xs'=>'DDC::XS', pp=>'DDC::PP', any=>'', default=>'');
129 18 foreach (@_) {
130 11 ▶ if (/^:(\S+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^:(\S+)$/i) 11 0 11
131 11 $which = lc($1);
132 11 ▶ $which = $alias{$which} if (exists($alias{$which}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $alias{$which} 4 7 11
133 }
134 }
135  
136 ##-- sanity check(s)
137 18 ▶ if ($which) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which) 11 7 18
138 11 ▶ return $WHICH if ($which eq 'none'); ##-- don't map back-end (yet)
Branch: 1 branch
BranchTrueFalseTotal
if $which eq "none" 7 4 11
139 4 ▶ if ($WHICH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DDC::Any::WHICH) 0 4 4
140 0 ▶ carp(__PACKAGE__ . "::import() cannot override current back-end '$WHICH' -- ignoring user request '$which'")
Branch: 1 branch
BranchTrueFalseTotal
if $DDC::Any::WHICH ne $which 0 0 0
141 if ($WHICH ne $which);
142 0 return $WHICH;
143 }
144 }
145  
146 ##-- be safe anyways
147 11 undef $WHICH;
148 11 undef $COMPILER;
149  
150 ##-- load back-end
151 11 ▶ if (!$which || $which eq 'DDC::XS') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $which or $which eq "DDC::XS") 7 4 11
Condition66%2/3
not $which or $which eq "DDC::XS"
l!l&&r!l&&!r
704
MC/DC50%1/2
not $which or $which eq "DDC::XS"
not $which $which eq "DDC::XS"
Truth table66%2/3
not(A) or B
A= not $whichB= $which eq "DDC::XS"
ABresult
1X1
011
000
152 7 ▶ if (!$that->have_xs()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $that->have_xs) { } 7 0 7
153 7 ▶ die("DDC::Any::import(): failed to load DDC::XS back-end: $@") if (($which||'') eq 'DDC::XS');
Branch: 1 branch
BranchTrueFalseTotal
if ($which || "") eq "DDC::XS" 0 7 7
Condition50%1/2
$which || ''
l!l
07
MC/DC0%0/1
$which || ''
$which
Truth table50%1/2
A
A= $which
Aresult
11
00
154 } else {
155 0 $which = 'DDC::XS';
156 }
157 }
158 11 ▶ if (!$which || $which eq 'DDC::PP') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $which or $which eq "DDC::PP") 11 0 11
Condition66%2/3
not $which or $which eq "DDC::PP"
l!l&&r!l&&!r
740
MC/DC0%0/2
not $which or $which eq "DDC::PP"
not $which $which eq "DDC::PP"
Truth table66%2/3
not(A) or B
A= not $whichB= $which eq "DDC::PP"
ABresult
1X1
011
000
159 11 ▶ eval "use DDC::PP;" if (!$INC{'DDC/PP.pm'});
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"DDC/PP.pm"} 10 1 11
160 11 ▶ if (!$INC{'DDC/PP.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $INC{'DDC/PP.pm'}) { } 0 11 11
161 0 ▶ die("DDC::Any::import(): failed to load DDC::PP back-end: $@") if (($which||'') eq 'DDC::PP');
Branch: 1 branch
BranchTrueFalseTotal
if ($which || "") eq "DDC::PP" 0 0 0
Condition0%0/2
$which || ''
l!l
00
MC/DC0%0/1
$which || ''
$which
Truth table0%0/2
A
A= $which
Aresult
11
00
162 } else {
163 11 $which = 'DDC::PP';
164 }
165 }
166 11 ▶ die("DDC::Any::import(): failed to load any back-end") if (!$which);
Branch: 1 branch
BranchTrueFalseTotal
unless $which 0 11 11
167  
168 ##-- map back-end
169 11 $WHICH = $which;
170 11 mapstash($WHICH=>'DDC::Any', deep=>1, inherit=>-1, ignore=>qr{${WHICH}::(?:VERSION|COMPILER|parse|import|library_version|.*::new)$});
171 11 return $WHICH;
172 }
173  
174  
175 17 1; ##-- be happy
176  
177 __END__
« blib/lib/DDC/PP/CQCount.pm ↑ Summary blib/lib/DDC/PP/CQueryOptions.pm »