File Coverage

blib/lib/Devel/REPL/Plugin/Packages.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1 2     2   22512 use strict;
  2         4  
  2         66  
2 2     2   10 use warnings;
  2         4  
  2         139  
3             # ABSTRACT: Keep track of which package the user is in
4              
5             our $VERSION = '1.003029';
6              
7             use Devel::REPL::Plugin;
8 2     2   11 use namespace::autoclean;
  2         4  
  2         15  
9 2     2   8603  
  2         5  
  2         19  
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 # hide from PAUSE
53             Devel::REPL::Plugin::Packages::DefaultScratchpad;
54              
55             # declare empty scratchpad package for cleanliness
56              
57             1;
58              
59              
60             =pod
61              
62             =encoding UTF-8
63              
64             =head1 NAME
65              
66             Devel::REPL::Plugin::Packages - Keep track of which package the user is in
67              
68             =head1 VERSION
69              
70             version 1.003029
71              
72             =head1 SUPPORT
73              
74             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
75             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
76              
77             There is also an irc channel available for users of this distribution, at
78             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
79              
80             =head1 AUTHOR
81              
82             Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
83              
84             =head1 COPYRIGHT AND LICENCE
85              
86             This software is copyright (c) 2007 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
87              
88             This is free software; you can redistribute it and/or modify it under
89             the same terms as the Perl 5 programming language system itself.
90              
91             =cut