keep $VERSION right in the repo
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Packages.pm
1 use strict;
2 use warnings;
3 package Devel::REPL::Plugin::Packages;
4
5 our $VERSION = '1.003027';
6
7 use Devel::REPL::Plugin;
8 use namespace::autoclean;
9
10 our $PKG_SAVE;
11
12 has 'current_package' => (
13   isa      => 'Str',
14   is       => 'rw',
15   default  => 'Devel::REPL::Plugin::Packages::DefaultScratchpad',
16   lazy     => 1
17 );
18
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)
33   $line .= '
34 ; BEGIN { $Devel::REPL::Plugin::Packages::PKG_SAVE = __PACKAGE__; }';
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
41   $self->current_package($PKG_SAVE) if defined $PKG_SAVE;
42 };
43
44 around 'eval' => sub {
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);
50 };
51
52 package Devel::REPL::Plugin::Packages::DefaultScratchpad;
53
54 # declare empty scratchpad package for cleanliness
55
56 1;
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