This and other RFCs are available on the web at
http://dev.perl.org/rfc/
=head1 TITLE
Operators: Polymorphic comparisons
=head1 VERSION
Maintainer: Damian Conway <[EMAIL PROTECTED]>
Date: 7 August 2000
Version: 1
Mailing List: [EMAIL PROTECTED]
Number: 54
=head1 ABSTRACT
This RFC proposes that numeric comparison operators default to stringwise
comparison when both arguments are non-numeric strings.
=head1 DESCRIPTION
Currently the expression:
"cat" == "dog"
returns true.
It is proposed that if I<neither> argument of a numeric comparison
operator can be converted to a number, rather than both being converted
to zero, the two operands should be compared using the equivalent
stringwise comparison operator.
It is further proposed that the current warning:
Argument "%s" isn't numeric
be changed to:
Arguments of "%s" aren't numeric - using string comparison instead
=head1 RATIONALE
Perl has excellent support for generic programming, because of its dynamic
typing, generic data types, and interface polymorphism. Just about the only
place where that DWIM genericity breaks down is in comparisons.
It is difficult to construct many generic algorithms and data structures
in Perl, because there is no generic way to specify an ordering on
dynamically typed data. For example, one cannot simply code a
generic BST insertion method:
sub insert
{
my ($self, $key, $value) = @_;
if (!defined($self->{key}))
{
$self->{key} = $key;
return $self->{value} = $value;
}
my $compare = $key <=> $self->{key};
return $self->{value} = $value unless $compare;
$self->{$compare} = $self->new() unless $self->{$compare};
return $self->{$compare}->insert($key,$value);
}
This will work well for numeric keys, but fail miserably on most
string-based keys, because <=> will generally return 0 for most pairs of
strings.
The above code would work correctly however if <=> detected the string/string
comparison and automagically used cmp instead.
=head1 IMPLEMENTATION
Dammit, Jim, I'm a doctor, not an engineer!
=head1 REFERENCES
Chapter 12 of "Object Oriented Perl"