perl - Variable value not passing through loop -


i have loop checks see whether files in directory uploaded directory checking see if number of files constant.

starting out, knows there $before files in directory ("before" looks again).

use strict; use warnings; $after = 0; $before ="10";  until($before == $after) {     #check again, changes value of $after.     #now....     $after = "20";       if ( $after == $before ) {         print "after before. moving out of until because files there!\n";     }     else {         print "after isn't before.\n";         $before = $after;#set $before new value after update         $after = 0; #this updated in next update         sleep 1;     } } 

when run this, claims setting $before $after in else{}, but, in fact, $before remains 10, set before, , program loops endlessly.

the script runs correctly when remove mys inside else{}:

use strict; use warnings; $after = 0; $before ="10";  until( $before == $after ) {     #check again, changes value of $after.     #now....     $after = "20";       if($after == $before) {         print "after before. moving out of until because files there!\n";     }     else {         print "after isn't before.\n";         $before = $after;#set $before new value after update         $after = 0; #this updated in next update         sleep 1;     } } 

does mean $before defined 'my $before' within else not same variable '$before' defined above it?

when write code, always indent correctly. it's difficult see how code executes, , can hide errors when code poorly indented. also, use white space , don't double statements. 90% of programming maintenance, little typing make code easy understand saves ton of time in maintaining it.

i've reinvented code, read it.


when use my declare variable, declaring lexically scoped. means is defined in limited context. ususally thing. means variables disappear when no longer need them. however, means variables disappear when need them.

my variables live in block they're defined. if defined inside while/until loop, live within while loop. once exit while, go bye-bye. same thing if statement or for loop. in fact, if put curly braces, my variable, if defined in curly braces lose it's definition once outside:

{     $foo = "bar"; } print "$foo\n";   # whoops, $foo undefined! 

if need variable live outside of loop, needs defined outside of loop:

my $odd_count = 0; $number ( @number_list ) {     if ( $number % 2 ) {          $odd_count++;     } } print "there $odd_count numbers in list\n"; 

you can end redefining variable if redeclare my:

use warnings; use strict; use feature qw(say);  $foo = "bar"; {    # new block!      "initial value of \$foo: $foo";      $foo = "foo";   # using "my" on $foo      "changed value of \$foo: $foo"; } "final value of \$foo: $foo"; 

if run this, you'll get:

initial value of $foo: bar changed value of $foo: foo final value of $foo: bar 

that's because you've redeclared $foo inside block, have new variable $foo cover old value of $foo until block ends. once block ends, the old$foo` variable back, old value:

my $foo = "bar"; {    # new block!      "initial value of \$foo: $foo";      $foo = "foo";   # no "my" on $foo      "changed value of \$foo: $foo"; } "final value of \$foo: $foo"; 

if run this, you'll get:

initial value of $foo: bar changed value of $foo: foo final value of $foo: foo 

in case, removed my. when $foo = "foo";, i'm using same $foo defined outside block, once leave block, $foo still have old value.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -