Perl Tidy Perl Critic

5,182 views
5,048 views

Published on

‎Perl::Critic and Perl::Tidy

All you ever wanted to know but were afraid to ask

Published in: Technology, Education
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,182
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
42
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • 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.
  • 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
  • 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
  • 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
  • For most people the default is sufficient
  • 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.
  • Lets look at our initial poorly formatted code
  • You can cuddle Elses between their braces. With -ce
  • You can set the indenting level with -i=number
  • This is -i=20
  • In this slide you can see that the opening braces have been moved to their own line, using the P::T option of -bl
  • 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
  • ]
  • 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.
  • 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.
  • 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.
  • There are other uses for perl::tidy that are beyond the scope of this talk, that are discussed on the cpan module page.
  • 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
  • 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.
  • 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.
  • 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
  • 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.
  • 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’
  • 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
  • Perl Tidy Perl Critic

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

    ×