HTML::FormFu::Filter::Regex - regexp-based match/replace filter


HTML-FormFu documentation Contained in the HTML-FormFu distribution.

Index


Code Index:

NAME

Top

HTML::FormFu::Filter::Regex - regexp-based match/replace filter

SYNOPSIS

Top

The following filter would turn 1234-5678 into ****-****.

    type: Regex
    match: \d
    replace: *

DESCRIPTION

Top

Regular expression-based match / replace filter.

METHODS

Top

match

A regex object or string to be used in the "left-hand side" of a s///g regular expression.

Default Value: qr/./

replace

A string to be used in the "right-hand side" of a s///g regular expression. The string will replace every occurrence of match.

Default Value: ''

eval

Arguments: $bool

If true, the regex modifier /e is used, so that the contents of the replace string are eval'd.

This allows the use of variables such as $1 or any other perl expression.

AUTHOR

Top

Carl Franks, cfranks@cpan.org

LICENSE

Top

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.


HTML-FormFu documentation Contained in the HTML-FormFu distribution.

package HTML::FormFu::Filter::Regex;

use Moose;
extends 'HTML::FormFu::Filter';

use HTML::FormFu::Constants qw( $EMPTY_STR );

has match   => ( is => 'rw', traits => ['Chained'] );
has replace => ( is => 'rw', traits => ['Chained'] );
has eval    => ( is => 'rw', traits => ['Chained'] );

sub filter {
    my ( $self, $value ) = @_;

    return if !defined $value;

    my $match   = defined $self->match   ? $self->match   : qr/./;
    my $replace = defined $self->replace ? $self->replace : $EMPTY_STR;

    if ( $self->eval ) {
        $value =~ s/$match/$replace/gee;
    }
    else {
        $value =~ s/$match/$replace/g;
    }

    return $value;
}

__PACKAGE__->meta->make_immutable;

1;

__END__