CAB/Analyzer/Dyn.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 132 CC 12 SCAR 25.3
CC
12
Cov
0%
dumpPackage
42.0
ensureDynSubs
12.0
compileDynSub
12.0
CRAP 12.5
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::Dyn.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: generic analyzer API: dynamic code generation
6  
7 package DTA::CAB::Analyzer::Dyn;
8 use DTA::CAB::Analyzer;
9 use DTA::CAB::Utils;
10 use DTA::CAB::Datum ':all';
11 use Carp;
12 use strict;
13  
14 ##==============================================================================
15 ## Globals
16 ##==============================================================================
17  
18 our @ISA = qw(DTA::CAB::Analyzer);
19  
20 ##==============================================================================
21 ## Constructors etc.
22 ##==============================================================================
23  
24 ## $obj = CLASS_OR_OBJ->new(%args)
25 ## + object structure, new
26 ## (
27 ## ##-- code generation options
28 ## analyze${Which}Code => $str, ##-- code for analyze${Which} method
29 ##
30 ## ##-- generated code
31 ## analyze${Which}Sub => \&sub, ##-- compiled code for analyze${Which} method
32 ## )
33 sub new {
34 my $that = shift;
35 return $that->SUPER::new(@_);
36 }
37  
38 ## undef = $anl->dropClosures();
39 ## + drops 'analyze${which}' closures
40 ## + currently does nothing
41 sub dropClosures {
42 my $anl = shift;
43 my ($which);
44 foreach $which (qw(Document Types Tokens Sentences Local Clean)) {
45 delete($anl->{"analyze${which}"});
46 }
47 return $anl->SUPER::dropClosures(@_);
48 }
49  
50 ##==============================================================================
51 ## Methods: I/O
52 ##==============================================================================
53  
54 ##--------------------------------------------------------------
55 ## Methods: I/O: Input: all
56  
57 ## $bool = $anl->prepare()
58 ## $bool = $anl->prepare(\%opts)
59 ## + inherited: wrapper for ensureLoaded(), autoEnable(), initInfo()
60 ## + override appends ensureDynSubs() call
61 sub prepare {
62 my $anl = shift;
63 $anl->SUPER::prepare() || return 0;
64 $anl->ensureDynSubs() || return 0;
65 $anl->analyzeDyn('Prepare',$anl,@_);
66 return 1;
67 }
68  
69 ##==============================================================================
70 ## Methods: Dynamic Closures
71  
72 ## $bool = $anl->ensureDynSubs()
73 ## + ensures subs are defined for all analyze${Which} methods
74 sub ensureDynSubs {
75 my $anl = shift;
76 my ($which,$sub);
77 foreach $which (qw(Prepare Document Types Tokens Sentences Local Clean)) {
78 $anl->{"analyze${which}"} = $anl->compileDynSub($which) if (!UNIVERSAL::isa($anl->{"analyze${which}"},'CODE'));
79 if (!UNIVERSAL::isa($anl->{"analyze${which}"},'CODE')) {
80 $anl->logcluck("ensureDynSubs(): no analysis sub for '$which'");
81 }
82 }
83 return 1;
84 }
85  
86 ## \&sub = $anl->compileDynSub($which)
87 ## + returns compiled analyze${Which} sub
88 sub compileDynSub {
89 my ($anl,$which) = @_;
90 my ($code);
91 if (defined($code=$anl->dynSubCode($which))) {
92 my $sub = eval $code;
93 $anl->logcluck("compileDynSub($which): could not compile analysis sub {$code}: $@") if (!$sub);
94 return $sub;
95 }
96 return DTA::CAB::Analyzer->can("analyze${which}"); ##-- default: just wrap superclass method
97 }
98  
99 ## $code = $anl->dynSubCode($which)
100 ## + returns code for analyze${Which} sub
101 sub dynSubCode {
102 my ($anl,$which) = @_;
103 return $anl->{"analyze${which}Code"} if (defined($anl->{"analyze${which}Code"}));
104 return undef;
105 }
106  
107 ## undef = dumpPackage(%opts)
108 ## + %opts:
109 ## file => $file_or_handle,
110 ## package => $pkgname,
111 sub dumpPackage {
112 my ($anl,%opts) = @_;
113 $opts{file} = '-' if (!defined($opts{file}));
114 my $fh = ref($opts{file}) ? $opts{file} : IO::File->new(">$opts{file}");
115 $anl->logdie("open failed for '$opts{file}': $!") if (!defined($fh));
116  
117 $fh->print("package ".($opts{package} || (ref($anl) ."::dump")).";\n",
118 "use ", ref($anl), ";\n",
119 "our \@ISA = (", ref($anl), ");\n",
120 );
121 my ($code,$which);
122 foreach $which (qw(Document Types Tokens Sentences Local Clean)) {
123 if (defined($code=$anl->dynSubCode($which))) {
124 $code =~ s/^\s*sub/sub analyze${which}/;
125 $fh->print($code,"\n");
126 }
127 }
128 $fh->print("1; ##-- be happy\n");
129 $fh->close() if (!ref($opts{file}));
130 }
131  
132 ##==============================================================================
133 ## Methods: Analysis: v1.x
134  
135 ##------------------------------------------------------------------------
136 ## Methods: Analysis: v1.x: API: Dyn
137  
138 ## $rc = $anl->analyzeDyn($which,@args)
139 ## + wrapper for $anl->{"analyze${which}"}->(@args)
140 sub analyzeDyn {
141 return $_[0]->{"analyze$_[1]"}->(@_[2..$#_]) if (UNIVERSAL::isa($_[0]->{"analyze$_[1]"},'CODE'));
142 return undef;
143 }
144  
145 ##------------------------------------------------------------------------
146 ## Methods: Analysis: v1.x: API
147  
148 ## $doc = $anl->analyzeDocument($doc,\%opts)
149 ## + analyze a DTA::CAB::Document $doc
150 ## + top-level API routine
151 sub analyzeDocument { return $_[0]->analyzeDyn('Document',@_); }
152  
153 ## $doc = $anl->analyzeTypes($doc,\%types,\%opts)
154 ## + perform type-wise analysis of all (text) types in $doc->{types}
155 sub analyzeTypes { return $_[0]->analyzeDyn('Types',@_); }
156  
157 ## $doc = $anl->analyzeTokens($doc,\%opts)
158 ## + perform token-wise analysis of all tokens $doc->{body}[$si]{tokens}[$wi]
159 sub analyzeTokens { return $_[0]->analyzeDyn('Tokens',@_); }
160  
161 ## $doc = $anl->analyzeSentences($doc,\%opts)
162 ## + perform sentence-wise analysis of all sentences $doc->{body}[$si]
163 sub analyzeSentences { return $_[0]->analyzeDyn('Sentences',@_); }
164  
165 ## $doc = $anl->analyzeLocal($doc,\%opts)
166 ## + perform analyzer-local document-level analysis of $doc
167 sub analyzeLocal { return $_[0]->analyzeDyn('Local',@_); }
168  
169 ## $doc = $anl->analyzeClean($doc,\%opts)
170 ## + cleanup any temporary data associated with $doc
171 sub analyzeClean { return $_[0]->analyzeDyn('Clean',@_); }
172  
173  
174 1; ##-- be happy
175  
176 __END__