perl-unicode

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

2003-01-29 10:30:06
Autrijus Tang <autrijus(_at_)autrijus(_dot_)org> writes:
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.

It is compile time only because it is supposed to be lexically scoped,
i.e. values are stored in 'cop'.

package Foo;

 use open OUT => ':locale';

 {
  use open OUT => ':encoding(UTF-16le)';
  ... 
 } 

 if ($open::modes{__PACKAGE__}{OUT} ... )
  {
   # bug: 'our' variable has been clobbered
  }
  
  
 


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.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (FreeBSD)

iD8DBQE+NvePtLPdNzw1AaARAm5fAJ9cURDB+e2FO88Aa+ULzJxACOWwAACfSiy0
i/vf6NBdmU5ynqXHU66nRso=
=keaI
-----END PGP SIGNATURE-----
-- 
Nick Ing-Simmons
http://www.ni-s.u-net.com/

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