[SR-Users] how can string "0" be equal to int 0?

Andrei Pelinescu-Onciul andrei at iptel.org
Fri Oct 15 12:30:17 CEST 2010


On Oct 15, 2010 at 12:29, Juha Heinanen <jh at tutpro.com> wrote:
> i have stored in htable a key_value=0 with key_type=1, i.e., string.
> 
> then i have this piece of test code:
> 
>     if (defined($sht(htable=>$var(carrier_key)::id))) {
>         xlog("L_INFO", "$sht(htable=>$var(carrier_key)::id) is defined\n");
>     } else {
>         xlog("L_INFO", "$sht(htable=>$var(carrier_key)::id) is NOT defined\n");
>     };
>     $var(carrier_id) = $sht(htable=>$var(carrier_key)::id);
>     if ($var(carrier_id) == (int)0) {
>         xlog("L_INFO", "$sht(htable=>$var(carrier_key)::id) is not found\n");
>     } else {
>         xlog("L_INFO", "$sht(htable=>$var(carrier_key)::id) is found\n");
>     };
> 
> and when the piece is executed, this is printed to syslog:
> 
> Oct 15 12:20:33 squeeze /usr/sbin/trunk-proxy[2915]: INFO: 0 is defined
> Oct 15 12:20:33 squeeze /usr/sbin/trunk-proxy[2915]: INFO: 0 is not found
> 
> i understand the first line, because the entry is in the htable, but how
> is it possible that string "0" is equal to int 0?

Because "==" works only with arguments of the same type, 0 it's
autoconverted to "0". The alternative would be to log an error.

Here are the rules for ==:
/* if left is string, eval left & right as string and  use string diff.
 * if left is int eval as int using int diff
 * if left is undef, look at right and convert to right type
 */


Andrei



More information about the sr-users mailing list