I need to insert a string only once after matching the comments. This should be trivial but maybe I'm too tired to think it through. :( There are a few hundred files and they contain different syntaxes for the comments. I found a similar question at this node but the solution would match multiple times. My code currently updates the first line of the file and I want it to update after all the current comments. Thanks for any tips!
open (IN, 'file.txt'); my $out = ''; my $newComment = 0; while (<IN>) { if(($newComment < 1) && ($_ ne /^\#|^--|^\/\/^\s*\/\*/|^\s*\*|\*\/ +$/)) { $out .= "#This file was updated with the fix"; $out .= "\n"; $newComment++; } $out .= $_; } open (OUT, '>output.txt'); print OUT $out or die "Can't print to OUT: $!"; close OUT; close IN; __DATA__ /* * This is the new function * It has the following parameters: * * 1. Filename * 2. URL */ # All the following are valid comment styles: # hello perl style -- hello sql style /* hello c style * continue c style end c style */ // c++ c style #Revision date: 2005-10-10 --start of file <-- here is where the new comment should go. configurationtype=search url=http://www.google.com --End of file

In reply to inserting string only once after matching a pattern by Anonymous Monk

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.