Re: Help! Strings -> Numbers

[email protected] (Michael Lazzaro) Fri, 22 Nov 2002 10:49:08 -0800
Newsgroups perl.perl6.documentation
Message-ID <[email protected]>
On Thursday, November 21, 2002, at 11:22  AM, Tanton Gibbs wrote:
>> It would be very useful in the second half of a s///, too (grab a
>> match, rewrite it in a canonical form):
>>
>>      $s ~~ s/<number>/<number:MoneyFormat>/;
>
> once again, I think it could just be
>         $s ~~ s/<number>/$1.as(MoneyFormat)/

Or, using hypothetical vars:

    $s ~~ s/ $num := <number> /$num.as(MoneyFormat)/;

(This is a bit off-topic, but it's a tiny bit sad that you can name 
subrules, but you can't refer to them by name in the interpolation 
unless you use hypothetical vars.  Eh, no biggie: it would introduce 
too many problems to allow it.)

Still, the idea of having "input rules" and "output rules" that are 
conceptual mirrors of each other just seems so... sweeeeet.  I find the 
syntax

         $s ~~ s/<number>/<number:MoneyFormat>/;
or
         $s ~~ s/<number:$n>/<$n:MoneyFormat>/;
or even
         $s ~~ s/<number>/<$1:%-4.2f>/

to be wonderfully appealing.


On Thursday, November 21, 2002, at 11:49  AM, Dave Whipp wrote:
> The inverse-regex was discussed on perl6-language a few months
> back (I think the context was an alternative to pack/unpack): I
> don't recall any definitive conclusions. A simple proposal to pass
> a match object/hash as an input to the rule falls foul of too many
> reverse ambiguities. But a constrained regex syntax could be made
> to work.

I thought so too, but I haven't been able to find it again.  If anyone 
has a link, please let me know.


MikeL