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