On Oct 30, 11:15 am, [EMAIL PROTECTED] (Chas. Owens) wrote:
> 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)

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


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


Reply via email to