basic packaging code
[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' ];
59aedffc 6use 5.8.1; # might work with earlier perls but probably not
7
8our $VERSION = '1.000000';
afe61f9c 9
10with 'MooseX::Object::Pluggable';
11
12has 'term' => (
13 is => 'rw', required => 1,
14 default => sub { Term::ReadLine->new('Perl REPL') }
15);
16
17has 'prompt' => (
18 is => 'rw', required => 1,
19 default => sub { '$ ' }
20);
21
22has 'out_fh' => (
23 is => 'rw', required => 1, lazy => 1,
24 default => sub { shift->term->OUT || \*STDOUT; }
25);
26
27sub run {
28 my ($self) = @_;
29 while ($self->run_once) {
30 # keep looping
31 }
32}
33
34sub run_once {
35 my ($self) = @_;
36 my $line = $self->read;
37 return unless defined($line); # undefined value == EOF
911a1c24 38 my @ret = $self->eval($line);
afe61f9c 39 $self->print(@ret);
40 return 1;
41}
42
43sub read {
44 my ($self) = @_;
45 return $self->term->readline($self->prompt);
46}
47
911a1c24 48sub eval {
49 my ($self, $line) = @_;
50 my ($to_exec, @rest) = $self->compile($line);
51 return @rest unless defined($to_exec);
52 my @ret = $self->execute($to_exec);
53 return @ret;
54}
55
56sub compile {
85cd2780 57 my $_REPL = shift;
58 my $compiled = eval $_REPL->wrap_as_sub($_[0]);
59 return (undef, $_REPL->error_return("Compile error", $@)) if $@;
911a1c24 60 return $compiled;
61}
62
63sub wrap_as_sub {
64 my ($self, $line) = @_;
65 return qq!sub {\n!.$self->mangle_line($line).qq!\n}\n!;
66}
67
68sub mangle_line {
69 my ($self, $line) = @_;
70 return $line;
71}
72
afe61f9c 73sub execute {
48ddfeae 74 my ($self, $to_exec, @args) = @_;
75 my @ret = eval { $to_exec->(@args) };
76 return $self->error_return("Runtime error", $@) if $@;
afe61f9c 77 return @ret;
78}
79
911a1c24 80sub error_return {
81 my ($self, $type, $error) = @_;
82 return "${type}: ${error}";
83}
84
afe61f9c 85sub print {
86 my ($self, @ret) = @_;
87 my $fh = $self->out_fh;
59aedffc 88 no warnings 'uninitialized';
afe61f9c 89 print $fh "@ret";
90}
91
59aedffc 92=head1 NAME
93
94Devel::REPL - a modern perl interactive shell
95
96=head1 SYNOPSIS
97
98 my $repl = Devel::REPL->new;
99 $repl->load_plugin($_) for qw(History LexEnv);
100 $repl->run
101
102Alternatively, use the 're.pl' script installed with the distribution
103
104=head1 AUTHOR
105
106Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
107
108=head1 LICENSE
109
110This library is free software under the same terms as perl itself
111
112=cut
113
afe61f9c 1141;