Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Declaring a Variable [for BEGINNERS]

by ikegami (Pope)
on Dec 27, 2006 at 21:17 UTC ( #591941=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://591941]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-12-04 17:30 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (30 votes). Check out past polls.