File Coverage

blib/lib/Devel/REPL.pm
Criterion Covered Total %
statement 17 73 23.2
branch 0 22 0.0
condition 0 3 0.0
subroutine 6 22 27.2
pod 0 16 0.0
total 23 136 16.9


line stmt bran cond sub pod time code
1             # ABSTRACT: A modern perl interactive shell
2              
3             our $VERSION = '1.003029';
4              
5             use Term::ReadLine;
6 2     2   1918 use Moose;
  2         3992  
  2         52  
7 2     2   907 use namespace::autoclean;
  2         820544  
  2         14  
8 2     2   14058 use 5.008001; # backwards compat, doesn't warn like 5.8.1
  2         14445  
  2         8  
9 2     2   131  
  2         6  
10             with 'MooseX::Object::Pluggable';
11              
12             use Devel::REPL::Error;
13 2     2   973  
  2         5  
  2         1706  
14             has 'term' => (
15             is => 'rw',
16             lazy => 1,
17             default => sub { Term::ReadLine->new('Perl REPL') }
18             );
19              
20             has 'prompt' => (
21             is => 'rw',
22             default => sub { '$ ' }
23             );
24              
25             has 'out_fh' => (
26             is => 'rw',
27             lazy => 1,
28             default => sub { shift->term->OUT || \*STDOUT; }
29             );
30              
31             has 'exit_repl' => (
32             is => 'rw',
33             default => sub { 0 }
34             );
35              
36             my ($self) = @_;
37             while ($self->run_once_safely) {
38 0     0 0   # keep looping unless we want to exit REPL
39 0           last if $self->exit_repl;
40             }
41 0 0         }
42              
43             my ($self, @args) = @_;
44              
45             my $ret = eval { $self->run_once(@args) };
46 0     0 0    
47             if ($@) {
48 0           my $error = $@;
  0            
49             eval { $self->print("Error! - $error\n"); };
50 0 0         return 1;
51 0           } else {
52 0           return $ret;
  0            
53 0           }
54             }
55 0            
56             my ($self) = @_;
57              
58             my $line = $self->read;
59             return unless defined($line); # undefined value == EOF
60 0     0 0    
61             my @ret = $self->formatted_eval($line);
62 0            
63 0 0         $self->print(@ret) unless $self->exit_repl;
64              
65 0           return 1;
66             }
67 0 0          
68             my ( $self, @args ) = @_;
69 0            
70             my @ret = $self->eval(@args);
71              
72             return $self->format(@ret);
73 0     0 0   }
74              
75 0           my ( $self, @stuff ) = @_;
76              
77 0           if ( $self->is_error($stuff[0]) ) {
78             return $self->format_error(@stuff);
79             } else {
80             return $self->format_result(@stuff);
81 0     0 0   }
82             }
83 0 0          
84 0           my ( $self, @stuff ) = @_;
85              
86 0           return @stuff;
87             }
88              
89             my ( $self, $error ) = @_;
90             return $error->stringify;
91 0     0 0   }
92              
93 0           my ( $self, $thingy ) = @_;
94             blessed($thingy) and $thingy->isa("Devel::REPL::Error");
95             }
96              
97 0     0 0   my ($self) = @_;
98 0           return $self->term->readline($self->prompt);
99             }
100              
101             my ($self, $line) = @_;
102 0     0 0   my $compiled = $self->compile($line);
103 0 0         return $compiled unless defined($compiled) and not $self->is_error($compiled);
104             return $self->execute($compiled);
105             }
106              
107 0     0 0   my ( $_REPL, @args ) = @_;
108 0           my $compiled = eval $_REPL->wrap_as_sub(@args);
109             return $_REPL->error_return("Compile error", $@) if $@;
110             return $compiled;
111             }
112 0     0 0    
113 0           my ($self, $line, %args) = @_;
114 0 0 0       return qq!sub {\n!. ( $args{no_mangling} ? $line : $self->mangle_line($line) ).qq!\n}\n!;
115 0           }
116              
117             my ($self, $line) = @_;
118             return $line;
119 0     0 0   }
120 0            
121 0 0         my ($self, $to_exec, @args) = @_;
122 0           my @ret = eval { $to_exec->(@args) };
123             return $self->error_return("Runtime error", $@) if $@;
124             return @ret;
125             }
126 0     0 0    
127 0 0         my ($self, $type, $error) = @_;
128             return Devel::REPL::Error->new( type => $type, message => $error );
129             }
130              
131 0     0 0   my ($self, @ret) = @_;
132 0           my $fh = $self->out_fh;
133             no warnings 'uninitialized';
134             print $fh "@ret";
135             print $fh "\n" if $self->term->ReadLine =~ /Gnu/;
136 0     0 0   }
137 0            
  0            
138 0 0         1;
139 0            
140              
141             =pod
142              
143 0     0 0   =encoding UTF-8
144 0            
145             =head1 NAME
146              
147             Devel::REPL - A modern perl interactive shell
148 0     0 0    
149 0           =head1 VERSION
150 2     2   20  
  2         4  
  2         269  
151 0           version 1.003029
152 0 0          
153             =head1 SYNOPSIS
154              
155             my $repl = Devel::REPL->new;
156             $repl->load_plugin($_) for qw(History LexEnv);
157             $repl->run
158              
159             Alternatively, use the 're.pl' script installed with the distribution
160              
161             system$ re.pl
162              
163             =head1 DESCRIPTION
164              
165             This is an interactive shell for Perl, commonly known as a REPL - Read,
166             Evaluate, Print, Loop. The shell provides for rapid development or testing
167             of code without the need to create a temporary source code file.
168              
169             Through a plugin system, many features are available on demand. You can also
170             tailor the environment through the use of profiles and run control files, for
171             example to pre-load certain Perl modules when working on a particular project.
172              
173             =head1 USAGE
174              
175             To start a shell, follow one of the examples in the L</"SYNOPSIS"> above.
176              
177             Once running, the shell accepts and will attempt to execute any code given. If
178             the code executes successfully you'll be shown the result, otherwise an error
179             message will be returned. Here are a few examples:
180              
181             $_ print "Hello, world!\n"
182             Hello, world!
183             1
184             $_ nosuchfunction
185             Compile error: Bareword "nosuchfunction" not allowed while "strict subs" in use at (eval 130) line 5.
186              
187             $_
188              
189             In the first example above you see the output of the command (C<Hello,
190             world!>), if any, and then the return value of the statement (C<1>). Following
191             that example, an error is returned when the execution of some code fails.
192              
193             Note that the lack of semicolon on the end is not a mistake - the code is
194             run inside a Block structure (to protect the REPL in case the code blows up),
195             which means a single statement doesn't require the semicolon. You can add one
196             if you like, though.
197              
198             If you followed the first example in the L</"SYNOPSIS"> above, you'll have the
199             L<History|Devel::REPL::Plugin::History> and L<LexEnv|Devel::REPL::Plugin::LexEnv>
200             plugins loaded (and there are many more available).
201             Although the shell might support "up-arrow" history, the History plugin adds
202             "bang" history to that so you can re-execute chosen commands (with e.g.
203             C<!53>). The LexEnv plugin ensures that lexical variables declared with the
204             C<my> keyword will automatically persist between statements executed in the
205             REPL shell.
206              
207             When you C<use> any Perl module, the C<import()> will work as expected - the
208             exported functions from that module are available for immediate use:
209              
210             $_ carp "I'm dieeeing!\n"
211             String found where operator expected at (eval 129) line 5, near "carp "I'm dieeeing!\n""
212             (Do you need to predeclare carp?)
213             Compile error: syntax error at (eval 129) line 5, near "carp "I'm dieeeing!\n""
214             BEGIN not safe after errors--compilation aborted at (eval 129) line 5.
215              
216             $_ use Carp
217              
218             $_ carp "I'm dieeeing!\n"
219             I'm dieeeing!
220             at /usr/share/perl5/Lexical/Persistence.pm line 327
221             1
222             $_
223              
224             To quit from the shell, hit C<Ctrl+D> or C<Ctrl+C>.
225              
226             MSWin32 NOTE: control keys won't work if TERM=dumb
227             because readline functionality will be disabled.
228              
229             =head2 Run Control Files
230              
231             For particular projects you might well end up running the same commands each
232             time the REPL shell starts up - loading Perl modules, setting configuration,
233             and so on. A run control file lets you have this done automatically, and you
234             can have multiple files for different projects.
235              
236             By default the C<re.pl> program looks for C<< $HOME/.re.pl/repl.rc >>, and
237             runs whatever code is in there as if you had entered it at the REPL shell
238             yourself.
239              
240             To set a new run control file that's also in that directory, pass it as a
241             filename like so:
242              
243             system$ re.pl --rcfile myproject.pc
244              
245             If the filename happens to contain a forward slash, then it's used absolutely,
246             or realive to the current working directory:
247              
248             system$ re.pl --rcfile /path/to/my/project/repl.rc
249              
250             Within the run control file you might want to load plugins. This is covered in
251             L</"The REPL shell object"> section, below.
252              
253             =head2 Profiles
254              
255             To allow for the sharing of run control files, you can fashion them into a
256             Perl module for distribution (perhaps via the CPAN). For more information on
257             this feature, please see the L<Devel::REPL::Profile> manual page.
258              
259             A C<Standard> profile ships with C<Devel::REPL>; it loads the following plugins
260             (note that some of these require optional features -- or you can also use the
261             C<Minimal> profile):
262              
263             =over 4
264              
265             =item *
266              
267             L<Devel::REPL::Plugin::History>
268              
269             =item *
270              
271             L<Devel::REPL::Plugin::LexEnv>
272              
273             =item *
274              
275             L<Devel::REPL::Plugin::DDS>
276              
277             =item *
278              
279             L<Devel::REPL::Plugin::Packages>
280              
281             =item *
282              
283             L<Devel::REPL::Plugin::Commands>
284              
285             =item *
286              
287             L<Devel::REPL::Plugin::MultiLine::PPI>
288              
289             =item *
290              
291             L<Devel::REPL::Plugin::Colors>
292              
293             =item *
294              
295             L<Devel::REPL::Plugin::Completion>
296              
297             =item *
298              
299             L<Devel::REPL::Plugin::CompletionDriver::INC>
300              
301             =item *
302              
303             L<Devel::REPL::Plugin::CompletionDriver::LexEnv>
304              
305             =item *
306              
307             L<Devel::REPL::Plugin::CompletionDriver::Keywords>
308              
309             =item *
310              
311             L<Devel::REPL::Plugin::CompletionDriver::Methods>
312              
313             =item *
314              
315             L<Devel::REPL::Plugin::ReadlineHistory>
316              
317             =back
318              
319             =head2 Plugins
320              
321             Plugins are a way to add functionality to the REPL shell, and take advantage of
322             C<Devel::REPL> being based on the L<Moose> object system for Perl 5. This
323             means it's simple to 'hook into' many steps of the R-E-P-L process. Plugins
324             can change the way commands are interpreted, or the way their results are
325             output, or even add commands to the shell environment.
326              
327             A number of plugins ship with C<Devel::REPL>, and more are available on the
328             CPAN. Some of the shipped plugins are loaded in the default profile, mentioned
329             above. These plugins can be loaded in your F< $HOME/.re.pl/repl.rc > like:
330              
331             load_plugin qw( CompletionDriver::Global DumpHistory );
332              
333             Writing your own plugins is not difficult, and is discussed in the
334             L<Devel::REPL::Plugin> manual page, along with links to the manual pages of
335             all the plugins shipped with C<Devel::REPL>.
336              
337             =head2 The REPL shell object
338              
339             From time to time you'll want to interact with or manipulate the
340             C<Devel::REPL> shell object itself; that is, the instance of the shell you're
341             currently running.
342              
343             The object is always available through the C<$_REPL> variable. One common
344             requirement is to load an additional plugin, after your profile and run
345             control files have already been executed:
346              
347             $_ $_REPL->load_plugin('Timing');
348             1
349             $_ print "Hello again, world!\n"
350             Hello again, world!
351             Took 0.00148296356201172 seconds.
352             1
353             $_
354              
355             =head1 OPTIONAL FEATURES
356              
357             In addition to the prerequisites declared in this distribution, which should be automatically installed by your L<CPAN> client, there are a number of optional features, used by
358             additional plugins. You can install any of these features by installing this
359             distribution interactively (e.g. C<cpanm --interactive Devel::REPL>).
360              
361             =for comment I hope to automatically generate this data via a Pod::Weaver section
362              
363             =over 4
364              
365             =item *
366              
367             Completion plugin - extensible tab completion
368              
369             =item *
370              
371             DDS plugin - better format results with Data::Dump::Streamer
372              
373             =item *
374              
375             DDC plugin - even better format results with Data::Dumper::Concise
376              
377             =item *
378              
379             INC completion driver - tab complete module names in use and require
380              
381             =item *
382              
383             Interrupt plugin - traps SIGINT to kill long-running lines
384              
385             =item *
386              
387             Keywords completion driver - tab complete Perl keywords and operators
388              
389             =item *
390              
391             LexEnv plugin - variables declared with "my" persist between statements
392              
393             =item *
394              
395             MultiLine::PPI plugin - continue reading lines until all blocks are closed
396              
397             =item *
398              
399             Nopaste plugin - upload a session\'s input and output to a Pastebin
400              
401             =item *
402              
403             PPI plugin - PPI dumping of Perl code
404              
405             =item *
406              
407             Refresh plugin - automatically reload libraries with Module::Refresh
408              
409             =back
410              
411             =head1 SEE ALSO
412              
413             =over 4
414              
415             =item *
416              
417             L<A comparison of various REPLs|https://www.shadowcat.co.uk/blog/matt-s-trout/mstpan-17/>
418              
419             =back
420              
421             =head1 SUPPORT
422              
423             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Devel-REPL>
424             (or L<bug-Devel-REPL@rt.cpan.org|mailto:bug-Devel-REPL@rt.cpan.org>).
425              
426             There is also an irc channel available for users of this distribution, at
427             L<C<#devel> on C<irc.perl.org>|irc://irc.perl.org/#devel-repl>.
428              
429             =head1 AUTHOR
430              
431             Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
432              
433             =head1 CONTRIBUTORS
434              
435             =for stopwords Karen Etheridge Shawn M Moore Chris Marshall Matt S Trout Oliver Gorwits יובל קוג'מן (Yuval Kogman) Arthur Axel 'fREW' Schmidt Alexis Sukrieh Andrew epitaph Jesse Luehrs Norbert Buchmuller Tomas Doran (t0m) Dagfinn Ilmari Mannsåker Dave Houston Zakariyya Mughal Ash Berlin Justin Hunter mgrimes naquad Ryan Niebur Stevan Little
436              
437             =over 4
438              
439             =item *
440              
441             Karen Etheridge <ether@cpan.org>
442              
443             =item *
444              
445             Shawn M Moore <code@sartak.org>
446              
447             =item *
448              
449             Chris Marshall <devel.chm.01@gmail.com>
450              
451             =item *
452              
453             Matt S Trout <mst@shadowcat.co.uk>
454              
455             =item *
456              
457             Oliver Gorwits <oliver@cpan.org>
458              
459             =item *
460              
461             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
462              
463             =item *
464              
465             Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
466              
467             =item *
468              
469             Alexis Sukrieh <sukria+perl@sukria.net>
470              
471             =item *
472              
473             Andrew Moore <amoore@cpan.org>
474              
475             =item *
476              
477             epitaph <unknown>
478              
479             =item *
480              
481             Jesse Luehrs <doy@tozt.net>
482              
483             =item *
484              
485             Norbert Buchmuller <norbi@nix.hu>
486              
487             =item *
488              
489             Tomas Doran (t0m) <bobtfish@bobtfish.net>
490              
491             =item *
492              
493             Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
494              
495             =item *
496              
497             Dave Houston <dhouston@cpan.org>
498              
499             =item *
500              
501             Zakariyya Mughal <zaki.mughal@gmail.com>
502              
503             =item *
504              
505             Ash Berlin <ash_github@firemirror.com>
506              
507             =item *
508              
509             Justin Hunter <justin.d.hunter@gmail.com>
510              
511             =item *
512              
513             mgrimes <mgrimes@cpan.org>
514              
515             =item *
516              
517             naquad <naquad@bd8105ee-0ff8-0310-8827-fb3f25b6796d>
518              
519             =item *
520              
521             Ryan Niebur <ryan@debian.org>
522              
523             =item *
524              
525             Stevan Little <stevan.little@iinteractive.com>
526              
527             =back
528              
529             =head1 COPYRIGHT AND LICENCE
530              
531             This software is copyright (c) 2007 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
532              
533             This is free software; you can redistribute it and/or modify it under
534             the same terms as the Perl 5 programming language system itself.
535              
536             =cut