Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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.


Comment on Re: Declaring a Variable [for BEGINNERS]
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-07-26 03:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls