5 use namespace::clean -except => [ 'meta' ];
6 use 5.008001; # backwards compat, doesn't warn like 5.8.1
8 our $VERSION = '1.003001'; # 1.3.1
10 with 'MooseX::Object::Pluggable';
12 use Devel::REPL::Error;
15 is => 'rw', required => 1,
16 default => sub { Term::ReadLine->new('Perl REPL') }
20 is => 'rw', required => 1,
21 default => sub { '$ ' }
25 is => 'rw', required => 1, lazy => 1,
26 default => sub { shift->term->OUT || \*STDOUT; }
31 while ($self->run_once_safely) {
37 my ($self, @args) = @_;
39 my $ret = eval { $self->run_once(@args) };
43 eval { $self->print("Error! - $error\n"); };
53 my $line = $self->read;
54 return unless defined($line); # undefined value == EOF
56 my @ret = $self->formatted_eval($line);
64 my ( $self, @args ) = @_;
66 my @ret = $self->eval(@args);
68 return $self->format(@ret);
72 my ( $self, @stuff ) = @_;
74 if ( $self->is_error($stuff[0]) ) {
75 return $self->format_error(@stuff);
77 return $self->format_result(@stuff);
82 my ( $self, @stuff ) = @_;
88 my ( $self, $error ) = @_;
89 return $error->stringify;
93 my ( $self, $thingy ) = @_;
94 blessed($thingy) and $thingy->isa("Devel::REPL::Error");
99 return $self->term->readline($self->prompt);
103 my ($self, $line) = @_;
104 my $compiled = $self->compile($line);
105 return $compiled unless defined($compiled) and not $self->is_error($compiled);
106 return $self->execute($compiled);
110 my ( $_REPL, @args ) = @_;
111 my $compiled = eval $_REPL->wrap_as_sub(@args);
112 return $_REPL->error_return("Compile error", $@) if $@;
117 my ($self, $line, %args) = @_;
118 return qq!sub {\n!. ( $args{no_mangling} ? $line : $self->mangle_line($line) ).qq!\n}\n!;
122 my ($self, $line) = @_;
127 my ($self, $to_exec, @args) = @_;
128 my @ret = eval { $to_exec->(@args) };
129 return $self->error_return("Runtime error", $@) if $@;
134 my ($self, $type, $error) = @_;
135 return Devel::REPL::Error->new( type => $type, message => $error );
139 my ($self, @ret) = @_;
140 my $fh = $self->out_fh;
141 no warnings 'uninitialized';
143 print $fh "\n" if $self->term->ReadLine =~ /Gnu/;
148 Devel::REPL - a modern perl interactive shell
152 my $repl = Devel::REPL->new;
153 $repl->load_plugin($_) for qw(History LexEnv);
156 Alternatively, use the 're.pl' script installed with the distribution
162 This is an interactive shell for Perl, commonly known as a REPL - Read,
163 Evaluate, Print, Loop. The shell provides for rapid development or testing
164 of code without the need to create a temporary source code file.
166 Through a plugin system, many features are available on demand. You can also
167 tailor the environment through the use of profiles and run control files, for
168 example to pre-load certain Perl modules when working on a particular project.
172 To start a shell, follow one of the examples in the L</"SYNOPSIS"> above.
174 Once running, the shell accepts and will attempt to execute any code given. If
175 the code executes successfully you'll be shown the result, otherwise an error
176 message will be returned. Here are a few examples:
178 $_ print "Hello, world!\n"
182 Compile error: Bareword "nosuchfunction" not allowed while "strict subs" in use at (eval 130) line 5.
186 In the first example above you see the output of the command (C<Hello,
187 world!>), if any, and then the return value of the statement (C<1>). Following
188 that example, an error is returned when the execution of some code fails.
190 Note that the lack of semicolon on the end is not a mistake - the code is
191 run inside a Block structure (to protect the REPL in case the code blows up),
192 which means a single statement doesn't require the semicolon. You can add one
195 If you followed the first example in the L</"SYNOPSIS"> above, you'll have the
196 History and LexEnv plugins loaded (and there are many more available).
197 Although 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.
199 C<!53>). The LexEnv plugin ensures that lexical variables declared with the
200 C<my> keyword will automatically persist between statements executed in the
203 When you C<use> any Perl module, the C<import()> will work as expected - the
204 exported functions from that module are available for immediate use:
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.
214 $_ carp "I'm dieeeing!\n"
216 at /usr/share/perl5/Lexical/Persistence.pm line 327
220 To quit from the shell, hit C<control+d> or C<control+c>.
222 =head2 Run Control Files
224 For particular projects you might well end up running the same commands each
225 time the REPL shell starts up - loading Perl modules, setting configuration,
226 and so on. A run control file lets you have this done automatically, and you
227 can have multiple files for different projects.
229 By default the C<re.pl> program looks for C<< $HOME/.re.pl/repl.rc >>, and
230 runs whatever code is in there as if you had entered it at the REPL shell
233 To set a new run control file that's also in that directory, pass it as a
236 system$ re.pl --rcfile myproject.pc
238 If the filename happens to contain a forwardslash, then it's used absolutely,
239 or realive to the current working directory:
241 system$ re.pl --rcfile /path/to/my/project/repl.rc
243 Within the run control file you might want to load plugins. This is covered in
244 L</"The REPL shell object"> section, below.
248 To allow for the sharing of run control files, you can fashion them into a
249 Perl module for distribution (perhaps via the CPAN). For more information on
250 this feature, please see the L<Devel::REPL::Profile> manual page.
252 A default profile ships with C<Devel::REPL>; it loads the following plugins:
258 L<Devel::REPL::Plugin::History>
262 L<Devel::REPL::Plugin::LexEnv>
266 L<Devel::REPL::Plugin::DDS>
270 L<Devel::REPL::Plugin::Packages>
274 L<Devel::REPL::Plugin::Commands>
278 L<Devel::REPL::Plugin::MultiLine::PPI>
284 Plugins are a way to add funcionality to the REPL shell, and take advantage of
285 C<Devel::REPL> being based on the L<Moose> object system for Perl 5. This
286 means it's simple to 'hook into' many steps of the R-E-P-L process. Plugins
287 can change the way commands are interpreted, or the way their results are
288 output, or even add commands to the shell environment.
290 A number of plugins ship with C<Devel::REPL>, and more are available on the
291 CPAN. Some of the shipped plugins are loaded in the default profile, mentioned
294 Writing your own plugins is not difficult, and is discussed in the
295 L<Devel::REPL::Plugin> manual page, along with links to the manual pages of
296 all the plugins shipped with C<Devel::REPL>.
298 =head2 The REPL shell object
300 From time to time you'll want to interact with or manipulate the
301 C<Devel::REPL> shell object itself; that is, the instance of the shell you're
304 The object is always available through the C<$_REPL> variable. One common
305 requirement is to load an additional plugin, after your profile and run
306 control files have already been executed:
308 $_ $_REPL->load_plugin('Timing');
310 $_ print "Hello again, world!\n"
312 Took 0.00148296356201172 seconds.
318 In addition to the contents of the standard Perl distribution, you will need
329 L<MooseX::Object::Pluggable> >= 0.0009
333 L<MooseX::Getopt> >= 0.15
337 L<MooseX::AttributeHelpers> >= 0.14
349 L<Lexical::Persistence>
353 L<Data::Dump::Streamer>
375 Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
381 =item Stevan Little - stevan (at) iinteractive.com
383 =item Alexis Sukrieh - sukria+perl (at) sukria.net
387 =item mgrimes - mgrimes (at) cpan dot org
389 =item Shawn M Moore - sartak (at) gmail.com
397 This library is free software under the same terms as perl itself