Perl 5.10.1 Smartmatch Num ~~ Numish

Discussion in 'Perl Misc' started by tuser, Aug 30, 2009.

  1. tuser

    tuser Guest

    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 ?
    tuser, Aug 30, 2009
    #1
    1. Advertising

  2. On 2009-08-31, Ben Morrow <> wrote:
    >> 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, but I would guess it's so
    > that something like
    >
    > my $x = 3;
    > print $x;
    > "3.0" ~~ $x;
    >
    > does what is expected. The 'print' statement has converted $x into a
    > dualvar, and there is no way to tell which part (string or number) came
    > 'first',


    I hope there is. At least there was a way - after I "regularized"
    Perl handling of numbers.

    Obviously, there MUST be a way - otherwise one would not be able to
    "correctly" translate a 2-headed value into a 3-headed one... (Ask if
    one wants to know more details. ;-)

    Hope this helps,
    Ilya
    Ilya Zakharevich, Sep 1, 2009
    #2
    1. Advertising

  3. On 2009-09-01, Ben Morrow <> wrote:
    >> > Numeric conversion between IV, UV and NV don't use the string value
    >> > at all.

    >>
    >> This is a bug. Consider $a = "1234568790123445678". After you use it in
    >> an NV context, it might be converted to 1234568790123445504; this
    >> value MUST be stored in the NV slot to avoid future slowdowns.
    >>
    >> However, this value may be exactly representable as an integer value.
    >> So the stored in NV slot value MUST be marked as "not trustworthy", so
    >> that a future conversion to integer would go PV --> IV, not NV --> IV.
    >>
    >> All this was working about 10 years ago...

    >
    > Now it appears to do the IV conversion straight away (to preserve the
    > precision):


    PV --> NV is only one of 6 possible pathways; doing *this one*
    straight away is not going to be a waste of space, sine PVNV has an IV
    slot anyway; however, the others might have serious effects on memory
    usage...

    Yours,
    Ilya
    Ilya Zakharevich, Sep 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D. Shane Fowlkes
    Replies:
    2
    Views:
    569
    D. Shane Fowlkes
    Dec 16, 2004
  2. bbmerong
    Replies:
    0
    Views:
    117
    bbmerong
    Jan 17, 2008
  3. bbmerong
    Replies:
    0
    Views:
    149
    bbmerong
    Jan 18, 2008
  4. bbmerong
    Replies:
    2
    Views:
    139
    Ken Bloom
    Jan 18, 2008
  5. David Combs
    Replies:
    3
    Views:
    136
    Dr.Ruud
    Apr 11, 2011
Loading...

Share This Page