e36ee9654d54baaa9969b502f2e210368988c4a1
[kivitendo-erp.git] / modules / override / Devel / REPL / Plugin / AutoloadModules.pm
1 package Devel::REPL::Plugin::AutoloadModules;
2
3 use Moose::Role;
4 use namespace::clean -except => [ 'meta' ];
5 use Data::Dumper;
6
7 has 'autoloaded' => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
8
9 my $re = qr/Runtime error: Can.t locate object method "\w+" via package "\w+" \(perhaps you forgot to load "(\w+)"\?\)/;
10 around 'execute' => sub {
11   my $orig = shift;
12   my $self = shift;
13
14   my @re = $self->$orig(@_);                           # original call
15
16   return @re unless defined $re[0] && $re[0] =~ /$re/; # if there is no "perhaps you forgot" error, just return
17   my $module = $1;                                     # save the missing package name
18
19   return @re if $self->autoloaded->{$module};          # if we tried to load it before, give up and return the error
20
21   $self->autoloaded->{$module} = 1;                    # make sure we don't try this again
22   $self->eval("use SL::$module");                      # try to load the missing module
23
24   @re = $self->$orig(@_);                              # try again
25
26   return @re;
27 };
28
29 1;