Subject: | Adding FileHandle support |
The attached patch provides support for passing a filehande as a parameter when calling localize and localized.
diff -Naur File-LocalizeNewlines-0.01/lib/File/LocalizeNewlines.pm new/lib/File/LocalizeNewlines.pm
--- File-LocalizeNewlines-0.01/lib/File/LocalizeNewlines.pm 2005-01-28 00:39:40.000000000 +1100
+++ new/lib/File/LocalizeNewlines.pm 2005-02-03 10:24:03.455657948 +1100
@@ -32,6 +32,7 @@
use base 'Class::Default';
use File::Find::Rule ();
use File::Slurp ();
+use FileHandle ();
use vars qw{$VERSION};
BEGIN {
@@ -139,8 +140,8 @@
=head2 localized $file
-The C<localized> method takes an argument of a single file name and
-tests it to see it is localized correctly.
+The C<localized> method takes an argument of a single file name or
+file handle and tests it to see it is localized correctly.
Returns true if localized correctly, false if not, or C<undef> on error.
@@ -148,7 +149,7 @@
sub localized {
my $self = shift->_self;
- my $file = (defined $_[0] and -f $_[0]) ? shift : return undef;
+ my $file = (defined $_[0] and -f $_[0]) ? shift : (defined $_[0] and ref $_[0]) ? shift : return undef;
my $newline = $self->newline;
my $content = File::Slurp::read_file( $file );
@@ -191,9 +192,9 @@
=head2 localize $file | $dir
-The C<localize> method takes a file or directory as argument and localizes
-the newlines of the file, or all files within the directory (that match the
-filter if one was provided).
+The C<localize> method takes a file, file handle or directory as argument
+and localizes the newlines of the file, or all files within the directory
+(that match the filter if one was provided).
Returns the number of files that were localized, zero if no files needed to
be localized, or C<undef> on error.
@@ -202,10 +203,10 @@
sub localize {
my $self = shift->_self;
- my $path = (defined $_[0] and -e $_[0]) ? shift : return undef;
+ my $path = (defined $_[0] and -e $_[0]) ? shift : (defined $_[0] and ref $_[0]) ? shift : return undef;
# Switch on file or dir
- -f $path
+ -f $path or ref $_[0]
? $self->_localize_file( $path )
: $self->_localize_dir( $path );
}
@@ -235,7 +236,7 @@
sub _localize_file {
my $self = shift->_self;
- my $file = (defined $_[0] and -f $_[0]) ? shift : return undef;
+ my $file = (defined $_[0] and -f $_[0]) ? shift : (defined $_[0] and ref $_[0]) ? shift : return undef;
# Does the file need to be localised
my $newline = $self->newline;
diff -Naur File-LocalizeNewlines-0.01/t/02_main.t new/t/02_main.t
--- File-LocalizeNewlines-0.01/t/02_main.t 2005-01-28 00:39:40.000000000 +1100
+++ new/t/02_main.t 2005-02-03 10:28:17.206702532 +1100
@@ -15,10 +15,11 @@
}
}
-use Test::More tests => 42;
+use Test::More tests => 46;
use File::Slurp ();
use File::Find::Rule ();
use File::LocalizeNewlines ();
+use FileHandle ();
use constant FLN => 'File::LocalizeNewlines';
use constant FFR => 'File::Find::Rule';
@@ -32,6 +33,8 @@
File::Slurp::write_file( $not_file2, "foo\015\012bar\015baz" );
is( length(File::Slurp::read_file( $not_file )), 12, 'both.txt is the right length' );
is( length(File::Slurp::read_file( $not_file2 )), 12, 'both.pm is the right length' );
+my $local_handle = new FileHandle("< $local_file");
+my $not_handle = new FileHandle("< $not_file");
END {
unlink $local_file if -e $local_file;
@@ -106,9 +109,13 @@
my $Object = FLN->new;
isa_ok( $Object, FLN );
ok( $Object->localized( $local_file ), '->localized returns true for known-local file' );
+ ok( $Object->localized( $local_handle ), '->localized returns true for known-local file handle' );
ok( ! $Object->localized( $not_file ), '->localized returns true for known-local file' );
+ ok( ! $Object->localized( $not_file ), '->localized returns true for known-local file handle' );
ok( FLN->localized( $local_file ), 'static->localized returns false for known-not-local file' );
+ ok( FLN->localized( $local_handle ), 'static->localized returns false for known-not-local file handle' );
ok( ! FLN->localized( $not_file ), 'static->localized returns false for known-not-local file' );
+ ok( ! FLN->localized( $not_handle ), 'static->localized returns false for known-not-local file handle' );
}