Commit | Line | Data |
d1edabcf |
1 | package open; |
ac27b0f5 |
2 | use Carp; |
16fe6d59 |
3 | $open::hint_bits = 0x20000; |
4 | |
0c4f7ff0 |
5 | our $VERSION = '1.01'; |
b75c8c73 |
6 | |
16fe6d59 |
7 | sub import { |
dfebf958 |
8 | my ($class,@args) = @_; |
9 | croak("`use open' needs explicit list of disciplines") unless @args; |
16fe6d59 |
10 | $^H |= $open::hint_bits; |
ac27b0f5 |
11 | my ($in,$out) = split(/\0/,(${^OPEN} || '\0')); |
12 | my @in = split(/\s+/,$in); |
13 | my @out = split(/\s+/,$out); |
dfebf958 |
14 | while (@args) { |
15 | my $type = shift(@args); |
16 | my $discp = shift(@args); |
ac27b0f5 |
17 | my @val; |
dfebf958 |
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 |
22 | } |
23 | push(@val,":$layer"); |
24 | if ($layer =~ /^(crlf|raw)$/) { |
25 | $^H{"open_$type"} = $layer; |
16fe6d59 |
26 | } |
ac27b0f5 |
27 | } |
28 | if ($type eq 'IN') { |
29 | $in = join(' ',@val); |
30 | } |
31 | elsif ($type eq 'OUT') { |
32 | $out = join(' ',@val); |
16fe6d59 |
33 | } |
34 | else { |
ac27b0f5 |
35 | croak "Unknown discipline class '$type'"; |
16fe6d59 |
36 | } |
37 | } |
ac27b0f5 |
38 | ${^OPEN} = join('\0',$in,$out); |
16fe6d59 |
39 | } |
40 | |
41 | 1; |
42 | __END__ |
d1edabcf |
43 | |
44 | =head1 NAME |
45 | |
46 | open - perl pragma to set default disciplines for input and output |
47 | |
48 | =head1 SYNOPSIS |
49 | |
16fe6d59 |
50 | use open IN => ":crlf", OUT => ":raw"; |
d1edabcf |
51 | |
52 | =head1 DESCRIPTION |
53 | |
d151aa0e |
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). |
16fe6d59 |
57 | |
7d3b96bb |
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 |
d151aa0e |
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. |
7d3b96bb |
65 | |
d151aa0e |
66 | When open() is given an explicit list of layers they are appended to |
67 | the list declared using this pragma. |
7d3b96bb |
68 | |
69 | Directory handles may also support disciplines in future. |
70 | |
71 | =head1 NONPERLIO FUNCTIONALITY |
72 | |
d151aa0e |
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. |
16fe6d59 |
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 |
d151aa0e |
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. |
7d3b96bb |
82 | |
83 | =head1 IMPLEMENTATION DETAILS |
d1edabcf |
84 | |
0c4f7ff0 |
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. |
16fe6d59 |
93 | |
d1edabcf |
94 | =head1 SEE ALSO |
95 | |
7d3b96bb |
96 | L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>, L<PerlIO> |
d1edabcf |
97 | |
98 | =cut |