This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix open.pm to work via XS-implemented method calls rather
[perl5.git] / lib / open.pm
CommitLineData
d1edabcf 1package open;
ac27b0f5 2use Carp;
16fe6d59
GS
3$open::hint_bits = 0x20000;
4
0c4f7ff0 5our $VERSION = '1.01';
b75c8c73 6
16fe6d59 7sub import {
dfebf958
NIS
8 my ($class,@args) = @_;
9 croak("`use open' needs explicit list of disciplines") unless @args;
16fe6d59 10 $^H |= $open::hint_bits;
ac27b0f5
NIS
11 my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
12 my @in = split(/\s+/,$in);
13 my @out = split(/\s+/,$out);
dfebf958
NIS
14 while (@args) {
15 my $type = shift(@args);
16 my $discp = shift(@args);
ac27b0f5 17 my @val;
dfebf958
NIS
18 foreach my $layer (split(/\s+/,$discp)) {
19 $layer =~ s/^://;
0c4f7ff0 20 unless(PerlIO::Layer::->find($layer)) {
dfebf958 21 carp("Unknown discipline layer '$layer'");
ac27b0f5
NIS
22 }
23 push(@val,":$layer");
24 if ($layer =~ /^(crlf|raw)$/) {
25 $^H{"open_$type"} = $layer;
16fe6d59 26 }
ac27b0f5
NIS
27 }
28 if ($type eq 'IN') {
29 $in = join(' ',@val);
30 }
31 elsif ($type eq 'OUT') {
32 $out = join(' ',@val);
16fe6d59
GS
33 }
34 else {
ac27b0f5 35 croak "Unknown discipline class '$type'";
16fe6d59
GS
36 }
37 }
ac27b0f5 38 ${^OPEN} = join('\0',$in,$out);
16fe6d59
GS
39}
40
411;
42__END__
d1edabcf
GS
43
44=head1 NAME
45
46open - perl pragma to set default disciplines for input and output
47
48=head1 SYNOPSIS
49
16fe6d59 50 use open IN => ":crlf", OUT => ":raw";
d1edabcf
GS
51
52=head1 DESCRIPTION
53
d151aa0e
JH
54Full-fledged support for I/O disciplines is now implemented provided
55Perl is configured to use PerlIO as its IO system (which is now the
56default).
16fe6d59 57
7d3b96bb
NIS
58The C<open> pragma serves as one of the interfaces to declare default
59"layers" (aka disciplines) for all I/O.
60
61The C<open> pragma is used to declare one or more default layers for
d151aa0e
JH
62I/O operations. Any open(), readpipe() (aka qx//) and similar
63operators found within the lexical scope of this pragma will use the
64declared defaults.
7d3b96bb 65
d151aa0e
JH
66When open() is given an explicit list of layers they are appended to
67the list declared using this pragma.
7d3b96bb
NIS
68
69Directory handles may also support disciplines in future.
70
71=head1 NONPERLIO FUNCTIONALITY
72
d151aa0e
JH
73If Perl is not built to use PerlIO as its IO system then only the two
74pseudo-disciplines ":raw" and ":crlf" are available.
16fe6d59
GS
75
76The ":raw" discipline corresponds to "binary mode" and the ":crlf"
77discipline corresponds to "text mode" on platforms that distinguish
78between the two modes when opening files (which is many DOS-like
d151aa0e
JH
79platforms, including Windows). These two disciplines are no-ops on
80platforms where binmode() is a no-op, but perform their functions
81everywhere if PerlIO is enabled.
7d3b96bb
NIS
82
83=head1 IMPLEMENTATION DETAILS
d1edabcf 84
0c4f7ff0
NIS
85There is a class method in C<PerlIO::Layer> C<find> which is implemented as XS code.
86It is called by C<import> to validate the layers:
87
88 PerlIO::Layer::->find("perlio")
89
90The return value (if defined) is a Perl object, of class C<PerlIO::Layer> which is
91created by the C code in F<perlio.c>. As yet there is nothing useful you can do with the
92object at the perl level.
16fe6d59 93
d1edabcf
GS
94=head1 SEE ALSO
95
7d3b96bb 96L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO>
d1edabcf
GS
97
98=cut