Skip to content

Commit

Permalink
Replace Class::Load with Module::Runtime:
Browse files Browse the repository at this point in the history
Module::Runtime is a faster module than Class::Load (even with
Class::Load::XS available) and is already available by our stack.

If and when App::Cmd removes its dependency on Class::Load, we
will not require this by our stack at all.
  • Loading branch information
xsawyerx authored and veryrusty committed May 4, 2016
1 parent 18d8951 commit 014b1d8
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 37 deletions.
2 changes: 0 additions & 2 deletions dist.ini
Expand Up @@ -88,7 +88,6 @@ JSON = 0
URI::Escape = 0
parent = 0
Template::Tiny = 0
Class::Load = 0
Return::MultiLevel = 0
Import::Into = 0
Safe::Isa = 0
Expand Down Expand Up @@ -118,7 +117,6 @@ Scope::Upper = 0
; Used by Dancer2::Session::YAML
YAML = 0.86
Fcntl = 0
Class::Load::XS = 0
MIME::Types = 0

; -- test requirements
Expand Down
6 changes: 3 additions & 3 deletions lib/Dancer2.pm
Expand Up @@ -4,7 +4,7 @@ package Dancer2;
use strict;
use warnings;
use List::Util 'first';
use Class::Load 'load_class';
use Module::Runtime 'use_module';
use Import::Into;
use Dancer2::Core;
use Dancer2::Core::App;
Expand Down Expand Up @@ -85,8 +85,7 @@ sub import {
$final_args{dsl} ||= $config_dsl;

# load the DSL, defaulting to Dancer2::Core::DSL
load_class( $final_args{dsl} );
my $dsl = $final_args{dsl}->new( app => $app );
my $dsl = use_module( $final_args{dsl} )->new( app => $app );
$dsl->export_symbols_to( $caller, \%final_args );
}

Expand All @@ -103,6 +102,7 @@ sub _set_import_method_to_caller {
};

{
## no critic
no strict 'refs';
no warnings 'redefine';
*{"${caller}::import"} = $import;
Expand Down
3 changes: 1 addition & 2 deletions lib/Dancer2/CLI/Command/gen.pm
Expand Up @@ -13,7 +13,6 @@ use File::Spec::Functions;
use File::ShareDir 'dist_dir';
use File::Basename qw/dirname basename/;
use Dancer2::Template::Simple;
use Class::Load 'try_load_class';

my $SKEL_APP_FILE = 'lib/AppFile.pm';

Expand Down Expand Up @@ -92,7 +91,7 @@ sub execute {
_create_manifest($files_to_copy, $app_path);
_add_to_manifest_skip($app_path);

if ( ! try_load_class('YAML') ) {
if ( ! eval { require YAML; 1; } ) {
print <<NOYAML;
*****
WARNING: YAML.pm is not installed. This is not a full dependency, but is highly
Expand Down
4 changes: 2 additions & 2 deletions lib/Dancer2/Core/App.pm
Expand Up @@ -9,7 +9,7 @@ use Return::MultiLevel ();
use Safe::Isa;
use Sub::Quote;
use File::Spec;
use Class::Load qw/ load_class /;
use Module::Runtime 'use_module';

use Plack::Middleware::FixMissingBodyInRedirect;
use Plack::Middleware::Head;
Expand Down Expand Up @@ -68,7 +68,7 @@ sub _with_plugin {
}

push @{ $self->plugins },
$plugin = load_class($plugin)->new( app => $self );
$plugin = use_module($plugin)->new( app => $self );

return $plugin;
}
Expand Down
1 change: 0 additions & 1 deletion lib/Dancer2/Core/DSL.pm
Expand Up @@ -4,7 +4,6 @@ package Dancer2::Core::DSL;

use Moo;
use Carp;
use Class::Load 'load_class';
use Dancer2::Core::Hook;
use Dancer2::FileUtils;
use Dancer2::Core::Response::Delayed;
Expand Down
6 changes: 3 additions & 3 deletions lib/Dancer2/Core/Factory.pm
Expand Up @@ -3,7 +3,7 @@ package Dancer2::Core::Factory;

use Moo;
use Dancer2::Core;
use Class::Load 'try_load_class';
use Module::Runtime 'use_module';
use Carp 'croak';

sub create {
Expand All @@ -13,8 +13,8 @@ sub create {
$name = Dancer2::Core::camelize($name);
my $component_class = "Dancer2::${type}::${name}";

my ( $ok, $error ) = try_load_class($component_class);
$ok or croak "Unable to load class for $type component $name: $error";
eval { use_module($component_class); 1; }
or croak "Unable to load class for $type component $name: $@";

return $component_class->new(%options);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Dancer2/Core/MIME.pm
Expand Up @@ -4,7 +4,6 @@ package Dancer2::Core::MIME;

use Moo;

use Class::Load 'try_load_class';
use Plack::MIME;
use Dancer2::Core::Types;

Expand All @@ -14,7 +13,7 @@ use Dancer2::Core::Types;
# as MIME::Types fails to load its mappings from the DATA handle. See
# t/04_static_file/003_mime_types_reinit.t and GH#136.
BEGIN {
if ( try_load_class('MIME::Types') ) {
if ( eval { require MIME::Types; 1; } ) {
my $mime_types = MIME::Types->new(only_complete => 1);
Plack::MIME->set_fallback(
sub {
Expand Down
5 changes: 2 additions & 3 deletions lib/Dancer2/Core/Request.pm
Expand Up @@ -10,7 +10,6 @@ use Encode;
use HTTP::Body;
use URI;
use URI::Escape;
use Class::Load 'try_load_class';
use Safe::Isa;
use Hash::MultiValue;

Expand All @@ -37,8 +36,8 @@ sub $_ { \$_[0]->env->{ 'HTTP_' . ( uc $_ ) } }
_EVAL

# check presence of XS module to speedup request
our $XS_URL_DECODE = try_load_class('URL::Encode::XS');
our $XS_PARSE_QUERY_STRING = try_load_class('CGI::Deurl::XS');
our $XS_URL_DECODE = eval { require URL::Encode::XS; 1; };
our $XS_PARSE_QUERY_STRING = eval { require CGI::Deurl::XS; 1; };

our $_id = 0;

Expand Down
5 changes: 2 additions & 3 deletions lib/Dancer2/Core/Role/SessionFactory.pm
Expand Up @@ -5,7 +5,6 @@ use Moo::Role;
with 'Dancer2::Core::Role::Engine';

use Carp 'croak';
use Class::Load 'try_load_class';
use Dancer2::Core::Session;
use Dancer2::Core::Types;
use Digest::SHA 'sha1';
Expand Down Expand Up @@ -104,8 +103,8 @@ sub create {

{
my $COUNTER = 0;
my $CPRNG_AVAIL = try_load_class('Math::Random::ISAAC::XS') &&
try_load_class('Crypt::URandom');
my $CPRNG_AVAIL = eval { require Math::Random::ISAAC::XS; 1; } &&
eval { require Crypt::URandom; 1; };

# don't initialize until generate_id is called so the ISAAC algorithm
# is seeded after any pre-forking
Expand Down
4 changes: 2 additions & 2 deletions lib/Dancer2/Serializer/YAML.pm
Expand Up @@ -4,7 +4,7 @@ package Dancer2::Serializer::YAML;
use Moo;
use Carp 'croak';
use Encode;
use Class::Load 'load_class';
use Module::Runtime 'use_module';

with 'Dancer2::Core::Role::Serializer';

Expand All @@ -16,7 +16,7 @@ sub from_yaml { __PACKAGE__->deserialize(@_) }
sub to_yaml { __PACKAGE__->serialize(@_) }

# class definition
sub BUILD { load_class('YAML') }
sub BUILD { use_module('YAML') }

sub serialize {
my ( $self, $entity ) = @_;
Expand Down
5 changes: 2 additions & 3 deletions t/classes/Dancer2-Core-Request/serializers.t
Expand Up @@ -3,7 +3,6 @@ use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Class::Load 'try_load_class';

{
package App::CBOR; ## no critic
Expand All @@ -21,10 +20,10 @@ use Class::Load 'try_load_class';
}

subtest 'Testing with CBOR' => sub {
try_load_class('CBOR::XS')
eval { require CBOR::XS; 1; }
or plan skip_all => 'CBOR::XS is needed for this test';

try_load_class('Dancer2::Serializer::CBOR')
eval { require Dancer2::Serializer::CBOR; 1; }
or plan skip_all => 'Dancer2::Serializer::CBOR is needed for this test';

App::CBOR->setup;
Expand Down
4 changes: 2 additions & 2 deletions t/deserialize.t
Expand Up @@ -63,7 +63,7 @@ my $app = App->to_app;
use utf8;
use JSON;
use Encode;
use Class::Load 'load_class';
use Module::Runtime 'use_module';

note "Verify Serializers decode into characters"; {
my $utf8 = '∮ E⋅da = Q, n → ∞, ∑ f(i) = ∏ g(i)';
Expand All @@ -73,7 +73,7 @@ note "Verify Serializers decode into characters"; {

for my $type ( qw/Dumper JSON YAML/ ) {
my $class = "Dancer2::Serializer::$type";
load_class($class);
use_module($class);

my $serializer = $class->new();
my $body = $serializer->serialize({utf8 => $utf8});
Expand Down
3 changes: 1 addition & 2 deletions t/hooks.t
Expand Up @@ -4,11 +4,10 @@ use Test::More;
use File::Spec;
use Plack::Test;
use HTTP::Request::Common;
use Class::Load 'try_load_class';
use Capture::Tiny 0.12 'capture_stderr';
use JSON;

try_load_class('Template')
eval { require Template; 1; }
or plan skip_all => 'Template::Toolkit not present';

my $tests_flags = {};
Expand Down
3 changes: 1 addition & 2 deletions t/memory_cycles.t
Expand Up @@ -2,10 +2,9 @@ use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Class::Load 'try_load_class';
use Plack::Test;

try_load_class('Test::Memory::Cycle')
eval { require Test::Memory::Cycle; 1; }
or plan skip_all => 'Test::Memory::Cycle not present';

{
Expand Down
5 changes: 2 additions & 3 deletions t/session_object.t
Expand Up @@ -7,12 +7,11 @@ use Test::Fatal;

use Dancer2::Core::Session;
use Dancer2::Session::Simple;
use Class::Load 'try_load_class';

my $ENGINE = Dancer2::Session::Simple->new;

my $CPRNG_AVAIL = try_load_class('Math::Random::ISAAC::XS')
&& try_load_class('Crypt::URandom');
my $CPRNG_AVAIL = eval { require Math::Random::ISAAC::XS; 1; }
&& eval { require Crypt::URandom; 1; };

note $CPRNG_AVAIL
? "Crypto strength tokens"
Expand Down
3 changes: 1 addition & 2 deletions t/time.t
Expand Up @@ -3,12 +3,11 @@
use strict;
use warnings;
use Test::More;
use Class::Load 'try_load_class';

my $mocked_epoch = 1355676244; # "Sun, 16-Dec-2012 16:44:04 GMT"

# The order is important!
try_load_class('Test::MockTime')
eval { require Test::MockTime; 1; }
or plan skip_all => 'Test::MockTime not present';

Test::MockTime::set_fixed_time($mocked_epoch);
Expand Down

0 comments on commit 014b1d8

Please sign in to comment.