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 { '> ' }
12 around 'read' => sub {
14 my ($self, @args) = @_;
15 my $line = $self->$orig(@args);
18 while (needs_continuation($line)) {
19 my $orig_prompt = $self->prompt;
20 $self->prompt($self->continuation_prompt);
22 my $append = $self->read(@args);
23 $line .= $append if defined($append);
25 $self->prompt($orig_prompt);
27 # ^D means "shut up and eval already"
28 return $line if !defined($append);
34 sub needs_continuation
37 my $document = PPI::Document->new(\$line);
38 return 0 if !defined($document);
40 # this could use more logic, such as returning 1 on s/foo/ba<Enter>
41 my $unfinished_structure = sub
43 my ($document, $element) = @_;
44 return 0 unless $element->isa('PPI::Structure');
45 return 1 unless $element->start && $element->finish;
49 return $document->find_any($unfinished_structure);
58 Devel::REPL::Plugin::MultiLine::PPI - read lines until all blocks are closed
67 my $repl = Devel::REPL->new;
68 $repl->load_plugin('LexEnv');
69 $repl->load_plugin('History');
70 $repl->load_plugin('MultiLine::PPI');
75 Plugin that will collect lines until you have no unfinished structures. This
76 lets you write subroutines, C<if> statements, loops, etc. more naturally.
78 For example, without a MultiLine plugin,
84 will throw a compile error, because that C<if> statement is incomplete. With a
97 you may write the code across multiple lines, such as in C<irb> and C<python>.
99 This module uses L<PPI>. This plugin is named C<MultiLine::PPI> because someone
100 else may conceivably implement similar behavior some other less
101 dependency-heavy way.
109 Shawn M Moore, C<< <sartak at gmail dot com> >>
111 =head1 COPYRIGHT AND LICENSE
113 Copyright (C) 2007 by Shawn M Moore
115 This library is free software; you can redistribute it and/or modify
116 it under the same terms as Perl itself.