X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/kivitendo-erp.git/blobdiff_plain/deb4d2dbb676d7d6f69dfe7815d6e0cb09bd4a44..53593baa211863fbf66540cf1bcc36c8fb37257f:/Devel/REPL/Plugin/AutoloadModules.pm diff --git a/Devel/REPL/Plugin/AutoloadModules.pm b/Devel/REPL/Plugin/AutoloadModules.pm new file mode 100644 index 000000000..e36ee9654 --- /dev/null +++ b/Devel/REPL/Plugin/AutoloadModules.pm @@ -0,0 +1,29 @@ +package Devel::REPL::Plugin::AutoloadModules; + +use Moose::Role; +use namespace::clean -except => [ 'meta' ]; +use Data::Dumper; + +has 'autoloaded' => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); + +my $re = qr/Runtime error: Can.t locate object method "\w+" via package "\w+" \(perhaps you forgot to load "(\w+)"\?\)/; +around 'execute' => sub { + my $orig = shift; + my $self = shift; + + my @re = $self->$orig(@_); # original call + + return @re unless defined $re[0] && $re[0] =~ /$re/; # if there is no "perhaps you forgot" error, just return + my $module = $1; # save the missing package name + + return @re if $self->autoloaded->{$module}; # if we tried to load it before, give up and return the error + + $self->autoloaded->{$module} = 1; # make sure we don't try this again + $self->eval("use SL::$module"); # try to load the missing module + + @re = $self->$orig(@_); # try again + + return @re; +}; + +1;