1 package PerlIO::via::dynamic;
7 PerlIO::via::dynamic - dynamic PerlIO layers
12 $p = PerlIO::via::dynamic->new
14 sub { $_[1] =~ s/\$Filename[:\w\s\-\.\/\\]*\$/\$Filename: $fname\$/e},
16 sub { $_[1] =~ s/\$Filename[:\w\s\-\.\/\\]*\$/\$Filename\$/});
18 binmode $fh, $p->via; # deprecated
22 C<PerlIO::via::dynamic> is used for creating dynamic L<PerlIO>
23 layers. It is useful when the behavior or the layer depends on
24 variables. You should not use this module as via layer directly (ie
27 Use the constructor to create new layers, with two arguments:
28 translate and untranslate. Then use C<$p->via ($fh)> to wrap the
29 handle. Once <$fh> is destroyed, the temporary namespace for the IO
30 layer will be removed.
32 Note that PerlIO::via::dynamic uses the scalar fields to reference to
33 the object representing the dynamic namespace.
41 A function that translate buffer upon I<write>.
45 A function that translate buffer upon I<read>.
49 Use C<READ> instead of C<FILL> for the layer. Useful when caller
50 expect exact amount of data from read, and the C<untranslate> function
51 might return different length.
53 By default C<PerlIO::via::dynamic> creates line-based layer to make
54 C<translate> implementation easier.
60 use Symbol qw(delete_package gensym);
61 use Scalar::Util qw(weaken);
65 die "this should not be via directly"
66 if $_[0] eq __PACKAGE__;
67 my $p = bless gensym(), $_[0];
69 if ($] == 5.010000 && ref($_[-1]) eq 'GLOB') {
70 # This is to workaround a core bug in perl 5.10.0, see
71 # http://rt.perl.org/rt3/Public/Bug/Display.html?id=54934
73 Internals::SetRefCount($_[-1], Internals::GetRefCount($_[-1])+1);
76 # make sure the blessed glob is destroyed
77 # earlier than the object representing the namespace.
78 ${*$p} = ${"$_[0]::EGO"};
90 my $line = readline( $_[1] );
91 $_[0]->untranslate ($line) if defined $line;
96 my $ret = read $_[3], $_[1], $_[2];
97 return $ret unless $ret > 0;
98 $_[0]->untranslate ($_[1]);
99 return length ($_[1]);
104 $_[0]->translate($buf);
105 $_[2]->autoflush (1);
106 (print {$_[2]} $buf) ? length ($buf) : -1;
110 seek ($_[3], $_[1], $_[2]);
114 my ($class, %arg) = @_;
116 my $package = 'PerlIO::via::dynamic'.substr("$self", 7, -1);
119 our \@ISA = qw($class);
125 for (qw/translate untranslate/) {
126 *{"$package\::$_"} = delete $arg{$_}
130 unless ($self->{use_read}) {
131 *{"$package\::FILL"} = *PerlIO::via::dynamic::_FILL;
133 bless $self, $package;
134 ${"$package\::EGO"} = $self;
135 weaken ${"$package\::EGO"};
140 my ($self, $fh) = @_;
141 my $via = ':via('.ref ($_[0]).')';
147 binmode ($fh, $via) or die $!;
148 if (defined ${*$fh}) {
149 if (defined @{*$fh}) {
150 warn "handle $fh cannot hold references, namespace won't be cleaned";
164 return unless UNIVERSAL::isa ($self, 'HASH');
165 return if $self->{nogc};
168 my $ref = ref($self);
169 my ($leaf) = ($ref =~ /([^:]+)$/);
172 for my $sym (keys %{$ref.'::'}) {
173 undef ${$ref.'::'}{$sym}
177 delete $PerlIO::via::{$leaf};
182 Chia-liang Kao E<lt>clkao@clkao.orgE<gt>
186 Copyright 2004 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>.
188 This program is free software; you can redistribute it and/or modify it
189 under the same terms as Perl itself.
191 See L<http://www.perl.com/perl/misc/Artistic.html>