Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

"42" ~~ "42.0" is false in 5.10

by szabgab (Priest)
on Dec 24, 2007 at 12:59 UTC ( [id://658876]=perlquestion: print w/replies, xml ) Need Help??

szabgab has asked for the wisdom of the Perl Monks concerning the following question:

Yesterday I posted Smart Match is not recursive in 5.10 now I found another issue that is valid based on the documentation but is strange to me. This is false:
"42" ~~ "42.0"
Both sides are Strings though they are both numish strings. IMHO they should be compared as numbers but they are compared as strings. Does anyone have an explanation why is that so?

Replies are listed 'Best First'.
Re: "42" ~~ "42.0" is false in 5.10
by syphilis (Archbishop) on Dec 24, 2007 at 13:17 UTC
    IMHO they should be compared as numbers but they are compared as strings

    Well ... they *are* both strings ... and you are doing nothing that dictates that those strings should be numified. The behaviour seems totally reasonable to me.

    OTOH, "42" * 1 ~~ "42.0" * 1 should (and does) evaluate as true - because, in this instance, something *is* being done to numify the strings.

    Cheers,
    Rob

      Hi,

      To me it looks right:

      If it looks like a string, it will be treated as string.

      Here's a small programm to test the new ~~ Operator.

      use strict; use warnings; use 5.010; ## No quotes &test_smartmatch(42, 42); # yields 42 smartmatches 42 &test_smartmatch(42,42.0); # yields 42 smartmatches 42 ## Single Quotes &test_smartmatch(42,'42.0'); # yields 42 smartmatches 42.0 &test_smartmatch('42','42.0'); # yields 42 doesn't smartmatch 42.0 ## Double Quotes &test_smartmatch("42","42.0"); # yields 42 doesn't smartmatch 42.0 ########################################################### sub test_smartmatch { my $lhs = shift @_; # Left Hand Side (LHS) my $rhs = shift @_; # Right Hand Side (RHS) if ( $lhs ~~ $rhs ) { say "$lhs smartmatches $rhs"; } else { say "$lhs doesn't smartmatch $rhs"; } } ## ########################################################### __END__

      Just my 2 cents.

      Hope this helps.

Re: "42" ~~ "42.0" is false in 5.10
by FunkyMonk (Chancellor) on Dec 24, 2007 at 13:09 UTC
    From perlsyn
    the first row that applies, in either order, determines the match beha +viour ... Num numish[!] numeric equality $a == $b Any Str string equality $a eq $b Any Num numeric equality $a == $b Any Any string equality $a eq $b

    So surely it's using the second case (Any ~~ Str) using string equality.

      yes, I see that it is correct based on the perlsyn. The question what is the clever reasoning behind the fact that there is no numish ~~ numish using == rule in the table slightly above the Any ~~ Str?

      Indeed. So then 42 ~~ "42.0" would probably be true. I need to get 5.10 installed. I wonder how long before the ubuntu devs will have it ready.

      -Paul

Re: "42" ~~ "42.0" is false in 5.10
by TOD (Friar) on Dec 24, 2007 at 13:07 UTC
    i don't know anything about perl 5.10 yet. however, a string is a string, and from a string's point of view 42 is rather not 42.0 - retrieve a 0.0 value through DBI and ask whether it's true or not. ;)
    --------------------------------
    masses are the opiate for religion.
Re: "42" ~~ "42.0" is false in 5.10
by ysth (Canon) on Dec 24, 2007 at 21:23 UTC
      The +sperm and sperm+ operators?

      :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://658876]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found