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;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |