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;
}
sub _export_into {
- my ($class, $app_package) = @_;
+ my ($class, $app_package, $app_file) = @_;
{
no strict 'refs';
*{"${app_package}::PSGI_ENV"} = sub () { -1 };
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;
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
--- /dev/null
+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__;