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



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.
.



Relevant Pages

  • Re: While query
    ... > always compiles the comma operator into the same ops: ... > two operators described in perlop will always produce the same results ... the Perl compiler is implemented and looking only at the observable ...
    (comp.lang.perl.misc)
  • Re: Parentheses in function calls
    ... although it's sometimes difficult to tell whether a particular comma is the comma operator or a list argument separator. ... array or associative array; ... This all works mostly because Perl functions don't really have multiple parameters - they have just one parameter of list type, so there's really no difference between ... (that is, unless xyzzy is prototyped such that the first argument is evaluated in scalar context, or (in the last case) ...
    (comp.lang.c)
  • Re: comma operator
    ... I'm reading up on perl today and find this in perldoc perlop: ... Binary "," is the comma operator. ...
    (comp.lang.perl.misc)
  • Re: about a statement
    ... comma operator has a higher precedence than 'or' operator, ... eval returns undef and perl will ... so your rewritten code will work exactly the same as the original code. ...
    (perl.beginners)