Assign method call to hash value?

gvim gvimrc at gmail.com
Tue Jan 29 11:54:05 GMT 2013


On 29/01/2013 03:39, Sam Kington wrote:
> As Mike mentioned, you almost certainly want to say { ... valid => scalar $r->valid, ... } instead; checking the documentation for Data::FormValidator and seeing what it says the missing, valid and unknown methods return in list context vs scalar context, might help.
>
> https://metacpan.org/module/Data::FormValidator#check- says "It returns its results as a Data::FormValidator::Results object" and e.g. the documentation for the missing method says
>
>> In an array context it returns the list of fields which are missing. In a scalar context, it returns an array reference to the list of missing fields.
>>
>> If called with an argument, it returns true if that field is missing, undef otherwise.
>
> So you could replace the last 5 lines of your sub with e.g.
>
>      return {
>          passed  => $passed,
>          valid   => scalar $r->valid,
>          missing => scalar $r->missing,
>          invalid => scalar $r->invalid,
>          unknown => scalar $r->unknown,
>      };
>
> Once written like that, it becomes clear that there's some repetition going on.
>
> There's two things you could do at this point. Depending on what you need to do later on, you might just say
>
>      return { passed => $passed, form_validator_results => $r };
>
> and let the calling code fish out the stuff it needs. This might be mildly more efficient if it ever turns out that calling the valid, missing, invalid and unknown methods ahead of time was wasteful if the calling code didn't need that information. Or it might not; benchmark your code, if performance is a problem, with Devel::NYTProf, and then decide.
>
> Alternatively, say e.g.
>
>      return {
>          passed => ($r->success && !$r->has_unknown),
>          map { $_ => scalar $r->$_ } qw(valid missing invalid unknown)
>      };
>
> (and you can get rid of the $passed variable earlier) to make this a simple transformation.
>
> I forget when the use of variables as method names - for the $r->$_ bit - was introduced; it was probably perl 5.6.0, but it might be as late as 5.8.0. Either way, it should be safe for all but paleolithic environments.
>
> Sam
>

I tried passing the $r object as:

return { .... results => $r .....};

...... before posting but calling ->method produced the same problems at 
the other end. This may be more to do with basic Perl than DFV but I'm 
still not clear why assigning $r->method as a hash value doesn't invoke 
a scalar context, which is what I wanted rather than the array context 
option for $r->method. In the end I found:

$results{missing} = $r->missing if $r->has_missing;
return \%results;

.... more useful anyway so problem solved.

gvim




More information about the london.pm mailing list