in reply to Testing For Numberness Vs Stringness
If you really need to differentiate between the two, you might need to store some metadata. A simplistic example:
sub are_equal { return unless ($_[0][0] eq $_[1][0]); return $_[0][1] eq $_[1][1] if ($_[0][0] eq 'STRING'); return $_[0][1] == $_[1][1] if ($_[0][0] eq 'NUMBER'); die("Bad data"); } $a = [ NUMBER => 3 ]; $b = [ STRING => '3.0' ]; print(are_equal($a, $b)?'equal':'not equal', $/);
A more comprehensive, more scalable solution might use classes with overrided operators.
|
|---|