3 package Devel::REPL::Plugin::Packages;
4 # ABSTRACT: Keep track of which package the user is in
6 our $VERSION = '1.003030';
8 use Devel::REPL::Plugin;
9 use namespace::autoclean;
13 has 'current_package' => (
16 default => 'Devel::REPL::Plugin::Packages::DefaultScratchpad',
20 around 'wrap_as_sub' => sub {
22 my ($self, @args) = @_;
23 my $line = $self->$orig(@args);
24 # prepend package def before sub { ... }
25 return q!package !.$self->current_package.qq!;\n${line}!;
28 around 'mangle_line' => sub {
30 my ($self, @args) = @_;
31 my $line = $self->$orig(@args);
32 # add a BEGIN block to set the package around at the end of the sub
33 # without mangling the return value (we save it off into a global)
35 ; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }';
39 after 'execute' => sub {
41 # if we survived execution successfully, save the new package out the global
42 $self->current_package($PKG_SAVE) if defined $PKG_SAVE;
45 around 'eval' => sub {
47 my ($self, @args) = @_;
48 # localise the $PKG_SAVE global in case of nested evals
50 return $self->$orig(@args);
53 package # hide from PAUSE
54 Devel::REPL::Plugin::Packages::DefaultScratchpad;
56 # declare empty scratchpad package for cleanliness