in reply to Tie::File failing with Unicode/UTF-8 encoding?
I don't think it is supported. The option you used is called discipline and that is perl 5.6 talk , that feature is now called PerlIO layers
Also, that feature is not documented in Tie::File ; And, in 5.6, that feature only supported :raw and :crlf, as :encoding did not exist ; You should file a bug report (did I say that?)
But you can work around this limitation by doing your own encoding/decoding. This seems to work
#!/usr/bin/perl -- use Path::Class; use constant THISFILE => file( __FILE__ )->absolute->stringify; use constant THISDIR => file( THISFILE )->dir->stringify; use strict; use warnings; use Tie::File; use Fcntl 'O_RDWR', 'O_CREAT'; use Encode; chdir THISDIR or die Fudge( 'chdir', THISDIR ); Main( @ARGV ); exit( 0 ); sub Main { binmode STDOUT; my $FileName = "".file( THISFILE . '.tf.utf8.txt' ); tie my(@Tied), 'Tie::File', $FileName, recsep => "\x0D\x0A", mode => O_RDWR | O_CREAT, or die Fudge( tie => $FileName ); if( @Tied > 2 ){ print 'size ', int @Tied, "\n"; print map { decode('UTF-8', $_ ); "($_)" } @Tied; } else { push @Tied, encode('UTF-8', chr $_) for 272 .. 30000; } untie @Tied; } sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; } __END__
So you might subclass it like this (untested)
package Tie::File::UTF8; use parent qw[ Tie::File ]; sub TIEARRAY { my $class = shift; $class->SUPER::new( @_, recsep => "\x0D\x0A", discipline => ':raw', ); } sub FETCH { my( $self, $n ) = @_; my $rec = $self->SUPER::FETCH( $n ); $rec = decode( 'UTF-8', $rec ); $rec; } sub STORE { my( $self, $n, $rec ) = @_; $rec = encode( 'UTF-8', $rec ); $self->SUPER::FETCH( $n, $rec ); }
I don't know about more exotic encodings , but it seems UTF-8 could be supported easily
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tie::File failing with Unicode/UTF-8 encoding?
by remiah (Hermit) on Nov 04, 2012 at 02:24 UTC | |
by HelenCr (Monk) on Nov 05, 2012 at 17:20 UTC | |
by remiah (Hermit) on Nov 05, 2012 at 22:10 UTC | |
by HelenCr (Monk) on Nov 07, 2012 at 10:29 UTC | |
by remiah (Hermit) on Nov 08, 2012 at 10:29 UTC | |
by Anonymous Monk on Nov 04, 2012 at 12:55 UTC | |
by HelenCr (Monk) on Nov 04, 2012 at 15:45 UTC | |
by Anonymous Monk on Nov 04, 2012 at 16:11 UTC |