PATCH: File::Temp fix on WindowsNT/VMS
[p5sagit/p5-mst-13.2.git] / lib / open.pm
1 package open;
2 use Carp;
3 $open::hint_bits = 0x20000;
4
5 use vars qw(%layers @layers);
6
7 # Populate hash in non-PerlIO case
8 %layers = (crlf => 1, raw => 0) unless (@layers);
9
10 sub import {
11     shift;
12     die "`use open' needs explicit list of disciplines" unless @_;
13     $^H |= $open::hint_bits;
14     my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
15     my @in  = split(/\s+/,$in);
16     my @out = split(/\s+/,$out);
17     while (@_) {
18         my $type = shift;
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;
28             }
29         }
30         if ($type eq 'IN') {
31             $in  = join(' ',@val);
32         }
33         elsif ($type eq 'OUT') {
34             $out = join(' ',@val);
35         }
36         else {
37             croak "Unknown discipline class '$type'";
38         }
39     }
40     ${^OPEN} = join('\0',$in,$out);
41 }
42
43 1;
44 __END__
45
46 =head1 NAME
47
48 open - perl pragma to set default disciplines for input and output
49
50 =head1 SYNOPSIS
51
52     use open IN => ":crlf", OUT => ":raw";
53
54 =head1 DESCRIPTION
55
56 The open pragma is used to declare one or more default disciplines for
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
60 influenced by this pragma.
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.
71
72 =head1 UNIMPLEMENTED FUNCTIONALITY
73
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
79 available by the special discipline name ":DEFAULT", and could be used
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.
83
84     open FH, "<:para :DEFAULT", $file or die "can't open $file: $!";
85
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
92 =head1 SEE ALSO
93
94 L<perlfunc/"binmode">, L<perlfunc/"open">, L<perlunicode>
95
96 =cut