On Fri, Nov 15, 2002 at 11:50:52PM +0200, [EMAIL PROTECTED] wrote:
> Michael Lazzaro writes:
>  > Let's summarize some of the string-to-num issues:
>  > 
>  >    my int $i = literal "0xff";   # 255
>  > 
>  > 
>  > (3) -- We want to be able to parse a string as a number using a very 
>  > _specific_ rule; for example, if a user is expected to enter a value in 
>  > a specific format, like octal "755" or hex "FF", we want to "know" what 
>  > we're expecting.  That implies there may be functions for each common 
>  > format:
>  > 
>  >    my int $i = sci "1e33";
>  >    my int $i = hex "00ff";
>  > 
> 
> the default behaiviour of "literal" should be same as perl parsing of
> literals. but sub literal can have additional parameters . 


Could we come up with a name other than "literal"?  This function's
job is to take a string (which may be a literal, user input, or
whatever), and turns it into a number--the human-expected version of
that number, not necessarily the result you would get if you simply
numified the string.

How about "to_num"?  Or even just "num"?

 

> also it seems that there sould be symmetry ( as was declared ) between
> types and type - casting functions 
> 
> so , probably "literal" should be called "num" or "int" or "uint16" ,
> depending on  WYW . or the casting may be let to happen in two stages
> : string -> num -> specific num type  ,e.g. uint16

How about if we got adverbial on the problem:

        my $str = "0xff";
        my $i = num $str;            
        my $i = num $str : uint16;   
        my $i = num "1.0_731e3" : float;
        my $i = num "1.0_898_798_798_794e7" : double;

And this makes an interesting corner case:

        my $i = num "10_000" : byte;  # value too large for 1 byte

I can see two ways to handle this--we can have a compile-time warning,
or we could store it to the smallest type that will hold the value and
issue a warning.  


>  > (4) -- We want to be able to output numbers to strings according to 
>  > specific formatting rules.  One way to do this (aside from using 
>  > sprintf) is to use sprintf-style formatting strings, perhaps as 
>  > properties to tie them to individual instances:
>  > 
>  >     my int $i = literal "0xff" but as_string('%02x');

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;

--Dks

Reply via email to