Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Which of the following is a typical way of writing Perl code?
print 1..10; print for (1..10); for (1..10) {print;}
Is any one of them more "correct" than the other?

Relatedly, is it more common to have a space between "for" and the open bracket "("?

Thanks in advance.

Replies are listed 'Best First'.
Re: Question on syntax
by pc88mxer (Vicar) on Apr 23, 2008 at 02:45 UTC
    Is any one of them more "correct" than the other?
    No - at least not by me. Note that the first one is substantially different from the latter two (calls print once instead of once for each element.)
    Relatedly, is it more common to have a space between "for" and the open bracket "("?
    I think it makes it more readable. I also put spaces around curly braces when they are used for code blocks, e.g. for (1..10) { print }

    Update: For comparison, here's the (default) output from Perl::Tidy:

    print 1 .. 10; for ( 1 .. 10 ) { print; } print for ( 1 .. 10 );
Re: Question on syntax
by j1n3l0 (Friar) on Apr 23, 2008 at 09:36 UTC
    As has been pointed out, the first line is different from the other two - it is printing the entire list at once whilst the other two are printing it one element at a time.

    IMHO, as long as its not a one-liner you should do the following:

    for my $number ( 1 .. 10 ) { print $number; }

    This would allow you/someone else to more easily add more steps to the loop:

    for my $number ( 1 .. 10 ) { $number *= $number; print $number, "\n"; }

    Hope that helps


    Smoothie, smoothie, hundre prosent naturlig!
      This would allow you/someone else to more easily add more steps to the loop

      The "easy to add another line" thing is a bit of a red herring; any decent editor can convert a postfix for to a block for with a single keypress.

      The real reason to prefer block for is that it allows you to bind the loop variable to something other than $_ (eg your $number), which often makes for more readable code.

Re: Question on syntax
by grizzley (Chaplain) on Apr 23, 2008 at 09:51 UTC

    In bigger scripts or to write one-liner? I used syntax print for 1..10 (no parens at all - this syntax IMHO is designed to economize on parens, too), for short lines, but in bigger projects lines became longer and soon I had to break those lines somehow and to decide which line is short and which one long. And decided to go back to 'normal' syntax in every case.

    for(1..10) { print; }

    if there is only one line of code inside loop/if. No space after 'for' but space after '{' and before '}'.

    And if second line of code must be added inside block or in 'for' there is more code, I always change to 'normal' mode:

    for(1..10) { print; $cnt++; }
    for(1..10, 12..30, 74..112) { print }
Re: Question on syntax
by mscharrer (Hermit) on Apr 23, 2008 at 08:33 UTC
    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".

      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.
Re: Question on syntax
by Anonymous Monk on Apr 23, 2008 at 04:21 UTC
    C:\>perl -le"print for 1..10 1 2 3 4 5 6 7 8 9 10 C:\>