1 / 30

Perl::Critic and Perl::Tidy

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.

harper
Download Presentation

Perl::Critic and Perl::Tidy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl::Critic and Perl::Tidy Your guide to better programming practices.

  2. 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.

  3. Perl::Tidy • Take the following code: if ( $toOrCC =~ /(sometest)/i || $subject =~ /Some_other_test/i ) { my $code="here"; if($var >=1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  4. Perl::Tidy • Take the following code: if ( $toOrCC =~ /(sometest)/i || $subject =~ /Some_other_test/i ) { my $code="here"; if($var >=1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } } PRETTY UGLY HUH? Lets see what Perl::Tidy can do!

  5. Perl::Tidy • Default Perl::Critic behavior: if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = "here"; if ( $var >= 1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  6. Perl::Tidy • For most individuals this is good enough

  7. 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

  8. Perl::Tidy • Take the following code: if ( $toOrCC =~ /(sometest)/i || $subject =~ /Some_other_test/i ) { my $code="here"; if($var >=1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  9. Perl::Tidy • Perltidy -ce # (Cuddled Elses) if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = "here"; if ( $var >= 1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  10. 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 = "here"; if ( $var >= 1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  11. 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 = "here"; if ( $var >= 1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  12. 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 = "here"; if ( $var >= 1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  13. 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 = "here"; if ( $var >= 1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  14. Perl::Tidy • Another formatting option: • This is -bli -i=2 if ( $toOrCC =~ /(sometest)/i || $subject =~ /\[Some_other_test\]/i ) { my $code = "here"; if ( $var >= 1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }

  15. Perl::Tidy • Continuation Indentation • -ci=14 Changes : my $filename = $home_dir."/".$username."/".$projectfile."/".$file; To: my $filename = $home_dir . "/" . $username . "/" . $projectfile . "/” . $file; Uses the line length variable set with -l=n

  16. 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

  17. 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

  18. 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

  19. Perl::Critic • What is it, • What is it good for?

  20. 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

  21. 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)

  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 "severity = 4” • 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 block: • ## no critic • open(FILE,”>$path”); • ## use critic

  27. Perl::Critic Perl::Critic options/features: • Turn off a specific policy for a single line: • $email = ‘foo@bar.com’ ## no critic(RequireInterpolation) • Turn off A specific policy for a function or block of code: • sub complex_fcn { ## no critic(ExcessComplexity)

  28. 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)!

  29. Perl::Critic • Use Test::Perl::Critic::Progressive: • This module is best for existing projects • No new violations. • Can require you to clean up as you go. • However, it can get confused if you roll back.

  30. Perl::Critic and Perl::Tidy Thank you

More Related