‎ Perl::Critic and Perl::Tidy Your guide to better programming practices.
What is Perl::Tidy and Perl::Critic? Perl::Tidy  a script which indents and reformats Perl code to make it easier to read.  Perl::Critic  is basically a source code analyzer, that applies best practices to your code.
Perl::Tidy Take the following code: if ( $toOrCC =~ /(sometest)/i  || $subject =~ /Some_other_test/i ) { my $code=&quot;here&quot;; if($var >=1 && $var <= 5 )  { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy Take the following code: if ( $toOrCC =~ /(sometest)/i  || $subject =~ /Some_other_test/i ) { my $code=&quot;here&quot;; if($var >=1 && $var <= 5 )  { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } } PRETTY UGLY HUH? Lets see what Perl::Tidy can do!
Perl::Tidy Default Perl::Critic behavior: if (  $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy For most individuals this is good enough
Perl::Tidy For most individuals this is good enough However some groups of coders have certain coding standards that are different from the default perl::Critic behavior
Perl::Tidy Take the following code: if ( $toOrCC =~ /(sometest)/i  || $subject =~ /Some_other_test/i ) { my $code=&quot;here&quot;; if($var >=1 && $var <= 5 )  { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy Perltidy -ce # (Cuddled Elses) if (  $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy You can change the indenting level with -i=n This is -i=2 if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy You can change the indenting level with -i=n This is -i=20 if (  $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy You can have the opening braces on the left: This is -bl if (  $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy You can have the opening braces on the left: This is -bli if (  $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy Another formatting option: This is -bli -i=2 if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
Perl::Tidy Continuation Indentation -ci=14  Changes  : my $filename = $home_dir.&quot;/&quot;.$username.&quot;/&quot;.$projectfile.&quot;/&quot;.$file; To: my $filename = $home_dir . &quot;/&quot; . $username . &quot;/&quot; . $projectfile . &quot;/” . $file; Uses the line length variable set with -l=n
Perl::Tidy ~/.perltidyrc All the options of perltidy can be put into a config file One option per line # is a comment line  My conf file is two lines: -i=4 # indent 4 -bli  # new blocks get a new line and are indented Ignore the config file with -npro or set a different config file with -pro=filename
Perl::Tidy Other formatting options can be researched at  http://perltidy.sourceforge.net/stylekey.html -ole=s  specify output line ending (s=dos or win, mac, unix) -o=file name of the output file (only if single input file) -oext=s change output extension from 'tdy' to s -opath=path  change path to be 'path' for output files -b  backup original to .bak and modify file in-place -bext=s change default backup extension from 'bak' to s -q  deactivate error messages (for running under editor) -w  include non-critical warning messages in the .ERR error output -syn  run perl -c to check syntax (default under unix systems) -log  save .LOG file, which has useful diagnostics
Perl::Tidy Other formatting options can be researched at  http://perltidy.sourceforge.net/stylekey.html -f  force perltidy to read a binary file -g  like -log but writes more detailed .LOG file, for debugging scripts -opt  write the set of options actually used to a .LOG file -st  send output to standard output, STDOUT -se  send error output to standard error output, STDERR -v  display version number to standard output and quit
Perl::Critic What is it, What is it good for?
Perl::Critic Help you find in order of severity from highest to lowest: Bugs Bad practices Cases where you are not complying with common style practices Readability issues Perl::Critic Rates violations from 1 to 5 with five being the most serious violations
Perl::Critic How it works: %  perlcritic goodcode.pl   goodcode.pl source OK %  perlcritic badcode.pl Code before strictures are enabled at line 4, column 1.  See page 429 of PBP.  (Severity: 5)
Perl::Critic Perl::Critic options/features: Default severity is 5, you can change it with the flag -n where n=1..5
Perl::Critic Perl::Critic options/features : Default severity is 5, you can change it with the flag -n where n=1..5 -top 7 will show you the top 7 code violations
Perl::Critic Perl::Critic options/features: Default severity is 5, you can change it with the flag -n where n=1..5 -top 7 will show you the top 7 code violations ~/.perlcriticrc Change severity of policies [ControlStructures::ProhibitPostfixControls] severity = cruel  # Same as &quot;severity = 2” Change dis/allowed policies: [ControlStructures::ProhibitPostfixControls]  allow = if unless
Perl::Critic Perl::Critic options/features: ~/.perlcriticrc Turn off specific policies: [-NamingConventions::ProhibitMixedCaseVars]  [-NamingConventions::ProhibitMixedCaseSubs]
Perl::Critic Perl::Critic options/features: Turn off Perl::Critic for a single line: open(FILE,”>$path”); ## no critic Turn off Perl::Critic for a number of lines: ## no critic open(FILE,”>$path”); ## use critic These can be ignored with -force
Perl::Critic You  can create a ‘.t’ file to run as you ‘make test’ ~/.perlcriticrc -profile=.perlcriticrc Uses Module::Pluggable so the policies are easily loaded (or not loaded)!

Perl Tidy Perl Critic

  • 1.
    ‎ Perl::Critic andPerl::Tidy Your guide to better programming practices.
  • 2.
    What is Perl::Tidyand Perl::Critic? Perl::Tidy a script which indents and reformats Perl code to make it easier to read. Perl::Critic is basically a source code analyzer, that applies best practices to your code.
  • 3.
    Perl::Tidy Take thefollowing code: if ( $toOrCC =~ /(sometest)/i || $subject =~ /Some_other_test/i ) { my $code=&quot;here&quot;; if($var >=1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 4.
    Perl::Tidy Take thefollowing code: if ( $toOrCC =~ /(sometest)/i || $subject =~ /Some_other_test/i ) { my $code=&quot;here&quot;; if($var >=1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } } PRETTY UGLY HUH? Lets see what Perl::Tidy can do!
  • 5.
    Perl::Tidy Default Perl::Criticbehavior: if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 6.
    Perl::Tidy For mostindividuals this is good enough
  • 7.
    Perl::Tidy For mostindividuals this is good enough However some groups of coders have certain coding standards that are different from the default perl::Critic behavior
  • 8.
    Perl::Tidy Take thefollowing code: if ( $toOrCC =~ /(sometest)/i || $subject =~ /Some_other_test/i ) { my $code=&quot;here&quot;; if($var >=1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 9.
    Perl::Tidy Perltidy -ce# (Cuddled Elses) if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 10.
    Perl::Tidy You canchange the indenting level with -i=n This is -i=2 if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 11.
    Perl::Tidy You canchange the indenting level with -i=n This is -i=20 if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 12.
    Perl::Tidy You canhave the opening braces on the left: This is -bl if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 13.
    Perl::Tidy You canhave the opening braces on the left: This is -bli if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 14.
    Perl::Tidy Another formattingoption: This is -bli -i=2 if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = &quot;here&quot;; if ( $var >= 1 && $var <= 5 ) { my $other_code = &quot;here&quot;; } else { my $other_code = &quot;here&quot;; } }
  • 15.
    Perl::Tidy Continuation Indentation-ci=14 Changes : my $filename = $home_dir.&quot;/&quot;.$username.&quot;/&quot;.$projectfile.&quot;/&quot;.$file; To: my $filename = $home_dir . &quot;/&quot; . $username . &quot;/&quot; . $projectfile . &quot;/” . $file; Uses the line length variable set with -l=n
  • 16.
    Perl::Tidy ~/.perltidyrc Allthe options of perltidy can be put into a config file One option per line # is a comment line My conf file is two lines: -i=4 # indent 4 -bli # new blocks get a new line and are indented Ignore the config file with -npro or set a different config file with -pro=filename
  • 17.
    Perl::Tidy Other formattingoptions can be researched at http://perltidy.sourceforge.net/stylekey.html -ole=s specify output line ending (s=dos or win, mac, unix) -o=file name of the output file (only if single input file) -oext=s change output extension from 'tdy' to s -opath=path change path to be 'path' for output files -b backup original to .bak and modify file in-place -bext=s change default backup extension from 'bak' to s -q deactivate error messages (for running under editor) -w include non-critical warning messages in the .ERR error output -syn run perl -c to check syntax (default under unix systems) -log save .LOG file, which has useful diagnostics
  • 18.
    Perl::Tidy Other formattingoptions can be researched at http://perltidy.sourceforge.net/stylekey.html -f force perltidy to read a binary file -g like -log but writes more detailed .LOG file, for debugging scripts -opt write the set of options actually used to a .LOG file -st send output to standard output, STDOUT -se send error output to standard error output, STDERR -v display version number to standard output and quit
  • 19.
    Perl::Critic What isit, What is it good for?
  • 20.
    Perl::Critic Help youfind in order of severity from highest to lowest: Bugs Bad practices Cases where you are not complying with common style practices Readability issues Perl::Critic Rates violations from 1 to 5 with five being the most serious violations
  • 21.
    Perl::Critic How itworks: % perlcritic goodcode.pl goodcode.pl source OK % perlcritic badcode.pl Code before strictures are enabled at line 4, column 1. See page 429 of PBP. (Severity: 5)
  • 22.
    Perl::Critic Perl::Critic options/features:Default severity is 5, you can change it with the flag -n where n=1..5
  • 23.
    Perl::Critic Perl::Critic options/features: Default severity is 5, you can change it with the flag -n where n=1..5 -top 7 will show you the top 7 code violations
  • 24.
    Perl::Critic Perl::Critic options/features:Default severity is 5, you can change it with the flag -n where n=1..5 -top 7 will show you the top 7 code violations ~/.perlcriticrc Change severity of policies [ControlStructures::ProhibitPostfixControls] severity = cruel # Same as &quot;severity = 2” Change dis/allowed policies: [ControlStructures::ProhibitPostfixControls] allow = if unless
  • 25.
    Perl::Critic Perl::Critic options/features:~/.perlcriticrc Turn off specific policies: [-NamingConventions::ProhibitMixedCaseVars] [-NamingConventions::ProhibitMixedCaseSubs]
  • 26.
    Perl::Critic Perl::Critic options/features:Turn off Perl::Critic for a single line: open(FILE,”>$path”); ## no critic Turn off Perl::Critic for a number of lines: ## no critic open(FILE,”>$path”); ## use critic These can be ignored with -force
  • 27.
    Perl::Critic You can create a ‘.t’ file to run as you ‘make test’ ~/.perlcriticrc -profile=.perlcriticrc Uses Module::Pluggable so the policies are easily loaded (or not loaded)!

Editor's Notes

  • #3 It is highly configurable, capable of matching the style and formatting needs of any programmer. It makes your code Easier to read and Easier to maintain. Perl::Critic: Most of P::C’s rules come from Damian Conway’s book Perl Best Practices. While PBP was the inspiration for PC, the rules it inforces are not limited to PBP And in fact P::C is also very customizable, so that you can add your own rules.
  • #4 Let’s look at P::T If you look at this code, it’s pretty bad. The indentation is non existant, and it seems that the author didn’t want to decide if a new block of code after an if statement should start on the same line, or a new line. Basically this is really hard to read
  • #5 Let’s look at P::T If you look at this code, it’s pretty bad. The indentation is non existant, and it seems that the author didn’t want to decide if a new block of code after an if statement should start on the same line, or a new line. Basically this is really hard to read
  • #6 This is the default behavior of Perl::Tidy. As you can see, it is easy to read, and easy to understand the flow of the program. If you get nothing else out of my talk, I would hope that you start to use Perl::Tidy and Perl::Critic with just the default settings
  • #7 For most people the default is sufficient
  • #8 For most people the default is sufficient For those programmers that need a bit more though, there are a whole bunch of options for perl tidy that allows them to customize the formating that perl::tidy spits out.
  • #9 Lets look at our initial poorly formatted code
  • #10 You can cuddle Elses between their braces. With -ce
  • #11 You can set the indenting level with -i=number
  • #12 This is -i=20
  • #13 In this slide you can see that the opening braces have been moved to their own line, using the P::T option of -bl
  • #14 This is my favorite because I use emacs, and a -bli (or block left indent) most closely matches the default emacs indentation. You can also combine these options to get formatting styles that to most people would seem odd, but overall are still readable
  • #15 ]
  • #16 P::T also has option to break lines in the middle as it becomes necessary. The -ci option, or ‘continuation indentation’ option will break lines apart, and indent them the number of spaces indicated. This will also use the -l=80 value that has been set, and will break the lines appropriately.
  • #17 Once you know what options you like, you can create a config file in your home directory that will set your desired options every time you run perl::tidy. You can ignore a config file with the flag -npro, or you can set your own with -pro=filename.
  • #18 Here is a list of some of thee other options that Perl tidy has. This list is available at perltidy.sourceforge.net. Or by typing perltidy -h on the command line.
  • #19 There are other uses for perl::tidy that are beyond the scope of this talk, that are discussed on the cpan module page.
  • #20 If you were in this room for the previous talk, you would have heard Peter talk about Best Practices. This part of my presentation will show you how to take advantage of P::C to follow best practices. P::C Is a souce code analyzer that scans your code for errors and violations of best practice rules. It is sort of like lint for perl
  • #21 P::C Uses ppi to open and parse perl files. It searches for violations that can lead to bugs, bad style, and most importantly: readability issues. The violations are rated 1-5, with 5 being the most severe violations.
  • #22 After you install P::C using it is simple. It installs an executable in your bin directory. You simply call the executable with the filename and if there is nothing wrong it will print OK, and exit. If there are problems with the code, if it is one of the PBP violations, it will tell you what page to read about the violation.
  • #23 The defaut severity level is 5. If you are running this for the first time, running with severity -5 is probably a good idea, and then working your way down, (or up) to -3 or even -1. Running at -3 is the recommended severity level for the lower levels of severity are mostly style policies, and are subjective as to if they are necessary to inforce
  • #24 Perl Critic also has a -top option where it will show only the top violations. This is good when you don’t want to be overwhelmed by the number of violations, you just want to deal with the violations one bite at a time.
  • #25 Just like Perl::Tidy, You can add a .perlcriticrc file to your home directory to configure how you want Perl::Critic to run. Here you can see that this config is setting the severity of the postfix controls to severity 2. The second part changes the policy to allow a postfix ‘if’ and ‘unless’
  • #26 Just like Perl::Tidy, You can add a .perlcriticrc file to your home directory to configure how you want Perl::Critic to run. Here you can see that this config is allowing