1 package Devel::REPL::Plugin::Packages;
4 use vars qw($PKG_SAVE);
6 has 'current_package' => (
9 default => 'Devel::REPL::Plugin::Packages::DefaultScratchpad',
13 around 'wrap_as_sub' => sub {
15 my ($self, @args) = @_;
16 my $line = $self->$orig(@args);
17 # prepend package def before sub { ... }
18 return q!package !.$self->current_package.qq!;\n${line}!;
21 around 'mangle_line' => sub {
23 my ($self, @args) = @_;
24 my $line = $self->$orig(@args);
25 # add a BEGIN block to set the package around at the end of the sub
26 # without mangling the return value (we save it off into a global)
27 $line .= '; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }';
31 after 'execute' => sub {
33 # if we survived execution successfully, save the new package out the global
34 $self->current_package($PKG_SAVE);
37 around 'eval' => sub {
39 my ($self, @args) = @_;
40 # localise the $PKG_SAVE global in case of nested evals
42 return $self->$orig(@args);
45 package Devel::REPL::Plugin::Packages::DefaultScratchpad;
47 # declare empty scratchpad package for cleanliness