bhushanQA has asked for the wisdom of the Perl Monks concerning the following question:

I have this code:
if ($resp->is_success) { my $location_id= $resp->header( 'Location' ); #my $str = print "Received reply: $message\n"; # print "$message\n"; $location_id=~ /.*\/(\d+)$/; my $location_id= $1 || ''; print "$location_id\n"; }
The variable location_id's scope is inside the box only, but I want to use the same variable and its value. How Can I use that variable and its value outside the box.

Replies are listed 'Best First'.
Re: how to get the local variable value outside the box
by hippo (Archbishop) on Jun 30, 2015 at 10:01 UTC
    1. Please use <code> tags. Without them your code is very hard to read. Fixed now.
    2. To what box do you refer?
    3. I can only guess that this is a scoping problem. Try declaring your variable first.
    my $location_id; if ($resp->is_success) { $location_id = $resp->header( 'Location' ); } print $location_id;

    Update: Now that the code is legible it is easy to see that you have declared $location_id twice in the same block. If you had used warnings this would have been alerted to you. Don't double-declare.

      Please check now
Re: how to get the local variable value outside the box
by 1nickt (Canon) on Jun 30, 2015 at 10:43 UTC

    As Hippo said:

    Your question was how to use $location_id outside the box (the scope in which it was declared; your sub, or if statement...)

    But you have a separate problem which is that you declare my $location_id twice. As Hippo said, you should always use warnings; at the top of your script, and perl will tell you about these errors. You must only declare a variable once.

    When you have declared it with my $location_id, it will stay in existence until the end of your scope. If you have a "box" after you declare the variable, the variable will be available in the "box," and again once you exit the "box," reflecting any changes you made to its value inside the "box."

    use strict; use warnings; my $location_id = 'foo'; if ($resp->is_success) { # the box? $location_id = 'bar'; } say $location_id; # prints 'bar'
Re: how to get the local variable value outside the box
by marinersk (Priest) on Jun 30, 2015 at 15:14 UTC

    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?

Re: how to get the local variable value outside the box
by AnomalousMonk (Archbishop) on Jun 30, 2015 at 18:38 UTC