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

Discussion in 'Perl Misc' started by praveen.kane@gmail.com, Oct 3, 2011.

  1. Guest

    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 :
    -----------------
    Perl should throw proper compilation error saying some thing like
    'incorrect operator : Execution of xyz.pl aborted due to compilation
    errors" incorrect .
     
    , Oct 3, 2011
    #1
    1. Advertising

  2. * 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.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
    25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
     
    Bjoern Hoehrmann, Oct 3, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mg

    Evaluates a supplied string

    mg, Dec 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    403
  2. Paul Verbelen
    Replies:
    2
    Views:
    4,937
    Joe Kesselman
    Jun 14, 2006
  3. Eric Lilja
    Replies:
    3
    Views:
    342
    Gianni Mariani
    May 20, 2007
  4. Andrew
    Replies:
    5
    Views:
    269
    Peter Otten
    Nov 23, 2008
  5. Laurent Claessens

    1/2 evaluates to 0

    Laurent Claessens, Oct 12, 2011, in forum: Python
    Replies:
    15
    Views:
    264
    88888 dihedral
    Oct 14, 2011
Loading...

Share This Page