Re: String to Num (was Re: Numeric Literals (Summary))

[email protected] (Michael Lazzaro) Wed, 20 Nov 2002 11:57:33 -0800
Newsgroups perl.perl6.documentation
Message-ID <[email protected]>
On Wednesday, November 20, 2002, at 11:01  AM, Dave Storrs wrote:
>>> Actually, this would be a good reason to have a function called
>>> "literal" -- if it went both ways.  So, I could do this:
>>>
>>> 	print literal(200+55):hex;  # ==  print "0xff";
>>> 	print literal("0xff));      # ==  print 255;
>
> [email protected] wrote:
>> why not str ?
>>> 	print str( 200+55 :"format"=>"hex");  # ==  print "0xff";
>>> 	print str("0xff");      # ==  print 255;
>
> It's just a cognitive difference--in my example, literal() would
> return a string if given a number and return a number if given a
> string (in each case, the output would be precisely what you would get
> had you written the input as a literal in the other format, hence the
> 'literal' name).  In your example, str() does the same thing--but I
> would suggest that a function named 'str' should always return a
> string.

.... and _I'm_ trying to promote the reuse of the old "oct/hex" 
functions to do a similar both-way thing, such that:

     my $i = num '10';    # $i is a num, 10.0
     my $i = int '10';    # $i is an int

     my $i = sci '10';    # $i is a num, 10.0
     my $i = bin '10';    # $i == 2
     my $i = oct '10';    # $i == 8
     my $i = hex '10';    # $i == 16

     my int $i = 255;
     my str $s = num $i;  # output numeric $i as normal
     my str $s = int $i;  # output numeric $i as normal
     my str $s = sci $i;  # output $i as exponential notation
     my str $s = bin $i;  # output $i as binary
     my str $s = oct $i;  # output $i as octal
     my str $s = hex $i;  # output $i as hex

.... but people just aren't biting, so far.  Don't see why not, I think 
it's keen.

;-)

MikeL