perl-critic

Perl critic policy violation in checking index of substring in a string

风流意气都作罢 提交于 2020-01-17 15:21:31
问题 for my $item (@array) { if (index($item, '$n') != -1) { print "HELLO\n"; } } Problem is: Perl critic gives below policy violation. String may require interpolation at line 168, near '$item, '$n''. (Severity: 1) Please advise how do I fix this? 回答1: In this case the analyzer either found a bug or is plain wrong in flagging your code. Are you looking for a literal " $n " in $item , or for what $n variable evaluates to? If you want to find the literal $n characters then there is nothing wrong

Perl critic error on splitting string into array using regexp

别等时光非礼了梦想. 提交于 2020-01-07 10:05:20
问题 sub func { my ($n) = @_; return unless ($n); my @array; push @array, $1 while $n =~ / ((?: [^(),]+ | ( \( (?: [^()]+ | (?2) )* \) ) )+) (?: ,\s* | $) /xg; return \@array; } for my $item (@array) { if (index($item, '$n') != -1) { print "HELLO\n"; } } I have above regex to split some string into array. It works fine. Problem is : Perl critic gives below errors. Please advise how do i fix this. Capture variable used outside conditional at line 150, near 'push @array, $1 while $n =~ /'. (Severity

Perl critic error on splitting string into array using regexp

夙愿已清 提交于 2020-01-07 09:59:47
问题 sub func { my ($n) = @_; return unless ($n); my @array; push @array, $1 while $n =~ / ((?: [^(),]+ | ( \( (?: [^()]+ | (?2) )* \) ) )+) (?: ,\s* | $) /xg; return \@array; } for my $item (@array) { if (index($item, '$n') != -1) { print "HELLO\n"; } } I have above regex to split some string into array. It works fine. Problem is : Perl critic gives below errors. Please advise how do i fix this. Capture variable used outside conditional at line 150, near 'push @array, $1 while $n =~ /'. (Severity

Perlcritic - Two argument “open” error

↘锁芯ラ 提交于 2019-12-22 06:55:00
问题 I have a script and I am trying to elimate bad practices using perlcritic. One line I have is as follows: open(my($FREESPCHK), $cmdline ) || &zdie($MSG_PASSTHRU,"Error checking free space of file system."); This gives this error: Two-argument "open" used at line xxx, column x. See page 207 of PBP. (Severity: 5) Any ideas on how to fix it? 回答1: To make Perl Critic shut up, but do no real good at all, just modify the code to: open(my $PIPE_FROM_FREESPCHK, "-|", $cmdline) || zdie($MSG_PASSTHRU,

How do I make Perl::Critic show the offending policy in its output?

喜你入骨 提交于 2019-12-21 20:28:53
问题 I've been told it's possible to have Perl::Critic show the policy name that is offending in its output. But I can't recall what I have to do to turn this on. How can this be enabled in a perlcritic.rc ? 回答1: The --verbose command-line option can control this-- if you don't want to specify your own format, try the values 8 or 9 . Both of these built-in formats contain the policy name. If you like one of them, you can add this line to your .perlcriticrc : verbose = 8 (Or 9, if you prefer that

Is there a better way to write Perl regexes with /x so the code is still easy to read?

我的梦境 提交于 2019-12-12 08:38:00
问题 I ran Perl::Critic on one of my scripts, and got this message: Regular expression without "/x" flag at line 21, column 26. See page 236 of PBP. I looked up the policy information here, and I understand that writing regular expressions in extended mode will help anyone who is looking at the code. However, I am stuck as how to convert my code to use the /x flag. CPAN Example: # Match a single-quoted string efficiently... m{'[^\\']*(?:\\.[^\\']*)*'}; #Huh? # Same thing with extended format... m{

Is it better to croak() or to die() when something bad happens in Perl?

天大地大妈咪最大 提交于 2019-12-08 14:42:13
问题 perlcritic complaints that the following code, some boilerplate DBI stuff that works perfectly fine, should croak instead of die: # Connect to database my $db_handle = DBI->connect( $url, $user, $password ) or die $DBI::errstr; All this, while die seems to work fine for me. I would think for a samurai Perl warrior, croak is less honorable than actually die when things go awry. Jokes apart Why should I croak instead of die ? What are the consequences of not heeding perlcritic's advice? 回答1:

Should a subroutine always return explicitly?

假如想象 提交于 2019-12-07 19:46:33
问题 If perlcritic says "having no returns in a sub is wrong", what is the alternative if they really aren't needed? I've developed two apparently bad habits: I explicitly assign variables to the '$main::' namespace. I then play with those variables in subs. For example, I might do.. #!/usr/bin/perl use strict; use warnings; @main::array = (1,4,2,6,1,8,5,5,2); &sort_array; &push_array; &pop_array; sub sort_array{ @main::array = sort @main::array; for (@main::array){ print "$_\n"; } } sub push

Should a subroutine always return explicitly?

只谈情不闲聊 提交于 2019-12-06 14:28:48
If perlcritic says "having no returns in a sub is wrong", what is the alternative if they really aren't needed? I've developed two apparently bad habits: I explicitly assign variables to the '$main::' namespace. I then play with those variables in subs. For example, I might do.. #!/usr/bin/perl use strict; use warnings; @main::array = (1,4,2,6,1,8,5,5,2); &sort_array; &push_array; &pop_array; sub sort_array{ @main::array = sort @main::array; for (@main::array){ print "$_\n"; } } sub push_array{ for ( 1 .. 9 ){ push @main::array, $_; } } sub pop_array { for ( 1 .. 3 ){ pop @main::array; } } I