perl -e "$a = qq(ab); $a + 3; print $a;"  # ab

No. You didn't assign the results of $a + 3 back to $a, so I don't know what you would have expected it to do. The results I would expect would be 3

IMHO, ab11 is more reasonable.

This is documented in perlop (search for "Auto-increment"). The critical portion is:

the increment is done as a string, preserving each character within its range, with carry [emphasis added]
If it were implemented the way you propose, then the with carry part would no longer be valid. Since you are able to just do a string append of a numeric value to implement your use case ($base . $counter++), and it becomes much more difficult to do the string carry use case, IMHO it makes much more since to implement the string increment magic* as it currently stands.

* I am not arguing for, or against, the string magic increment here, as I have not given enough thought to make an argument considering both sides. However, I can point to cases where I have used it in the past.

--MidLifeXis


In reply to Re: string operations of arithmetic by MidLifeXis
in thread string operations of arithmetic by xiaoyafeng

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.