SlideShare a Scribd company logo
1 of 32
An Introduction To  Perl Critic YAPC::NA 2007 Houston, TX Josh McAdams
What is Perl Critic? - Think 'use warnings' or 'use strict' ... only more (or less) - Why more? ... Perl Critic looks beyond mere syntatical correctness - for instance, are you using CamelCase sub  names? - Why less? ... Perl Critic doesn't actually compile your code - Your code is parsed as a 'document' thanks to PPI - Invalid code could possibly pass a critique
A Basic Example Let's write some code... #!/usr/bin/perl print “Roger That Houston”; ... I told you it was basic hello_houston.pl
A Basic Example So, let's run  perlcritic... --(0)> perlcritic hello_houston.pl  Code before strictures are enabled at line 3, column 1.  See page 429 of PBP.  (Severity: 5) ... what, we have problems already?
A Basic Example What just happened? --(0)> perlcritic hello_houston.pl  Code before strictures are enabled at line 3, column 1.  See page 429 of PBP.  (Severity: 5) It told us how bad our code is! and it complained about a violation and where to find more details on why what we did was wrong. We ran perlcritic
Perl Critic Policies - What is Perl Critic judging our code on? ... Policies ... in this case TestingAndDebugging::RequireUseStrict - What are Policies? ...  rules that Perl Critic enforces ... Perl Critic is packaged with many ... they can be big things that can lead to bugs (our example) ... or little things that are mostly cosmetic ... found in the Perl::Critic::Policy namespace - Who comes up with these Policies? ... the Perl Critic maintainers ... Perl Critic extension authors ... You ... let's see an example of a cosmetic policy
A Cosmetic Example Here's the code... #!/usr/bin/perl use strict; use warnings; sub CamelCaseSub { print "Hey, I'm a CamelCase subroutine"; return; } CamelCaseSub(); any bets on what the error is? cosmetic_example.pl
A Cosmetic Example So, let's run  perlcritic... --(0)> perlcritic cosmetic_example.pl  cosmetic_example.pl source OK ... wow, we have great code!
A Cosmetic Example Not so fast...  --(0)> perlcritic -severity 1 cosmetic_example.pl  RCS keywords $Id$ not found at line 1, column 1.  See page 441 of PBP.  (Severity: 2) RCS keywords $Revision$, $HeadURL$, $Date$ not found at line 1, column 1.  See page 441 of PBP.  (Severity: 2) RCS keywords $Revision$, $Source$, $Date$ not found at line 1, column 1.  See page 441 of PBP.  (Severity: 2) No "VERSION" variable found at line 1, column 1.  See page 404 of PBP.  (Severity: 2) Mixed-case subroutine name at line 6, column 1.  See page 44 of PBP.  (Severity: 1) ... eek, what happened?
Policy Severities - Who opened the flood gates? ... we did, by asking for a new severity - What are severities? ...  weights assigned to policies ... most severe violation = 5 ... least severe violation = 1 - Who assigns these severities? ... module authors ... if you disagree, you can change the severity
Severity Levels -  What are the severity levels? 5 = gentle (the only one checked by default) 4 = stern 3 = harsh 2 = cruel 1 = brutal - When you request any severity you get all severities  above that level - You can request a severity level by number or by name - It can be helpful to start examining your code at gentle  and work your way down
Yet Another Example Here's the code... #!/usr/bin/perl  # $Id$ use strict; use warnings; use Config; our $VERSION = 0.1; print "Hipster" if $Config{'osname'} eq 'darwin'; any bets on what the error is? yet_another_ex.pl
Yet Another Example Just like we expected... --(0)> perlcritic -brutal yet_another_ex.pl  Postfix control "if" used at line 11, column 19.  See pages 93,94 of PBP.  (Severity: 2) ... but I like the postfix if?
Excluding Policies We can selectively exclude policies... --(0)> perlcritic -brutal -exclude    ProhibitPostfixControls yet_another_ex.pl trailing_if.pl source OK ... better, but a PITA
Persistent Configuration - How about making my preferences persistent? ... sure, just create a .perlcriticrc file - .perlcriticrc? ...  it's just a configuration file with your preferences ... basically an INI file format ... place it in your home directory ... or specify it on the command line ... let's see a file
Excluding Policies Let's see the file... --(0)> cat ~/.perlcriticrc exclude = ControlStructures::ProhibitPostfixControls ... okay, not much there, but it saves some typing
Excluding Policies And it does the trick... --(0)> perlcritic -brutal yet_another_ex.pl  yet_another_ex.pl source OK ... there are other options though
Excluding Policies Here's an alternative method... --(0)> cat ~/.perlcriticrc [-ControlStructures::ProhibitPostfixControls] ... so, why one way or the other?
Excluding Policies The 'exclude =' configuration gets ignored when the 'exclude' option is used on the command line  ... also, what if 'if' is the only postfix operator I want?
Persistent Configuration Pass arguments to the policy constructor... --(0)> cat ~/.perlcriticrc [ControlStructures::ProhibitPostfixControls] allow = if ... let's check out a bigger example
Persistent Configuration A more interesting configuration file... --(0)> cat ~/.perlcriticrc severity = 2 top = 5 exclude = Editor::RequireEmacsFileVariables  [ControlStructures::ProhibitPostfixControls] severity = 4 allow = if unless theme = iffy ... themes?
Themes - What are these themes you speak of? ... similar to tags or labels ... think del.icio.us ... themes classify Policies and allow them to be grouped  together for purposes of running or excluding - What are some common themes? core = the policies that come packaged with Perl Critic pbp = policies that apply to Perl Best Practices bugs = policies that typically indicate bugs in your code ... there are many/infinitely more, just look at the policy docs
Specifying Themes Let's specify some themes to look for... --(0)>  perlcritic -theme '(pbp || (bugs && security)) && !cosmetic' trailing_if.pl  trailing_if.pl source OK ...  perlcritic -list  shows all policies, themes and severities
One Time Exceptions - What happens if I want to respect a policy, but for some  reason can't in this one instance? ... for instance sub-classing DBI and overriding  'connect'... package DBIxUseless; use warnings; use strict; use base qw(DBI); our $VERSION = 1; sub connect { return shift->SUPER::connect(@_) } 1; ... let's see what happens
One Time Exceptions - Running perl critic --(0)>  perlcritic DBIxUseless.pm  Subroutine name is a homonym for builtin function at line 9, column 1.  See page 177 of PBP.  (Severity: 4) ... I just can't help it though
A Pseudo-Solution package DBIxUseless; use warnings; use strict; use base qw(DBI); our $VERSION = 1; sub connect { ## no critic (ProhibitBuiltinHomonyms) return shift->SUPER::connect(@_)  } 1; Pseudo-pragma allows for you to turn off all or specific criticisms for individual lines of code
A Pseudo-Solution #!/usr/bin/perl use warnings; use strict; ## no critic (ProhibitPostfixControls) print “Hi” if $^O eq 'darwin'; print “Hello” if $^O =~ /win/i; ## use critic It even works with blocks of code ... you can use theme names too
In Review - That is Perl Critic in a nutshell ...  the system consists of a bunch of policies ... that have a name ... and a severity ... and one (maybe zero) or more themes ... the system can be configured ... by selectively blocking off criticism for specific code ... by ignoring policies ... by changing the severity of policies ... by looking a specific themes ... by tweaking the policies ... severity ... themes ... configuration ... but you still have to remember to run perlcritic, unless
Criticism Pragma package DBIxUseless; use warnings; use strict; use base qw(DBI); use criticism; our $VERSION = 1; sub connect { return shift->SUPER::connect(@_) } 1; You can use the criticism pragma... ... scary!
Criticism Pragma -  The criticism pragma ... runs Perl Critic every time you run your code ... keeps you from having to remember to run perlcritic ... creates additional runtime overhead ... is easy to accidentally leave in your production code ... there has to be a better way
Test::Perl::Critic use Test::Perl::Critic; all_critic_ok(); -  Test::Perl::Critic ... criticism in your test suite ... now when you run 'make test', you get Perl Critic ... but it's not good to distribute these test in public modules ... these should be for development only ... you don't want your modules not installing because  of Perl Critic
In Review (again) -  Perl Critic is ... a highly-configurable static source code analyzer ... a tool to help you write better code ... a reminder of good coding practices ... an easily extendable system (see Wednesday's talk) ... a development aid, not a distribution dependency ... not a  guarantee  that you'll write quality code

More Related Content

What's hot

Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With PythonSiddhi
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentationThanh Robi
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDPaweł Michalik
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
Test your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practiceTest your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practiceSebastian Marek
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtestWill Shen
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyonddn
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to heroJeremy Cook
 
New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmanndpc
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnitMindfire Solutions
 
PhpUnit Best Practices
PhpUnit Best PracticesPhpUnit Best Practices
PhpUnit Best PracticesEdorian
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHPNick Belhomme
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code PrinciplesYeurDreamin'
 
Python Debugging Fundamentals
Python Debugging FundamentalsPython Debugging Fundamentals
Python Debugging Fundamentalscbcunc
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentalscbcunc
 

What's hot (20)

Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With Python
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
PHPUnit
PHPUnitPHPUnit
PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDD
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Test your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practiceTest your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practice
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtest
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmann
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
PhpUnit Best Practices
PhpUnit Best PracticesPhpUnit Best Practices
PhpUnit Best Practices
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
Mastering Namespaces in PHP
Mastering Namespaces in PHPMastering Namespaces in PHP
Mastering Namespaces in PHP
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Python Debugging Fundamentals
Python Debugging FundamentalsPython Debugging Fundamentals
Python Debugging Fundamentals
 
Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentals
 

Similar to YAPC::NA 2007 - An Introduction To Perl Critic

YAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl CriticYAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl Criticjoshua.mcadams
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::ClassCurtis Poe
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate WorksGoro Fuji
 
New Features Of Test Unit 2.x
New Features Of Test Unit 2.xNew Features Of Test Unit 2.x
New Features Of Test Unit 2.xdjberg96
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command LineMarcos Rebelo
 
Владимир Перепелица "Модули"
Владимир Перепелица "Модули"Владимир Перепелица "Модули"
Владимир Перепелица "Модули"Media Gorod
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMockYing Zhang
 
Puppet Loves RSpec, Why You Should, Too
Puppet Loves RSpec, Why You Should, TooPuppet Loves RSpec, Why You Should, Too
Puppet Loves RSpec, Why You Should, TooPuppet
 
Puppet loves RSpec, why you should, too
Puppet loves RSpec, why you should, tooPuppet loves RSpec, why you should, too
Puppet loves RSpec, why you should, tooDennis Rowe
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# DevelopersCory Foy
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Linux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLiteLinux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLitePVS-Studio
 
Continuous Integration Testing in Django
Continuous Integration Testing in DjangoContinuous Integration Testing in Django
Continuous Integration Testing in DjangoKevin Harvey
 
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott NottinghamAutomated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott NottinghamPuppet
 
Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2nottings
 
Exploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsExploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsMarian Marinov
 
Computer programming
Computer programmingComputer programming
Computer programmingXhyna Delfin
 

Similar to YAPC::NA 2007 - An Introduction To Perl Critic (20)

YAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl CriticYAPC::NA 2007 - Customizing And Extending Perl Critic
YAPC::NA 2007 - Customizing And Extending Perl Critic
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::Class
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
How Xslate Works
How Xslate WorksHow Xslate Works
How Xslate Works
 
New Features Of Test Unit 2.x
New Features Of Test Unit 2.xNew Features Of Test Unit 2.x
New Features Of Test Unit 2.x
 
Perl In The Command Line
Perl In The Command LinePerl In The Command Line
Perl In The Command Line
 
Владимир Перепелица "Модули"
Владимир Перепелица "Модули"Владимир Перепелица "Модули"
Владимир Перепелица "Модули"
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
Puppet Loves RSpec, Why You Should, Too
Puppet Loves RSpec, Why You Should, TooPuppet Loves RSpec, Why You Should, Too
Puppet Loves RSpec, Why You Should, Too
 
Puppet loves RSpec, why you should, too
Puppet loves RSpec, why you should, tooPuppet loves RSpec, why you should, too
Puppet loves RSpec, why you should, too
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Coding standard
Coding standardCoding standard
Coding standard
 
Linux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLiteLinux version of PVS-Studio couldn't help checking CodeLite
Linux version of PVS-Studio couldn't help checking CodeLite
 
Continuous Integration Testing in Django
Continuous Integration Testing in DjangoContinuous Integration Testing in Django
Continuous Integration Testing in Django
 
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott NottinghamAutomated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
Automated Puppet Testing - PuppetCamp Chicago '12 - Scott Nottingham
 
Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2Puppet camp chicago-automated_testing2
Puppet camp chicago-automated_testing2
 
Exploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsExploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your plugins
 
Computer programming
Computer programmingComputer programming
Computer programming
 

More from joshua.mcadams

Open Flash Chart And Perl
Open Flash Chart And PerlOpen Flash Chart And Perl
Open Flash Chart And Perljoshua.mcadams
 
Introduction To Testing With Perl
Introduction To Testing With PerlIntroduction To Testing With Perl
Introduction To Testing With Perljoshua.mcadams
 
Thank A Cpan Contributor Today
Thank A Cpan Contributor TodayThank A Cpan Contributor Today
Thank A Cpan Contributor Todayjoshua.mcadams
 
Utility Modules That You Should Know About
Utility Modules That You Should Know AboutUtility Modules That You Should Know About
Utility Modules That You Should Know Aboutjoshua.mcadams
 
YAPC::NA 2007 - Epic Perl Coding
YAPC::NA 2007 - Epic Perl CodingYAPC::NA 2007 - Epic Perl Coding
YAPC::NA 2007 - Epic Perl Codingjoshua.mcadams
 
Lightning Talk: An Introduction To Scrum
Lightning Talk: An Introduction To ScrumLightning Talk: An Introduction To Scrum
Lightning Talk: An Introduction To Scrumjoshua.mcadams
 

More from joshua.mcadams (6)

Open Flash Chart And Perl
Open Flash Chart And PerlOpen Flash Chart And Perl
Open Flash Chart And Perl
 
Introduction To Testing With Perl
Introduction To Testing With PerlIntroduction To Testing With Perl
Introduction To Testing With Perl
 
Thank A Cpan Contributor Today
Thank A Cpan Contributor TodayThank A Cpan Contributor Today
Thank A Cpan Contributor Today
 
Utility Modules That You Should Know About
Utility Modules That You Should Know AboutUtility Modules That You Should Know About
Utility Modules That You Should Know About
 
YAPC::NA 2007 - Epic Perl Coding
YAPC::NA 2007 - Epic Perl CodingYAPC::NA 2007 - Epic Perl Coding
YAPC::NA 2007 - Epic Perl Coding
 
Lightning Talk: An Introduction To Scrum
Lightning Talk: An Introduction To ScrumLightning Talk: An Introduction To Scrum
Lightning Talk: An Introduction To Scrum
 

Recently uploaded

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Recently uploaded (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

YAPC::NA 2007 - An Introduction To Perl Critic

  • 1. An Introduction To Perl Critic YAPC::NA 2007 Houston, TX Josh McAdams
  • 2. What is Perl Critic? - Think 'use warnings' or 'use strict' ... only more (or less) - Why more? ... Perl Critic looks beyond mere syntatical correctness - for instance, are you using CamelCase sub names? - Why less? ... Perl Critic doesn't actually compile your code - Your code is parsed as a 'document' thanks to PPI - Invalid code could possibly pass a critique
  • 3. A Basic Example Let's write some code... #!/usr/bin/perl print “Roger That Houston”; ... I told you it was basic hello_houston.pl
  • 4. A Basic Example So, let's run perlcritic... --(0)> perlcritic hello_houston.pl Code before strictures are enabled at line 3, column 1. See page 429 of PBP. (Severity: 5) ... what, we have problems already?
  • 5. A Basic Example What just happened? --(0)> perlcritic hello_houston.pl Code before strictures are enabled at line 3, column 1. See page 429 of PBP. (Severity: 5) It told us how bad our code is! and it complained about a violation and where to find more details on why what we did was wrong. We ran perlcritic
  • 6. Perl Critic Policies - What is Perl Critic judging our code on? ... Policies ... in this case TestingAndDebugging::RequireUseStrict - What are Policies? ... rules that Perl Critic enforces ... Perl Critic is packaged with many ... they can be big things that can lead to bugs (our example) ... or little things that are mostly cosmetic ... found in the Perl::Critic::Policy namespace - Who comes up with these Policies? ... the Perl Critic maintainers ... Perl Critic extension authors ... You ... let's see an example of a cosmetic policy
  • 7. A Cosmetic Example Here's the code... #!/usr/bin/perl use strict; use warnings; sub CamelCaseSub { print "Hey, I'm a CamelCase subroutine"; return; } CamelCaseSub(); any bets on what the error is? cosmetic_example.pl
  • 8. A Cosmetic Example So, let's run perlcritic... --(0)> perlcritic cosmetic_example.pl cosmetic_example.pl source OK ... wow, we have great code!
  • 9. A Cosmetic Example Not so fast... --(0)> perlcritic -severity 1 cosmetic_example.pl RCS keywords $Id$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2) RCS keywords $Revision$, $HeadURL$, $Date$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2) RCS keywords $Revision$, $Source$, $Date$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2) No "VERSION" variable found at line 1, column 1. See page 404 of PBP. (Severity: 2) Mixed-case subroutine name at line 6, column 1. See page 44 of PBP. (Severity: 1) ... eek, what happened?
  • 10. Policy Severities - Who opened the flood gates? ... we did, by asking for a new severity - What are severities? ... weights assigned to policies ... most severe violation = 5 ... least severe violation = 1 - Who assigns these severities? ... module authors ... if you disagree, you can change the severity
  • 11. Severity Levels - What are the severity levels? 5 = gentle (the only one checked by default) 4 = stern 3 = harsh 2 = cruel 1 = brutal - When you request any severity you get all severities above that level - You can request a severity level by number or by name - It can be helpful to start examining your code at gentle and work your way down
  • 12. Yet Another Example Here's the code... #!/usr/bin/perl # $Id$ use strict; use warnings; use Config; our $VERSION = 0.1; print "Hipster" if $Config{'osname'} eq 'darwin'; any bets on what the error is? yet_another_ex.pl
  • 13. Yet Another Example Just like we expected... --(0)> perlcritic -brutal yet_another_ex.pl Postfix control "if" used at line 11, column 19. See pages 93,94 of PBP. (Severity: 2) ... but I like the postfix if?
  • 14. Excluding Policies We can selectively exclude policies... --(0)> perlcritic -brutal -exclude ProhibitPostfixControls yet_another_ex.pl trailing_if.pl source OK ... better, but a PITA
  • 15. Persistent Configuration - How about making my preferences persistent? ... sure, just create a .perlcriticrc file - .perlcriticrc? ... it's just a configuration file with your preferences ... basically an INI file format ... place it in your home directory ... or specify it on the command line ... let's see a file
  • 16. Excluding Policies Let's see the file... --(0)> cat ~/.perlcriticrc exclude = ControlStructures::ProhibitPostfixControls ... okay, not much there, but it saves some typing
  • 17. Excluding Policies And it does the trick... --(0)> perlcritic -brutal yet_another_ex.pl yet_another_ex.pl source OK ... there are other options though
  • 18. Excluding Policies Here's an alternative method... --(0)> cat ~/.perlcriticrc [-ControlStructures::ProhibitPostfixControls] ... so, why one way or the other?
  • 19. Excluding Policies The 'exclude =' configuration gets ignored when the 'exclude' option is used on the command line ... also, what if 'if' is the only postfix operator I want?
  • 20. Persistent Configuration Pass arguments to the policy constructor... --(0)> cat ~/.perlcriticrc [ControlStructures::ProhibitPostfixControls] allow = if ... let's check out a bigger example
  • 21. Persistent Configuration A more interesting configuration file... --(0)> cat ~/.perlcriticrc severity = 2 top = 5 exclude = Editor::RequireEmacsFileVariables [ControlStructures::ProhibitPostfixControls] severity = 4 allow = if unless theme = iffy ... themes?
  • 22. Themes - What are these themes you speak of? ... similar to tags or labels ... think del.icio.us ... themes classify Policies and allow them to be grouped together for purposes of running or excluding - What are some common themes? core = the policies that come packaged with Perl Critic pbp = policies that apply to Perl Best Practices bugs = policies that typically indicate bugs in your code ... there are many/infinitely more, just look at the policy docs
  • 23. Specifying Themes Let's specify some themes to look for... --(0)> perlcritic -theme '(pbp || (bugs && security)) && !cosmetic' trailing_if.pl trailing_if.pl source OK ... perlcritic -list shows all policies, themes and severities
  • 24. One Time Exceptions - What happens if I want to respect a policy, but for some reason can't in this one instance? ... for instance sub-classing DBI and overriding 'connect'... package DBIxUseless; use warnings; use strict; use base qw(DBI); our $VERSION = 1; sub connect { return shift->SUPER::connect(@_) } 1; ... let's see what happens
  • 25. One Time Exceptions - Running perl critic --(0)> perlcritic DBIxUseless.pm Subroutine name is a homonym for builtin function at line 9, column 1. See page 177 of PBP. (Severity: 4) ... I just can't help it though
  • 26. A Pseudo-Solution package DBIxUseless; use warnings; use strict; use base qw(DBI); our $VERSION = 1; sub connect { ## no critic (ProhibitBuiltinHomonyms) return shift->SUPER::connect(@_) } 1; Pseudo-pragma allows for you to turn off all or specific criticisms for individual lines of code
  • 27. A Pseudo-Solution #!/usr/bin/perl use warnings; use strict; ## no critic (ProhibitPostfixControls) print “Hi” if $^O eq 'darwin'; print “Hello” if $^O =~ /win/i; ## use critic It even works with blocks of code ... you can use theme names too
  • 28. In Review - That is Perl Critic in a nutshell ... the system consists of a bunch of policies ... that have a name ... and a severity ... and one (maybe zero) or more themes ... the system can be configured ... by selectively blocking off criticism for specific code ... by ignoring policies ... by changing the severity of policies ... by looking a specific themes ... by tweaking the policies ... severity ... themes ... configuration ... but you still have to remember to run perlcritic, unless
  • 29. Criticism Pragma package DBIxUseless; use warnings; use strict; use base qw(DBI); use criticism; our $VERSION = 1; sub connect { return shift->SUPER::connect(@_) } 1; You can use the criticism pragma... ... scary!
  • 30. Criticism Pragma - The criticism pragma ... runs Perl Critic every time you run your code ... keeps you from having to remember to run perlcritic ... creates additional runtime overhead ... is easy to accidentally leave in your production code ... there has to be a better way
  • 31. Test::Perl::Critic use Test::Perl::Critic; all_critic_ok(); - Test::Perl::Critic ... criticism in your test suite ... now when you run 'make test', you get Perl Critic ... but it's not good to distribute these test in public modules ... these should be for development only ... you don't want your modules not installing because of Perl Critic
  • 32. In Review (again) - Perl Critic is ... a highly-configurable static source code analyzer ... a tool to help you write better code ... a reminder of good coding practices ... an easily extendable system (see Wednesday's talk) ... a development aid, not a distribution dependency ... not a guarantee that you'll write quality code