Hmm, not such a good illustration. You initialise $counter with the string '1', not with the number 1. Perl is smart enough to Do What You Mean, but you are telling people who read your code the wrong thing.

You have also carried over the OP's bad habit of needlessly interpolating variables into strings. Using interpolation for "$filename.[$counter]" is appropriate but elsewhere you should simply use the variable: rename $existingFile, $filename.

If there is no algorithmic reason to use the post increment operators, always use pre increment. Sticking the ++ or -- out the front (++$counter) makes them easier to see which is a Good Thing.

Perl is the programming world's equivalent of English

In reply to Re^2: regex renaming with existing files by GrandFather
in thread regex renaming with existing files by ArifS

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.