This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make "real" layers of ":utf8" and ":raw".
[perl5.git] / lib / open.pm
CommitLineData
d1edabcf 1package open;
ac27b0f5 2use Carp;
16fe6d59
GS
3$open::hint_bits = 0x20000;
4
ac27b0f5
NIS
5use vars qw(%layers @layers);
6
7# Populate hash in non-PerlIO case
8%layers = (crlf => 1, raw => 0) unless (@layers);
9
dfebf958
NIS
10# warn join(',',keys %layers);
11
b75c8c73
MS
12our $VERSION = '1.00';
13
16fe6d59 14sub import {
dfebf958
NIS
15 my ($class,@args) = @_;
16 croak("`use open' needs explicit list of disciplines") unless @args;
16fe6d59 17 $^H |= $open::hint_bits;
ac27b0f5
NIS
18 my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
19 my @in = split(/\s+/,$in);
20 my @out = split(/\s+/,$out);
dfebf958
NIS
21 while (@args) {
22 my $type = shift(@args);
23 my $discp = shift(@args);
ac27b0f5 24 my @val;
dfebf958
NIS
25 foreach my $layer (split(/\s+/,$discp)) {
26 $layer =~ s/^://;
ac27b0f5 27 unless(exists $layers{$layer}) {
dfebf958 28 carp("Unknown discipline layer '$layer'");
ac27b0f5
NIS
29 }
30 push(@val,":$layer");
31 if ($layer =~ /^(crlf|raw)$/) {
32 $^H{"open_$type"} = $layer;
16fe6d59 33 }
ac27b0f5
NIS
34 }
35 if ($type eq 'IN') {
36 $in = join(' ',@val);
37 }
38 elsif ($type eq 'OUT') {
39 $out = join(' ',@val);
16fe6d59
GS
40 }
41 else {
ac27b0f5 42 croak "Unknown discipline class '$type'";
16fe6d59
GS
43 }
44 }
ac27b0f5 45 ${^OPEN} = join('\0',$in,$out);
16fe6d59
GS
46}
47
481;
49__END__
d1edabcf
GS
50
51=head1 NAME
52
53open - perl pragma to set default disciplines for input and output
54
55=head1 SYNOPSIS
56
16fe6d59 57 use open IN => ":crlf", OUT => ":raw";
d1edabcf
GS
58
59=head1 DESCRIPTION
60
d1edabcf 61The open pragma is used to declare one or more default disciplines for
16fe6d59
GS
62I/O operations. Any open() and readpipe() (aka qx//) operators found
63within the lexical scope of this pragma will use the declared defaults.
64Neither open() with an explicit set of disciplines, nor sysopen() are
642f9deb 65influenced by this pragma.
16fe6d59
GS
66
67Only the two pseudo-disciplines ":raw" and ":crlf" are currently
68available.
69
70The ":raw" discipline corresponds to "binary mode" and the ":crlf"
71discipline corresponds to "text mode" on platforms that distinguish
72between the two modes when opening files (which is many DOS-like
73platforms, including Windows). These two disciplines are currently
74no-ops on platforms where binmode() is a no-op, but will be
75supported everywhere in future.
d1edabcf 76
16fe6d59 77=head1 UNIMPLEMENTED FUNCTIONALITY
d1edabcf 78
16fe6d59
GS
79Full-fledged support for I/O disciplines is currently unimplemented.
80When they are eventually supported, this pragma will serve as one of
81the interfaces to declare default disciplines for all I/O.
82
83In future, any default disciplines declared by this pragma will be
9bafe919 84available by the special discipline name ":DEFAULT", and could be used
16fe6d59
GS
85within handle constructors that allow disciplines to be specified.
86This would make it possible to stack new disciplines over the default
87ones.
d1edabcf 88
9bafe919 89 open FH, "<:para :DEFAULT", $file or die "can't open $file: $!";
d1edabcf 90
16fe6d59
GS
91Socket and directory handles will also support disciplines in
92future.
93
94Full support for I/O disciplines will enable all of the supported
95disciplines to work on all platforms.
96
d1edabcf
GS
97=head1 SEE ALSO
98
16fe6d59 99L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>
d1edabcf
GS
100
101=cut