> > > my str $s = sprintf("%x", $i); # 30 characters
> > > my str $s = hex $i; # 19 characters
> > > my $s = ~hex $i; # 16 characters
> >
> > I think these are good, but I really think that Larry's idea of an "as"
> > function is the best. Not only does it provide a decent syntax
> > my str $s = $i.as("%x");
> >
> > it also has the ability to be overloaded for various user defined types.
> >
>
> I do agree that having it be a method (and hence overloadable) is the
> best solution. I just wish there were some way to get away from those
> dratted sprintf format strings.
Well, for the general case, you could create convienence functions that
handle getting the correct format
sub hex { return "%x"; }
print "\$i is $i.as($(hex))"
or
my $str = $i.as(hex);
Or, if you really dislike the notation, you could create a different kind of
wrapper
sub hex { return shift().as("%x"); }
my $str = hex $i;
This would have the benefit of just "forwarding" the call as well as a
prefix operator form.
You could also pass it arguments for formatting options.
my $str = hex(4) $i;
I'm not sure on the syntax, but if hex could return a function, which then
gets applied to $i it is possible.
Tanton