I no longer use this plugin in my bundle
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Packages.pm
CommitLineData
1716b200 1use strict;
2use warnings;
80f0324c 3package Devel::REPL::Plugin::Packages;
9cfc1536 4
54beb05d 5our $VERSION = '1.003027';
6
6f4f9516 7use Devel::REPL::Plugin;
aa8b7647 8use namespace::autoclean;
80f0324c 9
28e93f31 10our $PKG_SAVE;
80f0324c 11
12has 'current_package' => (
13 isa => 'Str',
14 is => 'rw',
ae5e19ec 15 default => 'Devel::REPL::Plugin::Packages::DefaultScratchpad',
80f0324c 16 lazy => 1
17);
18
ae5e19ec 19around '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
27around '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
38after '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 44around '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 52package Devel::REPL::Plugin::Packages::DefaultScratchpad;
80f0324c 53
ae5e19ec 54# declare empty scratchpad package for cleanliness
55
561;
cfd1094b 57
58__END__
59
60=head1 NAME
61
62Devel::REPL::Plugin::Packages - Keep track of which package the user is in
63
64=cut
65