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

Perl::Critic and Perl::Tidy

  • Upload
    haroun

  • View
    92

  • Download
    0

Embed Size (px)

DESCRIPTION

Perl::Critic and Perl::Tidy. Your guide to better programming practices. What is Perl::Tidy and Perl::Critic?. www.thedailywtf.com. What is Perl::Tidy and Perl::Critic?. Perl::Tidy a script which indents and reformats Perl code to make it easier to read. - PowerPoint PPT Presentation

Citation preview

Page 1: Perl::Critic and Perl::Tidy

Perl::Critic and Perl::Tidy

Your guide to better programming practices.

Page 2: Perl::Critic and Perl::Tidy

What is Perl::Tidy and Perl::Critic?

www.thedailywtf.com

Page 3: Perl::Critic and Perl::Tidy

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 4: Perl::Critic and Perl::Tidy

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 5: Perl::Critic and Perl::Tidy

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 6: Perl::Critic and Perl::Tidy

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 7: Perl::Critic and Perl::Tidy

Perl::Tidy• For most individuals the default

behavior is good enough

Page 8: Perl::Critic and Perl::Tidy

Perl::Tidy• For most individuals the default

behavior is good enough

• However some groups of coders have certain coding standards that are different from the default perl::Critic behavior

Page 9: Perl::Critic and Perl::Tidy

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 10: Perl::Critic and Perl::Tidy

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 11: Perl::Critic and Perl::Tidy

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 12: Perl::Critic and Perl::Tidy

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 13: Perl::Critic and Perl::Tidy

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 14: Perl::Critic and Perl::Tidy

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 15: Perl::Critic and Perl::Tidy

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 16: Perl::Critic and Perl::Tidy

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 17: Perl::Critic and Perl::Tidy

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 18: Perl::Critic and Perl::Tidy

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 19: Perl::Critic and Perl::Tidy

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 20: Perl::Critic and Perl::Tidy

Perl::Critic

• What is it,

• What is it good for?

Page 21: Perl::Critic and Perl::Tidy

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 22: Perl::Critic and Perl::Tidy

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 23: Perl::Critic and Perl::Tidy

Perl::Critic

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

flag -n where n=1..5

Page 24: Perl::Critic and Perl::Tidy

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 25: Perl::Critic and Perl::Tidy

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 26: Perl::Critic and Perl::Tidy

Perl::Critic

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

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

Page 27: Perl::Critic and Perl::Tidy

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

Page 28: Perl::Critic and Perl::Tidy

Perl::Critic

Perl::Critic options/features:• Turn off a specific policy for a single line:

– $email = ‘[email protected]’ ## no critic(RequireInterpolation)

• Turn off A specific policy for a function or block of code:– sub complex_fcn { ## no critic(ExcessComplexity)

… some complex code

}

Page 29: Perl::Critic and Perl::Tidy

Perl::Critic

• You can create a ‘.t’ file to run as you ‘make test’,• Just don’t upload that test to CPAN

Page 30: Perl::Critic and Perl::Tidy

Perl::Critic and Perl::Tidy

• Any Questions?

• More info:– http://search.cpan.org/~elliotjs/Perl-Critic-1.080/lib/Perl/Critic.pm– http://search.cpan.org/~shancock/Perl-Tidy-20071205/lib/Perl/Tidy.

pm