From: Stevan Little Date: Tue, 23 Oct 2007 18:34:47 +0000 (+0000) Subject: fixing the utf8 handling for files X-Git-Tag: 0_09~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6237d568359ed9a46294aa26414fca42f40f450c;p=gitmo%2FMooseX-Storage.git fixing the utf8 handling for files --- diff --git a/Changes b/Changes index ef1400c..42246ab 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,11 @@ Revision history for MooseX-Storage 0.09 * MooseX::Storage::Util - added support to deal with utf8 strings correctly + + * MooseX::Storage::Engine::File + MooseX::Storage::Engine::AtomicFile + - fixed utf8 handling when storing string + - added tests for this * t/ - added a test for the utf8 handling diff --git a/lib/MooseX/Storage/Engine/IO/AtomicFile.pm b/lib/MooseX/Storage/Engine/IO/AtomicFile.pm index 6c58228..fc081df 100644 --- a/lib/MooseX/Storage/Engine/IO/AtomicFile.pm +++ b/lib/MooseX/Storage/Engine/IO/AtomicFile.pm @@ -2,9 +2,10 @@ package MooseX::Storage::Engine::IO::AtomicFile; use Moose; +use utf8 (); use IO::AtomicFile; -our $VERSION = '0.02'; +our $VERSION = '0.03'; our $AUTHORITY = 'cpan:STEVAN'; extends 'MooseX::Storage::Engine::IO::File'; @@ -13,6 +14,7 @@ sub store { my ($self, $data) = @_; my $fh = IO::AtomicFile->new($self->file, 'w') || confess "Unable to open file (" . $self->file . ") for storing : $!"; + $fh->binmode(':utf8') if utf8::is_utf8($data); print $fh $data; $fh->close() || confess "Could not write atomic file (" . $self->file . ") because: $!"; diff --git a/lib/MooseX/Storage/Engine/IO/File.pm b/lib/MooseX/Storage/Engine/IO/File.pm index eb92a04..02d1bcd 100644 --- a/lib/MooseX/Storage/Engine/IO/File.pm +++ b/lib/MooseX/Storage/Engine/IO/File.pm @@ -2,9 +2,10 @@ package MooseX::Storage::Engine::IO::File; use Moose; +use utf8 (); use IO::File; -our $VERSION = '0.02'; +our $VERSION = '0.03'; our $AUTHORITY = 'cpan:STEVAN'; has 'file' => ( @@ -24,6 +25,7 @@ sub store { my ($self, $data) = @_; my $fh = IO::File->new($self->file, 'w') || confess "Unable to open file (" . $self->file . ") for storing : $!"; + $fh->binmode(':utf8') if utf8::is_utf8($data); print $fh $data; } diff --git a/t/104_io_w_utf8.t b/t/104_io_w_utf8.t new file mode 100644 index 0000000..39753ad --- /dev/null +++ b/t/104_io_w_utf8.t @@ -0,0 +1,80 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +BEGIN { + # NOTE: + # this is because JSON::XS is + # the only one which really gets + # utf8 correct + # - SL + BEGIN { + $ENV{JSON_ANY_ORDER} = qw(XS); + $ENV{JSON_ANY_CONFIG} = "utf8=1"; + } + plan tests => 8; + use_ok('MooseX::Storage'); +} + +use utf8; + +{ + package Foo; + use Moose; + use MooseX::Storage; + + with Storage( 'format' => 'JSON', 'io' => 'File' ); + + has 'utf8_string' => ( + is => 'rw', + isa => 'Str', + default => sub { "ネットスーパー (Internet Shopping)" } + ); +} + +my $file = 'temp.json'; + +{ + my $foo = Foo->new; + isa_ok( $foo, 'Foo' ); + + $foo->store($file); +} + +{ + my $foo = Foo->load($file); + isa_ok($foo, 'Foo'); + + is($foo->utf8_string, + "ネットスーパー (Internet Shopping)", + '... got the string we expected'); +} + +no utf8; + +unlink $file; + +{ + my $foo = Foo->new( + utf8_string => 'Escritório' + ); + isa_ok( $foo, 'Foo' ); + + $foo->store($file); +} + +{ + my $foo = Foo->load($file); + isa_ok($foo, 'Foo'); + + ok(utf8::is_utf8($foo->utf8_string), '... the string is still utf8'); + + is($foo->utf8_string, + "Escritório", + '... got the string we expected'); +} + +unlink $file; diff --git a/t/105_io_atomic_w_utf8.t b/t/105_io_atomic_w_utf8.t new file mode 100644 index 0000000..8b5678c --- /dev/null +++ b/t/105_io_atomic_w_utf8.t @@ -0,0 +1,80 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +BEGIN { + # NOTE: + # this is because JSON::XS is + # the only one which really gets + # utf8 correct + # - SL + BEGIN { + $ENV{JSON_ANY_ORDER} = qw(XS); + $ENV{JSON_ANY_CONFIG} = "utf8=1"; + } + plan tests => 8; + use_ok('MooseX::Storage'); +} + +use utf8; + +{ + package Foo; + use Moose; + use MooseX::Storage; + + with Storage( 'format' => 'JSON', 'io' => 'AtomicFile' ); + + has 'utf8_string' => ( + is => 'rw', + isa => 'Str', + default => sub { "ネットスーパー (Internet Shopping)" } + ); +} + +my $file = 'temp.json'; + +{ + my $foo = Foo->new; + isa_ok( $foo, 'Foo' ); + + $foo->store($file); +} + +{ + my $foo = Foo->load($file); + isa_ok($foo, 'Foo'); + + is($foo->utf8_string, + "ネットスーパー (Internet Shopping)", + '... got the string we expected'); +} + +no utf8; + +unlink $file; + +{ + my $foo = Foo->new( + utf8_string => 'Escritório' + ); + isa_ok( $foo, 'Foo' ); + + $foo->store($file); +} + +{ + my $foo = Foo->load($file); + isa_ok($foo, 'Foo'); + + ok(utf8::is_utf8($foo->utf8_string), '... the string is still utf8'); + + is($foo->utf8_string, + "Escritório", + '... got the string we expected'); +} + +unlink $file;