WebSource/Extract/regexp.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 91 CC 7 SCAR 30.5
CC
7
Cov
0%
handle
42.0
_init_
6.0
BEGIN
2.0
CRAP 21.1
1 package WebSource::Extract::regexp;
2 use strict;
3 use WebSource::Parser;
4 use Carp;
5  
6 our @ISA = ('WebSource::Module');
7  
8 =head1 NAME
9  
10 WebSource::Extract::regexp - Extract information using a regular expression
11  
12 =head1 DESCRIPTION
13  
14 The regexp B<Extract> operator allows to extract information based on a regular
15 expression.
16  
17  
18  
19 =head1 METHODS
20  
21 See WebSource::Module
22  
23 =cut
24  
25 sub _init_ {
26 my $self = shift;
27 $self->SUPER::_init_;
28 my $wsd = $self->{wsdnode};
29 if($wsd) {
30 $self->{regexp} = $wsd->findvalue('regexp');
31 my @keys = map { $_->textContent } $wsd->findnodes('map/key');
32 $self->{keys} = \@keys;
33 $self->{slot} = $wsd->getAttribute("apply-to");
34 }
35 $self->{regexp} or croak "No regular expression given";
36 return $self;
37 }
38  
39 sub handle {
40 my $self = shift;
41 my $env = shift;
42 my $re = $self->{regexp};
43 my %meta = %$env;
44  
45 my $slot = $self->{slot} ? $self->{slot} : "data";
46 $self->log(5,"Extracting with m/$re/ from $slot");
47 my $str = $slot eq "data" ? $env->dataString : $env->{$slot};
48  
49 if(my @keys = @{$self->{keys}}) {
50 if($str =~ m/$re/) {
51 my %map;
52 no strict "refs";
53 for(my $i=0; $i<=$#keys && $i<9; $i++) {
54 $map{$keys[$i]} = ${$i+1};
55 }
56 use strict "refs";
57 $map{data} and $map{type} = "text/string";
58 return WebSource::Envelope->new(
59 %meta,
60 %map,
61 );
62 } else {
63 return ();
64 }
65 } else {
66 # matching part is data
67 if($str =~ m/$re/) {
68 $slot eq "data" and $meta{type} = "text/string";
69 return WebSource::Envelope->new(
70 %meta,
71 $slot => $&,
72 );
73 } else {
74 return ();
75 }
76 }
77 }
78  
79 =head1 SEE ALSO
80  
81 WebSource
82  
83 =cut
84  
85 1;