Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Problems with overriding a Perl::Critic policy - SOLVED

by boftx (Deacon)
on Sep 22, 2013 at 21:57 UTC ( #1055204=note: print w/replies, xml ) Need Help??

in reply to Problems with overriding a Perl::Critic policy - SOLVED

Thanks to a nudge from the post above (thanks!) I tried delegating to Subroutines::RequireArgUnpacking instead of sub-classing and now I can exclude that one but have my policy still execute.

Here is the gist of the technique I used. Please keep in mind that this is strictly proof of concept at this point. I will post the final code to Meditations when it is done as an RFC. I know already that I will want further suggestions on how to fine tune the regex expressions being used as well as how to go about integrating the existing P::C test harness for regression tests.

package Perl::Critic::Policy::Local::RequireArgUnpackOrShift; use strict; use warnings; use Readonly; use Perl::Critic::Utils qw< :booleans :characters :severities words_from_string >; use Perl::Critic::Policy::Subroutines::RequireArgUnpacking; use base 'Perl::Critic::Policy'; Readonly::Scalar my $AT_ARG => q{@_}; ## no critic (InterpolationOfMet +achars) Readonly::Scalar my $DESC => qq{Always unpack $AT_ARG first}; Readonly::Scalar my $EXPL => [178]; my $rau = Perl::Critic::Policy::Subroutines::RequireArgUnpacking->new; my @delegations = ( qw( supported_parameters default_severity default_themes applies_to _is_size_check _is_postfix_foreach _is_cast_of_array _is_cast_of_scalar _get_arg_symbols _magic_finder ) ); my @has_self = ( qw( _is_unpack _is_delegation ) ); { no strict 'refs'; for (@delegations) { my $attribute = $_; *{ __PACKAGE__ . '::' . $attribute } = sub { my $fq_sname = join('::',ref($rau),$attribute); return &{$fq_sname}(@_); }; } for (@has_self) { my $attribute = $_; *{ __PACKAGE__ . '::' . $attribute } = sub { my $self = shift; return $rau->$attribute(@_); }; } } sub violates { my ( $self, $elem, undef ) = @_; warn "In OrShift violates\n"; # do some nifty new stuff here, delegate all methods and subroutin +es # to the original policy except for my tests. return; } 1;


Corrected items in the arrays at the top.

Before someone asks, yes, I could have done the delegation much easier with Moose. I decided against that because Perl::Critic does not use Moose and is 5.6 compliant. I wanted to keep anything I added the same if it is eventually released to CPAN.

Second, I did not use AUTOLOAD for two reasons: a) anything that depends upon using a "obj->can()" test would fail, and b) I had to have two different types of delegation based on whether or not the call would be to a simple sub-routine or an object method call (passing in the object as the first element.)

On time, cheap, compliant with final specs. Pick two.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055204]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2018-01-23 04:45 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (238 votes). Check out past polls.