Re: Perl if statements evaluates wrongly if we use '=>' as operator
- From: Tad McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Mon, 03 Oct 2011 15:34:43 -0500
praveen.kane@xxxxxxxxx <praveen.kane@xxxxxxxxx> wrote:
Perl if statements evaluates wrongly if we use '=>' as operator
No it doesn't.
Steps to reproduce :
---------------------------
1. Try to compare the scalar value using '=>' operator (comma
operator)
That is not possible, as the comma operator does not do comparisons...
Below is the Perl code where issue will able to reproduce.
___________________________________________
#!/usr/bin/perl
$count = 10;
print "Count value : 10 \n";
if ($count => 20) {
The comma operator evaluates its left operand and discards
the result, then evaluates its right operand.
So you have the equivalent of
if (20) {
there, which is clearly true.
So, Perl is not doing anything "wrongly" here...
print "Count value is greater than 20 \n ";
.... you are doing something wrongly here, as you have not
tested to see if $count is >= 20 anywhere.
}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/
As it should.
1 ,irrespective of condition .
As it should.
2. Successfully complete execution and executes if block irrespective
of condition.
As is should.
we expect :
-----------------
Perl should throw proper compilation error
Why?
You have valid syntax.
Your error is in semantics, not in syntax.
If you want to do a comparison, you should use a comparison operator.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
.
- References:
- Perl if statements evaluates wrongly if we use '=>' as operator
- From: praveen.kane@xxxxxxxxx
- Perl if statements evaluates wrongly if we use '=>' as operator
- Prev by Date: Re: Perl if statements evaluates wrongly if we use '=>' as operator
- Next by Date: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
- Previous by thread: Re: Perl if statements evaluates wrongly if we use '=>' as operator
- Next by thread: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.9 $)
- Index(es):
Relevant Pages
|