300 likes | 582 Views
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.
E N D
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="here"; if($var >=1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }
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!
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"; } }
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="here"; if($var >=1 && $var <= 5 ) { my $other_code = "here"; } else { my $other_code = "here"; } }
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"; } }
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"; } }
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"; } }
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"; } }
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"; } }
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"; } }
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
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 "severity = 4” • 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 block: • ## no critic • open(FILE,”>$path”); • ## use critic
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)
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::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.
Perl::Critic and Perl::Tidy Thank you