[guest - Thu Oct 13 03:24:59 2005]:
Show quoted text> I was reading the perldocs on 'use' and 'require' last night and I
> officially decided that I like your Policy much better than mine.
> Aside from permitting run-time inclusion and discouraging people from
> using it in a non-portable way (i.e. hard-coded paths), it also
> discourages people from writing *.pl libraries (which was my goal all
> along). So in the next release, ProhibitRequireStrings will replace
> ProhibitRequireStatements.
>
> I might code it a little differently. Rather than searching for a
> Statement::Include, I'll look for 'require' as a Token::Word and then
> verify that the parent is a Statement::Include. That way, I don't
> have to go hunting for the 'require' token again since I already have
> it in hand. It might look like this (untested):
>
> sub violates {
> my ($self, $elem, $doc) = @_;
> $elem->isa('PPI::Token::Word') && $elem eq 'require' || return;
> $elem->parent->isa('PPI::Statement::Include') || return;
> my $sib = $elem->snext_sibling() || return;
> if( ! $sib->isa('PPI::Token::Word') ) {
> #Return violation...
> }
> return; #ok!
> }
>
> Since violates() is called in a very tight loop, I think this sort of
> short-circuting allows you to bail out as soon as possible. But
> that's just my humble opinion. Let me know if you think my approach
> is awkward or just plain incorrect. Technically, we should also
> accommodate the 'require VERSION EXPR' variety. But I think that's
> probably pretty rare so I'm willing to ingore it for now.
>
> Thanks for submitting all the patches. I have to do my real job now,
> but I'll stick them into Perl::Critic as soon as I can.
>
> -Jeff
Your solution probably works fine, although the ::Word search concerns
me due to other problems I've had with the construct (i.e. hashkeys).
I think the 'require VERSION EXPR' variety is already covered by the
code you've offered since we never look past the first argument.
As for efficiency, there's no significant difference between looking for
::Word or ::Include since you're looking at all nodes anyway. The best
way to get a performance boost would be for this policy to have a way to
tell the caller that it doesn't care about anything that's not a
::Include and then never call the violates() method in the first place.
But that's a discussion for another time and another forum.
-- Chris