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:

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

Open Issues

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.