Re: Pragmas use strict and use warnings



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

.



Relevant Pages

  • Re: Pragmas use strict and use warnings
    ... global variables must be fully qualified. ... or to disable strict 'vars' on a variable-by-variable ... assert "use strict forces you to declare your variables". ... if the resulting error message was something along the ...
    (perl.beginners)
  • Re: using "our" across blocks
    ... : sub foo { ... use strict; ... import the globals defined in BEGIN/END blocks (which works by the ...
    (perl.beginners)
  • Re: array of hashrefs (nested)
    ... Except that when you do "use strict;", ... that all your undeclared variables are globals. ... You have to declare ...
    (comp.lang.perl.misc)
  • Re: Syntax error
    ... > You should always declare all variables as lexically scoped in the ... With regards to your advice, I am not familiar with the term ... I do "use strict" in all of my normal perl scripts. ... added both the "use strict" and "use warnings" line, ...
    (comp.lang.perl.misc)
  • Re: Recurse over a hash without knowing any elements (except top level)
    ... > I have a hash that is several levels deep. ... You forgot to declare $depth. ... "use strict" would have told you about ... By choosing not to "use strict" you instruct Perl to assume any ...
    (comp.lang.perl)