Thread Variablen Scoping (15 answers)
Opened by Student87 at 2013-02-10 19:37

Linuxer
 2013-02-15 23:40
#165825 #165825
User since
2006-01-27
3875 Artikel
HausmeisterIn

user image
Hi, bin zwar nicht pq, aber ich erlaube mir, auf die Dokumentation zu verweisen: perldoc -f strict

http://perldoc.perl.org/strict.html#strict-vars

perldoc strict: strict vars
This generates a compile-time error if you access a variable that was neither explicitly declared (using any of my, our, state, or use vars ) nor fully qualified. (Because this is to avoid variable suicide problems and subtle dynamic scoping issues, a merely local variable isn't good enough.) See my, our, state, local, and vars.
Code: (dl )
1
2
3
4
5
6
7
    use strict 'vars';
$X::foo = 1; # ok, fully qualified
my $foo = 10; # ok, my() var
local $baz = 9; # blows up, $baz not declared before
package Cinna;
our $bar; # Declares $bar in current package
$bar = 'HgS'; # ok, global declared via pragma

The local() generated a compile-time error because you just touched a global name without fully qualifying it.

Because of their special use by sort(), the variables $a and $b are exempted from this check.
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Variablen Scoping