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