The use strict Pragma
Perl tends to be a permissive language.[105] But maybe you want Perl to impose a little discipline; that can be arranged with the use strict pragma.
A pragma is a hint to a compiler, telling it something about the code. In this case, the use strict pragma tells Perl’s internal compiler that it should enforce some good programming rules for the rest of this block or source file.
Why would this be important? Well, imagine that you’re composing your program, and you type a line like this one:
$bamm_bamm = 3; # Perl creates that variable automatically
Now, you keep typing for a while. After that line has scrolled off the top of the screen, you type this line to increment the variable:
$bammbamm += 1; # Oops!
Since Perl sees a new variable name (the underscore is significant in a variable name), it creates a new variable and increments that one. If you’re lucky and smart, you’ve turned on warnings, and Perl can tell you that you used one or both of those global variable names once in your program. But if you’re merely smart, you used each name more than once, and Perl won’t be able to warn you.
To tell Perl you’re ready to be more restrictive, put the use strict pragma at the top of your program (or in any block or file where you want to enforce these rules):
use strict; # Enforce some good programming rules
Now, among other restrictions,[106] Perl will insist that you declare every new variable, usually done with my:[107]
my $bamm_bamm = 3; # New lexical variable
If ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access