1 package Devel::REPL::Plugin::Packages;
2 use Devel::REPL::Plugin;
4 use namespace::clean -except => [ "meta" ];
6 use vars qw($PKG_SAVE);
8 has 'current_package' => (
11 default => 'Devel::REPL::Plugin::Packages::DefaultScratchpad',
15 around 'wrap_as_sub' => sub {
17 my ($self, @args) = @_;
18 my $line = $self->$orig(@args);
19 # prepend package def before sub { ... }
20 return q!package !.$self->current_package.qq!;\n${line}!;
23 around 'mangle_line' => sub {
25 my ($self, @args) = @_;
26 my $line = $self->$orig(@args);
27 # add a BEGIN block to set the package around at the end of the sub
28 # without mangling the return value (we save it off into a global)
30 ; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }';
34 after 'execute' => sub {
36 # if we survived execution successfully, save the new package out the global
37 $self->current_package($PKG_SAVE) if defined $PKG_SAVE;
40 around 'eval' => sub {
42 my ($self, @args) = @_;
43 # localise the $PKG_SAVE global in case of nested evals
45 return $self->$orig(@args);
48 package Devel::REPL::Plugin::Packages::DefaultScratchpad;
50 # declare empty scratchpad package for cleanliness
58 Devel::REPL::Plugin::Packages - Keep track of which package the user is in