Of course, in this particular case, the correct, optimal solution is trivial: my @grid = ( '01' .. '12' ) x 12;

Actually, no. That builds a single dimension array with 144 elements, not a 2 dimension array of 12 elements with 12 elements each.

In the more general case, I prefer another way, because it maintains symmetry: my @grid = map @$_, ( [ '01' .. '12' ] ) x 12;

Let's see.

  1. You build an anonymous array of twelve elements;
  2. replicate the reference to that 12 times into a list;
  3. Pass that list to map(*), where you dereference that reference 12 times to produce a list of 144 scalars which get assigned to the array you are initialising.

And after all that, for the sake of "symmetry", you still end up with the wrong thing?

I just read that back and it sounds sarcastic, but it is not intended to be. I relish the opportunity to explain and defend my choices of coding style that so many people apparently find...shall we say eccentric :)

We may never agree on this, but what you are trying to avoid  1 .. 12; in favour of  x 12;. Ie. You would like to avoid the generation of a list that doesn't get used except for the side-effect it produces.

But, in your code, you are producing a list of 12 references that only get used for the side-effect it produces! What is the difference between a list of 12 integers and a list of 12 references? Besides the memory consumed.

Whilst I agree that if/when the language supportes an x-like operator that executes it's left argument 12 times rather than executing it once and replicating it twelve times, I would use that in preference.

But until that operator becomes available, all those shinnanagins to avoid generating a list of integers that is only used for the side-effect of the iterations it causes, to me, introduces more obfusication, mis-direction and potential for misunderstanding and errors, than using the list for it's side-effects.

(*)Using the non-block form which even TheDamian agrees with me (in PBP) should be avoided.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^11: Thread on Joel on software forum : "I hate Perl programmers." by BrowserUk
in thread Thread on Joel on software forum : "I hate Perl programmers." by techcode

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.