Perl Anti-Patterns Overview
Perl Anti-Patterns Overview
Not Using strict and warnings
Not Using strict and warnings
use strict;
and use warnings;
at the beginning of your Perl scripts. These pragmas help catch common programming errors like typos in variable names, using undefined variables, and other potential issues.Using Barewords
Using Barewords
Not Checking Return Values
Not Checking Return Values
or die
or or warn
to handle errors appropriately.Using Two-Argument open
Using Two-Argument open
open()
instead of the two-argument form. The three-argument form explicitly separates the mode from the filename, making your code safer and more readable.Not Using Lexical Variables
Not Using Lexical Variables
my
) instead of global variables. Lexical variables have limited scope, making your code more maintainable and less prone to bugs from unexpected variable modifications.Using Symbolic References
Using Symbolic References
Using Comma as Statement Separator
Using Comma as Statement Separator
Not Using Proper Error Handling
Not Using Proper Error Handling
Try::Tiny
instead of bare eval
blocks. This helps avoid subtle issues with $@
and ensures proper error propagation.Using Perl4-style File Handles
Using Perl4-style File Handles
my
) instead of global filehandles. Lexical filehandles are automatically closed when they go out of scope, reducing the risk of resource leaks.Not Using Modules
Not Using Modules
Using Indirect Object Syntax
Using Indirect Object Syntax
->
) notation instead, which is clearer and less ambiguous.Not Using Named Parameters
Not Using Named Parameters
Not Using Proper Scoping
Not Using Proper Scoping
my
to limit the scope of variables to where they are needed. This reduces the risk of variable name collisions and unexpected behavior.Using Excessive Regular Expressions
Using Excessive Regular Expressions
index()
, substr()
, and split()
when appropriate.Not Using Proper Documentation
Not Using Proper Documentation
Using Excessive Magic Variables
Using Excessive Magic Variables
$_
, @_
, $1
, etc. While they can make code concise, they can also make it harder to understand, especially for less experienced Perl developers.Not Using Proper Package Organization
Not Using Proper Package Organization
Using Bareword Filehandles
Using Bareword Filehandles
my
) instead, which are safer and automatically closed when they go out of scope.Not Using Proper Testing
Not Using Proper Testing
Test::More
. This helps catch bugs early and ensures your code works as expected.Using Prototypes Incorrectly
Using Prototypes Incorrectly
Not Using Proper OO Techniques
Not Using Proper OO Techniques
Moose
, Moo
, or Object::Tiny
instead of writing your own object system from scratch. These frameworks provide features like attributes, inheritance, and type checking.Using Excessive Punctuation Variables
Using Excessive Punctuation Variables
$1
, $2
, etc. Assign them to named variables for clarity.