Skip to content

Commit

Permalink
Add replace filter class.
Browse files Browse the repository at this point in the history
  • Loading branch information
racke committed Aug 30, 2012
1 parent 7739beb commit fa8588a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions 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), <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;

0 comments on commit fa8588a

Please sign in to comment.