Commit | Line | Data |
1716b200 |
1 | use strict; |
2 | use warnings; |
80f0324c |
3 | package Devel::REPL::Plugin::Packages; |
9cfc1536 |
4 | |
54beb05d |
5 | our $VERSION = '1.003027'; |
6 | |
6f4f9516 |
7 | use Devel::REPL::Plugin; |
aa8b7647 |
8 | use namespace::autoclean; |
80f0324c |
9 | |
28e93f31 |
10 | our $PKG_SAVE; |
80f0324c |
11 | |
12 | has 'current_package' => ( |
13 | isa => 'Str', |
14 | is => 'rw', |
ae5e19ec |
15 | default => 'Devel::REPL::Plugin::Packages::DefaultScratchpad', |
80f0324c |
16 | lazy => 1 |
17 | ); |
18 | |
ae5e19ec |
19 | around 'wrap_as_sub' => sub { |
20 | my $orig = shift; |
21 | my ($self, @args) = @_; |
22 | my $line = $self->$orig(@args); |
23 | # prepend package def before sub { ... } |
24 | return q!package !.$self->current_package.qq!;\n${line}!; |
25 | }; |
26 | |
27 | around 'mangle_line' => sub { |
28 | my $orig = shift; |
29 | my ($self, @args) = @_; |
30 | my $line = $self->$orig(@args); |
31 | # add a BEGIN block to set the package around at the end of the sub |
32 | # without mangling the return value (we save it off into a global) |
46407908 |
33 | $line .= ' |
34 | ; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }'; |
ae5e19ec |
35 | return $line; |
36 | }; |
37 | |
38 | after 'execute' => sub { |
39 | my ($self) = @_; |
40 | # if we survived execution successfully, save the new package out the global |
7c7c35c1 |
41 | $self->current_package($PKG_SAVE) if defined $PKG_SAVE; |
ae5e19ec |
42 | }; |
43 | |
80f0324c |
44 | around 'eval' => sub { |
ae5e19ec |
45 | my $orig = shift; |
46 | my ($self, @args) = @_; |
47 | # localise the $PKG_SAVE global in case of nested evals |
48 | local $PKG_SAVE; |
49 | return $self->$orig(@args); |
80f0324c |
50 | }; |
51 | |
ae5e19ec |
52 | package Devel::REPL::Plugin::Packages::DefaultScratchpad; |
80f0324c |
53 | |
ae5e19ec |
54 | # declare empty scratchpad package for cleanliness |
55 | |
56 | 1; |
cfd1094b |
57 | |
58 | __END__ |
59 | |
60 | =head1 NAME |
61 | |
62 | Devel::REPL::Plugin::Packages - Keep track of which package the user is in |
63 | |
64 | =cut |
65 | |