1 package Devel::REPL::Plugin::MultiLine::PPI;
5 use namespace::clean -except => [ 'meta' ];
7 has 'continuation_prompt' => (
8 is => 'rw', required => 1, lazy => 1,
9 default => sub { '> ' }
13 is => 'rw', required => 1, lazy => 1,
17 around 'read' => sub {
19 my ($self, @args) = @_;
20 my $line = $self->$orig(@args);
23 while ($self->line_needs_continuation($line)) {
24 my $orig_prompt = $self->prompt;
25 $self->prompt($self->continuation_prompt);
27 $self->line_depth($self->line_depth + 1);
28 my $append = $self->read(@args);
29 $self->line_depth($self->line_depth - 1);
31 $line .= $append if defined($append);
33 $self->prompt($orig_prompt);
35 # ^D means "shut up and eval already"
36 return $line if !defined($append);
42 sub line_needs_continuation
46 my $document = PPI::Document->new(\$line);
47 return 0 if !defined($document);
49 # this could use more logic, such as returning 1 on s/foo/ba<Enter>
50 my $unfinished_structure = sub
52 my ($document, $element) = @_;
53 return 0 unless $element->isa('PPI::Structure');
54 return 1 unless $element->start && $element->finish;
58 return $document->find_any($unfinished_structure);
67 Devel::REPL::Plugin::MultiLine::PPI - read lines until all blocks are closed
76 my $repl = Devel::REPL->new;
77 $repl->load_plugin('LexEnv');
78 $repl->load_plugin('History');
79 $repl->load_plugin('MultiLine::PPI');
84 Plugin that will collect lines until you have no unfinished structures. This
85 lets you write subroutines, C<if> statements, loops, etc. more naturally.
87 For example, without a MultiLine plugin,
93 will throw a compile error, because that C<if> statement is incomplete. With a
106 you may write the code across multiple lines, such as in C<irb> and C<python>.
108 This module uses L<PPI>. This plugin is named C<MultiLine::PPI> because someone
109 else may conceivably implement similar behavior some other less
110 dependency-heavy way.
118 Shawn M Moore, C<< <sartak at gmail dot com> >>
120 =head1 COPYRIGHT AND LICENSE
122 Copyright (C) 2007 by Shawn M Moore
124 This library is free software; you can redistribute it and/or modify
125 it under the same terms as Perl itself.