Like swiftone said, for the general case you really want a full-blown parser. But if you can narrow the possibilities, a regex solution may work.

My first assumption would be that all the print statements you want to eliminate are contained in a single line. This seems to be the case from your example. In this case, I would suggest processing line-by-line instead of joining the whole thing in a single string and processing that.

My second assumption would be that the prints you want to eliminate will have one of the following forms:

print "string" print("string") print HANDLE "string" print(HANDLE "string")
where spacing can be arbitrary, handles are always all-uppercase, and the string does not contain double quotes.

Given this, I would do something like the following:

while(<FILE>) { s/print(\s+|\s*\(\s*)([A-Z]+\s+)?"string"(\s*\))?/1/; print; }
This is untested. Notice that I am substituting the print statement with a 1, so that it does not break things like:
print "string" if (somecond) becomes 1 if (somecond) something if print HANDLE "string" something if 1 etc.

--ZZamboni


In reply to RE: A Reg Exp Question by ZZamboni
in thread A Reg Exp Question 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.