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

Reference in numeric context?

Thread Next
From:
Michael G Schwern
Date:
April 27, 2001 08:52
Subject:
Reference in numeric context?
Message ID:
20010427165254.I545@blackrider.blackstar.co.uk
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'
268515692

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   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
Stupid am I?  Stupid like a fox!

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