• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
An Introduction To Perl Critic
 

An Introduction To Perl Critic

on

  • 20,309 views

Josh McAdams presents an introduction to Perl Critic at the Nordic Perl workshop 2007.

Josh McAdams presents an introduction to Perl Critic at the Nordic Perl workshop 2007.

Statistics

Views

Total Views
20,309
Views on SlideShare
20,267
Embed Views
42

Actions

Likes
5
Downloads
215
Comments
2

4 Embeds 42

http://www.slideshare.net 31
http://thomas-fahle.blogspot.com 9
http://209.85.135.104 1
http://www.fachak.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    An Introduction To Perl Critic An Introduction To Perl Critic Presentation Transcript

    • An Introduction To Perl Critic Nordic Perl Workshop 2007 An Introduction To Perl Critic Josh McAdams
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 What is Perl Critic? - Think 'use warnings' or 'use strict' ... only more (or less) - Why more? ... Perl Critic looks beyond mere syntatical correctness - for instance, are you using CamelCase sub names? - Why less? ... Perl Critic doesn't actually compile your code - Your code is parsed as a 'document' thanks to PPI - Invalid code could possibly pass a critique
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 A Basic Example #!/usr/bin/perl print “Hello, Denmark ”; ... okay, a VERY basic example Here's the code...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 A Basic Example --(0)> perlcritic hello_denmark.pl Code before strictures are enabled at line 3, column 1. See page 429 of PBP. (Severity: 5) ... what, we have problems? So, let's run perlcritic...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 A Basic Example --(0)> perlcritic hello_denmark.pl Code before strictures are enabled at line 3, column 1. See page 429 of PBP. (Severity: 5) What just happened... We ran the 'perlcritic' program and it complained about a violation in our code at a specific line and column and even told us where in PBP we can find why this is bad code! and how bad our code is
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Perl Critic Policies - What is Perl Critic judging our code on? ... Policies ... in this case TestingAndDebugging::RequireUseStrict - What are Policies? ... rules that perl critic enforces ... perl critic is packaged with many ... the can be big things that are probably bugs (or example) ... or little things that are mostly cosmetic ... found in the Perl::Critic::Policy namespace - Who comes up with these Policies? ... the Perl Critic maintainers ... Perl Critic extension authors ... You ... let's see an example of a cosmetic policy
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 A Cosmetic Example #!/usr/bin/perl use strict; use warnings; sub CamelCaseSub { print "Hey, I'm a CamelCase subroutine "; return; } CamelCaseSub(); Here's the code... ... any idea of what the error will be?
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 A Cosmetic Example --(0)> perlcritic -severity 1 cosmetic.pl Use Emacs file variables to declare coding style at line 1, column 1. Emacs can read per-file settings. (Severity: 2) RCS keywords $Id$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2) RCS keywords $Revision$, $HeadURL$, $Date$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2) RCS keywords $Revision$, $Source$, $Date$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2) Missing Perl version at line 1, column 1. Add "use v5.6.0" or similar. (Severity: 1) No "VERSION" variable found at line 1, column 1. See page 404 of PBP. (Severity: 2) Mixed-case subroutine name at line 6, column 1. See page 44 of PBP. (Severity: 1) ... ugh, that's more than we expected? So, let's run perlcritic...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Policy Severity - Who opened the flood gates? ... we did, by asking for a new severity - What are severities? ... weights assigned to policies ... most severe violation = 5 ... least severe violation = 1 - Who assigns these severities? ... module authors ... if you disagree, you can change the severity
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Severity Levels - What are the severity levels? 5 = gentle (the only one checked by default) 4 = stern 3 = harsh 2 = cruel 1 = brutal - When you request any severity you get all severities above that level - You can request a severity level by number or by name - It can be helpful to start examining your code at gentle and work your way down
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Another Example #!/usr/bin/perl -*- cperl -*- # $Id$ use strict; use warnings; use Config; our $VERSION = 1; print "Hipster " if $Config{'osname'} eq 'darwin'; Here's the code... ... any idea of what the error will be?
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Another Example --(0)> perlcritic -severity 2 trailing_if.pl Postfix control "if" used at line 11, column 19. See pages 93,94 of PBP. (Severity: 2) ... but what if I like the trailing if? So, let's run perlcritic...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Excluding Policies --(0)> perlcritic -2 -exclude ProhibitPostfixControls trailing_if.pl trailing_if.pl source OK ... that's what I wanted, but what a PITA I can exclude policies that I disagree with on the command line...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Persistent Configuration - How about making my preferences persistent? ... sure, just create a .perlcriticrc file - .perlcriticrc? ... it's just a configuration file with your preferences ... basically an INI file format ... place it in your home directory ... or specify it on the command line ... let's see a file
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Persistent Configuration --(0)> cat ~/.perlcriticrc exclude = ControlStructures::ProhibitPostfixControls ... an admittingly lame configuration file Let's see the file...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Persistent Configuration --(0)> perlcritic -2 trailing_if.pl trailing_if.pl source OK ... let's see something cooler But it does the trick...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Persistent Configuration --(0)> cat ~/.perlcriticrc severity = 2 top = 5 exclude = Editor::RequireEmacsFileVariables Miscellanea::RequireRcsKeywords [ControlStructures::ProhibitPostfixControls] severity = 4 allow = if unless theme = iffy Here's a more interesting file... ... theme? The default severity to report How many violations to report Policies to ignore Change the default severity for a policy Pass arguments to the policy constructor, in this case telling it to allow trailing ifs and unlesses Apply a new theme
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Themes - What are these themes you speak of? ... similar to tags or labels ... think del.icio.us ... themes classify Policies and allow them to be grouped together for purposes of running or excluding - What are some common themes? core = the policies that come packaged with Perl Critic pbp = policies that apply to Perl Best Practices bugs = policies that typically indicate bugs in your code ... there are many/infinitely more, just look at the policy docs
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Specifying Themes --(0)> perlcritic -theme '(pbp || (bugs && security)) && !cosmetic' trailing_if.pl trailing_if.pl source OK ... do a 'perlcritic -list' to see all policies, themes, and severities Let's specify themes to look for...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 I Want To Be Good, I Just Can't - What happens if I want to respect a policy, but for some reason can't in this one instance? ... for instance sub-classing DBI and overriding 'connect' package DBIxUseless; use warnings; use strict; use base qw(DBI); our $VERSION = 1; sub connect { return shift->SUPER::connect(@_) } 1;
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 But He Made Me Do It --(0)> perlcritic DBIxUseless.pm Subroutine name is a homonym for builtin function at line 9, column 1. See page 177 of PBP. (Severity: 4) ... doh, I can't help it, I have to override 'connect' Let's see what happens...
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 A Pseudo-Solution package DBIxUseless; use warnings; use strict; use base qw(DBI); our $VERSION = 1; sub connect { ## no critic (ProhibitBuiltinHomonyms) return shift->SUPER::connect(@_) } 1; Pseudo-pragma allows for you to turn off all or specific criticisms for individual lines of code or blocks of code (in this case)
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 A Pseudo-Solution #!/usr/bin/perl use warnings; use strict; ## no critic (ProhibitPostfixControls) print “Hi ” if $^O eq 'darwin'; print “Hello ” if $^O =~ /win/i; ## use critic - This even works with themes You can also create sections of your code that are not meant for critique
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Review - That is Perl Critic in a nutshell ... the system consists of a bunch of policies ... that have a name ... and a severity ... and one (maybe zero) or more themes ... the system can be configured ... by selectively blocking off criticism for specific code ... by ignoring policies ... by changing the severity of policies ... by looking a specific themes ... by tweaking the policies ... severity ... themes ... configuration ... but you still have to remember to run perlcritic, unless
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Criticism Pragma - You use the criticism pragma package DBIxUseless; use warnings; use strict; use base qw(DBI); use criticism; our $VERSION = 1; sub connect { return shift->SUPER::connect(@_) } 1;
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Criticism Pragma - The criticism pragma ... runs Perl Critic every time you run your code ... keeps you from having to remember to run perlcritic ... creates additional runtime overhead ... is easy to accidentally leave in your production code ... there has to be a better way
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 Test::Perl::Critic - Test::Perl::Critic ... criticism in your test suite ... now when you run 'make test', you get Perl Critic ... but it's not good to distribute these test in public modules ... these should be for development only ... you don't want your modules not installing because of Perl Critic use Test::Perl::Critic; all_critic_ok();
    • An Introduction To Perl Critic Nordic Perl Workshop 2007 In Review - Perl Critic is ... a highly-configurable static source code analyzer ... a tool to help you write better code ... a reminder of good coding practices ... an easily extendable system (see tomorrows talk) ... a development aid, not a distribution dependency ... not a guarantee that you'll write quality code