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