Andrew_Levenson has asked for the wisdom of the Perl Monks concerning the following question:

I was playing with the do{}while() tutorial script
$value=15; do{ print "$value\n"; $value=$value-1; } while($value>10);
And noticed that while if I changed $value>10 to $value>=10 it ran it as "less than or equal to", but if I changed it to $value=>10 it kept running into the negative thousands.
What is the difference between >= and =>?

Replies are listed 'Best First'.
Re: Syntax discrepancy?
by japhy (Canon) on Mar 08, 2006 at 16:14 UTC
    > is the "less than" operator. >= is the "less than or equal to" operator. => is the "fat comma" operator, which is like a comma but allows its left-hand operand to be a bareword without Perl giving a hissy fit.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Syntax discrepancy?
by dragonchild (Archbishop) on Mar 08, 2006 at 16:15 UTC
    >= is "Greater-than-or-equal-to". => is a comma that auto-quotes its left-hand side.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Syntax discrepancy?
by rinceWind (Monsignor) on Mar 08, 2006 at 16:15 UTC

    The operator => is not greater or equal, but the "fat comma", so Perl would parse your expression as

    while($value, 10)

    which will always be true.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

Re: Syntax discrepancy?
by spiritway (Vicar) on Mar 08, 2006 at 16:25 UTC

    So, to expand on [id://japhy]'s answer, your 'while($value => 10)' phrase would be equivalent to while($value , "10"). It would always been 'true', so the loop would never terminate. The two terms you used, '=>' and '>=', are not equivalent in Perl. Order is imporant. 'House cat' is not the same as 'cat house'. Check out perldoc perlop for details.

Re: Syntax discrepancy?
by SamCG (Hermit) on Mar 08, 2006 at 20:43 UTC
    Hi Andrew,

    You could gain a lot of knowledge about these sort of issues by picking up a book such as Learning Perl (The "Llama book", and umm, no, I'm not part of any vast O'Reilly conspiracy). I know you're in high school and probably on a limited budget, but you should consider it if possible. It's a quick book to go through; you might be able to get it from your local library.

    You can also go through documentation for this (which is what I'm sure some will suggest), but the documentation isn't really designed for teaching. Some can learn from it, but I found books better for an initial grounding.
Re: Syntax discrepancy?
by ambrus (Abbot) on Mar 08, 2006 at 19:32 UTC

    Just like $value=-2 and $value-=2 aren't the same either: the first one stores -2 to the variable, the second subtracts 2 from its current value and stores it back. Similarly, != is quite different from =! etc.