/
Replace.pm
79 lines (48 loc) · 1.46 KB
/
Replace.pm
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package Template::Flute::Filter::Replace;
use strict;
use warnings;
use base 'Template::Flute::Filter';
=head1 NAME
Template::Flute::Filter::Replace - Substitutes literal text in a string.
=head1 SYNOPSIS
$replace_filter = Template::Flute::Filter::Replace->new(options => {
from => 'foo',
to => 'bar',
});
$barbar = $replace->filter('foobar');
=head1 DESCRIPTION
The replace filter locates a substring within a string and substitutes
each occurence with another string.
=head1 METHODS
=head2 init
Initializes object with replace options.
=cut
sub init {
my ($self, %args) = @_;
for (qw/from to/) {
$self->{$_} = $args{options}->{$_};
}
}
=head2 filter
Carries out the actual substitution and returns
the filtered value.
=cut
sub filter {
my ($self, $value) = @_;
my ($qtd);
return $value unless defined $self->{from}
&& length $self->{from};
$qtd = quotemeta($self->{from});
$value =~ s/$qtd/$self->{to}/g;
return $value;
}
=head1 AUTHOR
Stefan Hornburg (Racke), <racke@linuxia.de>
=head1 LICENSE AND COPYRIGHT
Copyright 2012 Stefan Hornburg (Racke) <racke@linuxia.de>.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut
1;