develooper Front page | perl.perl5.porters | Postings from December 2001

Overloading and boolean values: an inconsistency?

Thread Next
From:
Robin Houston
Date:
December 19, 2001 06:54
Subject:
Overloading and boolean values: an inconsistency?
Message ID:
20011219145443.A20701@puffinry.freeserve.co.uk
When perl determines the boolean value of an ordinary scalar, the
string value takes precedence over the numeric value. This can be
easily seen by experimentation, or by looking at the definition of
SvTRUE in sv.h.

If a scalar is SvPOK, then the numeric value is not even considered
when deciding the truth value.

However, this general principle does not apply to overloaded objects.
For example:

  my $x = bless {}, "x";
  print $x ? "true\n" : "false\n";

  package x;
  use overload q{""} => sub{ "" };
  use overload q{0+} => sub{ 23 };

prints "true".

I find this surprising, and it seems inconsistent. Is it deliberate?
If so, why?

Cheers,
 .robin.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About