This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlfunc: re-document old split() @_ side effect
[perl5.git] / lib / meta_notation.pm
1 use strict;
2 use warnings;
3
4 # A tiny private library routine which is a helper to several Perl core
5 # modules, to allow a paradigm to be implemented in a single place.  The name,
6 # contents, or even the existence of this file may be changed at any time and
7 # are NOT to be used by anything outside the Perl core.
8
9 sub _meta_notation ($) {
10
11     # Returns a copy of the input string with the nonprintable characters
12     # below 0x100 changed into printables.  Any ASCII printables or above 0xFF
13     # are unchanged.  (XXX Probably above-Latin1 characters should be
14     # converted to \X{...})
15     #
16     # \0 .. \x1F (which are "\c@" .. "\c_") are changed into ^@, ^A, ^B, ...
17     # ^Z, ^[, ^\, ^], ^^, ^_
18     # \c? is changed into ^?.
19     #
20     # The above accounts for all the ASCII-range nonprintables.
21     #
22     # On ASCII platforms, the upper-Latin1-range characters are converted to
23     # Meta notation, so that \xC1 becomes 'M-A', \xE2 becomes 'M-b', etc.
24     # This is how it always has worked, so is continued that way for backwards
25     # compatibility.  The range \x80 .. \x9F becomes M-^@ .. M-^A, M-^B, ...
26     # M-^Z, M-^[, M-^\, M-^], M-^, M-^_
27     #
28     # On EBCDIC platforms, the upper-Latin1-range characters are converted
29     # into '\x{...}'  Meta notation doesn't make sense on EBCDIC platforms
30     # because the ASCII-range printables are a mixture of upper bit set or
31     # not.  [A-Za-Z0-9] all have the upper bit set.  The underscore likely
32     # doesn't; and other punctuation may or may not.  There's no simple
33     # pattern.
34
35     my $string = shift;
36
37     $string =~ s/([\0-\037])/
38                sprintf("^%c",utf8::unicode_to_native(ord($1)^64))/xeg;
39     $string =~ s/\c?/^?/g;
40     if (ord("A") == 65) {
41         $string =~ s/([\200-\237])/sprintf("M-^%c",(ord($1)&0177)^64)/eg;
42         $string =~ s/([\240-\377])/sprintf("M-%c"  ,ord($1)&0177)/eg;
43     }
44     else {
45         no warnings 'experimental::regex_sets';
46         # Leave alone things above \xff
47         $string =~ s/( (?[ [\x00-\xFF] & [:^print:]])) /
48                   sprintf("\\x{%X}", ord($1))/xaeg;
49     }
50
51     return $string;
52 }
53 1