in reply to $a++ allowed by $a-- is not ! why?

Yep. Bummer, eh? From the perlop docs:
The auto-increment operator has a little extra builtin magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern "/^[a-zA-Z]*[0-9]*\z/", the increment is done as a string, preserving each character within its range, with carry:
print ++($foo = '99'); # prints '100' print ++($foo = 'a0'); # prints 'a1' print ++($foo = 'Az'); # prints 'Ba' print ++($foo = 'zz'); # prints 'aaa'
The auto-decrement operator is not magical.

UPDATE:
Sorry for not saying this originally ... but i have no idea why the auto-decrement operator is not magical. But, how often (japh's not included) do you need to decrement a string? Incrementing strings is a handy way to get a new value, say for an identifier. When you need a new id, just increment. I may not be right about this, but i am guessing that this was the movitation behind making the auto-increment operator magical.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: $a++ allowed by $a-- is not ! why?
by davido (Cardinal) on Aug 30, 2003 at 17:17 UTC
    I can not say why -- isn't magical, but it occurred to me that the .. range operator only works going from little to big, and works much in the same way as ++ increments, and with similar magic.

    Try the following example:

    print 0..9,"\n";

    You get 0123456789. Now try the following code:

    print 9..0,"\n";

    You don't get much of anything at all. Camel says, "The range operator (in a list context) makes use of the magical autoincrement algorithm if the operands are strings."

    What isn't stated, but perhaps can be deduced, is that the range operator internally uses the autoincrement function, which when fed strings, behaves magically, and when fed numbers behaves normally. But the point is, the range operators don't use the autodecrement function.

    It seems to me (and this is pure speculation) that the autoincrement operator was given its special magic so that the range operator can construct things like A..Z. Again, this is speculation, but .. needed to be able to handle A..Z, .. internally used the autoincrement function, so to make it easy, autoincrement was given magic so that the range operator could become a more useful operator, not limited only to numbers.

    Now as for why the range operator doesn't enumerate descending-value lists also, that's another question, and I shouldn't speculate. ;)

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

      "the autoincrement operator was given its special magic so that the range operator can construct things like A..Z"

      This makes so much more sense. :) By the way, working around the lack of -- magic isn't so easy, but working around the .. limitation is, if you can spare the memory overhead for large lists:

      print for reverse 1..99;

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Re: $a++ allowed by $a-- is not ! why?
by abhishes (Friar) on Aug 30, 2003 at 16:46 UTC
    Hello Jeffa,

    Thanks for your reply. However, why is the auto-decrement operator not magical in nature?

    regards,
    Abhishek.
      However, why is the auto-decrement operator not magical in nature?

      Suppose it was, what would the following print:

      my $a = "a"; $a --; print $a, "\n";

      Abigail

        It would output "\`\n" if it acted upon the ordinal value.