forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Perl#8 from xenu/xenu/rfc-slurp-mode
New RFC: command-line flag for slurping
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Command-line flag for slurping | ||
|
||
## Preamble | ||
|
||
Author: Tomasz Konojacki <me@xenu.pl> | ||
Sponsor: | ||
ID: 0011 | ||
Status: Draft | ||
|
||
## 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 than `undef` 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 RFC. | ||
|
||
- 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](https://perldoc.perl.org/5.34.0/perlrun#-s). | ||
|
||
## 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. |