[tpm] dereferencing anonymous hashes

Uri Guttman uri at stemsystems.com
Fri Apr 13 14:16:50 PDT 2007


>>>>> "RJ" == Rob Janes <janes.rob at gmail.com> writes:

  RJ> here's more of my 2 cents (have fun Uri! :)

  RJ> I notice there's two examples of for, but no examples of multiple
  RJ> lexical (my) variable assignments in for.

the reason is that the c style for loop should almost never be used.

  RJ> for ($i = 1, $j = 6; $i < 10; $i++, $j++) {

i used to similar things in c. i would never in perl as i rarely need
integer indexes in perl. i can practically count the number of c style
loops i write on one hand. in fact i just checked. in 9933 lines of the
stem project, i found 1 c style for loop. and i don't go out of my way
to avoid them. they just don't enter my perl vocabulary anymore. perl
has such nice arrays and lists that indexing into them in a c style loop
makes little sense. you loop OVER a list of things (such as hashes) and
work on them. if you need an index into 2 arrays they should be
refactored or designed to be an array of hashes. accessing an array
element in a for list is much faster than using an index variable too.

  RJ> for (my $i = 1; $i < 10; $i++) {

for my $i ( 1 .. 10 ) {

also less chance of a fencepost error with the for list style.


  RJ> for (my $i=1, my $j=6; $i < 10; $i++, $j++) { print "$i\n" }

for my $i ( 1 .. 10 ) {

	my $j = $i + 6 ;

much easier to read and follow IMO. again less chance of fencespost
errors.

you just need to change how you think. remember i coded in c for over 20
years and did some deep complex stuff in it. i have long ago
transitioned to perl and i don't ever think about c style for
loops. they are just about never needed in perl and coding them is
usually a red flag to me that a better design is warranted.

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


More information about the toronto-pm mailing list