File Coverage

blib/lib/WebSource/Filter/script.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package WebSource::Filter::script;
2 1     1   1743 use strict;
  1         3  
  1         33  
3 1     1   5 use Carp;
  1         2  
  1         63  
4              
5 1     1   3726 use File::Temp qw/tmpnam/;
  1         24679  
  1         68  
6              
7 1     1   58 use WebSource::Filter;
  0            
  0            
8             our @ISA = ('WebSource::Filter');
9              
10             =head1 NAME
11              
12             WebSource::Filter::script - Use a script for filtering
13              
14             =head1 DESCRIPTION
15              
16             A filter operator of type script allows to determine what to do with
17             the input based on an inline or external script. The data of the input
18             is passed as an argument to the script.
19              
20             =head1 SYNOPSIS
21              
22             B
23              
24            
25            
26            
27              
28             or
29              
30            
31             forward-to="somemodules" />
32              
33             =head1 METHODS
34              
35             =cut
36              
37              
38              
39             sub new {
40             my $class = shift;
41             my %params = @_;
42             my $self = bless \%params, $class;
43             $self->SUPER::_init_;
44              
45             $self->{seen} = {};
46             my $wsd = $self->{wsdnode};
47             if($wsd) {
48             if($wsd->hasAttribute("script-file")) {
49             $self->{scriptfile} = $wsd->getAttribute("script-file");
50             } else {
51             my $sf;
52             ($sf,$self->{scriptfile}) = tmpnam();
53             my $content = $wsd->textContent;
54             $content =~ s/^[\s\n]+//;
55             print $sf $content;
56             close($sf);
57             system("chmod +x " . $self->{scriptfile});
58             }
59             }
60             $self->{scriptfile} or die "No script file set for ",$self->{name},"\n";
61             -x $self->{scriptfile} or die $self->{scriptfile}, " is not executable\n";
62             return $self;
63             }
64              
65             sub handle {
66             my $self = shift;
67             return map { $self->keep($_) ? $_ : () } @_;
68             }
69              
70             sub keep {
71             my $self = shift;
72             my $env = shift;
73             my $val = $env->dataString;
74             my $script = $self->{scriptfile};
75             my $res = system($script,$val);
76             return $res == 0;
77             }
78              
79             =head1 SEE ALSO
80              
81             WebSource
82              
83             =cut
84              
85             1;
86