| 1 | package open::layers; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 20 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | # series of layers delimited by colons and consisting of non-space characters | ||||||||||||||||||||||||||||||||||||
| 10 | # allow spaces before and between layers because core does, but don't require them | ||||||||||||||||||||||||||||||||||||
| 11 | # we require a leading colon even though core doesn't, because it's expected anyway | ||||||||||||||||||||||||||||||||||||
| 12 | 20 | my $LAYERS_SPEC = qr/\A\s*(?::[^\s:]+\s*)+\z/; | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | sub import { | ||||||||||||||||||||||||||||||||||||
| 15 | 45 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=21 SCAR=31.1
| |||||||||||||||||||||||||||||||||||||
| 16 | 45 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 17 | 28 | my $arg = shift; | |||||||||||||||||||||||||||||||||||
| 18 | 28 | ▶ | if (ref $arg or ref \$arg eq 'GLOB') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
ref $arg or ref \$arg eq "GLOB"
MC/DC100%2/2
ref $arg or ref \$arg eq "GLOB"
ref $arg
ref \$arg eq "GLOB"
Truth table100%3/3
A or B
A= ref $argB= ref \$arg eq "GLOB"
| |||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | Carp::croak "open::layers: No layer provided for handle $arg" unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 20 | 2 | my $layer = shift; | |||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | Carp::croak "open::layers: Invalid layer specification $layer" unless $layer =~ m/$LAYERS_SPEC/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | binmode $arg, $layer or Carp::croak "open::layers: binmode $arg failed: $!"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 23 | } elsif ($arg =~ m/\ASTD(IN|OUT|ERR|IO)\z/) { | ||||||||||||||||||||||||||||||||||||
| 24 | 16 | my $which = $1; | |||||||||||||||||||||||||||||||||||
| 25 | 16 | ▶ | Carp::croak "open::layers: No layer provided for handle $arg" unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 26 | 16 | my $layer = shift; | |||||||||||||||||||||||||||||||||||
| 27 | 16 | ▶ | Carp::croak "open::layers: Invalid layer specification $layer" unless $layer =~ m/$LAYERS_SPEC/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 28 | 16 | ▶ | my @handles = $which eq 'IN' ? \*STDIN | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 29 | : $which eq 'OUT' ? \*STDOUT | ||||||||||||||||||||||||||||||||||||
| 30 | : $which eq 'ERR' ? \*STDERR | ||||||||||||||||||||||||||||||||||||
| 31 | : (\*STDIN, \*STDOUT, \*STDERR); | ||||||||||||||||||||||||||||||||||||
| 32 | 16 | ▶ | binmode $_, $layer or Carp::croak "open::layers: binmode $_ failed: $!" for @handles; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 33 | } elsif ($arg =~ m/\A(rw|r|w)\z/) { | ||||||||||||||||||||||||||||||||||||
| 34 | 10 | my $which = $1; | |||||||||||||||||||||||||||||||||||
| 35 | 10 | ▶ | Carp::croak "open::layers: No layer provided for $arg handles" unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 36 | 10 | my $layer = shift; | |||||||||||||||||||||||||||||||||||
| 37 | 10 | ▶ | Carp::croak "open::layers: Invalid layer specification $layer" unless $layer =~ m/$LAYERS_SPEC/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 38 | 10 | my @layers = $layer =~ m/(:[^\s:]+)/g; # split up the layers so we can set ${^OPEN} like open.pm | |||||||||||||||||||||||||||||||||||
| 39 | 10 | ▶ | my ($in, $out) = split /\0/, (${^OPEN} || "\0"), -1; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
${^OPEN} || "\000"
MC/DC100%1/1
${^OPEN} || "\000"
${^OPEN}
Truth table100%2/2
A
A= ${^OPEN}
| |||||||||||||||||||||||||||||||||||||
| 40 | 10 | ▶ | if ($which ne 'w') { # r, rw | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 41 | 8 | $in = join ' ', @layers; | |||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||
| 43 | 10 | ▶ | if ($which ne 'r') { # w, rw | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 44 | 8 | $out = join ' ', @layers; | |||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||
| 46 | 10 | ${^OPEN} = join "\0", $in, $out; | |||||||||||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||||||||||
| 48 | 0 | Carp::croak "open::layers: Unknown flag $arg"; | |||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | 20 | 1; | |||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||
| 57 | open::layers - Set default PerlIO layers | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||||||||||
| 62 | # set default layers for open() in this lexical scope | ||||||||||||||||||||||||||||||||||||
| 63 | use open::layers r => ':encoding(UTF-8)'; | ||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||
| 65 | # encoding layer no longer applied to handles opened here | ||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | use open::layers r => ':encoding(cp1252)', w => ':encoding(UTF-8)'; | ||||||||||||||||||||||||||||||||||||
| 68 | use open::layers rw => ':encoding(UTF-8)'; # all opened handles | ||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | # push layers on the standard handles (not lexical) | ||||||||||||||||||||||||||||||||||||
| 71 | use open::layers STDIN => ':encoding(UTF-8)'; | ||||||||||||||||||||||||||||||||||||
| 72 | use open::layers STDOUT => ':encoding(UTF-8)', STDERR => ':encoding(UTF-8)'; | ||||||||||||||||||||||||||||||||||||
| 73 | use open::layers STDIO => ':encoding(UTF-8)'; # shortcut for all of above | ||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | This pragma is a reimagination of the core L<open> pragma, which either pushes | ||||||||||||||||||||||||||||||||||||
| 78 | L<PerlIO> layers on the global standard handles, or sets default L<PerlIO> | ||||||||||||||||||||||||||||||||||||
| 79 | layers for handles opened in the current lexical scope (meaning, innermost | ||||||||||||||||||||||||||||||||||||
| 80 | braces or the file scope). The interface is redesigned to be more explicit and | ||||||||||||||||||||||||||||||||||||
| 81 | intuitive. See L</"COMPARISON TO open.pm"> for details. | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | =head1 ARGUMENTS | ||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | Each operation is specified in a pair of arguments. The first argument, the | ||||||||||||||||||||||||||||||||||||
| 86 | flag, specifies the target of the operation, which may be one of: | ||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | =over | ||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | =item STDIN, STDOUT, STDERR, STDIO | ||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | These strings indicate to push the layer(s) onto the associated standard handle | ||||||||||||||||||||||||||||||||||||
| 93 | with L<binmode()|perlfunc/binmode>, affecting usage of that handle globally, | ||||||||||||||||||||||||||||||||||||
| 94 | equivalent to calling L<binmode()|perlfunc/binmode> on the handle in a C<BEGIN> | ||||||||||||||||||||||||||||||||||||
| 95 | block. C<STDIO> is a shortcut to operate on all three standard handles. | ||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | Note that this will also affect reading from C<STDIN> via L<ARGV|perlvar/ARGV> | ||||||||||||||||||||||||||||||||||||
| 98 | (empty C<< <> >>, C<<< <<>> >>>, or L<readline()|perlfunc/readline>). | ||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | =item $handle | ||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | An arbitrary filehandle will have layer(s) pushed onto it directly, affecting | ||||||||||||||||||||||||||||||||||||
| 103 | all usage of that handle, similarly to the operation on standard handles. | ||||||||||||||||||||||||||||||||||||
| 104 | Handles must be passed as a glob or reference to a glob, B<not> a bareword. | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | Note that the handle must be opened in the compile phase (such as in a | ||||||||||||||||||||||||||||||||||||
| 107 | preceding C<BEGIN> block) in order to be available for this pragma to operate | ||||||||||||||||||||||||||||||||||||
| 108 | on it. | ||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | =item r, w, rw | ||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||
| 112 | These strings indicate to set the default layer stack for handles opened in the | ||||||||||||||||||||||||||||||||||||
| 113 | current lexical scope: C<r> for handles opened for reading, C<w> for handles | ||||||||||||||||||||||||||||||||||||
| 114 | opened for writing (or C<O_RDWR>), and C<rw> for all handles. | ||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||
| 116 | This lexical effect works by setting L<${^OPEN}|perlvar/${^OPEN}>, like the | ||||||||||||||||||||||||||||||||||||
| 117 | L<open> pragma and C<-C> switch. The functions L<open()|perlfunc/open>, | ||||||||||||||||||||||||||||||||||||
| 118 | L<sysopen()|perlfunc/sysopen>, L<pipe()|perlfunc/pipe>, | ||||||||||||||||||||||||||||||||||||
| 119 | L<socketpair()|perlfunc/socketpair>, L<socket()|perlfunc/socket>, | ||||||||||||||||||||||||||||||||||||
| 120 | L<accept()|perlfunc/accept>, and L<readpipe()|perlfunc/readpipe> (C<qx> or | ||||||||||||||||||||||||||||||||||||
| 121 | backticks) are affected by this variable. Indirect calls to these functions via | ||||||||||||||||||||||||||||||||||||
| 122 | modules like L<IO::Handle> occur in a different lexical scope, so are not | ||||||||||||||||||||||||||||||||||||
| 123 | affected, nor are directory handles such as opened by | ||||||||||||||||||||||||||||||||||||
| 124 | L<opendir()|perlfunc/opendir>. | ||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||
| 126 | Note that this will also affect implicitly opened read handles such as files | ||||||||||||||||||||||||||||||||||||
| 127 | opened by L<ARGV|perlvar/ARGV> (empty C<< <> >>, C<<< <<>> >>>, or | ||||||||||||||||||||||||||||||||||||
| 128 | L<readline()|perlfunc/readline>), but B<not> C<STDIN> via C<ARGV>, or | ||||||||||||||||||||||||||||||||||||
| 129 | L<DATA|perldata/"Special Literals">. | ||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||
| 131 | A three-argument L<open()|perlfunc/open> call that specifies layers will ignore | ||||||||||||||||||||||||||||||||||||
| 132 | any lexical defaults. A single C<:> (colon) also does this, using the default | ||||||||||||||||||||||||||||||||||||
| 133 | layers for the architecture. | ||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||
| 135 | use open::layers rw => ':encoding(UTF-8)'; | ||||||||||||||||||||||||||||||||||||
| 136 | open my $fh, '<', $file; # sets UTF-8 layer (and its implicit platform defaults) | ||||||||||||||||||||||||||||||||||||
| 137 | open my $fh, '>:unix', $file; # ignores UTF-8 layer and sets :unix | ||||||||||||||||||||||||||||||||||||
| 138 | open my $fh, '<:', $file; # ignores UTF-8 layer and sets platform defaults | ||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | =back | ||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||
| 142 | The second argument is the layer or layers to set. Multiple layers can be | ||||||||||||||||||||||||||||||||||||
| 143 | specified like C<:foo:bar>, as in L<open()|perlfunc/open> or | ||||||||||||||||||||||||||||||||||||
| 144 | L<binmode()|perlfunc/binmode>. | ||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | =head1 COMPARISON TO open.pm | ||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||
| 148 | =over | ||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||
| 150 | =item * | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | Unlike L<open>, C<open::layers> requires that the target of the operation is | ||||||||||||||||||||||||||||||||||||
| 153 | always specified so as to not confuse global and lexical operations. | ||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||
| 155 | =item * | ||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||
| 157 | Unlike L<open>, C<open::layers> can push layers to the standard handles without | ||||||||||||||||||||||||||||||||||||
| 158 | affecting handles opened in the lexical scope. | ||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||
| 160 | =item * | ||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | Unlike L<open>, multiple layers are not required to be space separated. | ||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||
| 164 | =item * | ||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | Unlike L<open>, duplicate existing encoding layers are not removed from the | ||||||||||||||||||||||||||||||||||||
| 167 | standard handles. Either ensure that nothing else is setting encoding layers on | ||||||||||||||||||||||||||||||||||||
| 168 | these handles, or use the C<:raw> pseudo-layer to "reset" the layers to a | ||||||||||||||||||||||||||||||||||||
| 169 | binary stream before applying text translation layers. | ||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | use open::layers STDIO => ':raw:encoding(UTF-16BE)'; | ||||||||||||||||||||||||||||||||||||
| 172 | use open::layers STDIO => ':raw:encoding(UTF-16BE):crlf'; # on Windows 5.14+ | ||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||
| 174 | =item * | ||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||
| 176 | Unlike L<open>, the C<:locale> pseudo-layer is not (yet) implemented. Consider | ||||||||||||||||||||||||||||||||||||
| 177 | installing L<PerlIO::locale> to support this layer. | ||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||
| 179 | =back | ||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||
| 181 | =head1 PERLIO LAYERS | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | PerlIO layers are described in detail in the L<PerlIO> documentation. Their | ||||||||||||||||||||||||||||||||||||
| 184 | implementation has several historical quirks that may be useful to know: | ||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | =over | ||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||
| 188 | =item * | ||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||
| 190 | Layers are an ordered stack; a read operation will go through the layers in the | ||||||||||||||||||||||||||||||||||||
| 191 | order they are set (left to right), and a write operation in the reverse order. | ||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||
| 193 | =item * | ||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||
| 195 | The C<:unix> layer implements the lowest level unbuffered I/O, even on Windows. | ||||||||||||||||||||||||||||||||||||
| 196 | Most other layers operate on top of this and usually a buffering layer like | ||||||||||||||||||||||||||||||||||||
| 197 | C<:perlio> or C<:crlf>, and these low-level layers make up the platform | ||||||||||||||||||||||||||||||||||||
| 198 | defaults. | ||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||
| 200 | =item * | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | Many layers are not real layers that actually implement I/O or translation; | ||||||||||||||||||||||||||||||||||||
| 203 | these are referred to as pseudo-layers. Some (like C<:utf8>) set flags on | ||||||||||||||||||||||||||||||||||||
| 204 | previous layers that change how they operate. Some (like C<:pop>) simply modify | ||||||||||||||||||||||||||||||||||||
| 205 | the existing set of layers. Some (like C<:raw>) may do both. | ||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||
| 207 | =item * | ||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||
| 209 | The C<:crlf> layer is not just a translation between C<\n> and C<CRLF>. On | ||||||||||||||||||||||||||||||||||||
| 210 | Windows, it is the layer that implements I/O buffering (like C<:perlio> on | ||||||||||||||||||||||||||||||||||||
| 211 | Unix-like systems), and operations that would remove the C<CRLF> translation | ||||||||||||||||||||||||||||||||||||
| 212 | (like L<binmode()|perlfunc/binmode> with no layers, or pushing a C<:raw> | ||||||||||||||||||||||||||||||||||||
| 213 | pseudo-layer) actually just disable the C<CRLF> translation flag on this layer. | ||||||||||||||||||||||||||||||||||||
| 214 | Since Perl 5.14, pushing a C<:crlf> layer on top of other translation layers on | ||||||||||||||||||||||||||||||||||||
| 215 | Windows correctly adds a C<CRLF> translation layer in that position. (On | ||||||||||||||||||||||||||||||||||||
| 216 | Unix-like systems, C<:crlf> is a mundane C<CRLF> translation layer.) | ||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||
| 218 | =item * | ||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||
| 220 | The C<:utf8> pseudo-layer sets a flag that indicates the preceding stack of | ||||||||||||||||||||||||||||||||||||
| 221 | layers will translate the input to Perl's internal upgraded string format, | ||||||||||||||||||||||||||||||||||||
| 222 | which may resemble UTF-8 or UTF-EBCDIC, or will translate the output from that | ||||||||||||||||||||||||||||||||||||
| 223 | format. It is B<not> an encoding translation layer, but an assumption about the | ||||||||||||||||||||||||||||||||||||
| 224 | byte stream; use C<:encoding(UTF-8)> or L<PerlIO::utf8_strict> to apply a | ||||||||||||||||||||||||||||||||||||
| 225 | translation layer. Any encoding translation layer will generally set the | ||||||||||||||||||||||||||||||||||||
| 226 | C<:utf8> flag, even when the desired encoding is not UTF-8, as they translate | ||||||||||||||||||||||||||||||||||||
| 227 | between the desired encoding and Perl's internal format. (The C<:bytes> | ||||||||||||||||||||||||||||||||||||
| 228 | pseudo-layer unsets this flag, which is very dangerous if encoding translation | ||||||||||||||||||||||||||||||||||||
| 229 | layers are used.) | ||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||
| 231 | =item * | ||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||
| 233 | I/O to an in-memory scalar variable instead of a file is implemented by the | ||||||||||||||||||||||||||||||||||||
| 234 | C<:scalar> layer taking the place of the platform defaults, see | ||||||||||||||||||||||||||||||||||||
| 235 | L<PerlIO::scalar>. The scalar is expected to act like a file, i.e. only contain | ||||||||||||||||||||||||||||||||||||
| 236 | or store bytes. | ||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||
| 238 | =item * | ||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||
| 240 | Layers specified when opening a handle, such as in a three-argument | ||||||||||||||||||||||||||||||||||||
| 241 | L<open()|perlfunc/open> or default layers set in L<${^OPEN}|perlvar/${^OPEN}> | ||||||||||||||||||||||||||||||||||||
| 242 | (via the lexical usage of this pragma or the L<open> pragma), will define the | ||||||||||||||||||||||||||||||||||||
| 243 | complete stack of layers on that handle. Certain layers implicitly include | ||||||||||||||||||||||||||||||||||||
| 244 | lower-level layers that are needed, for example C<:encoding(UTF-8)> will | ||||||||||||||||||||||||||||||||||||
| 245 | implicitly prepend the platform defaults C<:unix:perlio> (or similar). | ||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||
| 247 | =item * | ||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||
| 249 | In contrast, when adjusting layers on an existing handle with | ||||||||||||||||||||||||||||||||||||
| 250 | L<binmode()|perlfunc/binmode> (or the non-lexical usage of this pragma or the | ||||||||||||||||||||||||||||||||||||
| 251 | L<open> pragma), the specified layers are pushed at the end of the handle's | ||||||||||||||||||||||||||||||||||||
| 252 | existing layer stack, and any special operations of pseudo-layers take effect. | ||||||||||||||||||||||||||||||||||||
| 253 | So you can open an unbuffered handle with only C<:unix>, but to remove existing | ||||||||||||||||||||||||||||||||||||
| 254 | layers on an already open handle, you must push pseudo-layers like C<:pop> or | ||||||||||||||||||||||||||||||||||||
| 255 | C<:raw> (equivalent to calling L<binmode()|perlfunc/binmode> with no layers). | ||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||
| 257 | =back | ||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||
| 259 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||
| 261 | The L<PerlIO> layers and L<open> pragma have experienced several issues over | ||||||||||||||||||||||||||||||||||||
| 262 | the years, most of which can't be worked around by this module. It's | ||||||||||||||||||||||||||||||||||||
| 263 | recommended to use a recent Perl if you will be using complex layers; for | ||||||||||||||||||||||||||||||||||||
| 264 | compatibility with old Perls, stick to L<binmode()|perlfunc/binmode> (either | ||||||||||||||||||||||||||||||||||||
| 265 | with no layers for a binary stream, or with a single C<:encoding> layer). Here | ||||||||||||||||||||||||||||||||||||
| 266 | are some selected issues: | ||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||
| 268 | =over | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | =item * | ||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||
| 272 | Before Perl 5.8.8, L<open()|perlfunc/open> called with three arguments would | ||||||||||||||||||||||||||||||||||||
| 273 | ignore L<${^OPEN}|perlvar/${^OPEN}> and thus any lexical default layers. | ||||||||||||||||||||||||||||||||||||
| 274 | L<[perl #8168]|https://github.com/Perl/perl5/issues/8168> | ||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||
| 276 | =item * | ||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||
| 278 | Before Perl 5.8.9, the C<:crlf> layer did not preserve the C<:utf8> flag from | ||||||||||||||||||||||||||||||||||||
| 279 | an earlier encoding layer, resulting in an improper translation of the bytes. | ||||||||||||||||||||||||||||||||||||
| 280 | This can be worked around by adding the C<:utf8> pseudo-layer after C<:crlf> | ||||||||||||||||||||||||||||||||||||
| 281 | (even if it is not a UTF-8 encoding). | ||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||
| 283 | =item * | ||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||
| 285 | Before Perl 5.14, the C<:crlf> layer does not properly apply on top of another | ||||||||||||||||||||||||||||||||||||
| 286 | layer, such as an encoding layer, if it had also been applied earlier in the | ||||||||||||||||||||||||||||||||||||
| 287 | stack such as is default on Windows. Thus you could not usefully use a layer | ||||||||||||||||||||||||||||||||||||
| 288 | like C<:encoding(UTF-16BE)> with a following C<:crlf>. | ||||||||||||||||||||||||||||||||||||
| 289 | L<[perl #8325]|https://github.com/Perl/perl5/issues/8325> | ||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||
| 291 | =item * | ||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||
| 293 | Before Perl 5.14, the C<:pop>, C<:utf8>, or C<:bytes> pseudo-layers did not | ||||||||||||||||||||||||||||||||||||
| 294 | allow stacking further layers, like C<:pop:crlf>. | ||||||||||||||||||||||||||||||||||||
| 295 | L<[perl #11054]|https://github.com/perl/perl5/issues/11054> | ||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | =item * | ||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||
| 299 | Before Perl 5.14, the C<:raw> pseudo-layer reset the handle to an unbuffered | ||||||||||||||||||||||||||||||||||||
| 300 | state, rather than just removing text translation layers as when calling | ||||||||||||||||||||||||||||||||||||
| 301 | L<binmode()|perlfunc/binmode> with no layers. | ||||||||||||||||||||||||||||||||||||
| 302 | L<[perl #10904]|https://github.com/perl/perl5/issues/10904> | ||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||
| 304 | =item * | ||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||
| 306 | Before Perl 5.14, the C<:raw> pseudo-layer did not work properly with in-memory | ||||||||||||||||||||||||||||||||||||
| 307 | scalar handles. | ||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||
| 309 | =item * | ||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||
| 311 | Before Perl 5.16, L<use|perlfunc/use> and L<require()|perlfunc/require> are | ||||||||||||||||||||||||||||||||||||
| 312 | affected by lexical default layers when loading the source file, leading to | ||||||||||||||||||||||||||||||||||||
| 313 | unexpected results. L<[perl #11541]|https://github.com/perl/perl5/issues/11541> | ||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||
| 315 | =back | ||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||
| 317 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||
| 319 | Report any issues on the public bugtracker. | ||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||
| 321 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||
| 323 | Dan Book <dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||
| 325 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||
| 327 | This software is Copyright (c) 2020 by Dan Book. | ||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||
| 329 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||
| 331 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||
| 333 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||
| 335 | L<open>, L<PerlIO> | ||||||||||||||||||||||||||||||||||||