Subject: | Support for objects in @INC |
Adam et al --
This is not a bug report, but rather a request for a small new feature, and a patch that
implements it.
The current version searches for the share dir relative to the directories in @INC, and simply
skips any references in @INC. This is perfectly reasonable behavior of course, and probably
the only obvious thing to do with @INC entries that aren't strings.
However, we're using some interesting infrastructure that leverages the @INC objects
support, and the objects we create overload stringification to return the underlying directory
they represent. The code's purpose is to provide an INC method that checks for file existence
based on some cached data, thus avoiding filesystem I/O.
The attached patch does two things. First of all, I refactored the @INC searching code into a
single subroutine, and second, it simply does a directory test of the quoted entry. This allows
the code to work with our objects (or anyone's object, if they just overload "" to return the
directory), and I believe it should still skip other references just fine.
Please let me know if this feature and patch acceptable to you.
Thanks in advance, and all that....
Subject: | File-ShareDir-1.02-INC-objects.patch |
diff -r -u File-ShareDir-1.02/lib/File/ShareDir.pm File-ShareDir-1.02-INC-objects/lib/File/ShareDir.pm
--- File-ShareDir-1.02/lib/File/ShareDir.pm 2010-03-17 21:31:04.000000000 -0400
+++ File-ShareDir-1.02-INC-objects/lib/File/ShareDir.pm 2010-08-13 18:28:08.000000000 -0400
@@ -176,18 +176,8 @@
'auto', 'share', 'dist', $dist,
);
- # Find the full dir withing @INC
- foreach my $inc ( @INC ) {
- next unless defined $inc and ! ref $inc;
- my $dir = File::Spec->catdir( $inc, $path );
- next unless -d $dir;
- unless ( -r $dir ) {
- croak("Found directory '$dir', but no read permissions");
- }
- return $dir;
- }
+ return _search_inc_path( $path );
- return undef;
}
sub _dist_dir_old {
@@ -198,18 +188,8 @@
'auto', split( /-/, $dist ),
);
- # Find the full dir within @INC
- foreach my $inc ( @INC ) {
- next unless defined $inc and ! ref $inc;
- my $dir = File::Spec->catdir( $inc, $path );
- next unless -d $dir;
- unless ( -r $dir ) {
- croak("Found directory '$dir', but no read permissions");
- }
- return $dir;
- }
+ return _search_inc_path( $path );
- return undef;
}
=pod
@@ -252,18 +232,7 @@
_module_subdir( $module ),
);
- # Find the full dir withing @INC
- foreach my $inc ( @INC ) {
- next unless defined $inc and ! ref $inc;
- my $dir = File::Spec->catdir( $inc, $path );
- next unless -d $dir;
- unless ( -r $dir ) {
- croak("Found directory '$dir', but no read permissions");
- }
- return $dir;
- }
-
- return undef;
+ return _search_inc_path( $path );
}
sub _module_dir_old {
@@ -344,19 +313,10 @@
'auto', split( /-/, $dist ), $file,
);
- # Find the full dir withing @INC
- foreach my $inc ( @INC ) {
- next unless defined $inc and ! ref $inc;
- my $full = File::Spec->catdir( $inc, $path );
- next unless -e $full;
- unless ( -r $full ) {
- croak("Directory '$full', no read permissions");
- }
- return $full;
- }
+ my $dir = _search_inc_path( $path ) ||
+ croak("Failed to find shared file '$file' for dist '$dist'");
- # Couldn't find it
- croak("Failed to find shared file '$file' for dist '$dist'");
+ return $dir;
}
=pod
@@ -468,6 +428,28 @@
#####################################################################
# Support Functions
+sub _search_inc_path {
+
+ my $path = shift;
+
+ # Find the full dir within @INC
+ foreach my $inc ( @INC ) {
+ # NOTE: This allows objects in @INC that provide the
+ # directory by overloading stringification, and still
+ # skips references correctly.
+ next unless -d "$inc";
+ my $dir = File::Spec->catdir( $inc, $path );
+ next unless -d $dir;
+ unless ( -r $dir ) {
+ croak("Found directory '$dir', but no read permissions");
+ }
+ return $dir;
+ }
+
+ return undef;
+
+}
+
sub _module_subdir {
my $module = shift;
$module =~ s/::/-/g;