Difference between == , = and eq

雨燕双飞 提交于 2019-12-13 20:26:44

问题


I want to know the difference between these:

 my $a = 1;

and

 my $a == 1;

and

 my $a eq 1;

回答1:


== is used when comparing numeric values.

eq is used in comparing string values.

= is the assignment operator, not a comparison operator.




回答2:


eq is for testing string equality, == is the same thing but for numerical equality.


For More Click Here




回答3:


The last two statements do nothing, it's a good practice to use the directives:

use warnings;
use strict;

for example:

#!/usr/bin/perl
use warnings;
use strict;

my $a == 1;
my $b eq 1;

print "$a $b\n";

you should see some warning such as:

Useless use of numeric eq (==) in void context at main.pl line 5.
Useless use of string eq in void context at main.pl line 6.
Use of uninitialized value $a in numeric eq (==) at main.pl line 5.
Use of uninitialized value $b in string eq at main.pl line 6.
Use of uninitialized value $a in concatenation (.) or string at main.pl line 8.
Use of uninitialized value $b in concatenation (.) or string at main.pl line 8.



回答4:


You should never see the 2nd or 3rd examples in any perl program. If you do, it would not be farfetched to assume the original programmer meant something else (like my $a = 1;). Those would both give warning messages if you were were using the strict and warnings pragmas:

use strict;
use warnings;
my $a == 1;

# ==> Useless use of numeric eq (==) in void context at -e line 3.
# ==> Use of uninitialized value $a in numeric eq (==) at -e line 3.

You should also try to stay away from using $a or $b as variables in any perl program as these are considered special variables used when sorting. You can often get away with it, but it's best not to mess around with them.



来源:https://stackoverflow.com/questions/18396049/difference-between-and-eq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!