Re: Pragmas use strict and use warnings
- From: mritty@xxxxxxxxx (Paul Lalli)
- Date: Tue, 30 Oct 2007 10:22:50 -0700
On Oct 30, 11:15 am, chas.ow...@xxxxxxxxx (Chas. Owens) wrote:
On 10/30/07, Kaushal Shriyan <kaushalshri...@xxxxxxxxx> 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)
This is a common misperception. use strict 'vars' in fact does not
require anything to be declared. All it requires is that global
variables must be fully qualified. You cannot use the short-name of
globals that belong to the current package.
That is, without strict:
#!/usr/bin/perl
$foo = "hello, world\n";
print $foo;
__END__
is the same as:
#!/usr/bin/perl
$main::foo = "hello, world\n";
print $main::foo;
__END__
When Perl sees '$foo' and sees that no lexical with that name has been
declared in the current scope, it assumes you are talking about the
global $foo from the current package (which is in this small example,
'main'). It then pretends that you wrote $main::foo instead of $foo.
With strict 'vars' enabled, Perl makes the same assumption, but this
time tells you that the global variable $foo must be fully qualified
(ie, written $main::foo). It won't allow the shortcut now.
Regardless, you can still use global variables, and there is still no
requirement to declare them:
#!/usr/bin/perl
use strict;
$main::foo = "hello, world\n";
print $main::foo;
__END__
With strict enabled, the only way to use a short-name of a variable is
to declare a lexical of that name (the right choice) using 'my', or to
disable strict 'vars' on a variable-by-variable case using 'our' (the
wrong choice). This is what leads people to assert "use strict forces
you to declare your variables".
Paul Lalli
.
- Follow-Ups:
- Re: Pragmas use strict and use warnings
- From: Jenda Krynicky
- RE: Pragmas use strict and use warnings
- From: Kevin Viel
- Re: Pragmas use strict and use warnings
- References:
- Pragmas use strict and use warnings
- From: Kaushal Shriyan
- Re: Pragmas use strict and use warnings
- From: Chas. Owens
- Pragmas use strict and use warnings
- Prev by Date: Re: Syntax Checker Script
- Next by Date: Re: Perl 5.8.6 bug in open()?
- Previous by thread: Re: Pragmas use strict and use warnings
- Next by thread: RE: Pragmas use strict and use warnings
- Index(es):
Relevant Pages
|