I am trying to write a function which validates the username for an alphanumeric value and in the case of failure it should log my custom error message and return 0 to the called function instead of die-ing:
sub insertUser{
my ( $username, $password, $email, $name) = validate_pos( @_,
{ type => SCALAR,
regex => qr/^\w+$/,
on_fail => { $err->error("username validation failed"),return 0 }
},
{ type => SCALAR },
{ type => SCALAR },
{ type => SCALAR ,optional => 1,default => 99});
print "$username, $password, $email, $name ";
}
With the above code I am facing a problem like it’s still returning 0 in the success case.
Can anybody please help me in this regard and could anyone explain me why it is doing so?
The callback associated with
on_failis not supposed to return a value. It is supposed todiein some way.In the Params::Validate documentation, is the following explanation for the
on_failcallback:(emphasis mine)
The following code works by wrapping the validation routine in an
evalblock:The output from this is: