increment $VERSION after 1.003027 release
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Packages.pm
CommitLineData
1716b200 1use strict;
2use warnings;
80f0324c 3package Devel::REPL::Plugin::Packages;
9d2a4940 4# ABSTRACT: Keep track of which package the user is in
9cfc1536 5
5ac98890 6our $VERSION = '1.003028';
54beb05d 7
6f4f9516 8use Devel::REPL::Plugin;
aa8b7647 9use namespace::autoclean;
80f0324c 10
28e93f31 11our $PKG_SAVE;
80f0324c 12
13has 'current_package' => (
14 isa => 'Str',
15 is => 'rw',
ae5e19ec 16 default => 'Devel::REPL::Plugin::Packages::DefaultScratchpad',
80f0324c 17 lazy => 1
18);
19
ae5e19ec 20around 'wrap_as_sub' => sub {
21 my $orig = shift;
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}!;
26};
27
28around 'mangle_line' => sub {
29 my $orig = shift;
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)
46407908 34 $line .= '
35; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }';
ae5e19ec 36 return $line;
37};
38
39after 'execute' => sub {
40 my ($self) = @_;
41 # if we survived execution successfully, save the new package out the global
7c7c35c1 42 $self->current_package($PKG_SAVE) if defined $PKG_SAVE;
ae5e19ec 43};
44
80f0324c 45around 'eval' => sub {
ae5e19ec 46 my $orig = shift;
47 my ($self, @args) = @_;
48 # localise the $PKG_SAVE global in case of nested evals
49 local $PKG_SAVE;
50 return $self->$orig(@args);
80f0324c 51};
52
796cd7ec 53package # hide from PAUSE
54 Devel::REPL::Plugin::Packages::DefaultScratchpad;
80f0324c 55
ae5e19ec 56# declare empty scratchpad package for cleanliness
57
581;