Some Perl idioms
Recently I have worked with Perl again for some system monitoring scripts. Some people at Lunatech, where I work, are real Perl mavens. Here are some quick reminders
Constants
use constant CONSTANT_NAME => "constant value";
creates a compile time constant. It is mainly useful for clarifying intent, although if the right side is a more complex expression, for instance a calculation, there might be some performance benefit. Learn more about constants in the official Perl docs.
Regular expressions
-
my @matches = my $variable =~ <regex>
if there is a match, @matches will contain the values of the capture groups in the regular expression. -
my $string =~ s/<pattern>/<replacement>/
if any substitutions have been made, in addition to modifying $string, will evaluate to true. Handy for use in conditionals.