views:

37

answers:

2

I am using Params::Validate for validation, but at the callbacks section instead of defining the direct anonymous function, if I try giving the reference to that anonymous function it is directly jumping to the error logging area without printing the message inside the block (in case of passing correct value).

use Params::Validate qw(:all);
our $attributeCallback = sub {
    my $parameter = shift;
    $parameter =~ m/^\w+$/i ;
};

sub getSingleValue {
    eval { 
        my ($domainName, $attribute) = validate_pos( @_,
            { 
                type => SCALAR, 
                callbacks => {
                    'Domain name validation failed' => &$attributeCallback
                } 
            },
            { 
                type => SCALAR, 
                callbacks => {
                    'Attribute name validation failed' => sub { 
                        $_[0] =~ m/[\w.-]+$/i } 
                } 
            }
        );
        print "domain name is $domainName and attribute is $attribute";
        1;
    } 
        or do { 
        # catch 
        # Error Logging Area
    };
}

The actual error logged is something like "Undefined subroutine" ...

Can anyone tell why is this happening so?

+2  A: 

The reason it's failing is because you are calling the subroutine reference and therefore using its return value, rather than passing the actual reference. Instead of using

&$attributeCallback

Use

$attributeCallback
friedo
+5  A: 
{ type => SCALAR, callbacks =>{'Domain name validation failed' => &$attributeCallback } }

$attributeCallback is already a code reference. I think you just want to say

=> $attributeCallback

instead of

=> &$attributeCallback

&$attributeCallback will call the function and is like saying

callbacks => {'Domain name validation failed' => 1 }
mobrule
Thank you very much for response.......now its working
Fedrick