|
| 1 | +# `${^ENGLISH_NAME}` aliases for punctuation variables |
| 2 | + |
| 3 | +## Preamble |
| 4 | + |
| 5 | + Author: Graham Knop <[email protected]> |
| 6 | + ID: 0014 |
| 7 | + Status: Draft |
| 8 | + |
| 9 | +## Abstract |
| 10 | + |
| 11 | +Provide aliases for all punctuation variables with the form |
| 12 | +`${^ENGLISH_NAME}`, with the english name taken from the aliases currently |
| 13 | +provided by `English.pm`. |
| 14 | + |
| 15 | +## Motivation |
| 16 | + |
| 17 | +Punctuation variables are not very friendly to use. Many people consider them |
| 18 | +ugly, and they require you to memorize all of the relevant variables. It would |
| 19 | +be valuable to provide better names for these variables as part of the core |
| 20 | +language. `English.pm` does exist as an attempt at this but brings its own |
| 21 | +issues. The English names look like `$ENGLISH_NAME`, which is syntactically |
| 22 | +the same as a package variable that a user might define themselves. While not |
| 23 | +exactly rare, use of `English.pm` is not particularly common either. Many |
| 24 | +seasoned Perl programmers would not know the English names for many commonly |
| 25 | +used punctuation variables without consulting a reference. |
| 26 | + |
| 27 | +## Rationale |
| 28 | + |
| 29 | +Some of the newer superglobals are spelled like `${^ENGLISH_NAME}`. Providing |
| 30 | +aliases to all of the punctuation variables would allow them to be more self |
| 31 | +documenting, like the `English.pm` names, but without the being syntactically |
| 32 | +identical to a normal package variable. Being spelled like `${^ENGLISH_NAME}` |
| 33 | +makes it obvious that this is a special variable. This makes the magic |
| 34 | +behavior more obvious, and helps point to the correct reference material. |
| 35 | +Since there are already several "punctuation" variables with this spelling, it |
| 36 | +should be familiar to Perl programmers. It also means the variables are always |
| 37 | +available without needing to load an additional module. |
| 38 | + |
| 39 | +The names used by `English.pm` could mostly be re-used with the new spelling. |
| 40 | +This should help any Perl programmer that has preferred `English.pm` to |
| 41 | +understand the new form. |
| 42 | + |
| 43 | +## Specification |
| 44 | + |
| 45 | +For each variable listed in perlvar which includes an `English.pm` name like |
| 46 | +`$OS_ERROR`, provide an alias named `${^OS_ERROR}` as part of the core |
| 47 | +language. |
| 48 | + |
| 49 | +## Backwards Compatibility |
| 50 | + |
| 51 | +There won't be any conflict regarding syntax, as this is already valid syntax, |
| 52 | +and already referring to super-globals. This does present an issue, as |
| 53 | +variables with this syntax are exempt from strict. Attempting to use these |
| 54 | +variables will fail silently when run on older perls, or when misspelling the |
| 55 | +variable. |
| 56 | + |
| 57 | +A shim could be created to allow using these variables on older perls. It may |
| 58 | +not be able to be 100% compatible. |
| 59 | + |
| 60 | +## Security Implications |
| 61 | + |
| 62 | +Many of the punctuation variables relate to system state or errors. |
| 63 | +Misspelling a variable like `${^EVAL_ERROR}` (aka `$@`) could lead to an error |
| 64 | +being ignored rather than handled, which could lead to security concerns. |
| 65 | + |
| 66 | +## Examples |
| 67 | + |
| 68 | +```perl |
| 69 | +local $_; |
| 70 | +local ${^ARG}; |
| 71 | + |
| 72 | +my $pid = $$; |
| 73 | +my $pid = ${^PID}; |
| 74 | + |
| 75 | +my $error = $@; |
| 76 | +my $error = ${^EVAL_ERROR}; |
| 77 | + |
| 78 | +my %signals = %SIG; |
| 79 | +my %signals = %{^SIG}; |
| 80 | +``` |
| 81 | + |
| 82 | +## Prototype Implementation |
| 83 | + |
| 84 | +An initial attempt at a backwards compatibility shim: |
| 85 | +https://github.com/haarg/English-Globals/blob/master/lib/English/Globals.pm |
| 86 | + |
| 87 | +A more complete implementation written in XS: |
| 88 | +https://metacpan.org/pod/English::Name |
| 89 | + |
| 90 | +## Future Scope |
| 91 | + |
| 92 | +Most punctuation characters are already used for special variables, so a typo |
| 93 | +can't really be caught. With english names, there is a greater risk of typos, |
| 94 | +but also a greater possibility of catching them. Possibly variables with this |
| 95 | +format could have strict applied to them. |
| 96 | + |
| 97 | +Providing these aliases could make it possible to deprecate and remove some |
| 98 | +lesser-used punctuation variables, freeing them up for use as syntax rather |
| 99 | +than variables. This was previously done with the `$*` variable. |
| 100 | + |
| 101 | +## Rejected Ideas |
| 102 | + |
| 103 | +`English.pm` serves as a previous example of trying to make punctuation |
| 104 | +variable use more friendly, but has various caveats as covered in the |
| 105 | +"Motivation" section. |
| 106 | + |
| 107 | +Spelling these variables like `$^ENGLISH_NAME` is not possible because |
| 108 | +variables of the form `$^E` exist. When interpolating, `"$^OS_ERROR"` is |
| 109 | +parsed as `"${^O}S_ERROR"`. If this brace-less form was still desired, this |
| 110 | +RFC would still be a prerequisite. When interpolating, a delimited form is |
| 111 | +required to allow word characters to follow the variable. This delimited form |
| 112 | +would be the same `${^ENGLISH_NAME}` form proposed by this RFC. |
| 113 | + |
| 114 | +`$*ENGLISH_NAME` has been proposed, but conflicts with glob dereferencing. |
| 115 | +Outside string interpolation, that variable syntax is currently invalid and |
| 116 | +thus could be used. But when interpolating, a delimited form is needed. This |
| 117 | +would be `${*ENGLISH_NAME}`, but that syntax is already valid, as a scalar |
| 118 | +dereference of a glob. Trying to use `$*{ENGLISH_NAME}` is also a problem |
| 119 | +because it is currently valid syntax, refering to an element in the `%*` hash. |
| 120 | + |
| 121 | +## Open Issues |
| 122 | + |
| 123 | + - None at this time |
| 124 | + |
| 125 | +## Copyright |
| 126 | + |
| 127 | +Copyright (C) 2022, Graham Knop. |
| 128 | + |
| 129 | +This document and code and documentation within it may be used, redistributed and/or modified under the same terms as Perl itself. |
0 commit comments