| Start |
|---|
| Using Perl::Critic If you'd like to start implementing best practices, for yourself or your team, then Perl::Critic (opens new window) is the best place to start. The module is based on the Perl Best Practices (opens new window) book by Damien Conway and does a fairly good job implementing the suggestions made therein. Note: I should mention (and Conway himself says in the book) that these are suggestions. I've found the book provides solid reasoning in most cases, though I certainly don't agree with all of them. The important thing to remember is that, whatever practices you decide to adopt, you remain consistent. The more predictable your code is, the easier it will be to maintain. You can also try out Perl::Critic through your browser at perlcritic.com (opens new window). InstallationThis will install the basic ruleset and a perlcritic script that can be called from the command line. Basic UsageThe CPAN doc for perlcritic (opens new window) contains full documentation, so I will only be going over the most common use cases to get you started. Basic usage is to simply call perlcritic on the file: perlcritic works both on scripts and on modules. The -1 refers to the severity level of the rules you want to run against the script. There are five levels that correspond to how much Perl::Critic will pick apart your code. -5 is the most gentle and will only warn about potentially dangerous problems that could cause unexpected results. -1 is the most brutal and will complain about things as small as your code being tidy or not. In my experience, keeping code compliant with level 3 is good enough to keep out of danger without getting too persnickety. By default, any failures will list the reason and severity the rule triggers on: Viewing PoliciesYou can quickly see which rules are being triggered and why by utilizing perlcritic's --verbose option: Setting the level to 8 will show you the rule that triggered a warning: While a level of 11 will show the specific reasons why the rule exists: Ignoring CodeThere will be times when you can't comply with a Perl::Critic policy. In those cases, you can wrap special comments, "## use critic()" and "## no critic", around your code to make Perl::Critic ignore them. Simply add the rules you want to ignore in the parentheses (multiples can be separated by a comma). Make sure to wrap the entire code block or Critic may not recognize the ignore statement. Note that there are certain policies that are run on the document level and cannot be exempted this way. However, they can be turned off... Creating Permanent ExceptionsUsing ## no critic() is nice, but as you start to adopt coding standards, you will likely want to make permanent exceptions to certain rules. You can do this by creating a .perlcriticrc configuration file. This file will allow you to customize not only which policies are run, but how they are run. Using it is as simple as placing the file in your home directory (in Linux, unsure if it's the same place on Windows). Or, you can specify the config file when running the command using the --profile option: Again, the perlcritic CPAN page (opens new window) has a full list of these options. I will list some examples from my own config file: Apply basic settings: Disable a rule (note the dash in front of the policy name): Modifying a rule: ConclusionProperly utilized, Perl::Critic can be an invaluable tool to help teams keep their coding consistent and easily maintainable no matter what best practice policies you employ. |
Read Article
https://develop.horje.com/learn/1434/reference