Subject: | $entity->as_native implementation |
It would be very nice if the File::Path::Dir and File::Path::File
instances created via ->new_foreign() could be converted to instances
working with the native semantics (currently they only can be converted
to different foreign instances).
Subject: | Path-Class-as_native.diff |
Index: t/02-foreign.t
===================================================================
--- t/02-foreign.t (revision 193)
+++ t/02-foreign.t (working copy)
@@ -1,6 +1,6 @@
use Test;
use strict;
-BEGIN { plan tests => 28 }; #30, todo => [29,30] };
+BEGIN { plan tests => 31 }; #33, todo => [32,33] };
use Path::Class qw(file dir foreign_file foreign_dir);
ok(1);
@@ -11,6 +11,7 @@
ok $file->as_foreign('Win32'), 'dir\foo.txt';
ok $file->as_foreign('Mac'), ':dir:foo.txt';
ok $file->as_foreign('OS2'), 'dir/foo.txt';
+ok $file->as_native, Path::Class::File->new('dir', 'foo.txt');
if ($^O eq 'VMS') {
ok $file->as_foreign('VMS'), '[.dir]foo.txt';
@@ -22,6 +23,7 @@
ok $file, ':dir:foo.txt';
ok $file->as_foreign('Unix'), 'dir/foo.txt';
ok $file->dir, ':dir:';
+ok $file->as_native, Path::Class::File->new('dir', 'foo.txt');
my $dir = Path::Class::Dir->new_foreign('Unix', 'dir/subdir');
@@ -29,6 +31,7 @@
ok $dir->as_foreign('Win32'), 'dir\subdir';
ok $dir->as_foreign('Mac'), ':dir:subdir:';
ok $dir->as_foreign('OS2'), 'dir/subdir';
+ok $dir->as_native, Path::Class::Dir->new('dir', 'subdir');
if ($^O eq 'VMS') {
ok $dir->as_foreign('VMS'), '[.dir.subdir]';
Index: lib/Path/Class/Entity.pm
===================================================================
--- lib/Path/Class/Entity.pm (revision 193)
+++ lib/Path/Class/Entity.pm (working copy)
@@ -27,6 +27,8 @@
sub _spec_class {
my ($class, $type) = @_;
+ return undef if $type eq 'NATIVE';
+
die "Invalid system type '$type'" unless ($type) = $type =~ /^(\w+)$/; # Untaint
my $spec = "File::Spec::$type";
eval "require $spec; 1" or die $@;
@@ -80,6 +82,12 @@
return $self->new($self->_spec->abs2rel($self->stringify, @_));
}
+sub as_native {
+ my $self = shift;
+
+ return $self->as_foreign('NATIVE');
+}
+
sub stat { File::stat::stat("$_[0]") }
sub lstat { File::stat::lstat("$_[0]") }