On 10/30/07, Kaushal Shriyan <[EMAIL PROTECTED]> wrote:
snip
> Whats the exact purpose of use strict
snip


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.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to