Permalink
Browse files

GH 900 (backport from D2)

  • Loading branch information...
mokko committed Apr 3, 2013
1 parent 248921a commit 6ce7ca260edfce293e0d06e95ceb6092327f6fdc
Showing with 15 additions and 24 deletions.
  1. +1 −0 Makefile.PL
  2. +14 −24 lib/Dancer/ModuleLoader.pm
View
@@ -48,6 +48,7 @@ WriteMakefile1(
'MIME::Types' => '0',
'URI' => '1.59',
'Try::Tiny' => '0.09',
+ 'Module::Runtime' => '0',
# core
'File::Basename' => '0',
View
@@ -4,45 +4,35 @@ package Dancer::ModuleLoader;
use strict;
use warnings;
+use Module::Runtime qw/ use_module /;
sub load {
my ($class, $module, $version) = @_;
- # 0 is a valid version, so testing trueness of $version is not enough
- if (defined $version && length $version) {
- my ($res, $error) = $class->load_with_params($module);
- $res or return wantarray ? (0, $error) : 0;
- local $@;
- eval { $module->VERSION($version) };
- $error = $@;
- $error and return wantarray ? (0, $error) : 0;
- return 1;
- }
- # normal 'use', can be done via require + import
- my ($res, $error) = $class->load_with_params($module);
- return wantarray ? ($res, $error) : $res;
+
+ my ($res, $error) = $class->require($module, $version);
+ $res or return wantarray ? (0, $error) : 0;
}
sub require {
- my ($class, $module) = @_;
- local $@;
- my $module_filename = $module;
- $module_filename =~ s!::|'!/!g;
- $module_filename .= '.pm';
- eval { require $module_filename };
- my $error = $@;
- $error and return wantarray ? (0, $error) : 0;
- return 1;
+ my ($class, $module, $version) = @_;
+ print "version $version\n" if $version;
+ eval { defined $version ? use_module( $module, $version )
+ : use_module( $module ) }
+ or return wantarray ? (0, $@) : 0;
+ return 1; #success
}
sub load_with_params {
my ($class, $module, @args) = @_;
my ($res, $error) = $class->require($module);
$res or return wantarray ? (0, $error) : 0;
+
# From perlfunc : If no "import" method can be found then the call is
# skipped, even if there is an AUTOLOAD method.
if ($module->can('import')) {
+
# bump Exporter Level to import symbols in the caller
- local $Exporter::ExportLevel = ( $Exporter::ExportLevel || 0 ) + 1;
+ local $Exporter::ExportLevel = ($Exporter::ExportLevel || 0) + 1;
local $@;
eval { $module->import(@args) };
my $error = $@;
@@ -108,7 +98,7 @@ please simply "C<use ModuleYouNeed>" in your code and don't use this module.
=head2 load
-Runs a "C<use ModuleYouNeed>".
+Runs something like "C<use ModuleYouNeed>" at runtime.
use Dancer::ModuleLoader;
...

0 comments on commit 6ce7ca2

Please sign in to comment.