5 use namespace::autoclean;
6 use 5.008001; # backwards compat, doesn't warn like 5.8.1
8 our $VERSION = '1.003017';
10 with 'MooseX::Object::Pluggable';
12 use Devel::REPL::Error;
16 default => sub { Term::ReadLine->new('Perl REPL') }
21 default => sub { '$ ' }
27 default => sub { shift->term->OUT || \*STDOUT; }
37 while ($self->run_once_safely) {
38 # keep looping unless we want to exit REPL
39 last if $self->exit_repl;
44 my ($self, @args) = @_;
46 my $ret = eval { $self->run_once(@args) };
50 eval { $self->print("Error! - $error\n"); };
60 my $line = $self->read;
61 return unless defined($line); # undefined value == EOF
63 my @ret = $self->formatted_eval($line);
65 $self->print(@ret) unless $self->exit_repl;
71 my ( $self, @args ) = @_;
73 my @ret = $self->eval(@args);
75 return $self->format(@ret);
79 my ( $self, @stuff ) = @_;
81 if ( $self->is_error($stuff[0]) ) {
82 return $self->format_error(@stuff);
84 return $self->format_result(@stuff);
89 my ( $self, @stuff ) = @_;
95 my ( $self, $error ) = @_;
96 return $error->stringify;
100 my ( $self, $thingy ) = @_;
101 blessed($thingy) and $thingy->isa("Devel::REPL::Error");
106 return $self->term->readline($self->prompt);
110 my ($self, $line) = @_;
111 my $compiled = $self->compile($line);
112 return $compiled unless defined($compiled) and not $self->is_error($compiled);
113 return $self->execute($compiled);
117 my ( $_REPL, @args ) = @_;
118 my $compiled = eval $_REPL->wrap_as_sub(@args);
119 return $_REPL->error_return("Compile error", $@) if $@;
124 my ($self, $line, %args) = @_;
125 return qq!sub {\n!. ( $args{no_mangling} ? $line : $self->mangle_line($line) ).qq!\n}\n!;
129 my ($self, $line) = @_;
134 my ($self, $to_exec, @args) = @_;
135 my @ret = eval { $to_exec->(@args) };
136 return $self->error_return("Runtime error", $@) if $@;
141 my ($self, $type, $error) = @_;
142 return Devel::REPL::Error->new( type => $type, message => $error );
146 my ($self, @ret) = @_;
147 my $fh = $self->out_fh;
148 no warnings 'uninitialized';
150 print $fh "\n" if $self->term->ReadLine =~ /Gnu/;
155 Devel::REPL - a modern perl interactive shell
159 my $repl = Devel::REPL->new;
160 $repl->load_plugin($_) for qw(History LexEnv);
163 Alternatively, use the 're.pl' script installed with the distribution
169 This is an interactive shell for Perl, commonly known as a REPL - Read,
170 Evaluate, Print, Loop. The shell provides for rapid development or testing
171 of code without the need to create a temporary source code file.
173 Through a plugin system, many features are available on demand. You can also
174 tailor the environment through the use of profiles and run control files, for
175 example to pre-load certain Perl modules when working on a particular project.
179 To start a shell, follow one of the examples in the L</"SYNOPSIS"> above.
181 Once running, the shell accepts and will attempt to execute any code given. If
182 the code executes successfully you'll be shown the result, otherwise an error
183 message will be returned. Here are a few examples:
185 $_ print "Hello, world!\n"
189 Compile error: Bareword "nosuchfunction" not allowed while "strict subs" in use at (eval 130) line 5.
193 In the first example above you see the output of the command (C<Hello,
194 world!>), if any, and then the return value of the statement (C<1>). Following
195 that example, an error is returned when the execution of some code fails.
197 Note that the lack of semicolon on the end is not a mistake - the code is
198 run inside a Block structure (to protect the REPL in case the code blows up),
199 which means a single statement doesn't require the semicolon. You can add one
202 If you followed the first example in the L</"SYNOPSIS"> above, you'll have the
203 History and LexEnv plugins loaded (and there are many more available).
204 Although the shell might support "up-arrow" history, the History plugin adds
205 "bang" history to that so you can re-execute chosen commands (with e.g.
206 C<!53>). The LexEnv plugin ensures that lexical variables declared with the
207 C<my> keyword will automatically persist between statements executed in the
210 When you C<use> any Perl module, the C<import()> will work as expected - the
211 exported functions from that module are available for immediate use:
213 $_ carp "I'm dieeeing!\n"
214 String found where operator expected at (eval 129) line 5, near "carp "I'm dieeeing!\n""
215 (Do you need to predeclare carp?)
216 Compile error: syntax error at (eval 129) line 5, near "carp "I'm dieeeing!\n""
217 BEGIN not safe after errors--compilation aborted at (eval 129) line 5.
221 $_ carp "I'm dieeeing!\n"
223 at /usr/share/perl5/Lexical/Persistence.pm line 327
227 To quit from the shell, hit C<Ctrl+D> or C<Ctrl+C>.
229 MSWin32 NOTE: control keys won't work if TERM=dumb
230 because readline functionality will be disabled.
233 =head2 Run Control Files
235 For particular projects you might well end up running the same commands each
236 time the REPL shell starts up - loading Perl modules, setting configuration,
237 and so on. A run control file lets you have this done automatically, and you
238 can have multiple files for different projects.
240 By default the C<re.pl> program looks for C<< $HOME/.re.pl/repl.rc >>, and
241 runs whatever code is in there as if you had entered it at the REPL shell
244 To set a new run control file that's also in that directory, pass it as a
247 system$ re.pl --rcfile myproject.pc
249 If the filename happens to contain a forwardslash, then it's used absolutely,
250 or realive to the current working directory:
252 system$ re.pl --rcfile /path/to/my/project/repl.rc
254 Within the run control file you might want to load plugins. This is covered in
255 L</"The REPL shell object"> section, below.
259 To allow for the sharing of run control files, you can fashion them into a
260 Perl module for distribution (perhaps via the CPAN). For more information on
261 this feature, please see the L<Devel::REPL::Profile> manual page.
263 A default profile ships with C<Devel::REPL>; it loads the following plugins:
269 L<Devel::REPL::Plugin::History>
273 L<Devel::REPL::Plugin::LexEnv>
277 L<Devel::REPL::Plugin::DDS>
281 L<Devel::REPL::Plugin::Packages>
285 L<Devel::REPL::Plugin::Commands>
289 L<Devel::REPL::Plugin::MultiLine::PPI>
293 L<Devel::REPL::Plugin::Colors>
297 L<Devel::REPL::Plugin::Completion>
301 L<Devel::REPL::Plugin::CompletionDriver::INC>
305 L<Devel::REPL::Plugin::CompletionDriver::LexEnv>
309 L<Devel::REPL::Plugin::CompletionDriver::Keywords>
313 L<Devel::REPL::Plugin::CompletionDriver::Methods>
317 L<Devel::REPL::Plugin::ReadlineHistory>
323 Plugins are a way to add funcionality to the REPL shell, and take advantage of
324 C<Devel::REPL> being based on the L<Moose> object system for Perl 5. This
325 means it's simple to 'hook into' many steps of the R-E-P-L process. Plugins
326 can change the way commands are interpreted, or the way their results are
327 output, or even add commands to the shell environment.
329 A number of plugins ship with C<Devel::REPL>, and more are available on the
330 CPAN. Some of the shipped plugins are loaded in the default profile, mentioned
331 above. These plugins can be loaded in your C<< $HOME/.re.pl/repl.rc >> like:
333 load_plugin qw( CompletionDriver::Global DumpHistory );
335 Writing your own plugins is not difficult, and is discussed in the
336 L<Devel::REPL::Plugin> manual page, along with links to the manual pages of
337 all the plugins shipped with C<Devel::REPL>.
339 =head2 The REPL shell object
341 From time to time you'll want to interact with or manipulate the
342 C<Devel::REPL> shell object itself; that is, the instance of the shell you're
345 The object is always available through the C<$_REPL> variable. One common
346 requirement is to load an additional plugin, after your profile and run
347 control files have already been executed:
349 $_ $_REPL->load_plugin('Timing');
351 $_ print "Hello again, world!\n"
353 Took 0.00148296356201172 seconds.
359 In addition to the contents of the standard Perl distribution, you will need
370 L<MooseX::Object::Pluggable> >= 0.0009
374 L<MooseX::Getopt> >= 0.18
378 L<namespace::autoclean>
402 Optionally, some plugins if installed will require the following modules:
412 L<Data::Dump::Streamer>
416 L<Data::Dumper::Concise>
432 L<Lexical::Persistence>
446 Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
452 =item Stevan Little - stevan (at) iinteractive.com
454 =item Alexis Sukrieh - sukria+perl (at) sukria.net
458 =item mgrimes - mgrimes (at) cpan dot org
460 =item Shawn M Moore - sartak (at) gmail.com
462 =item Oliver Gorwits - oliver on irc.perl.org
464 =item Andrew Moore - C<< <amoore@cpan.org> >>
466 =item Norbert Buchmuller C<< <norbi@nix.hu> >>
468 =item Dave Houston C<< <dhouston@cpan.org> >>
476 This library is free software under the same terms as perl itself