Re: Pragmas use strict and use warnings
- From: chas.owens@xxxxxxxxx (Chas. Owens)
- Date: Tue, 30 Oct 2007 11:15:34 -0400
On 10/30/07, Kaushal Shriyan <kaushalshriyan@xxxxxxxxx> wrote:
snip
Whats the exact purpose of use strictsnip
The strict pragma has three effects (unless modified by arguments):
1. most variables must be declared (there are some exceptions)
2. only real references are allowed (symbolic references are
considered an error)
3. barewords are considered to be subroutines and error if there is no
definition
Unless you are writing a command line invocation, you want these
strictures. Here is an example of how it will save you time and
effort:
1. variables
my $foo = 5;
$fo++;
print "$foo\n";
Without strict this code runs and prints five even though the expected
value was six. In this example we can easily find the error, but in a
large program it is much harder. The strict pragma tells us that $fo
on line 2 was not declared.
2. references
Back in the dawn of time it was thought that it was a good idea to
allow you to say
$foo = 5;
$bar = "foo";
print $$bar, "\n";
Later it was realized that this is not a good idea and we got real references
my $foo = 5;
my $bar = \$foo;
print $$bar, "\n";
The strict pragma prevents your code from doing something weird if you
try to dereference a string and forces you to use the better way of
handling references.
3. barewords
Also back in the dawn of time, it was thought to be a good idea to let
barewords (a sequence of \w characters that Perl does not recognize)
be treated as strings:
print STDOUT foo, bar, baz, "\n";
Over time it was realized that barewords as strings was not a good
idea (they are confusing and can cause issues with subroutine name
resolution).
All of these strictures cause problems for old code, so they are not
made mandatory, but in truth, every modern Perl program should start
with the strict pragma.
.
- Follow-Ups:
- Re: Pragmas use strict and use warnings
- From: Paul Lalli
- Re: Pragmas use strict and use warnings
- References:
- Pragmas use strict and use warnings
- From: Kaushal Shriyan
- Pragmas use strict and use warnings
- Prev by Date: Re: Perl 5.8.6 bug in open()?
- Next by Date: Re: One sprintf() initializations problem
- Previous by thread: Pragmas use strict and use warnings
- Next by thread: Re: Pragmas use strict and use warnings
- Index(es):