Similarly, I don't see the reason to run the same pair of statements three times:
$s =~ s/^;.*\Z//m; chomp $s; $s =~ s/^;.*\Z//m; chomp $s; $s =~ s/^;.*\Z//m; chomp $s;
Once should be enough, no? And I doubt the chomp is useful here.
Again, remember we are dealing with a multi-line string. So running it once removes just the last comment line, not the last three comment lines. Also, please note that the first:
$s =~ s/^;.*\Z//m;
removes the contents of the last comment line of a multi-line string (note that \Z matches just the end of the (multi-line) string, not the end of each line). So if you ran it again without the chomp it would do nothing because you've have already removed the last comment line! The chomp is needed to remove the newline now sitting at the end of the string. An alternative to chomp, suggested above by tye, is to eschew the m modifier and remove the newline as part of the regex, like so:
s/\n;.*\Z//


In reply to Re^2: Removing multiple trailing comment lines from a string by eyepopslikeamosquito
in thread Removing multiple trailing comment lines from a string by eyepopslikeamosquito

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.