Set the application class' %INC entry to something useful tsibley/File-Share-compat
Thomas Sibley [Fri, 25 Jul 2014 23:33:27 +0000 (16:33 -0700)]
...and leave it alone if already set.

lib/Web/Simple.pm
t/inc.t [new file with mode: 0644]

index 1116d51..ad0a621 100644 (file)
@@ -9,8 +9,9 @@ our $VERSION = '0.028';
 
 sub import {
   my ($class, $app_package) = @_;
-  $app_package ||= caller;
-  $class->_export_into($app_package);
+  my ($caller_package, $caller_file) = caller;
+  $app_package ||= $caller_package;
+  $class->_export_into($app_package, $caller_file);
   eval "package $app_package; use Web::Dispatch::Wrapper; use Moo; 1"
     or die "Failed to setup app package: $@";
   strictures->import;
@@ -18,7 +19,7 @@ sub import {
 }
 
 sub _export_into {
-  my ($class, $app_package) = @_;
+  my ($class, $app_package, $app_file) = @_;
   {
     no strict 'refs';
     *{"${app_package}::PSGI_ENV"} = sub () { -1 };
@@ -26,7 +27,7 @@ sub _export_into {
     unshift(@{"${app_package}::ISA"}, 'Web::Simple::Application');
   }
   (my $name = $app_package) =~ s/::/\//g;
-  $INC{"${name}.pm"} = 'Set by "use Web::Simple;" invocation';
+  $INC{"${name}.pm"} ||= $app_file || 'Set by "use Web::Simple;" invocation';
 }
 
 1;
@@ -121,15 +122,14 @@ It also exports the following subroutines for use in dispatchers:
 
   redispatch_to '/somewhere';
 
-Finally, import sets
-
-  $INC{"NameOfApplication.pm"} = 'Set by "use Web::Simple;" invocation';
-
-so that perl will not attempt to load the application again even if
+Finally, import ensures that C<$INC{"NameOfApplication.pm"}> is set so that
+perl will not attempt to load the application again even if
 
   require NameOfApplication;
 
-is encountered in other code.
+is encountered in other code.  If the C<%INC> entry isn't already set by the
+time import is called, it's set to the caller's file or, failing that, the
+string C<Set by "use Web::Simple;" invocation>.
 
 One important thing to remember when using
 
diff --git a/t/inc.t b/t/inc.t
new file mode 100644 (file)
index 0000000..414d248
--- /dev/null
+++ b/t/inc.t
@@ -0,0 +1,22 @@
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More 'no_plan';
+use Plack::Test;
+
+{
+  use Web::Simple 'IncTest';
+  package IncTest;
+  sub dispatch_request  {
+    sub (GET) {
+      [ 200,
+        [ "Content-type" => "text/plain" ],
+        [ $INC{'IncTest.pm'} ]
+      ]
+    },
+  }
+}
+
+my $app = IncTest->new;
+
+is $app->run_test_request(GET => 'http://localhost/')->decoded_content, __FILE__;