Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Perl::Critic policy to catch quoted execution?

by Anonymous Monk
on Sep 23, 2022 at 10:10 UTC ( [id://11147079]=note: print w/replies, xml ) Need Help??


in reply to 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11147079]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found