File-Slurp-Unicode-0.7.1/000755 000765 000765 00000000000 11360462570 015323 5ustar00daviddavid000000 000000 File-Slurp-Unicode-0.7.1/Build.PL000444 000765 000765 00000000437 11360462570 016621 0ustar00daviddavid000000 000000 use Module::Build; Module::Build->new( module_name => 'File::Slurp::Unicode', license => 'perl', dist_author => 'David Caldwell ', requires => { perl => '5.10.0', 'File::Slurp' => 0, }, )->create_build_script; File-Slurp-Unicode-0.7.1/Changes000444 000765 000765 00000000464 11360462570 016620 0ustar00daviddavid000000 000000 File::Slurp::Unicode (0.7.1) * Added File::Slurp to dependencies. Not a core module after all. :-( -- David Caldwell Sun, 11 Apr 2010 17:06:28 -0700 File::Slurp::Unicode (0.7.0) * First release to CPAN -- David Caldwell Sat, 10 Apr 2010 16:34:35 -0700 File-Slurp-Unicode-0.7.1/MANIFEST000444 000765 000765 00000000230 11360462570 016445 0ustar00daviddavid000000 000000 Build.PL Changes lib/File/Slurp/Unicode.pm MANIFEST This list of files README t/00_basic.t t/10_read_file.t t/20_write_file.t t/30_unicode.t META.yml File-Slurp-Unicode-0.7.1/META.yml000444 000765 000765 00000000765 11360462567 016610 0ustar00daviddavid000000 000000 --- name: File-Slurp-Unicode version: 0.7.1 author: - 'David Caldwell ' abstract: Reading/Writing of Complete Files with Character Encoding Support license: perl resources: license: http://dev.perl.org/licenses/ requires: File::Slurp: 0 perl: 5.10.0 provides: File::Slurp::Unicode: file: lib/File/Slurp/Unicode.pm version: 0.7.1 generated_by: Module::Build version 0.280801 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.2.html version: 1.2 File-Slurp-Unicode-0.7.1/README000444 000765 000765 00000000760 11360462570 016204 0ustar00daviddavid000000 000000 File::Slurp::Unicode -------------------- This module wraps the standard File::Slurp package and adds character encoding support. For full documentation, run: perldoc lib/File/Slurp/Unicode.pm Installation ------------ To build, test and install: perl Build.PL ./Build ./Build test ./Build install License ------- Copyright 2010 David Caldwell This perl module is free software; it may be redistributed and/or modified under the same terms as Perl itself. File-Slurp-Unicode-0.7.1/lib/000755 000765 000765 00000000000 11360462570 016071 5ustar00daviddavid000000 000000 File-Slurp-Unicode-0.7.1/t/000755 000765 000765 00000000000 11360462570 015566 5ustar00daviddavid000000 000000 File-Slurp-Unicode-0.7.1/t/00_basic.t000444 000765 000765 00000000157 11360462570 017334 0ustar00daviddavid000000 000000 #!/usr/bin/perl use strict; use warnings; use Test::More; plan tests => 1; use_ok('File::Slurp::Unicode'); File-Slurp-Unicode-0.7.1/t/10_read_file.t000444 000765 000765 00000001372 11360462570 020166 0ustar00daviddavid000000 000000 #!/usr/bin/perl # This just tests that I mimic the File::Slurp APIs correctly. use strict; use warnings; use Test::More; plan tests => 5; use File::Slurp::Unicode; use File::Temp qw(tempfile); my ($fh, $name) = tempfile(); $fh->autoflush(1); my @expect = map { "$_\n" } qw(line1 line2 line3); my $expect = join '', @expect; print $fh $expect; my @lines = read_file $name; is_deeply(\@lines, \@expect, "list context"); my $data = read_file $name; is($data, $expect, "scalar context"); my $data_ref; read_file $name, buf_ref => \$data_ref; is($data_ref, $expect, "buf_ref"); $data_ref = read_file $name, scalar_ref => 1; is(${$data_ref}, $expect, "scalar_ref"); $data_ref = read_file $name, array_ref => 1; is_deeply($data_ref, \@expect, "array_ref"); File-Slurp-Unicode-0.7.1/t/20_write_file.t000444 000765 000765 00000001302 11360462570 020377 0ustar00daviddavid000000 000000 #!/usr/bin/perl # This just tests that I mimic the File::Slurp APIs correctly. use strict; use warnings; use Test::More; plan tests => 5; use File::Slurp::Unicode; use File::Temp qw(tempfile); my ($fh, $name) = tempfile(); $fh->autoflush(1); my @data = map { "$_\n" } qw(line1 line2 line3); my $data = join '', @data; print $fh $data; sub compare { my ($test_name) = @_; seek($fh, 0,0); is_deeply([$fh->getlines], \@data, $test_name); } write_file $name, @data; compare("list"); write_file $name, $data; compare("scalar"); write_file $name, \@data; compare("array ref"); write_file $name, \$data; compare("scalar ref"); write_file $name, { buf_ref => \$data }; compare("buf_ref"); File-Slurp-Unicode-0.7.1/t/30_unicode.t000444 000765 000765 00000002154 11360462570 017703 0ustar00daviddavid000000 000000 #!/usr/bin/perl # This tests the meat of what File::Slurp::Unicode does. use strict; use warnings; use Test::More; plan tests => 7; use File::Temp qw(tempfile); my ($fh, $name) = tempfile(); use Encode; use utf8; my $wide_data = "Dävîd"; my $bin_data = "D\xc3\xa4v\xc3\xaed"; my $latin_data = "D\xe4v\xeed"; use File::Slurp::Unicode; write_file $name, $wide_data; is(read_file($name), $wide_data, "Basic in/out parity"); write_file $name, $bin_data; is(read_file($name), $wide_data, "Don't touch byte coded strings"); is(read_file($name, encoding => 'binary'), $bin_data, "Binary encoding read"); write_file $name, { encoding => 'binary' }, $bin_data; is(read_file($name, encoding => 'binary'), $bin_data, "Binary encoding write"); eval { write_file $name, { encoding => 'binary' }, $wide_data; }; like($@, qr/Can't encode wide characters as binary/, "Writing wide characters as binary should die"); write_file $name, { encoding => 'latin1' }, $wide_data; is(read_file($name, encoding => 'binary'), $latin_data, "Latin1 encoding write"); is(read_file($name, encoding => 'latin1'), $wide_data, "Latin1 encoding read"); File-Slurp-Unicode-0.7.1/lib/File/000755 000765 000765 00000000000 11360462570 016750 5ustar00daviddavid000000 000000 File-Slurp-Unicode-0.7.1/lib/File/Slurp/000755 000765 000765 00000000000 11360462570 020055 5ustar00daviddavid000000 000000 File-Slurp-Unicode-0.7.1/lib/File/Slurp/Unicode.pm000444 000765 000765 00000011353 11360462570 022002 0ustar00daviddavid000000 000000 # Copyright (c) 2010 David Caldwell, All Rights Reserved. -*- cperl -*- package File::Slurp::Unicode; use strict; use warnings; our $VERSION = '0.7.1'; use base 'Exporter' ; our %EXPORT_TAGS = ( 'all' => [ qw( read_file write_file append_file read_dir ) ] ) ; our @EXPORT = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT_OK = qw( slurp ) ; *slurp = \&read_file ; use File::Slurp (); use Encode; use Carp; sub read_file { my ($file_name, %args) = @_ ; my $binary = ($args{encoding}||'') eq 'binary'; my $decode = sub { map { $binary ? $_ : decode($args{encoding} // 'utf8', $_) } @_; }; if ($args{array_ref}) { my $r = File::Slurp::read_file($file_name, %args); return [ $decode->(@$r)]; } elsif ($args{scalar_ref}) { my $r = File::Slurp::read_file($file_name, %args); return \($decode->($$r))[0]; } elsif (wantarray) { my @r = File::Slurp::read_file($file_name, %args); return $decode->(@r); } elsif (defined wantarray) { # scalar context my $r = File::Slurp::read_file($file_name, %args); return ($decode->($r))[0]; } elsif ($args{buf_ref}) { File::Slurp::read_file($file_name, %args); ${$args{buf_ref}} = ($decode->(${$args{buf_ref}}))[0]; return; # void context } croak "What on earth did you do to get here?"; } sub write_file { my $file_name = shift ; my $args = ( ref $_[0] eq 'HASH' ) ? shift : {} ; my $binary = ($args->{encoding}||'') eq 'binary'; my $encode = sub { map { !utf8::is_utf8($_) ? $_ : $binary ? croak "Can't encode wide characters as binary" : encode($args->{encoding} // 'utf8', $_) } @_ }; my @data; if ($args->{buf_ref}) { @data = $encode->(${$args->{buf_ref}}); } elsif (ref $_[0] eq 'SCALAR') { @data = $encode->(${$_[0]}); } elsif (ref $_[0] eq 'ARRAY') { @data = $encode->(@{$_[0]}); } else { @data = $encode->(@_); } File::Slurp::write_file($file_name, $args, @data); } sub append_file { my $file_name = shift ; my $args = ( ref $_[0] eq 'HASH' ) ? shift : {} ; $args->{append} = 1; write_file($file_name, $args, @_); } *read_dir = \&File::Slurp::read_dir; 1; __END__ =head1 NAME File::Slurp::Unicode - Reading/Writing of Complete Files with Character Encoding Support =head1 SYNOPSIS use File::Slurp::Unicode; my $text = read_file('filename', encoding => 'utf8'); my @lines = read_file('filename'); # utf8 is assumed if no encoding. write_file('filename', { encoding => 'utf16' }, @lines); # same as File::Slurp::write_file (ie. no encoding): write_file('filename', { encoding => 'binary' }, @lines); use File::Slurp::Unicode qw(slurp); my $text = slurp('filename', encoding => 'latin1'); =head1 DESCRIPTION This module wraps L and adds character encoding support through the B<< C >> parameter. It exports the same functions which take all the same parameters as File::Slurp. Please see the L documentation for basic usage; only the differences are described from here on out. =head2 B Pass in an argument called B<< C >> to change the file encoding. If no argument is passed in, UTF-8 encoding is assumed. The special encoding B<'binary'> is interpreted to mean that there should be no decoding done to the data after reading it. This is pretty much the same as calling C directly. This option is here only to make code which needs to read both binary and text files look uniform. =head2 B Pass in an argument called B<< C >> to change the file encoding. If no argument is passed in and no wide characters are present in the output data, then no conversion will be done. If there are wide characters in the output data then UTF-8 encoding is assumed. The special encoding B<'binary'> is interpreted to mean that there should be no encoding done to the data before writing. If you pass a wide string (a string with Perl's internal 'utf8 bit' set) to C and set the encoding to 'binary' it will die with an appropriate message. This is pretty much the same as calling C directly. This option is here only to make code which needs write both binary and text files look uniform. =head1 SEE ALSO L =head1 BUGS None known. Contact author or file a bug report on CPAN if you find any. =head1 COPYRIGHT This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Copyright (C) 2010 David Caldwell =head1 AUTHOR David Caldwell Edavid@porkrind.orgE L =head1 PROJECT HOME L =cut