Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Declaring a Variable [for BEGINNERS]

by ikegami (Patriarch)
on Dec 27, 2006 at 21:17 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-20 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found