On Wed, Nov 20, 2002 at 05:51:17PM -0500, Tanton Gibbs wrote:

> 
> It's going to be hard to beat sprintf( "%x", $i ) for clarity or
> conciseness.

Unfortunately, it's pretty easy to beat it for readability.  It's also
a holdover from C, an ancestor language that we are (at least to a
degree) trying to outgrow.

I actually rather like MikeL's suggestion for the unary ops; clear,
concise, and highly readable.  And look:

        my str $s = sprintf("%x", $i);    # 30 characters
        my str $s = hex $i;               # 19 characters
        my $s = ~hex $i;                  # 16 characters

These all do the same thing, but the third one supplies the string
context explicitly...and is half the size of the sprintf version.

As to the other formatting that sprintf supports, I would suggest
doing those with properties, not adverbs.  Here's why:

        #   Could you really tell what this does at a glance?
        my $s = ~hex:w4p2d0rs $i;   

        my $s = ~hex $i but width 4 
                        but precision 2 
                        but padded 0
                        but rightjustified
                        but usesign;

The second one is definitely longer...and substantially longer than
the equivalent sprintf.  But I also don't need to dig out my K&R to
remember how to write it.


As a tangent...one of the things that has bothered me about "but" and
"is" for properties since the beginning is that they make for
excessively long code.  Does this bother anyone else?

--Dks

Reply via email to