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