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".


In reply to Re: Question on syntax by mscharrer
in thread Question on syntax by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.