in reply to Exploiting Perls idea of what is a number

If you approach the equivalence from the other direction, it becomes easier:

Two strings are equal iff

  1. ... they are equal as characters
  2. or ... they are equal as numbers

When described that way, the expression becomes:

$a eq $b || $a == $b

This has the small drawback of equating undef and the empty string with 0 as well though.

Update It also has the other small drawback of considering all strings to be equal, which is a bit suboptimal...

Replies are listed 'Best First'.
Re^2: Exploiting Perls idea of what is a number (oops)
by tye (Sage) on Jul 08, 2008 at 12:57 UTC

    That has the large drawback of declaring that "foo" is the same as "cheesecake" (while emitting two warnings). :)

    - tye        

      Maybe !($a cmp $b || $a <=> $b) would work... nope.

      -Paul

      That has the large drawback of declaring that "foo" is the same as "cheesecake" (while emitting two warnings). :)

      In my particular application, a foo is indeed the same than a cheesecake!

      -- 
      Ronald Fischer <ynnor@mm.st>

        Then use $a == $b. The $a eq $b || is just obfuscation. The only thing it changes is that it prevents warnings when given two equal, non-numeric strings (because if $a eq $b, then it will also be true that $a == $b).

        - tye        

        Then all you need is
        my $result = do { no warnings 'numeric'; $x == $y };
Re^2: Exploiting Perls idea of what is a number
by rovf (Priest) on Jul 08, 2008 at 13:11 UTC
    $a eq $b || $a == $b

    I think I was too early with my response. Your suggestion, as elegant as it looks, would fail if $a and $b are unequal strings. In this case $a eq $b would result false, and $a == $b would be evaluated, yielding an error message because the arguments are not numeric...

    -- 
    Ronald Fischer <ynnor@mm.st>
Re^2: Exploiting Perls idea of what is a number
by rovf (Priest) on Jul 08, 2008 at 12:56 UTC

    Thank's a lot for the suggestions!! I think for my original problem I find your solution most convenient, but actually there is another part of the program where I now can replace my cumbersome regexp to test for numerics, by moritz's idea of using Scalar::Util.

    -- 
    Ronald Fischer <ynnor@mm.st>