kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use Perl::Critic module as part of my code review to check if there are any violations in the coding standards

use strict; use Perl::Critic qw(critique); my ($FILE) = $ARGV[0]; # Use custom parameters... my @violations = critique( {-format => 1}, $FILE ); foreach ( @violations ) { print $_,"\n"; }

when I run the above code, It is giving me the following errors

Local lexical variable "$PostTaxVLCCD" is not all lower case or all up +per case at line 721, column 5. See pages 45,46 of PBP. Local lexical variable "$TotalTaxVLCCD" is not all lower case or all u +pper case at line 721, column 5. See pages 45,46 of PBP. Local lexical variable "$PreTaxVLCCD" is not all lower case or all upp +er case at line 721, column 5. See pages 45,46 of PBP. Local lexical variable "$SDRPostTax" is not all lower case or all uppe +r case at line 721, column 5. See pages 45,46 of PBP. Local lexical variable "$Tax" is not all lower case or all upper case +at line 721, column 5. See pages 45,46 of PBP. Local lexical variable "$SDRPreTax" is not all lower case or all upper + case at line 721, column 5. See pages 45,46 of PBP.

When checking the error messages, it is showing like "See pages 45,46 of PBP."....But I want the Severity of the error thrown in the messages I've got. How can I achieve that..just like below in the Perl::Critic documentation

Verbosity Format Specification ----------- ---------------------------------------------------- +--- 1 "%f:%l:%c:%m\n", 2 "%f: (%l:%c) %m\n", 3 "%m at %f line %l\n", 4 "%m at line %l, column %c. %e. (Severity: %s)\n", 5 "%f: %m at line %l, column %c. %e. (Severity: %s)\ +n", 6 "%m at line %l, near '%r'. (Severity: %s)\n", 7 "%f: %m at line %l near '%r'. (Severity: %s)\n", 8 "[%p] %m at line %l, column %c. (Severity: %s)\n", 9 "[%p] %m at line %l, near '%r'. (Severity: %s)\n", 10 "%m at line %l, column %c.\n %p (Severity: %s)\n%d\ +n", 11 "%m at line %l, near '%r'.\n %p (Severity: %s)\n%d\ +n"

Regards, Kalyan

Original content restored above by GrandFather

I'm trying to use the perlcritic command line utility and is outputting all the violations inside my file, How can I distinguish each and every section of the violations. My intention is to parse the output of the perlcritic violations, assign some HTML tags and colors and mail them to the concerned user for fixing the issues.. How can I achieve that?

Replies are listed 'Best First'.
Re: Perl::Critic usage
by FalseVinylShrub (Chaplain) on Sep 25, 2009 at 06:04 UTC

    Hi.

    I'm not able to test it, as I don't have Perl Critic installed, but...

    Firstly have you considered using the perlcritic command line tool?

    Secondly, whether using command line or the module, I think you've used the parameter name format instead of verbose.

    Finally, the number 1 that you send to format/verbose doesn't seem to include the severity that you want. It seems from the documentation that you need to use a number 4 or greater. However, I'm not sure if those numbers apply to the module or just the command line, but you could you include the literal format specification.

    In short, try changing your script to this:

    use strict; use warnings; use Perl::Critic qw(critique); my ($FILE) = $ARGV[0]; # Use custom parameters... # # If 4 doesn't work for verbose, try putting the string # "%m at line %l, column %c. %e. (Severity: %s)\n" # (or whatever you want) in its place. my @violations = critique( { -verbose => 4 }, $FILE ); foreach ( @violations ) { print $_,"\n"; }

    Sorry for the formatting, not used to editing code in a web form...

    Hope this helps. Can't be sure because I can't test it. Post and say if it helps or not, and someone else who knows better may come along...

    FVS

    Update: PS. I noticed you're using strict, but not warnings. It's good style to use both and it may help you find errors.

      I'm trying to use the perlcritic command line utility and is outputting all the violations inside my file, How can I distinguish each and every section of the violations. My intention is to parse the output of the perlcritic violations, assign some HTML tags and colors and mail them to the concerned user for fixing the issues.. How can I achieve that?

        I see. Perhaps it is better to use the Perl::Critic module, if you are going to do further processing.

        As for distinguishing the violations, there are ways to set policies as to which to report, but I can't think of any way to explain them better than the documentation.

        You could make the violations easier to parse by specifying your own format string - comma separated or something.

        I don't think I can help more without getting into great details about your requirements, sorry.

        FVS.

Re: Perl::Critic usage
by toolic (Bishop) on Sep 25, 2009 at 12:52 UTC
    Since I am barely a Perl::Critic novice, I am not sure what you are asking. I recently decided I wanted to sort all violations by severity, and I created this little script to run perlcritic. It does not depend on a user-defined .perlcriticrc file, and it works OK for a 1-year-old version of Perl::Critic (1.092). There is undoubtedly a better way to do it, but it's a start. The POD is self-explanatory.
    #!/usr/bin/env perl =head1 Name B<pcsort> - Sort perlcritic 'brutal' output by severity =head1 Description Input is a Perl script or module. Prints to STDOUT. Example: pcsort foo.pl =cut use warnings FATAL => 'all'; use strict; use Data::Dumper; my $file = shift; my @aoa; my @lines = `perlcritic -1 $file`; chomp @lines; for (@lines) { if (/ Severity : \s+ (\d) /x) { my $sev = $1; push @{ $aoa[$sev] }, $_; } } print Dumper(\@aoa);
Getting Perl::Critic to emit violation information in the format that you want.
by elliot (Novice) on Sep 26, 2009 at 08:24 UTC

    You can get the all the violation data in any format you want. Use the --verbose parameter and specify a sprintf-style format for what you want.

    For example, you could use perlcritic --verbose '%p*%s*%f*%l' to get the shortened policy name, severity, file path, and line number separated by asterisks.

    You could even consider specifying HTML tags in the format you give, but I wouldn't do that: Perl::Critic doesn't know anything about escaping special characters. You'll need to specify the output in some form that is easy for you to parse and then escape that before combining it with your markup.

    See Perl::Critic::Violation for a description of all the possible sprintf formats you can use plus more examples.

    Alternatively, you could use Perl::Critic::critique() and get the Violation objects yourself and do what you want with the attributes directly.