perl-unicode

[Encode] is there any way to tell the current I/O disciplines ?

2002-03-30 12:07:33
NI-S,

I am now working on encoding.pm. I wrote a more elaborate version of jperl.t and the prognosis is bright.
  Now I am adding I/O control for encoding.pm and found one thing;
To make "no encoding" work with I/O control, the old state of line disciplines must be stored somewhere. But how are you going to poke PerlIO so you can do it?
  My current implementation looks like the one after the sig

Dan the encoding man
#===================
our %oIOs = ();

sub import {
    my $class = shift;
    my $name  = shift;
    my %arg = @_;
    $name ||= $ENV{PERL_ENCODING};

    # no default to make it more politically correct
    # $name = "latin1" unless defined $name;

    my $enc = find_encoding($name);
    unless (defined $enc) {
        require Carp;
        Carp::croak "Unknown encoding '$name'";
    }
    ${^ENCODING} = $enc;

    # Init PerlIO also
    # exists $arg{STD} or $arg{STD} = $name;

    return unless keys %arg;

    $arg{STDIN}  ||= $arg{STD};
    $arg{STDOUT} ||= $arg{STD};
    $arg{STDOUT} ||= $arg{STD};

    # needs to store the old line disciplines but how?

    eval qq{ use open IO  => "encoding($arg{IO})" }       if $arg{IO};
    eval qq{ use open IN  => "encoding($arg{IN})" }       if $arg{IN};
    eval qq{ use open OUT => "encoding($arg{OUT})" }      if $arg{OUT};
    eval qq{ binmode STDIN  => "encoding($arg{STDIN})" }  if $arg{STDIN};
eval qq{ binmode STDOUT => "encoding($arg{STDOUT})" } if $arg{STDOUT}; eval qq{ binmode STDERR => "encoding($arg{STDERR})" } if $arg{STDERR};

    eval qq{ use open IO  => "encoding($arg{IO})" }       if $arg{IO};
    eval qq{ use open IN  => "encoding($arg{IN})" }       if $arg{IN};
    eval qq{ use open OUT => "encoding($arg{OUT})" }      if $arg{OUT};
    eval qq{ binmode STDIN  => "encoding($arg{STDIN})" }  if $arg{STDIN};
eval qq{ binmode STDOUT => "encoding($arg{STDOUT})" } if $arg{STDOUT}; eval qq{ binmode STDERR => "encoding($arg{STDERR})" } if $arg{STDERR};
}

sub unimport{
    no warnings;
    undef ${^ENCODING};
    eval qq{ use open IO  => "encoding($oIOs{IO})" }       if $oIOs{IO};
    eval qq{ use open IN  => "encoding($oIOs{IN})" }       if $oIOs{IN};
    eval qq{ use open OUT => "encoding($oIOs{OUT})" }      if $oIOs{OUT};
eval qq{ binmode STDIN => "encoding($oIOs{STDIN})" } if $oIOs{STDIN}; eval qq{ binmode STDOUT => "encoding($oIOs{STDOUT})" } if $oIOs{STDOUT}; eval qq{ binmode STDERR => "encoding($oIOs{STDERR})" } if $oIOs{STDERR};
}

1;
__END__

.....and pod

<Prev in Thread] Current Thread [Next in Thread>
  • [Encode] is there any way to tell the current I/O disciplines ?, Dan Kogai <=