1 package Devel::REPL::Plugin::Packages;
2 use Devel::REPL::Plugin;
5 use MooX::Types::MooseLike::Base qw(Str);
7 use vars qw($PKG_SAVE);
9 has 'current_package' => (
12 default => sub { 'Devel::REPL::Plugin::Packages::DefaultScratchpad' },
16 around 'wrap_as_sub' => sub {
18 my ($self, @args) = @_;
19 my $line = $self->$orig(@args);
20 # prepend package def before sub { ... }
21 return q!package !.$self->current_package.qq!;\n${line}!;
24 around 'mangle_line' => sub {
26 my ($self, @args) = @_;
27 my $line = $self->$orig(@args);
28 # add a BEGIN block to set the package around at the end of the sub
29 # without mangling the return value (we save it off into a global)
31 ; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }';
35 after 'execute' => sub {
37 # if we survived execution successfully, save the new package out the global
38 $self->current_package($PKG_SAVE) if defined $PKG_SAVE;
41 around 'eval' => sub {
43 my ($self, @args) = @_;
44 # localise the $PKG_SAVE global in case of nested evals
46 return $self->$orig(@args);
49 package Devel::REPL::Plugin::Packages::DefaultScratchpad;
51 # declare empty scratchpad package for cleanliness
59 Devel::REPL::Plugin::Packages - Keep track of which package the user is in