in reply to Perl::Critic policy to catch quoted execution?

Re^5: perlcritic and OO Perl (including Moose) idioms ( Perl::Critic::Policy is xpaths ), Re: unknown code (ppi_dumper knows everything), https://metacpan.org/pod/PPI::Token::Quote::Interpolate
  • Comment on Re: Perl::Critic policy to catch quoted execution?

Replies are listed 'Best First'.
Re^2: Perl::Critic policy to catch quoted execution? ( Perl::Critic::Policy::ValuesAndExpressions::SubMethodCallsDontInterpolate )
by Anonymous Monk on Sep 24, 2022 at 08:45 UTC

    Preliminary version , probably false positive on proper interpolation

    https://metacpan.org/pod/PPI::Token::Quote::Double

    https://metacpan.org/pod/PPIx::QuoteLike

    https://metacpan.org/pod/Perl::Critic::Utils#is_method_call(-$element-)

    #!/usr/bin/perl -- use strict; use warnings; use Perl::Critic; my $code = \<<'__CODE__'; say "$hi ->bye"; say "$hi -> bye"; say "$hi->bye"; # line 3 say "U @{[~~gmtime]} GOTS $account->balance LEFTZ"; __CODE__ print "$$code\n"; my $critic = Perl::Critic->new( -verbose => 11, ### these fail me -theme => 'yourmother', -include => ['interpolate', ], ); $critic->config()->add_policy( -policy => 'Perl::Critic::Policy::ValuesAndExpressions::SubMethodC +allsDontInterpolate', ); my @violations = $critic->critique( $code ); print @violations; exit( 0 ); BEGIN { package Perl::Critic::Policy::ValuesAndExpressions::SubMethodCalls +DontInterpolate; $INC{ join('/',split '::', __PACKAGE__).'.pm' } = __FILE__; our $VERSION = '0.01'; use strict; use warnings; use Readonly; use parent 'Perl::Critic::Policy'; use Perl::Critic::Utils qw{ :severities }; sub supported_parameters { return () } sub default_severity { return $SEVERITY_LOWEST } # didn't work sub default_themes { return qw< yourmother > } # sub default_themes { return qw< bugs maintenance yourmother +> } sub applies_to { return qw/PPI::Statement/ } Readonly::Scalar my $DESC => q{sub/method calls dont interpolate. This + won't output what you seem to expect.}; Readonly::Scalar my $EXPL => q{Maybe $foo->bar() should be @{[ $foo->b +ar ]} cause sub/method calls don't interpolate like $variables.)}; sub violates { my ( $self, $elem, undef ) = @_; my $qqs = $elem->find('PPI::Token::Quote::Double') ; for my $qq ( @$qqs ){ my $content = $qq->content ; if( $content =~ /(?<!\\)(?:\\\\)*[\$]/ and $content =~ m{\w->\ +w} ){ return $self->violation( $DESC, $EXPL, $elem ); } } return; } 1; }

      Hey, thanks for taking a stab at it. I’ll play around with it.