Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

scoping inside the loop

by vyeddula (Novice)
on May 17, 2013 at 17:51 UTC ( #1033998=perlquestion: print w/ replies, xml ) Need Help??
vyeddula has asked for the wisdom of the Perl Monks concerning the following question:

Case1:
do { $i=0; print"the value of i inside the loop is :$i\n"; $i++; }until($i<=10)

On executing above script i am getting output the value of i inside the loop is :0 .I am expecting the loop should execute.Please clarify

Case2:
use strict; my $i=5; do { print"the value of i is :$i\n"; $i--; }until($i<=0);

This program is executing as expected

Case3:
use strict; my $i=5; print "the value of i outside the loop is $i\n"; do { print"now i entered inside the loop \n"; my $i=10; print "the value of i inside the loop is :$i\n"; $i--; }until($i<=0);

This is failing to go through the loop. Please clarify why scoping inside the loop is not getting triggered.

Comment on scoping inside the loop
Select or Download Code
Re: scoping inside the loop
by LanX (Canon) on May 17, 2013 at 17:59 UTC
    • Case1: you're resetting $i each time at the beginning
    • Case3 the $i scoped within the block is differentš from the outer one
      (update: and you are resetting the inner one each time again)

    Generally better use prefix-constructs like

    while () {...} , until () {...} or  for (;;) {...}

    e.g. Case 3:

    use strict; use warnings; my $i=42; print "outside: $i\n"; for (my $i=5; $i>0; $i--) { print "inside: $i\n"; }; print "outside: $i\n"; my $j=666; print "outside: $j\n"; { my $j=5; do { print"inside: $j\n"; $j--; } until($j<=0) } print "outside: $j\n";

    prints:

    outside: 42 inside: 5 inside: 4 inside: 3 inside: 2 inside: 1 outside: 42 outside: 666 inside: 5 inside: 4 inside: 3 inside: 2 inside: 1 outside: 666

    As you can see, does the postfix construction force you to an extra block to limit the scope of the inner $j.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    Update

    expanded code example

    Footnotes
    1) see Coping with Scoping

      Thanks LanX.Understood but in second case which is a working condition $i's scope is in outer loop but how it got tucked in to inside loop.My understanding is if my is before a variable it's existence is strictly with in that boundary. In case 2 my is associated with $i so i think it only resides outside or where it is defined but i am confused how it got transported to loop.

        No, up to the point that you declare an inner $i with my the outer one is used.

        No inner my $i means the outer one is everywhere available.

        These are universal rules for lexical variables in almost any language!

        Have a look at the scoping-article I linked.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: scoping inside the loop
by hdb (Parson) on May 17, 2013 at 19:00 UTC

    case 1 does not loop because the "until" condition is met immediately ($i=1 which is less then 10).

      and otherwise it's an endless-loop

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        That makes clear

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033998]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-07-26 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls