test path of compiling file rather than direct caller
[p5sagit/strictures.git] / lib / strictures.pm
index f890487..d986072 100644 (file)
@@ -5,7 +5,7 @@ use warnings FATAL => 'all';
 
 use constant _PERL_LT_5_8_4 => ($] < 5.008004) ? 1 : 0;
 
-our $VERSION = '1.004002'; # 1.4.2
+our $VERSION = '1.004004'; # 1.4.4
 
 sub VERSION {
   for ($_[1]) {
@@ -21,7 +21,7 @@ sub VERSION {
   shift->SUPER::VERSION(@_);
 }
 
-my $extras_load_warned;
+our $extra_load_states;
 
 our $Smells_Like_VCS = (-e '.git' || -e '.svn'
   || (-e '../../dist.ini' && (-e '../../.git' || -e '../../.svn')));
@@ -30,6 +30,16 @@ sub import {
   strict->import;
   warnings->import(FATAL => 'all');
 
+  my $caller_file;
+  my $depth = 0;
+  while (my @caller = caller(++$depth)) {
+    if ($caller[3] =~ /::BEGIN$/) {
+      # older perls report the BEGIN in the wrong file
+      $caller_file = $depth > 1 ? (caller($depth-1))[1] : $caller[1];
+      $caller_file = $caller[1];
+    }
+  }
+
   my $extra_tests = do {
     if (exists $ENV{PERL_STRICTURES_EXTRA}) {
       if (_PERL_LT_5_8_4 and $ENV{PERL_STRICTURES_EXTRA}) {
@@ -38,30 +48,30 @@ sub import {
       }
       $ENV{PERL_STRICTURES_EXTRA};
     } elsif (! _PERL_LT_5_8_4) {
-      !!((caller)[1] =~ /^(?:t|xt|lib|blib)/
+      !!($caller_file =~ /^(?:t|xt|lib|blib)/
          and $Smells_Like_VCS)
     }
   };
   if ($extra_tests) {
-    my @failed;
-    if (eval { require indirect; 1 }) {
-      indirect->unimport(':fatal');
-    } else {
-      push @failed, 'indirect';
-    }
-    if (eval { require multidimensional; 1 }) {
-      multidimensional->unimport;
-    } else {
-      push @failed, 'multidimensional';
-    }
-    if (eval { require bareword::filehandles; 1 }) {
-      bareword::filehandles->unimport;
-    } else {
-      push @failed, 'bareword::filehandles';
-    }
-    if (@failed and not $extras_load_warned++) {
-      my $failed = join ' ', @failed;
-      warn <<EOE;
+    $extra_load_states ||= do {
+
+      my (%rv, @failed);
+      foreach my $mod (qw(indirect multidimensional bareword::filehandles)) {
+        eval "require $mod; \$rv{'$mod'} = 1;" or do {
+          push @failed, $mod;
+
+          # courtesy of the 5.8 require bug
+          # (we do a copy because 5.16.2 at least uses the same read-only
+          # scalars for the qw() list and it doesn't seem worth a $^V check)
+
+          (my $file = $mod) =~ s|::|/|g;
+          delete $INC{"${file}.pm"};
+        };
+      }
+
+      if (@failed) {
+        my $failed = join ' ', @failed;
+        print STDERR <<EOE;
 strictures.pm extra testing active but couldn't load all modules. Missing were:
 
   $failed
@@ -73,7 +83,14 @@ of a strictures-using module you need to run:
 
 but these modules are not required by your users.
 EOE
-    }
+      }
+
+      \%rv;
+    };
+
+    indirect->unimport(':fatal') if $extra_load_states->{indirect};
+    multidimensional->unimport if $extra_load_states->{multidimensional};
+    bareword::filehandles->unimport if $extra_load_states->{'bareword::filehandles'};
   }
 }