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