in reply to Question on syntax

First one word for spacing: inserting spaces around keywords like for is good and should be done IMHO. For funtion calls I normally do it differently. In general I would recommend to use perltidy for all your code.

About your examble: It's a litte confusion what you like to show. In this special, actuall quite special, case print @array; is the common because it calls print only once.

Perl is about having multiple ways to do one thing, so your 2nd and 3rd line differ only in style, not in correctness. Note that the style in the 3rd line would also work for multiple commands while the 2nd line wouldn't.

If you generalise this example to the case that you like to call one function several times with each element of a list/array, then I would code it like this:

function($_) foreach (<list>); # or
function($good_name) foreach my $good_name (<list>);
but, again, this is a matter of style and the question if you plan or there is the good chance to ever add multiple commands in the loop. Then the sytle in your 3rd line would be the right choice, simple because of better code maintainability.

Also note that AFIK for and foreach do the same with the same arguments, you just should "use for when you mean for and use foreach when you mean foreach".

Replies are listed 'Best First'.
Re^2: Question on syntax
by ikegami (Patriarch) on Apr 23, 2008 at 08:38 UTC
    I hope you don't, cause
    function($good_name) foreach my $good_name (<list>);
    and specifically
    $good_name(<list>)
    are not valid Perl.
      Thanks for pointing this out. I of course meant the following code:
      foreach my $good_name (1..10) { function($good_name) }
      in reverse style. I was just assuming that it's valid Perl. I only used the reverse style with if or while statements so far. For a reason my for/foreach loops are always bigger.