in reply to Re^2: *Join* with more than one EXPR
in thread Join with more than one EXPR

I'm sorry, were the two nodes showing the "obvious" solution not enough? Well, there are three now, so I hope that finally suffices. :)

There are lots of ways to follow a variable's contents by a \w character or ' or :: or brackets/braces. Numerous of them I find less ugly than the "obvious" "${x}_", especially when you include cases like "${h{$k}}{$s}". This is simply a personal preference, not even something I bother to recommend to others. But it is worth demonstrating one or more of the alternatives on occasion.

Long runs of expressions separated by commas have a lot of advantages over longs runs of concatenations. Those advantages are just some of the reasons why I use join in places where others might use concatenations or sprintf or here-docs or large strings full of interpolations (or maybe even a templating system). The code that implements PerlMonks does a lot of building of strings and over the years I've tried a lot of different styles but join is the one that has survived, faring significantly better than the alternatives in the face of having to maintain such code.

Concatenation has the wrong precedence for even trivial cases like $i+1 . ") $a[$i]\n". Coders are quite used to formatting long runs of comma-separated expressions nicely while the "obvious" way to format a long run of concatenations (over multiple lines of code) is not clear, IME. The dot is easy to lose sight of and also easy to confuse with a comma. Conversely, the comma's cannonical positioning without whitespace in front of it and either at the end of a line or with whitespace after it, makes it easy to recognize. Thankfully, Perl allows the last expression to also be followed by a comma (not true with concatenating). join ' ' is also often a better choice, preventing some common mistakes like print "There are", $x->count(), "items.".

Okay, I'm tired. I'm sure there are still some advantages that I've neglected to mention just comparing concatenation to join. And I'm going to go to bed rather than mention any of the problems with sprintf or here-docs etc.

- tye