8

I'm trying to exclude checks of Perl Critic's RequireRcsKeywords in a single Perl script. I don't want to change my default policy in .perlcriticrc so I added a "no critic" line to the top of the source. Despite that change, Perl Critic still complains about the lack of RCS keywords.

Here is my test case (critictest.pl):

#!/usr/bin/perl
## no critic (RequireRcsKeywords)
use warnings;
use strict;
print "Hello, World.\n";

When I execute perlcritic -1 --verbose 8 critictest.pl I get the following output:

[Miscellanea::RequireRcsKeywords] RCS keywords $Id$ not found at line 1, column 1.  (Severity: 2)
[Miscellanea::RequireRcsKeywords] RCS keywords $Revision$, $HeadURL$, $Date$ not found at line 1, column 1.  (Severity: 2)
[Miscellanea::RequireRcsKeywords] RCS keywords $Revision$, $Source$, $Date$ not found at line 1, column 1.  (Severity: 2)
[Modules::RequireVersionVar] No package-scoped "$VERSION" variable found at line 1, column 1.  (Severity: 2)
[Miscellanea::ProhibitUselessNoCritic] Useless '## no critic' annotation at line 2, column 1.  (Severity: 2)
[InputOutput::RequireCheckedSyscalls] Return value of flagged function ignored - print at line 5, column 1.  (Severity: 1)

I know that Perl Critic is working because if I add ## no critic (RequireCheckedSyscalls) then that error in the output goes away. I also tried adding `## no critic (Miscellanea::RequireRcsKeywords) but that didn't cause any change. What is the correct way to tell Perl Critic to ignore the RequireRcsKeywords policy in my file without having to use an external policy file?

EDIT: I'm using Perl 5.10.1, Perl Critic 1.108, and Debian 6.0.3.

Starfish
  • 1,083
  • 10
  • 23

4 Answers4

4

You can add one-off adjustments to your .perlcriticrc file with --include and --exclude on the command line.

$ perlcritic --exclude RcsKeywords -1 --verbose 8 critictest.pl
[Modules::RequireVersionVar] No package-scoped "$VERSION" variable found at line 1, column 1.  (Severity: 2)
[InputOutput::RequireCheckedSyscalls] Return value of flagged function ignored - print at line 4, column 1.  (Severity: 1)
mob
  • 117,087
  • 18
  • 149
  • 283
2

Notice that the violation occurs on Line 1 of your file. If I delete your first line, I do not get the RCS violation. I suspect that this policy applies to the entire file and can only be ignored if the no critic pragma appears on the first line of your file.

Note also that it is telling you it is ignoring your pragma:

[Miscellanea::ProhibitUselessNoCritic] Useless '## no critic' annotation at line 2, column 1.  (Severity: 2)
toolic
  • 57,801
  • 17
  • 75
  • 117
  • Thanks for pointing that out. I had missed it. Deleting the first line does now let Perl Critic ignore the policy. Unfortunately, I can no longer run the command without prefixing it with `perl` because the `#!` is gone. – Starfish Feb 07 '12 at 19:09
  • 1
    Submit a request for this behavior: http://rt.cpan.org/NoAuth/Bugs.html?Dist=Perl-Critic – toolic Feb 07 '12 at 19:23
2

You can put both the shebang and the "no critic" annotation on the first line. Like this:

#!/usr/bin/perl -- ## no critic RequireRcsKeywords
Matteo
  • 14,696
  • 9
  • 68
  • 106
  • This makes the shebang line fail: `Can't open perl script "## no critic RequireRcsKeywords": No such file or directory` – mob Feb 07 '12 at 20:29
  • 3
    You could say `#!/usr/bin/perl -- ## no critic ...`, though (I think). – mob Feb 07 '12 at 20:35
0

A hack might be to use the following first line:

#!/usr/bin/perl -F## no critic (Miscellanea::RequireRcsKeywords)

The documentation says the -F flag doesn't do anything by itself so it should be harmless.

Marius Olsthoorn
  • 405
  • 3
  • 10