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