$d = "$a-${b}_$c";
Alternatively, use the simple string concatenation:
$d = $a . '-' . $b . '_' . $c;
| [reply] [d/l] [select] |
Thanks all, All of your suggestions would work.
| [reply] |
my $key= "$a-$b" . "_$c";
my $key= join '', $a, '-', $b, '_', $c;
# or
my $key= join '', "$a-$b", "_$c";
| [reply] [d/l] |
I avoid long runs of concatenation.
I don't want to criticize that, or try to change your mind, but it makes me wonder why you try to avoid them. Furthermore, you give three alternatives, but you exclude the obvious "$a-${b}_$c" which uses neither concatenations nor joins. Do you avoid long runs of interpolations as well?
| [reply] [d/l] |
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.
| [reply] [d/l] [select] |
$d = "$a-${b}_$c";
Update: missing {} added - Thanks moritz!
Perl's payment curve coincides with its learning curve.
| [reply] [d/l] |
my $d = $a . '-' . $b . '_' . $c;
If you don't play to join's strengths, don't use it. | [reply] [d/l] |
You received some good answers so I won't bother trying. I will however offer a pieces of advice.
Don't use one letter variable names for anything other loop variables, e.g. for my $i ( 0 .. 5 ).
Don't use $a or $b for anything other than a sort subroutine.
| [reply] [d/l] |