tldr/pages/common/perl.md
Waldir Pimenta dc9547925f perl: swap -M example with -0, for multiline regex
For completeness: the -0 option is actually used to specify the input record separator (as an octal number).
Without it, the record separator is the newline character, i.e. the files are processed line by line
(which doesn't allow find-replace expressions that include newlines to work).

According to the [documentation](http://perldoc.perl.org/perlrun.html#Command-Switches),
using plain `-0` is not guaranteed to have the effect of parsing the entire file,
because if the file does contain characters with octal value equal to the parameter passed to (or implied by) the -0 option,
these characters will be treated as line breaks.
However, if the value exceeds 377<sub>8</sub> (i.e. 255), it won't be matched to characters on the file.
777 is the preferred convention within that exceptional range, as the highest value that keeps to 3 octal digits.

Here we're forgoing such details and using -0 anyway, since for most cases this will be enough.
2017-05-22 19:47:03 +05:30

771 B

perl

The Perl 5 language interpreter.

  • Parse and execute a Perl script:

perl {{script.pl}}

  • Check syntax errors on a Perl script:

perl -c {{script.pl}}

  • Parse and execute a perl statement:

perl -e {{perl_statement}}

  • Run a Perl script in debug mode, using perldebug:

perl -d {{script.pl}}

  • Loo[p] over all lines of a file, editing them [i]n-place using a find/replace [e]xpression:

perl -p -i -e 's/{{find}}/{{replace}}/g' {{filename}}

  • Run a multi-line find/replace expression on a file (i.e. including line breaks):

perl -0 -p -i -e 's/{{foo\nbar}}/{{foobar}}/g' {{filename}}

  • Run a find/replace expression on a file, saving the original file with a given extension:

perl -p -i'.old' -e 's/{{find}}/{{replace}}/g' {{filename}}