Skip to content

Commit

Permalink
Merge pull request Perl#8 from xenu/xenu/rfc-slurp-mode
Browse files Browse the repository at this point in the history
New RFC: command-line flag for slurping
  • Loading branch information
leonerd committed Jan 28, 2022
2 parents 37dacbd + 389354e commit 621d948
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions rfcs/rfc0011.md
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.

0 comments on commit 621d948

Please sign in to comment.