The following code may do what you want:

use strict; use warnings; my @pairs = ( [undef, undef], [0, undef], [0, 0], ['', undef], ['', ''], [1, und +ef], [1, 1], [0, '0'], [0, 0], ['0', '0'], ['0e0', '0'], ['x', '0'], ['x', 0], ); for (@pairs) { my @pair = (@$_); print defined $pair[0] ? ">$pair[0]<" : 'undef'; print ', ' . (defined $pair[1] ? ">$pair[1]<" : 'undef'); print ': ' . (equals (@$_) ? "match\n" : "different\n"); } sub equals { my ($lhs, $rhs) = @_; #False if mix of defined and undef return 0 if defined $lhs != defined $rhs; # True if both undef return 1 if ! defined $lhs && ! defined $rhs; # False if different as numbers no warnings "numeric"; return 0 if (0 + $lhs) != (0 + $rhs); # False if different as strings return 0 if ('' . $lhs) ne ('' . $rhs); return 1; }

Prints:

undef, undef: match >0<, undef: different >0<, >0<: match ><, undef: different ><, ><: match >1<, undef: different >1<, >1<: match >0<, >0<: match >0<, >0<: match >0<, >0<: match >0e0<, >0<: different >x<, >0<: different >x<, >0<: different

DWIM is Perl's answer to Gödel

In reply to Re: check if 2 values are equal by GrandFather
in thread check if 2 values are equal by eXile

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.