Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Preprocessor Extensibility in SquishIt 0.9Taming RSS Feeds with XML::RSS and Template::Toolkit »
    comments

    Create HTML from output of Perl::Critic

    by Rob Earl on Sep 22, 2012 in categories Perl

    Perl::Critic is great. If you haven't tried it, you should. It can help you improve the quality of your code no end.

    I wrote this script to make it easier to view the feedback in the context of the code itself. When run in a directory containing Perl code it'll create a critic_html directory containing index.html summarizing the results:

    critic_html/index.html

    Clicking through to any of the files will give you all the violations found by Perl::Critic, inline with the code:

    In addition to Perl::Critic, the script uses Template::Toolkit to format the output. The code below and templates can be found in the attached critic_html.zip

    1. #!/usr/bin/perl
    2.  
    3. use strict;
    4. use warnings;
    5.  
    6. use Perl::Critic;
    7. use Perl::Critic::Utils;
    8.  
    9. use Template;
    10. use Cwd qw(abs_path);
    11. use File::Basename;
    12. use English qw(-no_match_vars);
    13.  
    14. use constant SEVERITY  => 1; # Include all violations.
    15. use constant SKIP_GOOD => 0; # Skip files with no violations?
    16.  
    17. mkdir 'critic_html';
    18. mkdir 'critic_html/src';
    19.  
    20. use autodie;
    21.  
    22. # Analyse all perl files below the current directory.
    23. my @files = Perl::Critic::Utils::all_perl_files(q{.});
    24.  
    25. my @summary = (); # Store statistics on each file processed.
    26.  
    27. foreach my $file (@files) {
    28.     # Create a new Critic for per file statistics.
    29.     my $critic = Perl::Critic->new( '-severity' => SEVERITY );
    30.  
    31.     my $file_safe = $file;
    32.     $file_safe =~ s/[\W]/_/g;
    33.  
    34.     my @violations = $critic->critique($file);
    35.     next if (!@violations && SKIP_GOOD);
    36.  
    37.     push @summary, { 'filename' => $file,
    38.                      'link'     => "src/$file_safe.html",
    39.                      'stats'    => $critic->statistics() };
    40.  
    41.     open my $FH, '<', $file;
    42.     my @lines = <$FH>;
    43.     close $FH;
    44.  
    45.     # Attach all violations to the line they were found on.
    46.     my @violations_by_line = ();
    47.     my $line_number = 1;
    48.     foreach my $line (@lines) {
    49.         # Get all the violations for the current line.
    50.         my @line_violations = grep { $_->line_number() == $line_number} @violations;
    51.         push @violations_by_line, { 'number'  => $line_number,
    52.                                     'content' => $line,
    53.                                     'violations' => \@line_violations };
    54.         $line_number++;
    55.     }
    56.  
    57.     write_html("critic_html/src/$file_safe.html", 'codefile', { 'title' => "Critic Analysis of $file",
    58.                                                                 'lines' => \@violations_by_line } );
    59. }
    60.  
    61. write_html('critic_html/index.html', 'index', { 'title' => 'Perl::Critic::HTML Summary',
    62.                                                 'files' => \@summary });
    63.  
    64. sub write_html {
    65.     my ($filename, $template, $data) = @_;
    66.  
    67.     # Include templates from the install directory.
    68.     my $tt = Template->new({ 'INCLUDE_PATH' => dirname(abs_path($PROGRAM_NAME)).'/templates' } );
    69.     print "Writing $filename\n";
    70.     open my $FILE, '>', $filename;
    71.     $tt->process($template, $data, $FILE);
    72.     close $FILE;
    73.  
    74.     return;
    75. }
    1866 views
    InstapaperVote on HN

    1 comment

    Comment from: Jeff Thalhammer [Visitor]
    Jeff Thalhammer Nice work -- this looks really sharp! I have lots of ideas to improve the perlcritic.com website, including inline violation reporting, like yours. Get in touch with me if you'd like to collaborate.
    09/23/12 @ 10:45

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)