File Coverage

blib/lib/HTML/Template/Compiled/Filter.pm
Criterion Covered Total %
statement 36 49 73.4
branch 0 6 0.0
condition 0 2 0.0
subroutine 12 14 85.7
pod 0 2 0.0
total 48 73 65.7


line stmt bran cond sub pod time code
1             package HTML::Template::Compiled::Filter;
2             our $VERSION = '1.002_001'; # TRIAL VERSION
3 1     1   5 use strict;
  1     1   2  
  1     1   30  
  1     1   4  
  1         1  
  1         16  
  1         3  
  1         2  
  1         17  
  1         4  
  1         2  
  1         19  
4 1     1   3 use warnings;
  1     1   2  
  1     1   32  
  1     1   3  
  1         2  
  1         25  
  1         12  
  1         2  
  1         28  
  1         2  
  1         2  
  1         26  
5              
6 1     1   4 use constant SUBS => 0;
  1     1   1  
  1     1   337  
  1     1   4  
  1         0  
  1         128  
  1         4  
  1         1  
  1         128  
  1         3  
  1         1  
  1         128  
7              
8             sub new {
9 0     0 0   my ($class, $spec) = @_;
10 0 0         if (ref $spec eq __PACKAGE__) {
11 0           return $spec;
12             }
13 0           my $self = [];
14 0           bless $self, $class;
15 0           $self->init($spec);
16 0           return $self;
17             }
18              
19             sub init {
20 0     0 0   my ($self, $spec) = @_;
21 0 0         if (ref $spec eq 'CODE') {
22 0           $self->[SUBS] = [
23             {
24             code => $spec,
25             format => 'scalar',
26             },
27             ];
28             }
29             else {
30 0 0         for my $filter (ref $spec eq 'ARRAY' ? @$spec : $spec) {
31 0           push @{ $self->[SUBS] }, {
32             format => $filter->{format} || 'scalar',
33 0   0       code => $filter->{'sub'},
34             };
35             }
36             }
37             }
38              
39             sub filter {
40             my ($self, $data) = @_;
41             for my $filter (@{ $self->[SUBS] }) {
42             if ($filter->{format} eq 'scalar') {
43             $filter->{code}->(\$data);
44             }
45             else {
46             my $lines = [split /(?:\n)/, $data];
47             $filter->{code}->($lines);
48             $data = join '', @$lines;
49             }
50             }
51             # inplace edit
52             $_[1] = $data;
53             }
54              
55             1;
56             __END__