Subject: | Allow recursive autouse from multiple @INC paths |
For recursive loading, the documentation currently states "Please note
that the loadings will only occur down a single branch of the include
path, whichever the top class is located in." Attached is a patch to
change this behavior to load from any @INC directory, no matter where
the top class is located.
Subject: | recursive-multiple-INC.txt |
diff --git a/lib/Class/Autouse.pm b/lib/Class/Autouse.pm
index ce51c91..d052c7f 100755
--- a/lib/Class/Autouse.pm
+++ b/lib/Class/Autouse.pm
@@ -668,24 +668,24 @@ sub _load ($) {
sub _children ($) {
_debug(\@_) if DEBUG;
- # Find where it is in @INC
my $base_file = _class_file(shift);
- my $inc_path = List::Util::first {
- -f File::Spec->catfile($_, $base_file)
- } @INC or return;
+ my $child_path = substr( $base_file, 0, length($base_file) - 3 );
- # Does the file have a subdirectory
- # i.e. Are there child classes
- my $child_path = substr( $base_file, 0, length($base_file) - 3 );
- my $child_path_full = File::Spec->catdir( $inc_path, $child_path );
- return 0 unless -d $child_path_full and -r _;
+ my @queue = ();
+
+ # Find where the file has subdirectories in @INC
+ # i.e. where there are child classes
+ for my $inc_path (@INC) {
+ my $child_path_full = File::Spec->catdir( $inc_path, $child_path );
+ next unless -d $child_path_full and -r _;
+ push @queue, [$child_path, $child_path_full];
+ }
# Main scan loop
local *FILELIST;
my ($dir, @files, @modules) = ();
- my @queue = ( $child_path );
while ( $dir = pop @queue ) {
- my $full_dir = File::Spec->catdir($inc_path, $dir);
+ my ($sub_dir, $full_dir) = @$dir;
# Read in the raw file list
# Skip directories we can't open
@@ -694,22 +694,22 @@ sub _children ($) {
closedir FILELIST;
# Iterate over them
- @files = map { File::Spec->catfile($dir, $_) } # Full relative path
- grep { ! /^\./ } @files; # Ignore hidden files
+ @files = grep { ! /^\./ } @files; # Ignore hidden files
foreach my $file ( @files ) {
- my $full_file = File::Spec->catfile($inc_path, $file);
+ my $full_file = File::Spec->catfile($full_dir, $file);
+ my $file_path = File::Spec->catfile($sub_dir, $file);
- # Add to the queue if its a directory we can descend
+ # Add to the queue if it's a directory we can descend
if ( -d $full_file and -r _ ) {
- push @queue, $file;
+ push @queue, [ $file_path, $full_file ];
next;
}
# We only want .pm files we can read
- next unless substr( $file, length($file) - 3 ) eq '.pm';
+ next unless substr( $file_path, length($file_path) - 3 ) eq '.pm';
next unless -f _;
- push @modules, $file;
+ push @modules, $file_path;
}
}
diff --git a/t/05_recursive.t b/t/05_recursive.t
index 2c363c2..8f68a88 100755
--- a/t/05_recursive.t
+++ b/t/05_recursive.t
@@ -11,11 +11,14 @@ BEGIN {
lib->import(
File::Spec->catdir(
File::Spec->curdir, 't', 'modules',
+ ),
+ File::Spec->catdir(
+ File::Spec->curdir, 't', 'modules2',
)
);
}
-use Test::More tests => 5;
+use Test::More tests => 6;
use Class::Autouse ();
@@ -28,3 +31,4 @@ ok( T->method, 'T is loaded' );
ok( T::A->method, 'T::A is loaded' );
ok( T::B->method, 'T::B is loaded' );
ok( T::B::G->method, 'T::B::G is loaded' );
+ok( T::C->method, 'T::C is loaded' );
diff --git a/t/modules2/T/C.pm b/t/modules2/T/C.pm
new file mode 100644
index 0000000..15577c6
--- /dev/null
+++ b/t/modules2/T/C.pm
@@ -0,0 +1,5 @@
+package T::C;
+
+sub method { 1 }
+
+1;