The second half of a substitution is not a regex, it's a double quotish context, so \s doesn't mean "a space character" (and even if it did \s stands for a lot of different blank chars, not just one) so you want to put a litteral space here. You'll have to put quotes around your code (or it will be broken into several arguments).

And, since it is a double quotish context $ENV{DEBUG} will be replaced straightaway, and you will end up with its value in your code. You should escape the $.

And last thing I saw: unless you use no indentation at all, the print probably isn't the first thing on the line. So you want to allow spaces in front of it. And you probably don't want your code to work on subs that have a name that starts by print, like printfoo

So, with those corrections, you would have: s/^\s+print\b(.*);$/print$1 if \$ENV{DEBUG};/g

Now, that's why it didn't work, but Corion's proposition is a far better idea. You'll avoid adding another modifier to a line that already has one. For exemple print $object->method() if ref $object eq 'Class'; would become print $object->method() if ref $object eq 'Class' if $ENV{DEBUG};, which is a syntax error.


In reply to Re: perl in line editing help by Eily
in thread perl in line editing help by ghosh123

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.