develooper Front page | perl.beginners | Postings from February 2002

Re: Difference between == and eq

Thread Previous | Thread Next
From:
Jeff 'japhy' Pinyan
Date:
February 6, 2002 23:08
Subject:
Re: Difference between == and eq
Message ID:
Pine.GSO.4.21.0202070206060.15873-100000@crusoe.crusoe.net
On Feb 7, Leon said:

>Apart from the fact that == is used for numeric comparison whilst eq is used
>for string comparison, it would be of interest to note that == when used
>with strings forces the strings to be read in numeric context which is zero.

Not all strings have a numeric value of zero.

  for ("foo", "123", "foo123", "123foo") {
    print "$_ + 0 = ", $_ + 0, "\n";
  }
  __END__
  foo + 0 = 0
  123 + 0 = 123
  foo123 + 0 = 0
  123foo + 0 = 123

Perl attempts to pull a valid numerical value from the front of the
string, and uses zero if no value is found.

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


Thread Previous | 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