develooper Front page | perl.module-authors | Postings from February 2009

Re: Perl Critic and (honest) hash references

Thread Previous | Thread Next
From:
Shlomi Fish
Date:
February 18, 2009 09:14
Subject:
Re: Perl Critic and (honest) hash references
Message ID:
200902181914.14896.shlomif@iglu.org.il
On Wednesday 18 February 2009 18:05:13 Roger Hall wrote:
> All,
>
>
>
> I'm a fan of Class::Std and Dr. Conway's efforts, but I'm bemused by a
> failing perlcritic test on a new module. I used a hash reference to hold
> several values to make it easier to dump with YAML, so my code is peppered
> with code like this:
>
>
>
> $config->{query}
>
>
>
> Perlcritic complains that "Private Member Data shouldn't be accessed
> directly" because "Accessing an objects data directly breaks encapsulation
> and should be avoided". I get that. Only problem: it's not an object. It's
> just a hashref.

Well, if I understand the ideology behind this warning in your case, then 
using a $config->{CONSTANT} where CONSTANT is any Perl constant string like 
say $config->{'name'}, $config->{'query'}, $config->{'address'} etc., is a 
Perl anti-pattern. That's because if you want to collect several values into 
one, then you really should use an object for that (while using accessors, 
etc.). I'm not saying I support it, but it's a valid philosophy.

I think $hash_ref->{$key}, where $key is any Perl variable or non-constant 
expression, should be fine, because then you use the $hash_ref as a bona-fide 
keys->values mapping (instead of a C-like struct), which is a valid use.

You can always exclude this particular P::C check from the .pm files where 
this pattern is exhibited, if you want.

Regards,

	Shlomi Fish (who doesn't like Inside-out objects) 

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Parody on "The Fountainhead" - http://xrl.us/bjria

<mauke>    I'm not interested in what you're doing; what are you trying to 
           achieve?
<PerlJam>  mauke: I'm trying to achieve world peace and this regex is
           the last thing standing in my way! ;)


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About