http://qs1969.pair.com?node_id=591941


in reply to Declaring a Variable [for BEGINNERS]

While one doesn't need to limit the existance of a variable to the minimal scope in which it is used, the post to which you refere was lax with at least its loop counter variables. There's value in declaring all the variables in one spot (to document them easily), but there's no rarely any reason to access loop counters outside of a loop (and there's probably a better way when there is a reason).

my $region1; my $region2; . . . foreach $region1 (@region_list) { foreach $region2 (@region_list) { ... } }

would surely be better as

foreach my $region1 (@region_list) { foreach my $region2 (@region_list) { ... } }

In fact, declaring the variables at the top forced the author to come up with new variable names. Instead of having two loops iterating using $region, he had to create $region1.

my $region; my $region1; . . . foreach $region (@region_list) { ... } . . . foreach $region1 (@region_list) { ... }

would surely be better as

foreach my $region (@region_list) { ... } . . . foreach my $region (@region_list) { ... }

Unrelated, the author could have used true constants for the constants, and used an array to store the times. This would have reduced the number of variables and improved readability.