Perl if statements evaluates wrongly if we use '=>' as operator

P

praveen.kane

Perl if statements evaluates wrongly if we use '=>' as operator

Steps to reproduce :
---------------------------
1. Try to compare the scalar value using '=>' operator (comma
operator)
Below is the Perl code where issue will able to reproduce.

___________________________________________
#!/usr/bin/perl

$count = 10;
print "Count value : 10 \n";

if ($count => 20) {
print "Count value is greater than 20 \n ";
}else {
print "Count value is less than 20 \n";
}

___________________________________________


OutPut :
___________________________________________
[root@osdc-pxe001 pramuSUITE]# perl new1.pl
Count value : 10
Count value is greater than 20
[root@osdc-pxe001 pramuSUITE]#

___________________________________________


Observation:
-------------------
1. Perl expression using '=>' operator is returning true/
1 ,irrespective of condition .
2. Successfully complete execution and executes if block irrespective
of condition.


we expect :
 
B

Bjoern Hoehrmann

* (e-mail address removed) wrote in comp.lang.perl.misc:
$count = 10;
print "Count value : 10 \n";

if ($count => 20) {
print "Count value is greater than 20 \n ";
}else {
print "Count value is less than 20 \n";
}

`perldoc perlop` says:

The "=>" operator is a synonym for the comma except that it causes its
left operand to be interpreted as a string if it begins with a letter
or underscore and is composed only of letters, digits and underscores.

It does not mean "equal or greater than". For that use "the greater than
or equal" `>=` operator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top