in reply to Legible or Obfuscated?

Your examples are tiny enough that it may not matter, but I usually do use the version with blocks, because it translates to English more easilly.

for $element (@array) { print "$element\n"; }
"For each element in the array, print it with a line break."
print "$_\n" for @array;
"Print the current temp variable, for each element in the array."

It's a combination of having to mentally reverse it, and the use of $_ that makes it less legible. I always use a named variable instead of $_ when I can. It's better documentation.

Replies are listed 'Best First'.
Re^2: Legible or Obfuscated?
by demerphq (Chancellor) on Aug 10, 2004 at 22:19 UTC

    I agree the example here isn't the greatest. For instance I would almost definately write that line as:

    print join("\n",@array),"\n";

    So a better example IMO would be something like:

    $count{$_}++ foreach @element;

    Which I would translate as a one liner to "increment the count for each element".

    I also think its worth mentioning the 'with' use of for:

    print $_*$_+2*$_+1 for $some->('complex')->{'lookup'}->[equation];

    which IMO translates to "print n2+2n+1 with n equaling some complex lookup equation."


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi