develooper Front page | perl.perl5.porters | Postings from September 2009

Re: Perl 5.10.1 Smartmatch Num ~~ Numish Options

Thread Previous | Thread Next
From:
Rafael Garcia-Suarez
Date:
September 1, 2009 14:55
Subject:
Re: Perl 5.10.1 Smartmatch Num ~~ Numish Options
Message ID:
b77c1dce0909011455pfdf7c6ch3b5d136878deea59@mail.gmail.com
2009/9/1 T User <tuser1@gmail.com>:
> On 31 août, 13:13, Ben Morrow <b...@morrow.me.uk> wrote:
>> Quoth tuser <tus...@gmail.com>:
>>
>> > Why is 2 ~~ '2.0' false in Perl 6 (case #A2), whereas the identical
>> > expression in Perl 5.10.1 (case #B2) is true ?
>>
>> > Or in other words:
>> > Why is there a rule in Perl 5.10.1 ("Num numish numeric equality"),
>> > where there is no equivalent in Perl 6 ?
>>
>> For a proper answer you would have to ask p5p
>
> Here is the question that goes to p5p:
>
>  In Perl 6, I find the following about smart matching:
>
> http://perlcabal.org/syn/S03.html#Smart_matching
>
> $_     X     Type of Match Implied Match if (given $_)
> ====== ===== ===================== ===================
> [...]
> Any    Num   numeric equality      +$_ == X
> Any    Str   string equality       ~$_ eq X
> [...]
> ====== ===== ===================== ===================
>
> That means in Perl 6:
> =====================
> case #A1: 2 ~~  2.0  translates into 2 ==  2.0  ==> true
> case #A2: 2 ~~ '2.0' translates into 2 eq '2.0' ==> false
>
> In perl 5.10.1, I find the following about smart matching:
>
> http://search.cpan.org/~dapm/perl-5.10.1-RC1/pod/perlsyn.pod#Smart_matching_in_detail
>
> $a      $b        Type of Match Implied    Matching Code
> ======  =====     =====================    =============
> [...]
> Any     Num       numeric equality         $a == $b
> Num     numish[4] numeric equality         $a == $b
> [...]
> Any     Any       string equality          $a eq $b
> ======  =====     =====================    =============
> [4] either a real number, or a string that looks like a number
>
> That means in Perl 5.10.1:
> ==========================
> case #B1: 2 ~~  2.0  translates into 2 ==  2.0  ==> true
> case #B2: 2 ~~ '2.0' translates into 2 == '2.0' ==> true
>
> The question I have is:
> Why is 2 ~~ '2.0' false in Perl 6 (case #A2), whereas the identical
> expression in Perl 5.10.1 (case #B2) is true ?
>
> Or in other words:
> Why is there a rule in Perl 5.10.1 ("Num numish numeric equality"),
> where there is no equivalent in Perl 6 ?

Because Perl 5 has a weaker type system.

In Perl 5 you can't make the difference between a string and a number.
You can just tell whether the conversion to a number will "loose"
some data (as when you do 2+"3a" and get the result 5 and a warning.)

The smartmatch dispatch is driven by what's on the right, except that
you can't tell the difference between a real Num and something num-ish
that you intended to use as a num. So the perl 5 rule can also be
loosely formulated as: "use == to compare if that's not going to warn;
else use eq".

This approach will hopefully save ~~ from the problems Perl 5 has with
bitwise operators (like &, that notes both numeric and string bitwise
and operators).

-- 
Upward behind the onstreaming it mooned.
    -- Borges

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About