Quick Upload

Loading...
Flash Player 9 (or above) is needed to view slideshows. We have detected that you do not have it on your computer.To install it, go here
Post to Twitter Post to Twitter
Share on Facebook
Myspace Hi5 Friendster Xanga LiveJournal Facebook Blogger Tagged Typepad Freewebs BlackPlanet gigya icons

An Introduction To Perl Critic

from joshua.mcadams, 2 years ago Add as contact

2878 views | 0 comments | 3 favorites | 2 embeds (Stats)

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

Embed customize close
 

More Info

This slideshow is Public

Views: 2878 Comments: 0 Favorites: 3 Downloads: 103

View Details: 2868 on Slideshare 10 from embeds
Most viewed embeds (Top 5): More
Flagged as inappropriate Flag as inappropriate

Flag as inappropriate

Select your reason for flagging this slideshow as inappropriate.

If needed, use the feedback form to let us know more details.

Slideshow Transcript

  1. Slide 1: An Introduction To Perl Critic Nordic Perl Workshop 2007 An Introduction To Perl Critic Josh McAdams
  2. Slide 2: 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
  3. Slide 3: An Introduction To Perl Critic Nordic Perl Workshop 2007 A Basic Example Here's the code... #!/usr/bin/perl print “Hello, Denmark ”; ... okay, a VERY basic example
  4. Slide 4: An Introduction To Perl Critic Nordic Perl Workshop 2007 A Basic Example So, let's run perlcritic... --(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?
  5. Slide 5: An Introduction To Perl Critic Nordic Perl Workshop 2007 A Basic Example What just happened... and it complained about a violation in our code at a specific line We ran the 'perlcritic' program and column --(0)> perlcritic hello_denmark.pl Code before strictures are enabled at line 3, column 1. See page 429 of PBP. (Severity: 5) and how bad and even told us where in PBP we can find our code is why this is bad code!
  6. Slide 6: 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
  7. Slide 7: An Introduction To Perl Critic Nordic Perl Workshop 2007 A Cosmetic Example Here's the code... #!/usr/bin/perl use strict; use warnings; sub CamelCaseSub { print "Hey, I'm a CamelCase subroutine "; return; } CamelCaseSub(); ... any idea of what the error will be?
  8. Slide 8: An Introduction To Perl Critic Nordic Perl Workshop 2007 A Cosmetic Example So, let's run perlcritic... --(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?
  9. Slide 9: 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
  10. Slide 10: 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
  11. Slide 11: An Introduction To Perl Critic Nordic Perl Workshop 2007 Another Example Here's the code... #!/usr/bin/perl -*- cperl -*- # $Id$ use strict; use warnings; use Config; our $VERSION = 1; print "Hipster " if $Config{'osname'} eq 'darwin'; ... any idea of what the error will be?
  12. Slide 12: An Introduction To Perl Critic Nordic Perl Workshop 2007 Another Example So, let's run perlcritic... --(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?
  13. Slide 13: An Introduction To Perl Critic Nordic Perl Workshop 2007 Excluding Policies I can exclude policies that I disagree with on the command line... --(0)> perlcritic -2 -exclude ProhibitPostfixControls trailing_if.pl trailing_if.pl source OK ... that's what I wanted, but what a PITA
  14. Slide 14: 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
  15. Slide 15: An Introduction To Perl Critic Nordic Perl Workshop 2007 Persistent Configuration Let's see the file... --(0)> cat ~/.perlcriticrc exclude = ControlStructures::ProhibitPostfixControls ... an admittingly lame configuration file
  16. Slide 16: An Introduction To Perl Critic Nordic Perl Workshop 2007 Persistent Configuration But it does the trick... --(0)> perlcritic -2 trailing_if.pl trailing_if.pl source OK ... let's see something cooler
  17. Slide 17: An Introduction To Perl Critic Nordic Perl Workshop 2007 Persistent Configuration Here's a more interesting file... The default severity to report --(0)> cat ~/.perlcriticrc How many violations to severity = 2 report top = 5 exclude = Editor::RequireEmacsFileVariables Miscellanea::RequireRcsKeywords [ControlStructures::ProhibitPostfixControls] Policies to ignore severity = 4 allow = if unless theme = iffy Change the default severity for a policy Pass arguments to the policy constructor, in this case telling it to allow trailing ifs and unlesses ... theme? Apply a new theme
  18. Slide 18: 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
  19. Slide 19: An Introduction To Perl Critic Nordic Perl Workshop 2007 Specifying Themes Let's specify themes to look for... --(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
  20. Slide 20: 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;
  21. Slide 21: An Introduction To Perl Critic Nordic Perl Workshop 2007 But He Made Me Do It Let's see what happens... --(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'
  22. Slide 22: An Introduction To Perl Critic Nordic Perl Workshop 2007 A Pseudo-Solution package DBIxUseless; Pseudo-pragma allows for you to turn off all or specific use warnings; criticisms for individual lines of code or blocks of code (in this use strict; case) use base qw(DBI); our $VERSION = 1; sub connect { ## no critic (ProhibitBuiltinHomonyms) return shift->SUPER::connect(@_) } 1;
  23. Slide 23: An Introduction To Perl Critic Nordic Perl Workshop 2007 A Pseudo-Solution #!/usr/bin/perl You can also create sections of use warnings; your code that are not meant for critique use strict; ## no critic (ProhibitPostfixControls) print “Hi ” if $^O eq 'darwin'; print “Hello ” if $^O =~ /win/i; ## use critic - This even works with themes
  24. Slide 24: 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
  25. Slide 25: 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;
  26. Slide 26: 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
  27. Slide 27: 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();
  28. Slide 28: 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