blib/lib/FieldParser.pm

filter: Statementstmt 80.0% 76 / 95 Branchbran 46.1% 12 / 26 Conditioncond 43.3% 13 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 88.2% 15 / 17 Podpod 80.0% 4 / 5 totaltotal 63.1% 120 / 190 SCAR 41.4
CC
30
Cov
67%
extractor
25.3
parser
14.8
__ANON__
5.7
CRAP 62.7
« blib/lib/HPPPM/ErrorHandler.pm ↑ Summary blib/lib/HPPPM/Demand/Management.pm »
1 package FieldParser;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use List::MoreUtils qw(first_index last_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our @EXPORT = qw(parser);
11 2 our @EXPORT_OK = qw(tokenizer weeder extractor);
12  
13 2 our $VERSION = '0.01';
14  
15 1 ▶sub Iterator(&) { $_[0] };
Subroutine Iterator: called CC=1 SCAR=0.0
Pod undocumented
16  
17  
18 sub tokenizer {
19 1 ▶ my $input = shift || die "No input passed!Exiting...";
Subroutine tokenizer: called CC=4 SCAR=14.9
Pod documented
Condition50%1/2
shift() || die('No input passed!Exiting...')
l!l
10
MC/DC0%0/1
shift() || die('No input passed!Exiting...')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
20 1 my $weedout = shift;
21 1 ▶ my $split_on = shift || qr/"\s+?"/;
Condition33%1/3
shift() || qr/"\s+?"/
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || qr/"\s+?"/
shift() qr/"\s+?"/
Truth table33%1/3
A || B
A= shift()B= qr/"\s+?"/
ABresult
1X1
011
000
22 1 ▶ my $ignore = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
23 1 my @tokens;
24  
25 1 @tokens = split /$split_on/, $input;
26  
27 return Iterator {
28 7 ▶ while( @tokens ) {
Subroutine __ANON__: called CC=5 SCAR=17.5
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens) 6 1 7
29 6 my $token = shift @tokens;
30
31 6 for my $to_ignore ( @{ $ignore } ) {
32 0 ▶ return $weedout->($token) if $token ne $to_ignore;
Branch: 1 branch
BranchTrueFalseTotal
if $token ne $to_ignore 0 0 0
33 }
34  
35 6 ▶ return $weedout->($token) if ! @{ $ignore };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$ignore;} 6 0 6
36 }
37 }
38 1 }
39  
40  
41 sub weeder {
42 1 ▶ my $weed = shift || qr/\<\/|\>|\<|\"|\'/;
Subroutine weeder: called CC=2 SCAR=7.5
Pod documented
Condition33%1/3
shift() || qr#\</|\>|\<|\"|\'#
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || qr#\</|\>|\<|\"|\'#
shift() qr#\</|\>|\<|\"|\'#
Truth table33%1/3
A || B
A= shift()B= qr#\</|\>|\<|\"|\'#
ABresult
1X1
011
000
43
44 return sub {
45 6 ▶ my $token = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
46  
47 6 ▶ $token =~ s/$weed//g if $token;
Branch: 1 branch
BranchTrueFalseTotal
if $token 6 0 6
48 6 return $token;
49 }
50  
51 1 }
52  
53  
54 sub extractor {
55 1 ▶ my $tokens = shift || die "No tokens passed!Exiting...";
Subroutine extractor: called CC=11 SCAR=32.3
Pod documented
Condition50%1/2
shift() || die('No tokens passed!Exiting...')
l!l
10
MC/DC0%0/1
shift() || die('No tokens passed!Exiting...')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
56 1 ▶ my $all = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
57 1 ▶ my $ignore = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
58 1 my $what = shift;
59 1 my $how = shift;
60 1 my %request;
61  
62 1 ▶ return $how->($tokens, $what) if ref $how eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $how eq "CODE" 0 1 1
63  
64 1 ▶ if (! $what) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $what) { } 1 0 1
65 1 for $what ( @{ $all } ) {
66 2 my ($s_idx, $e_idx);
67 2 ▶ $s_idx = first_index { $what eq $_ } @{ $tokens };
Subroutine __ANON__: called CC=1 SCAR=0.0
68 2 ▶ if ( $s_idx == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_idx == -1) 0 2 2
69 0 print "Search for $what failed!Error - $what doesn't exist";
70 0 next;
71 }
72  
73 2 ▶ $e_idx = last_index { $what eq $_ } @{ $tokens };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 2 ▶ if ( $s_idx == $e_idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_idx == $e_idx) 0 2 2
75 0 print "Search for $what failed!Error - Only one tag Found";
76 0 next;
77 }
78  
79 2 $request{$what} = [ @{ $tokens }[$s_idx+1..$e_idx-1] ];
80
81 }
82 1 return \%request;
83 }
84 else {
85 0 my ($s_idx, $e_idx);
86 0 ▶ $s_idx = first_index { $what eq $_ } @{ $tokens };
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 print "Search for $what failed!Error -
88 0 ▶ $what doesn't exist"; return \%request if $s_idx == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $s_idx == -1 0 0 0
89  
90 0 ▶ $e_idx = last_index { $what eq $_ } @{ $tokens };
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 print "Search for $what failed!Error -
92 0 ▶ Only one tag Found"; return \%request if $s_idx == $e_idx;
Branch: 1 branch
BranchTrueFalseTotal
if $s_idx == $e_idx 0 0 0
93
94 0 return [ @{ $tokens }[$s_idx+1..$e_idx-1] ];
95 }
96 }
97  
98  
99 sub parser {
100 1 ▶ my $inp = shift || die "No input passed!Exiting...";
Subroutine parser: called CC=11 SCAR=26.9
Pod documented
Condition50%1/2
shift() || die('No input passed!Exiting...')
l!l
10
MC/DC0%0/1
shift() || die('No input passed!Exiting...')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
101 1 ▶ my $extract = shift || die "No interested tags passed!Exiting...";
Condition50%1/2
shift() || die('No interested tags passed!Exiting...')
l!l
10
MC/DC0%0/1
shift() || die('No interested tags passed!Exiting...')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
102 1 ▶ my $sep = shift || qr/"\s+?"/;
Condition33%1/3
shift() || qr/"\s+?"/
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || qr/"\s+?"/
shift() qr/"\s+?"/
Truth table33%1/3
A || B
A= shift()B= qr/"\s+?"/
ABresult
1X1
011
000
103 #my $wo = shift || qr/\<\/|\>|\<|\"|\'/;
104 1 ▶ my $wo = shift || qr/\<\/|\>|\<|\"|\'|\s+$/;
Condition33%1/3
shift() || qr#\</|\>|\<|\"|\'|\s+$#
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || qr#\</|\>|\<|\"|\'|\s+$#
shift() qr#\</|\>|\<|\"|\'|\s+$#
Truth table33%1/3
A || B
A= shift()B= qr#\</|\>|\<|\"|\'|\s+$#
ABresult
1X1
011
000
105 1 ▶ my $it = shift || [];
Condition50%1/2
shift() || []
l!l
01
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
106 1 ▶ my $ig = shift || [];
Condition50%1/2
shift() || []
l!l
01
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
107 1 my ($inputs, $weed, @tokens, $token);
108  
109 1 $inputs = $inp;
110 1 ▶ $inputs = $inp->[0] if ref $inp eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $inp eq "ARRAY" 0 1 1
111  
112 1 $weed = weeder($wo);
113 1 ▶ if (! ref $inputs) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $inputs) { } 1 0 1
114 1 my $iter = tokenizer($inputs, $weed, $sep, $it);
115  
116 1 ▶ push @tokens, $token while ( $token = $iter->() );
Branch: 1 branch
BranchTrueFalseTotal
$token = &$iter() and do { push @tokens, $token } 6 1 7
117 }
118 else {
119 #push @tokens, $token for token @$inputs;
120 0 push @tokens, $weed->($_) for @$inputs;
121 }
122  
123 1 return extractor(\@tokens, $extract, $ig);
124 }
125  
126 2 1; # End of FieldParser
127  
128 __END__
129  
130 =head1 NAME
131  
132 FieldParser - A generic parser.
133  
134 =head1 VERSION
135  
136 Version 0.01
137  
138 =head1 SYNOPSIS
139  
140 A generic parser made on the principles of Higher Order
141 Programming.The parser is meant to parse the input and
142 store the parsed text in a hashref.
143  
144 use FieldParser;
145  
146 my $interesting_tags = ['requestType', 'serviceUrl'];
147  
148 my $input = '"<serviceUrl>" "http://d.com" "</serviceUrl>" "<requestType>" "AIS" "</requestType>"'
149  
150 my $parsed = FieldParser::parser($input, $interesting_tags);
151  
152 =head1 EXPORT
153  
154 parser (default export)
155  
156 tokenizer (ondemand export)
157  
158 weeder (ondemand export)
159  
160 extractor (ondemand export)
161  
162 =head1 SUBROUTINES
163  
164 =head2 tokenizer
165  
166 Convert raw input string into units of interest.Weedout and ignore
167 text not needed.
168  
169 =head2 weeder
170  
171 Sanitize input - remove weeds/unwanted text
172  
173 =head2 extractor
174  
175 Extract tokens embedded between specific tags.One can extract
176 tokens between a specific tag or ask for all tokens embedded
177 between all tags of interest.
178  
179 =head2 parser
180  
181 Intended interface to the outside unsuspecting world.Takes in the
182 raw input, interested tags, token separator(regexp), unwanted text
183 in tokens(regexp), unwanted tokens and all unwanted tokens between
184 specific tags.
185  
186 =head1 AUTHOR
187  
188 Varun Juyal, C<< <varunjuyal123 at yahoo.com> >>
189  
190 =head1 BUGS
191  
192 Please report any bugs or feature requests to C<bug-fieldparser at rt.cpan.org>, or through
193 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FieldParser>. I will be notified, and then you'll
194 automatically be notified of progress on your bug as I make changes.
195  
196 =head1 SUPPORT
197  
198 You can find documentation for this module with the perldoc command.
199  
200 perldoc FieldParser
201  
202  
203 You can also look for information at:
204  
205 =over 4
206  
207 =item * RT: CPAN's request tracker (report bugs here)
208  
209 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FieldParser>
210  
211 =item * AnnoCPAN: Annotated CPAN documentation
212  
213 L<http://annocpan.org/dist/FieldParser>
214  
215 =item * CPAN Ratings
216  
217 L<http://cpanratings.perl.org/d/FieldParser>
218  
219 =item * Search CPAN
220  
221 L<http://search.cpan.org/dist/FieldParser/>
222  
223 =back
224  
225  
226 =head1 ACKNOWLEDGEMENTS
227  
228  
229 =head1 LICENSE AND COPYRIGHT
230  
231 Copyright 2013 Varun Juyal.
232  
233 This program is free software; you can redistribute it and/or modify it
234 under the terms of either: the GNU General Public License as published
235 by the Free Software Foundation; or the Artistic License.
236  
237 See http://dev.perl.org/licenses/ for more information.
238  
239 =cut
240  
« blib/lib/HPPPM/ErrorHandler.pm ↑ Summary blib/lib/HPPPM/Demand/Management.pm »