3 package Devel::REPL::Plugin::Packages;
4 use Devel::REPL::Plugin;
6 use namespace::autoclean;
10 has 'current_package' => (
13 default => 'Devel::REPL::Plugin::Packages::DefaultScratchpad',
17 around 'wrap_as_sub' => sub {
19 my ($self, @args) = @_;
20 my $line = $self->$orig(@args);
21 # prepend package def before sub { ... }
22 return q!package !.$self->current_package.qq!;\n${line}!;
25 around 'mangle_line' => sub {
27 my ($self, @args) = @_;
28 my $line = $self->$orig(@args);
29 # add a BEGIN block to set the package around at the end of the sub
30 # without mangling the return value (we save it off into a global)
32 ; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }';
36 after 'execute' => sub {
38 # if we survived execution successfully, save the new package out the global
39 $self->current_package($PKG_SAVE) if defined $PKG_SAVE;
42 around 'eval' => sub {
44 my ($self, @args) = @_;
45 # localise the $PKG_SAVE global in case of nested evals
47 return $self->$orig(@args);
50 package Devel::REPL::Plugin::Packages::DefaultScratchpad;
52 # declare empty scratchpad package for cleanliness
60 Devel::REPL::Plugin::Packages - Keep track of which package the user is in