You start out with "you have $count left" and then some refactoring leads to $count being replaced by $new+$old and you have too much sense to do something bizarre like "you have ${\($new+$old)} left" (which doesn't scale well anyway when the strings and expressions grow to the point that more than one line is called for) so the next obvious choice is "you have " . $new+$old . " left" which is, unfortunately, quite wrong. Fixing it starts to give you a hint of how concatenation can suck, "you have " . ($new+$old) . " left".
Lessee, in Perl 6 we fixed the precedence of concatenation with respect to + (at your suggestion), so that part's okay...

If you want to keep your current style you can write:

$html ~= [~] createLink( $USER, "you" ), " have ", $new + $old, " message", 1 == $new+$old ?? "" !! "s", " left";
but you can also say:
$html ~= createLink( $USER, "you" ) ~ " have " ~ $new + $old ~ " message" ~ (1 == $new+$old ? "" : "s") ~ " left";
or you can interpolate with closures:
$html ~= "{createLink $USER, "you"} have { $new + $old } message{" +s" x ($new+$old == 1)} left";
or spread it out blockishly:
$html ~= "{ createLink $USER, "you" } have { $new + $old } message{ "s" x ($new+$old == 1) } left";
or you can use methods and subs:
$html ~= "$USER.createLink("you") have &pl($new+$old,"message") le +ft";
or some combination of those...
I've also come to dislike sprintf for such concatenations. I don't like the out-of-order nature between non-format-specifier parts of the first argument and the other items being concatenated into the result. And I don't like the separation between the format specs and the values being formatted. I've also seen too many mistakes like sprintf "bleh $foo%s bar", complex($expr) where $foo might contain a % character. In most cases I find that the majority of the parts are simply being concatenated so if any of the items need 'printf' formatting, then I use sprintf to format just that item:
$html .= join '',
    createLink( $USER, "you" ),
    " have a ",
    sprintf( "%+.2f", $rating ),
    " rating";
that would now look more like:
$html ~= [~] createLink( $USER, "you" ), " have a ", $rating.fmt("%+.2f"), " rating";
or maybe just:
$html ~= "{createLink $USER, "you"} have a $rating.fmt("%+.2f") ra +ting";
Which might lead to the suggestion of here-docs, which I really dislike since they can't be indented and can fail very confusingly in the face of invisible trailing whitespace.
Well, we fixed the indent part at least, though you're still on your own with trailing whitespace.
I hope that leads to some to think of suggesting a "real" templating system. That's often an excellent suggestion. Of course it isn't an appropriate replacement for tons of cases of concatenation. (:
Indeed. Though with enough Ways To Do It you almost don't need a templating system... :)

In reply to Re^2: Perl style: Arguing against three-argument join() (++join '') by TimToady
in thread Perl style: Arguing against three-argument join() by martin

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.