diff --git a/MANIFEST b/MANIFEST index a879120..74c9c3b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -15,6 +15,7 @@ lib/Template/Flute/Filter/Eol.pm lib/Template/Flute/Filter/CountryName.pm lib/Template/Flute/Filter/LanguageName.pm lib/Template/Flute/Filter/NobreakSingle.pm +lib/Template/Flute/Filter/Replace.pm lib/Template/Flute/Filter/Upper.pm lib/Template/Flute/Specification/Scoped.pm lib/Template/Flute/Specification/XML.pm diff --git a/lib/Template/Flute/Filter/Replace.pm b/lib/Template/Flute/Filter/Replace.pm new file mode 100644 index 0000000..ccb0c76 --- /dev/null +++ b/lib/Template/Flute/Filter/Replace.pm @@ -0,0 +1,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), + +=head1 LICENSE AND COPYRIGHT + +Copyright 2012 Stefan Hornburg (Racke) . + +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;