File Coverage

blib/lib/Pry.pm
Criterion Covered Total %
statement 13 38 34.2
branch 1 8 12.5
condition n/a
subroutine 5 9 55.5
pod 4 4 100.0
total 23 59 38.9


line stmt bran cond sub pod time code
1 1     1   30618 use 5.008001;
  1         4  
  1         40  
2 1     1   5 use strict;
  1         2  
  1         37  
3 1     1   6 use warnings;
  1         5  
  1         83  
4              
5             package Pry;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.002000';
9              
10 1     1   863 use Exporter::Shiny our @EXPORT = qw(pry);
  1         4537  
  1         9  
11              
12             # cargo-culted Win32 stuff... untested!
13             #
14             BEGIN {
15 1 50   1   723 if ($^O eq 'MSWin32') {
16 0           require Term::ANSIColor;
17 0           require Win32::Console::ANSI;
18 0           Win32::Console::ANSI->import;
19             }
20             };
21              
22             # a refinement for the Reply class
23             #
24             my $_say = sub {
25             require Term::ANSIColor;
26             shift;
27             my ($text, $colour) = (@_, "cyan");
28             print Term::ANSIColor::colored($text, "bold $colour"), "\n";
29             };
30              
31             our ($Lexicals, $Trace, $Already);
32              
33             # shim to pass lexicals to Reply
34             #
35             {
36             package #hide
37             Pry::_Lexicals;
38             our @ISA = qw( Reply::Plugin );
39 0     0 1   sub lexical_environment { $Lexicals }
40             $INC{'Pry/_Lexicals.pm'} = __FILE__;
41             }
42              
43             # the guts
44             #
45             sub pry ()
46             {
47 0     0 1   my ($caller, $file, $line) = caller;
48            
49 0 0         if ( $Already )
50             {
51 0           Reply->$_say(
52             "Pry is not re-entrant; not prying again at $file line $line",
53             "magenta",
54             );
55 0           return;
56             }
57 0           local $Already = 1;
58            
59 0           require Devel::StackTrace;
60 0           require Reply;
61 0           require PadWalker;
62            
63 0           $Lexicals = PadWalker::peek_my(1);
64 0           $Trace = Devel::StackTrace->new(
65             ignore_package => __PACKAGE__,
66             message => "Prying",
67             );
68            
69 0           my $repl = Reply->new(
70             config => ".replyrc",
71             plugins => [ "/Pry/_Lexicals" ],
72             );
73 0           $repl->step("package $caller");
74            
75 0           $repl->$_say("Prying at $file line $line", "magenta");
76 0           $repl->$_say("Current package: '$caller'");
77 0           $repl->$_say("Lexicals in scope: @{[ sort keys %$Lexicals ]}");
  0            
78 0           $repl->$_say("Ctrl+D to finish prying.", "magenta");
79 0           $repl->run;
80 0           $repl->$_say("Finished prying!", "magenta");
81             }
82              
83             # utils
84             #
85 0 0   0 1   sub Lexicals () { $Lexicals if $] }
86 0 0   0 1   sub Trace () { $Trace if $] }
87              
88             1;
89              
90             __END__
91              
92             =pod
93              
94             =begin trustme
95              
96             =item pry
97              
98             =end trustme
99              
100             =encoding utf-8
101              
102             =head1 NAME
103              
104             Pry - intrude on your code
105              
106             =head1 SYNOPSIS
107              
108             use Pry;
109            
110             ...;
111             pry;
112             ...;
113              
114             =head1 DESCRIPTION
115              
116             Kind of a bit like a debugger, kind of a bit like a REPL.
117              
118             This module gives you a function called C<pry> that you can drop into
119             your code anywhere. When Perl executes that line of code, it will stop
120             and drop you into a REPL. You can use the REPL to inspect any lexical
121             variables (and even alter them), call functions and methods, and so on.
122              
123             All the clever stuff is in the REPL. Rather than writing yet another
124             Perl REPL, Pry uses L<Reply>, which is an awesome yet fairly small REPL
125             with support for plugins that can do some really useful stuff, such as
126             auto-complete of function and variable names.
127              
128             Once you've finished using the REPL, just hit Ctrl+D and your code will
129             resume execution.
130              
131             =head1 UTILITIES
132              
133             The following functions are provided for your convenience. They cannot
134             be exported, so you should access them, from the REPL, using their
135             fully-qualified name.
136              
137             =over
138              
139             =item C<< Pry::Lexicals >>
140              
141             Returns a hashref of your lexical variables.
142              
143             =item C<< Pry::Trace >>
144              
145             Returns the stack trace as a L<Devel::StackTrace> object.
146              
147             =back
148              
149             =head1 CONFIGURATION
150              
151             Pry's REPL can be configured in the same way as L<Reply>.
152              
153             =head1 CAVEATS
154              
155             I imagine this probably breaks pretty badly in a multi-threaded or
156             multi-process scenario.
157              
158             =head1 BUGS
159              
160             Please report any bugs to
161             L<http://rt.cpan.org/Dist/Display.html?Queue=Pry>.
162              
163             =head1 SEE ALSO
164              
165             L<http://en.wikipedia.org/wiki/Read–eval–print_loop>,
166             L<Reply>.
167              
168             =head1 AUTHOR
169              
170             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
171              
172             =head1 COPYRIGHT AND LICENCE
173              
174             This software is copyright (c) 2014 by Toby Inkster.
175              
176             This is free software; you can redistribute it and/or modify it under
177             the same terms as the Perl 5 programming language system itself.
178              
179             =head1 DISCLAIMER OF WARRANTIES
180              
181             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
182             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
183             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
184