Integrate perlio:
[p5sagit/p5-mst-13.2.git] / lib / open.pm
index 8f5c138..82b043a 100644 (file)
@@ -1,23 +1,43 @@
 package open;
+use Carp;
 $open::hint_bits = 0x20000;
 
+use vars qw(%layers @layers);
+
+# Populate hash in non-PerlIO case
+%layers = (crlf => 1, raw => 0) unless (@layers);
+
 sub import {
     shift;
     die "`use open' needs explicit list of disciplines" unless @_;
     $^H |= $open::hint_bits;
+    my ($in,$out) = split(/\0/,(${^OPEN} || '\0'));
+    my @in  = split(/\s+/,$in);
+    my @out = split(/\s+/,$out);
     while (@_) {
        my $type = shift;
-       if ($type =~ /^(IN|OUT)\z/s) {
-           my $discp = shift;
-           unless ($discp =~ /^\s*:(raw|crlf)\s*\z/s) {
-               die "Unknown discipline '$discp'";
+       my $discp = shift;
+       my @val;
+       foreach my $layer (split(/\s+:?/,$discp)) {
+           unless(exists $layers{$layer}) {
+               croak "Unknown discipline layer '$layer'";
+           }
+           push(@val,":$layer");
+           if ($layer =~ /^(crlf|raw)$/) {
+               $^H{"open_$type"} = $layer;
            }
-           $^H{"open_$type"} = $discp;
+       }
+       if ($type eq 'IN') {
+           $in  = join(' ',@val);
+       }
+       elsif ($type eq 'OUT') {
+           $out = join(' ',@val);
        }
        else {
-           die "Unknown discipline class '$type'";
+           croak "Unknown discipline class '$type'";
        }
     }
+    ${^OPEN} = join('\0',$in,$out);
 }
 
 1;
@@ -37,7 +57,7 @@ The open pragma is used to declare one or more default disciplines for
 I/O operations.  Any open() and readpipe() (aka qx//) operators found
 within the lexical scope of this pragma will use the declared defaults.
 Neither open() with an explicit set of disciplines, nor sysopen() are
-not influenced by this pragma.
+influenced by this pragma.
 
 Only the two pseudo-disciplines ":raw" and ":crlf" are currently
 available.
@@ -56,12 +76,12 @@ When they are eventually supported, this pragma will serve as one of
 the interfaces to declare default disciplines for all I/O.
 
 In future, any default disciplines declared by this pragma will be
-available by the special discipline name ":def", and could be used
+available by the special discipline name ":DEFAULT", and could be used
 within handle constructors that allow disciplines to be specified.
 This would make it possible to stack new disciplines over the default
 ones.
 
-    open FH, "<:para :def", $file or die "can't open $file: $!";
+    open FH, "<:para :DEFAULT", $file or die "can't open $file: $!";
 
 Socket and directory handles will also support disciplines in
 future.