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

Overloading and boolean values: an inconsistency?

Thread Next
Robin Houston
December 19, 2001 06:54
Overloading and boolean values: an inconsistency?
Message ID:
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?


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About