3 $open::hint_bits = 0x20000;
5 # layers array and hash mainly manipulated by C code in perlio.c
6 use vars qw(%layers @layers);
8 # Populate hash in non-PerlIO case
9 %layers = (crlf => 1, raw => 0) unless (@layers);
11 # warn join(',',keys %layers);
13 our $VERSION = '1.00';
16 my ($class,@args) = @_;
17 croak("`use open' needs explicit list of disciplines") unless @args;
18 $^H |= $open::hint_bits;
19 my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
20 my @in = split(/\s+/,$in);
21 my @out = split(/\s+/,$out);
23 my $type = shift(@args);
24 my $discp = shift(@args);
26 foreach my $layer (split(/\s+/,$discp)) {
28 unless(exists $layers{$layer}) {
29 carp("Unknown discipline layer '$layer'");
32 if ($layer =~ /^(crlf|raw)$/) {
33 $^H{"open_$type"} = $layer;
39 elsif ($type eq 'OUT') {
40 $out = join(' ',@val);
43 croak "Unknown discipline class '$type'";
46 ${^OPEN} = join('\0',$in,$out);
54 open - perl pragma to set default disciplines for input and output
58 use open IN => ":crlf", OUT => ":raw";
62 Full-fledged support for I/O disciplines is now implemented provided
63 Perl is configured to use PerlIO as its IO system (which is now the
66 The C<open> pragma serves as one of the interfaces to declare default
67 "layers" (aka disciplines) for all I/O.
69 The C<open> pragma is used to declare one or more default layers for
70 I/O operations. Any open(), readpipe() (aka qx//) and similar
71 operators found within the lexical scope of this pragma will use the
74 When open() is given an explicit list of layers they are appended to
75 the list declared using this pragma.
77 Directory handles may also support disciplines in future.
79 =head1 NONPERLIO FUNCTIONALITY
81 If Perl is not built to use PerlIO as its IO system then only the two
82 pseudo-disciplines ":raw" and ":crlf" are available.
84 The ":raw" discipline corresponds to "binary mode" and the ":crlf"
85 discipline corresponds to "text mode" on platforms that distinguish
86 between the two modes when opening files (which is many DOS-like
87 platforms, including Windows). These two disciplines are no-ops on
88 platforms where binmode() is a no-op, but perform their functions
89 everywhere if PerlIO is enabled.
91 =head1 IMPLEMENTATION DETAILS
93 There are two package variables C<%layers> and C<@layers> which are
94 mainly manipulated by C code in F<perlio.c>, but are visible to the
97 print "Have ",join(',',keys %open::layers),"\n";
98 print "Using ",join(',',@open::layers),"\n";
100 The C<%open::layers> hash is a record of the available "layers" that
101 may be pushed onto a C<PerlIO> stream. The values of the hash are Perl
102 objects, of class C<PerlIO::Layer> which are created by the C code in
103 F<perlio.c>. As yet there is nothing useful you can do with the
104 objects at the perl level.
106 The C<@open::layers> array is the current set of layers and their
107 arguments. The array consists of layer => argument pairs and I<must>
108 always have even number of entries and the even entries I<must> be
109 C<PerlIO::Layer> objects or Perl will "die" when it attempts to open a
110 filehandle. In most cases the odd entry will be C<undef>, but in the
111 case of (say) ":encoding(iso-8859-1)" it will be 'iso-8859-1'. These
112 argument entries are currently restricted to being strings.
114 When a new C<PerlIO> stream is opened, the C code looks at the array
115 to determine the default layers to be pushed. So with care it is
116 possible to manipulate the default layer "stack":
118 splice(@PerlIO::layers,-2,2);
119 push(@PerlIO::layers,$PerlIO::layers{'stdio'} => undef);
123 L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO>