in reply to Re: Perl style: Arguing against three-argument join() (++join '')
in thread Perl style: Arguing against three-argument join()

As is traditional, I disagee. I find this far clearer than any other option shown:

$html .= sprintf '%s have %d message%s left', createLink( $USER, "you" ), $new + $old, $new + $old == 1 ? '' : 's' ;
  1. The general format (and the constant parts) of the message are clearly identifiable on the first line.
  2. All the variable parts are clearly identified.
  3. You have complete control over the formating.
    • Leading zeros. Hex/octal.
    • Graceful transition to sci notation if number get big or small.
    • Left/right justification if needed.
  4. And it's a fully extensible construct that deals nicely with very long string too.
    $html .= sprintf '%s have %d message%s left' . ' occupying 0x%x %s of server space', createLink( $USER, "you" ), $new + $old, $new + $old == 1 ? '' : 's', $space, $units, ;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^3: Perl style: Arguing against three-argument join() (++join '')
by ysth (Canon) on Jan 25, 2008 at 22:43 UTC
    That should be %s have %s message%s left'. Using %d in perl is rarely the right thing to do:
    $ perl -we'printf qq[%d\n], 3e9' 3000000000 $ ssh shaggy "perl -we'printf qq[%d\n], 3e9'" -1294967296
    %d is a C concept, and doesn't map well to Perl's pick-one-of-three possible formats to store a number.

    %e/%f/%g are also not completely problem free, since they could end up losing precision on a 64 bit int/53 bit mantissa perl.

      I get where you are coming from, but the day a (perlmonks?) user has nine billion messages in the system, I think there will be slightly greater problems than a malformed informational message.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Your point is also taken, but using it, even to print a known single digit number, shows that it's in one's toolbox, and it really shouldn't be.
Re^3: Perl style: Arguing against three-argument join() (scale)
by tye (Sage) on Jan 25, 2008 at 02:02 UTC

    I think the first example you give is quite nice.

    I quite dislike your second example, however. I find it far too difficult to map between the too-many format specifiers spread (rather willy-nilly) over multiple lines and the unnumbered list of expressions below. If the format specifiers had numbers or names, then it would bother me less. I understand your point of disliking having the over-all format broken up.

    I've run into enough cases quite a bit longer than your second example and just don't find that sprintf scales to such situations. It can make quite clear code for the small cases, but I dislike the risk of variables being interpretted directly into the format string (because I've seen it happen so often when sprintf is used) and especially don't like how it doesn't scale. So I quickly jump to the method that scales the best, and also produces quite readable code, IMHO.

    - tye