Valiant::Validator::With - Filter using a coderef and options opts
package Local::Test::User; use Moo; use Valiant::Filters; has 'name' => (is=>'ro', required=>1); filters name => ( with => { cb => sub { my ($class, $attrs, $name, $opts) = @_; return $attrs->{$name}.$opts->{a}; }, opts => +{ a=>'foo' }, }, with => sub { my ($class, $attrs, $name) = @_; return $attrs->{$name}.'bar'; }, with => [sub { my ($class, $attrs, $name, $opts) = @_; return $attrs->{$name}.$opts; }, 'baz'], ); my $user = Local::Test::User->new(name=>'john'); print $user->name; # 'johnfoobarbaz'
this filter allows you to make a custom subroutine reference into a filter, with options options for parameterization. You can use this when you have a very special filter need but don't feel like writing a custom filter by subclassing Valiant::Filter::Each.
You can pass parameters to the $opts hashref using the opts argument:
$opts
opts
my $filter = sub { my ($self, $class, $attrs, $attribute_name, $opts) = @_; my $old_value = $attrs->{$attribute_name}; # ... return $new_value }; filters my_attribute => ( with => { cb => $filter, opts => {arg => 2000}, }, );
You might find this useful in creating more parametered callbacks. However at this point you might wish to consider just writing a custom filter class.
This filter supports the follow shortcut forms:
validates attribute => ( with => sub { my $self = shift; ... }, ... ); validates attribute => ( with => [\&method, [1,2,3]], ... );
Which is the same as:
validates attribute => ( with => { cb => sub { ... }, }, ... ); validates attribute => ( with => { cb => \&method, opts => [1,2,3], }, ... );
Valiant, Valiant::Filter, Valiant::Filter::Each.
See Valiant
To install Valiant, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Valiant
CPAN shell
perl -MCPAN -e shell install Valiant
For more information on module installation, please visit the detailed CPAN module installation guide.