| 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
| |||||||||||||||||||||||||||||||||
| 30 | 103 | ▶ | $COMPILER = DDC::Any::CQueryCompiler->new() if (!$COMPILER); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | eval "use DDC::XS;" if (!$INC{'DDC/XS.pm'}); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 12 | ▶ | return 0 if (!$INC{'DDC/XS.pm'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | (my $xs_version = ($DDC::XS::VERSION||0)) =~ s/[^0-9\.]//g; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$DDC::XS::VERSION || 0
MC/DC0%0/1
$DDC::XS::VERSION || 0
$DDC::XS::VERSION
Truth table0%0/2
A
A= $DDC::XS::VERSION
| |||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return ($xs_version && $xs_version >= $MIN_XS_VERSION); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$xs_version && $xs_version >= $DDC::Any::MIN_XS_VERSION
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
| |||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$opts{'inherit'} || 0
$opts{'inherit'}
Truth table50%1/2
A
A= $opts{'inherit'}
| |||||||||||||||||||||||||||||||||
| 77 | 11 | ▶ | my $deep = exists($opts{deep}) ? $opts{deep} : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 11 | my $ignore = $opts{ignore}; | |||||||||||||||||||||||||||||||
| 79 | 11 | ▶ | $ignore = qr{$ignore} if (!ref($ignore)); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 88 | 8668 | ▶ | if ($ignore && "${src}::${src_sym}" =~ $ignore) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ignore and ("${src}::$src_sym") =~ /$ignore/
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/
| |||||||||||||||||||||||||||||||||
| 89 | ##-- ignored | ||||||||||||||||||||||||||||||||
| 90 | 649 | next; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | 8019 | ▶ | if ($deep && $src_sym =~ /::$/) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$deep and $src_sym =~ /::$/
MC/DC50%1/2
$deep and $src_sym =~ /::$/
$deep
$src_sym =~ /::$/
Truth table66%2/3
A and B
A= $deepB= $src_sym =~ /::$/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 131 | 11 | $which = lc($1); | |||||||||||||||||||||||||||||||
| 132 | 11 | ▶ | $which = $alias{$which} if (exists($alias{$which})); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | ##-- sanity check(s) | ||||||||||||||||||||||||||||||||
| 137 | 18 | ▶ | if ($which) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 11 | ▶ | return $WHICH if ($which eq 'none'); ##-- don't map back-end (yet) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | 4 | ▶ | if ($WHICH) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | carp(__PACKAGE__ . "::import() cannot override current back-end '$WHICH' -- ignoring user request '$which'") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not $which or $which eq "DDC::XS"
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"
| |||||||||||||||||||||||||||||||||
| 152 | 7 | ▶ | if (!$that->have_xs()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | die("DDC::Any::import(): failed to load DDC::XS back-end: $@") if (($which||'') eq 'DDC::XS'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$which || ''
MC/DC0%0/1
$which || ''
$which
Truth table50%1/2
A
A= $which
| |||||||||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||||||
| 155 | 0 | $which = 'DDC::XS'; | |||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | 11 | ▶ | if (!$which || $which eq 'DDC::PP') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $which or $which eq "DDC::PP"
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"
| |||||||||||||||||||||||||||||||||
| 159 | 11 | ▶ | eval "use DDC::PP;" if (!$INC{'DDC/PP.pm'}); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 160 | 11 | ▶ | if (!$INC{'DDC/PP.pm'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | die("DDC::Any::import(): failed to load DDC::PP back-end: $@") if (($which||'') eq 'DDC::PP'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$which || ''
MC/DC0%0/1
$which || ''
$which
Truth table0%0/2
A
A= $which
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||