perl regex works in script but not when executed on the command line

I'm trying to manipulate multiline CoffeeScript comments in a file using perl. This is my regex:

^\t*###[\S\s]*?^\t*###

When I run this in a script where data is the file data, it does what I expect and replaces all multiline comments with "foo":

$data =~ s{^\t*###[\S\s]*?^\t*###}{"foo"}gme;

However, when I run this on the command line the file is unchanged:

perl -pi -e 's{^\t*###[\S\s]*?^\t*###}{"foo"}gme' file.coffee

I've used similar commands with different regular expressions and without the 'm' option and they all work. Is it the m option that's causing the issue? I'm sure its something simple.

Answers


In the implicit loops set by -n and -p it can be useful to define the values of $/ and $\. Using the -0 option puts Perl in paragraph mode and the special value 0777 puts Perl into file slurp mode.

perl -0777 -i -pe 's{^\t*###[\S\s]*?^\t*###}{"foo"}gme' file.coffee

The perl documentation for the -n/-p option states:

assume "while (<>) { ... }" loop around program

This means that each time the -e expression is executed, $_ is one line of the input file. Your s/// expression is expecting to operate on the whole entire file at once, so it won't work in this mode.


Need Your Help

Tracking andorid API's used by other application installed on phone

java android

I am building an android application, but i am very beginner to this.

Selenium 2 Webdriver

selenium webdriver

How can we make a batch file for executing Selenium Webdriver projects in Java?