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