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

Reference in numeric context?

Thread Next
Michael G Schwern
April 27, 2001 08:52
Reference in numeric context?
Message ID:
I found this bit of code in a test...

  my $number = $sup->suppliers_discount;
  ok($number + 0 == $number, 'suppliers_discount returns a number');

The test was passing even though suppliers_discount() was returning a
hash reference!  For some odd reason references will evaluate to a
number in numeric context.

$ perl -wle '$h = {};  print $h + 0'

All the reference types work like this, even the mystical 'Regexp'.

I'm sure someone has some perfectly insane yet totally consistent
reason for this (I can't find it in the docs, but that doesn't mean
its not there).  The real question is, should using a reference as a
number be a warning?


Michael G. Schwern   <>
Perl6 Quality Assurance     <>	     Kwalitee Is Job One
Stupid am I?  Stupid like a fox!

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