in reply to problem with foreach

You can't stack statement modifiers, thus
$b ++ if ( $_ eq "b" ) foreach @a ;
can't possibly work. Unfortunately. See perlsyn:
Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending).
Note the stress on "single". That's what it means.

This will work:

( $_ eq "b" ) and $b ++ foreach @a ;
as will this:
foreach(@a) { $b ++ if ( $_ eq "b" ); }

p.s. I think it's not good style to detach the ++ operator from the lvalue it's working on. So, use $b++ instead of $b ++.

And in the

statement if condition;
syntax, the parens around the condition are unnecessary.

Replies are listed 'Best First'.
Re^2: problem with foreach
by jeanluca (Deacon) on Jun 16, 2006 at 08:34 UTC
    do you mean there is a difference between $b++ and $b ++ ?
      Apparently, there isn't. But I had to test it.

      I just have a very hard time reading and understanding it — while it's just such a small thing. Bring on the huge expressions!

      Your whitespace isn't improving readability — on the contrary.

      You should remember that there _is_ a difference betwixt ++$b and $b++, however. ++$b doesn't have the nuisance of making a copy of its original value to return before incrementing, and therefore it's slightly less efficient if, as in this case, you don't need to know what that value was.