perl-unicode

[PATCH] %open::modes to hold ${^OPEN} values for run-time access

2003-01-28 15:30:05
The attached patch implements the %open::modes hash, which holds
the compile time values for IN and OUT layers for run-time access.

This is needed if the module wishes to know the encoding it's under,
and offers a nice idiom:

    use open OUT => ':locale';
    # ... do things based on $open::modes{(__PACKAGE__)}{OUT} ...

Since previously, the :locale decision is only available from the
compile-time-only ${^OPEN} variable.

Thanks,
/Autrijus/

--- perl5.8/perl/lib/open.pm    Thu Jan 23 03:24:47 2003
+++ open.pm     Wed Jan 29 05:30:10 2003
@@ -4,6 +4,7 @@
 $open::hint_bits = 0x20000; # HINT_LOCALIZE_HH
 
 our $VERSION = '1.01';
+our %modes;
 
 my $locale_encoding;
 
@@ -118,6 +119,11 @@
        }
     }
     ${^OPEN} = join("\0",$in,$out) if $in or $out;
+
+    my $caller = caller();
+    $caller = caller(1) if $caller eq 'if';
+    $open::modes{$caller} = { IN => $in, OUT => $out };
+
     if ($std) {
        if ($in) {
            if ($in =~ /:utf8\b/) {
@@ -159,6 +165,8 @@
 
     use open ':std';
 
+    my $in_mode = $open::modes{(__PACKAGE__)}{IN};
+
 =head1 DESCRIPTION
 
 Full-fledged support for I/O layers is now implemented provided
@@ -174,6 +182,12 @@
 of input streams, and with the C<OUT> subpragma you can declare
 the default layers of output streams.  With the C<IO>  subpragma
 you can control both input and output streams simultaneously.
+
+Both the C<IN> and C<OUT> values are stored in the C<%open::modes>
+hash.  Its keys are the caller's packages (or the second-level calling
+package if the caller is C<if>); the values are hash references
+with two keys: C<IN> holds the input mode, and C<OUT> for the output
+mode.
 
 If you have a legacy encoding, you can use the C<:encoding(...)> tag.
 

Attachment: pgpB8R0eQDetB.pgp
Description: PGP signature

<Prev in Thread] Current Thread [Next in Thread>