$a = 'string';
if (defined ($a <=> $a)) { print "Oops"; }
####
package Foo;
use overload
'0+' => \&as_num;
sub new {
my $class = shift;
my $self = { value => shift };
bless $self, $class;
}
sub as_num {
my $self = shift;
return $self->{value};
}
package main;
my $x = new Foo(3);
my $z = (0+$x); # $x+0 also produces error
##
##
use Scalar::Util 'looks_like_number';
my $a = [ ];
my $h = { };
print looks_like_number($a), "\n";
print looks_like_number($h), "\n";
##
##
sub _numberlike {
return 1, unless defined $_[0];
# L manpage notes that NaN != NaN, so we can verify that
# numeric conversion function works properly along with the
# comparison operator.
no warnings;
return 1 if ((!ref $_[0]) || blessed($_[0])) &&
eval {
((0+$_[0]) == (0+$_[0])) && (($_[0] <=> $_[0])==0)
};
return;
}
##
##
sub _stringlike {
return 1 unless defined $_[0] && ref $_[0];
return 1 if (blessed $_[0]) &&
eval {
(("$_[0]" eq "$_[0]") && (($_[0] cmp $_[0])==0))
};
return;
}