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