in reply to how to get the local variable value outside the box
*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:
|
|
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:
|
|
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?
|
|---|