The for (used as a statement modifier) in the original line of code takes a list which consists of three entries: newline, some number of spaces, and an item shifted off an array. If you remove the shift the last entry in the list becomes another list (the elements in the array) appended to the list containing the first two items. There is nothing there to prefix each element following the first two elements of the list with the concatenation of the first two elements which is what your expectation implies.

If you want to achieve that you have to rewrite the statement a little. Consider:

my @pre_push = qw(test test2); my $level = 2; my $buff = ''; $buff .= $_ for map {"\n" . (' ' x ($level - 1)) . $_} @pre_push; print $buff;

Prints:

test test2

The map concatenates the prefix to each element of the array before passing the modified list on to the for.

Alternatively you could move the prefix concatenation to the assignment to the left of the for to give the same result and remove the map:

$buff .= "\n" . (' ' x ( $level - 1)) . $_ for @pre_push;
True laziness is hard work

In reply to Re^3: How do I read this line of code? by GrandFather
in thread How do I read this line of code? by pmarcoen

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.