27
Perl::Critic and Perl::Tidy Your guide to better prog ramming practices.

Perl Tidy Perl Critic

  • View
    5.824

  • Download
    4

Embed Size (px)

DESCRIPTION

‎Perl::Critic and Perl::TidyAll you ever wanted to know but were afraid to ask

Citation preview

Page 1: Perl Tidy Perl Critic

Perl::Critic and Perl::Tidy

Your guide to better programming practices.

Page 2: Perl Tidy Perl Critic

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.

Page 3: Perl Tidy Perl Critic

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";

}

}

Page 4: Perl Tidy Perl Critic

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!

Page 5: Perl Tidy Perl Critic

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";

}

}

Page 6: Perl Tidy Perl Critic

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

Page 7: Perl Tidy Perl Critic

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

Page 8: Perl Tidy Perl Critic

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";

}

}

Page 9: Perl Tidy Perl Critic

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";

}

}

Page 10: Perl Tidy Perl Critic

Perl::Tidy• You can change the indenting level with -i=n• This is -i=2if ( $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";

}

}

Page 11: Perl Tidy Perl Critic

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";

}

}

Page 12: Perl Tidy Perl Critic

Perl::Tidy• You can have the opening braces on the left:• This is -blif ( $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"; }}

Page 13: Perl Tidy Perl Critic

Perl::Tidy• You can have the opening braces on the left:• This is -bliif ( $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"; } }

Page 14: Perl Tidy Perl Critic

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";

}

}

Page 15: Perl Tidy Perl Critic

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

Page 16: Perl Tidy Perl Critic

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

Page 17: Perl Tidy Perl Critic

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

Page 18: Perl Tidy Perl Critic

Perl::TidyOther 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

Page 19: Perl Tidy Perl Critic

Perl::Critic

• What is it,

• What is it good for?

Page 20: Perl Tidy Perl Critic

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

Page 21: Perl Tidy Perl Critic

Perl::Critic

How it works:% perlcritic goodcode.pl goodcode.pl source OK

% perlcritic badcode.plCode before strictures are enabled at line 4,

column 1. See page 429 of PBP. (Severity: 5)

Page 22: Perl Tidy Perl Critic

Perl::Critic

Perl::Critic options/features:• Default severity is 5, you can change it with

the flag -n where n=1..5

Page 23: Perl Tidy Perl Critic

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

Page 24: Perl Tidy Perl Critic

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 = 2”

– Change dis/allowed policies:• [ControlStructures::ProhibitPostfixControls] allow = if unless

Page 25: Perl Tidy Perl Critic

Perl::Critic

Perl::Critic options/features:• ~/.perlcriticrc• Turn off specific policies:

[-NamingConventions::ProhibitMixedCaseVars] [-NamingConventions::ProhibitMixedCaseSubs]

Page 26: Perl Tidy Perl Critic

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

Page 27: Perl Tidy Perl Critic

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