refactor formatting/printing shit, introduce error object for error_return
[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
6edfdc07 8our $VERSION = '1.002001'; # 1.2.1
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
74 if ( blessed($stuff[0]) and $stuff[0]->isa("Devel::REPL::Error") ) {
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
afe61f9c 92sub read {
93 my ($self) = @_;
94 return $self->term->readline($self->prompt);
95}
96
911a1c24 97sub eval {
98 my ($self, $line) = @_;
99 my ($to_exec, @rest) = $self->compile($line);
100 return @rest unless defined($to_exec);
101 my @ret = $self->execute($to_exec);
102 return @ret;
103}
104
105sub compile {
e22aa835 106 my ( $_REPL, @args ) = @_;
107 my $compiled = eval $_REPL->wrap_as_sub(@args);
85cd2780 108 return (undef, $_REPL->error_return("Compile error", $@)) if $@;
911a1c24 109 return $compiled;
110}
111
112sub wrap_as_sub {
e22aa835 113 my ($self, $line, %args) = @_;
114 return qq!sub {\n!. ( $args{no_mangling} ? $line : $self->mangle_line($line) ).qq!\n}\n!;
911a1c24 115}
116
117sub mangle_line {
118 my ($self, $line) = @_;
119 return $line;
120}
121
afe61f9c 122sub execute {
48ddfeae 123 my ($self, $to_exec, @args) = @_;
124 my @ret = eval { $to_exec->(@args) };
125 return $self->error_return("Runtime error", $@) if $@;
afe61f9c 126 return @ret;
127}
128
911a1c24 129sub error_return {
130 my ($self, $type, $error) = @_;
e22aa835 131 return Devel::REPL::Error->new( type => $type, message => $error );
911a1c24 132}
133
afe61f9c 134sub print {
135 my ($self, @ret) = @_;
136 my $fh = $self->out_fh;
59aedffc 137 no warnings 'uninitialized';
afe61f9c 138 print $fh "@ret";
a66625d6 139 print $fh "\n" if $self->term->ReadLine =~ /Gnu/;
afe61f9c 140}
141
59aedffc 142=head1 NAME
143
144Devel::REPL - a modern perl interactive shell
145
146=head1 SYNOPSIS
147
148 my $repl = Devel::REPL->new;
149 $repl->load_plugin($_) for qw(History LexEnv);
150 $repl->run
151
152Alternatively, use the 're.pl' script installed with the distribution
153
950232b2 154 system$ re.pl
155
59aedffc 156=head1 AUTHOR
157
158Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
159
c1d5d500 160=head1 CONTRIBUTORS
161
162=over 4
163
164=item Stevan Little - stevan (at) iinteractive.com
165
166=item Alexis Sukrieh - sukria+perl (at) sukria.net
167
168=item epitaph
169
170=item mgrimes - mgrimes (at) cpan dot org
171
172=item Shawn M Moore - sartak (at) gmail.com
173
174=back
175
59aedffc 176=head1 LICENSE
177
178This library is free software under the same terms as perl itself
179
180=cut
181
afe61f9c 1821;