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