- Your question is all about scoping.
- hippo gave you a link for learning about scoping.
- Not using use strict;is interfering with your ability to see the problem.
- Not using use warnings;is also interfering with your ability to see the problem.
- You define a variable's scope using the mystatement*.
- The scope of a variable (where you can use it) is determined by where you define it
- If you want to use the variable outside of its current scope, move it
*There are other ways to define a variable, but let's keep it simple for now and just work with the mystatement.
Perhaps some code snippets will help you visualize this:
This one will fail, but will fail silently since there is no use strict;:
my $test1 = 1;
{
my $test2 = 2;
print " Inside: test1 = [$test1] test2 = [$test2]\n";
}
print "Outside: test1 = [$test1] test2 = [$test2]\n";
Results:
D:\PerlMonks>scope1-fail.pl
Inside: test1 = [1] test2 = [2]
Outside: test1 = [1] test2 = []
D:\PerlMonks>
|
<--- $test2 is blank!
|
Adding use strict; allows us to see the problem:
use strict;
my $test1 = 1;
{
my $test2 = 2;
print " Inside: test1 = [$test1] test2 = [$test2]\n";
}
print "Outside: test1 = [$test1] test2 = [$test2]\n";
Results:
D:\PerlMonks>scope2-fail.pl
Global symbol "$test2" requires explicit package name at D:\PerlMonks\
+scope2-fail.pl line 8.
Execution of D:\PerlMonks\scope2-fail.pl aborted due to compilation er
+rors.
D:\PerlMonks>
Adding my $test2;outside the block clears the error, but it still fails silently:
use strict;
my $test1 = 1;
my $test2;
{
my $test2 = 2;
print " Inside: test1 = [$test1] test2 = [$test2]\n";
}
print "Outside: test1 = [$test1] test2 = [$test2]\n";
Results:
D:\PerlMonks>scope3-fail.pl
Inside: test1 = [1] test2 = [2]
Outside: test1 = [1] test2 = []
D:\PerlMonks>
|
<--- $test2 is still blank!
|
Adding use warnings;allows us to see what is wrong:
use strict;
use warnings;
my $test1 = 1;
my $test2;
{
my $test2 = 2;
print " Inside: test1 = [$test1] test2 = [$test2]\n";
}
print "Outside: test1 = [$test1] test2 = [$test2]\n";
Results:
D:\PerlMonks>scope4-fail.pl
Inside: test1 = [1] test2 = [2]
Use of uninitialized value $test2 in concatenation (.) or string at D:
+\PerlMonks\scope4-fail.pl
line 10.
Outside: test1 = [1] test2 = []
D:\PerlMonks>
Finally, eliminating the duplicate mystatement (and adding other best practices like exit;and __END__ ), it runs and works:
use strict;
use warnings;
my $test1 = 1;
my $test2;
{
$test2 = 2;
print " Inside: test1 = [$test1] test2 = [$test2]\n";
}
print "Outside: test1 = [$test1] test2 = [$test2]\n";
exit;
__END__
Results:
D:\PerlMonks>scope5-win.pl
Inside: test1 = [1] test2 = [2]
Outside: test1 = [1] test2 = [2]
D:\PerlMonks>
A lot of people make the mistake of leaving off strictand warningson the misguided belief that it makes their work easier. The truth is quite the opposite, as you can see. Let the Perl interpretter debug your code for you. Why do it the hard way?
|