Command-line flag for slurping
Preamble
Author: Tomasz Konojacki <me@xenu.pl>
Sponsor:
ID: 0011
Status: Implemented
Abstract
Introduce a new command-line flag for perl
,
-g
, which sets $/
to undef
, and
thus enables slurp mode. It is a simpler alias for
-0777
.
Motivation
Slurping (i.e. reading a whole file at once, instead of line by line) is a very common operation in one-liners, and therefore it deserves its own dedicated flag.
Rationale
Currently, -0777
is the most common way to enable slurp
mode in one-liners. It’s a special case of -0number
. When
number
is above 0o377
it sets $/
to undef
, which enables slurp mode.
-0number
suffers from the following problems:
The input record separator has to be specified with an octal number, which is very unusual.
It’s overly general, it can set
$/
to any character. Users rarely need values other thanundef
or"\n"
.Its most common use, enabling slurp mode, is a special case hidden behind a magic number.
A dedicated flag for slurping would allow users to avoid the
peculiarities of -0number
.
Specification
-g
is an alias for -0777
, they are
completely equivalent.
Backwards Compatibility
No breakage is expected. perl -g
is currently a fatal
error.
Security Implications
Hopefully none.
Examples
# collapse consecutive newlines:
perl -i -gpE 's/\n+/\n/g' file.txt
Future Scope
Rejected Ideas
Long flag, e.g.
--slurp
. Perl currently doesn’t support long flags and adding them would be beyond the scope of this PPC.Alternative spellings of the flag, e.g.
-R
,-o
. The author believes none of them are better or worse than-g
. Unfortunately, the most natural choice,-s
, is already taken.
Open Issues
Copyright
Copyright (C) 2021 Tomasz Konojacki
This document and code and documentation within it may be used, redistributed and/or modified under the same terms as Perl itself.