Skip to content

Commit

Permalink
improved Mojo::Loader to allow Mojolicious recovering from tricky syn…
Browse files Browse the repository at this point in the history
…tax errors in Controllers
  • Loading branch information
kraih committed Nov 5, 2010
1 parent 10f264a commit 33672ca
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,5 +1,9 @@
This file documents the revision history for Perl extension Mojolicious.

0.999937 2010-11-04 00:00:00
- Improved Mojo::Loader to allow Mojolicious recovering from tricky
syntax errors in Controllers.

0.999936 2010-11-03 00:00:00
- Improved Mojo::Template performance slightly. (kimoto)
- Fixed a serious WebSocket bug.
Expand Down
37 changes: 26 additions & 11 deletions lib/Mojo/Loader.pm
Expand Up @@ -29,9 +29,15 @@ sub load {
return 1 unless $module;

# Already loaded
return if $module->can('new');
return if !$ENV{MOJO_RELOAD} && $module->can('new');

# Try
# Unload (forced reload)
my $key = $module;
$key =~ s/\:\:/\//g;
$key .= '.pm';
_unload($key);

# Load
eval "require $module";

# Catch
Expand Down Expand Up @@ -62,17 +68,10 @@ sub reload {
if ($mtime > $STATS->{$file}) {

# Debug
warn "\n$key -> $file modified, reloading!\n" if DEBUG;
warn "$key -> $file modified, reloading!\n" if DEBUG;

# Unload
delete $INC{$key};
my @subs = grep { index($DB::sub{$_}, "$file:") == 0 }
keys %DB::sub;
for my $sub (@subs) {
eval { undef &$sub };
carp "Can't unload sub '$sub' in '$file': $@" if $@;
delete $DB::sub{$sub};
}
_unload($key);

# Try
eval { require $key };
Expand Down Expand Up @@ -125,6 +124,22 @@ sub search {
return $modules;
}

sub _unload {
my $key = shift;

# Unload
my $file = $INC{$key};
delete $INC{$key};
return unless $file;
my @subs = grep { index($DB::sub{$_}, "$file:") == 0 }
keys %DB::sub;
for my $sub (@subs) {
eval { undef &$sub };
carp "Can't unload sub '$sub' in '$file': $@" if $@;
delete $DB::sub{$sub};
}
}

1;
__END__
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -35,7 +35,7 @@ __PACKAGE__->attr(static => sub { MojoX::Dispatcher::Static->new });
__PACKAGE__->attr(types => sub { MojoX::Types->new });

our $CODENAME = 'Hot Beverage';
our $VERSION = '0.999936';
our $VERSION = '0.999937';

our $AUTOLOAD;

Expand Down
4 changes: 2 additions & 2 deletions t/mojo/loader.t
Expand Up @@ -60,8 +60,8 @@ ok LoaderTest::A->can('new'), 'loaded successfully';
ok LoaderTest::B->can('new'), 'loaded successfully';
ok LoaderTest::C->can('new'), 'loaded successfully';

# Load unrelated class
ok $loader->load('LoaderTest'), 'loaded successfully';
# Class does not exist
ok $loader->load('LoaderTest'), 'nothing to load';

# Reload
my $file = IO::File->new;
Expand Down

0 comments on commit 33672ca

Please sign in to comment.