Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Variable scope in while loop

by euswdwj (Initiate)
on Dec 19, 2012 at 22:11 UTC ( #1009647=perlquestion: print w/replies, xml ) Need Help??
euswdwj has asked for the wisdom of the Perl Monks concerning the following question:

Having thought that one might be close to obtaining variable scope master in Perl such strange happenings presented today.

A while loop that never exits:

#!/usr/bin/perl use strict; while ( my $number != 10 ) { printf("Please, the number 10: "); $number = <STDIN>; }

A program that does not compile:

#!/usr/bin/perl use strict; while ( $number != 10 ) { printf("Please, the number 10: "); $number = <STDIN>; }

A while loop that works as expected:

#!/usr/bin/perl use strict; my $number; while ( $number != 10 ) { printf("Please, the number 10: "); $number = <STDIN>; }

While it is not my usual practice to program on a Wednesday, I have done so today. Could that be the reason or is there an other?

Replies are listed 'Best First'.
Re: Variable scope in while loop
by davido (Archbishop) on Dec 19, 2012 at 22:18 UTC

    In the first case, the scope of the $number variable is one iteration. Upon the next iteration, a new $number is created, and then tested whether or not it is equal to 10. It never will be; it's brand-spankin'-new.

    On the second version, it should be pretty obvious that $number is never being declared, which is a strictures violation.

    The third example is the only one that will work, because the lifetime of the $number variable needs to persist past the end of each iteration. The conditional test takes place at the beginning of the next iteration, so the variable's life must outlive the block in order to be tested.

    You might be thinking of while( my $input = <DATA> ) { .... }, which works because the expression populates $input from the <DATA> iterator before the conditional is tested.


Re: Variable scope in while loop
by frozenwithjoy (Priest) on Dec 19, 2012 at 22:18 UTC

    #1 is eternal because the my re-declares $number each time such that it gets reset to undef.

    #2 won't compile because you are using strict w/o declaring $number w/ my.

    #3 works because you put my $number outside the loop so that when the loop loops, $number contains the value you assigned to it instead of getting reset.

Re: Variable scope in while loop
by tobyink (Abbot) on Dec 19, 2012 at 22:19 UTC

    Basically, each time you say my $variable you get a brand, spanking new variable. So you're reading a number into the $number variable, but then you say my $number and get a brand, spanking new variable, which happens to have undef as it's value.

    my $number; $number = 40; my $number; if ($number > 39) { print "bigger than 39\n"; } elsif ($number < 1) { print "smaller than 1\n"; }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Variable scope in while loop
by Athanasius (Chancellor) on Dec 20, 2012 at 02:37 UTC

    Hello euswdwj, and welcome to the Monastery!

    In the case where the scope of the loop variable does not need to extend beyond the loop itself, a C-style for loop may be preferable to the more usual while:

    #! perl use Modern::Perl; my $number = 7; print "Before the loop, \$number is $number\n\n"; for (my $number = 0; $number != 10;) { print 'Please enter a number (10 to quit): '; chomp($number = <STDIN>); print "You entered: $number\n"; } print "\nAfter the loop, \$number is $number\n";

    Sample run:

    12:34 >perl Before the loop, $number is 7 Please enter a number (10 to quit): 1 You entered: 1 Please enter a number (10 to quit): 0 You entered: 0 Please enter a number (10 to quit): 100 You entered: 100 Please enter a number (10 to quit): 10 You entered: 10 After the loop, $number is 7 12:35 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009647]
Approved by marto
[stonecolddevin]: erix seems pretty good, i listened to "So Many Roads" and it was nice. Not everyone's cup of tea but this guy can absolutely shred, he and the guy from Mastodon are definitely up there: https://www. v=VzhMkSN9dQc
[erix]: ha, good stuff - loud and fast...
[stonecolddevin]: here's some Mastodon for when you have 13 minutes to have your mind blown: https://www. v=4pvfQtUhtNE
[karlgoethebier]: goes to Brownsville one more time..
erix squirrels away the linkies

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-06-22 21:48 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.