1. Your question is all about scoping.
  2. hippo gave you a link for learning about scoping.
  3. Not using use strict;is interfering with your ability to see the problem.
  4. Not using use warnings;is also interfering with your ability to see the problem.
  5. You define a variable's scope using the mystatement*.
  6. The scope of a variable (where you can use it) is determined by where you define it
  7. 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?


In reply to Re: how to get the local variable value outside the box by marinersk
in thread how to get the local variable value outside the box by bhushanQA

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.