71b97be6e6fc4a5596566c8b9b8d134b1bc74ff5
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / B / Concise.pm
1 #!/usr/bin/perl
2
3 package Devel::REPL::Plugin::B::Concise;
4 use Devel::REPL::Plugin;
5
6 use B::Concise ();
7
8 B::Concise::compileOpts(qw(-nobanner));
9
10 use namespace::autoclean;
11
12 sub BEFORE_PLUGIN {
13     my $self = shift;
14     $self->load_plugin('Turtles');
15 }
16
17 sub AFTER_PLUGIN {
18   my $self = shift;
19
20   my $prefix = $self->default_command_prefix; 
21
22   $self->add_turtles_matcher(qr/^
23     \#(concise) \s+
24     ( (?:\-\w+\s+)* ) # options for concise
25     (.*) # the code
26     /x);
27 }
28
29 sub expr_command_concise {
30   my ( $self, $eval, $opts, $code ) = @_;
31
32   die unless $code;
33
34   my %opts = map { $_ => 1 } (split /\s+/, $opts);
35
36   my $sub = $self->compile($code, no_mangling => !delete($opts{"-mangle"}) );
37
38   if ( $self->is_error($sub) ) {
39     return $self->format($sub);
40   } else {
41     open my $fh, ">", \my $out;
42     {
43       local *STDOUT = $fh;
44       B::Concise::compile(keys %opts, $sub)->();
45     }
46
47     return $out;
48   }
49 }
50
51 __PACKAGE__
52
53 __END__
54
55 =pod
56
57 =head1 NAME
58
59 Devel::REPL::Plugin::B::Concise - B::Concise dumping of expression optrees
60
61 =head1 SYNOPSIS
62
63   repl> #concise -exec -terse {
64   > foo => foo(),
65   > }
66   COP (0x138b1e0) nextstate 
67   OP (0x13bd280) pushmark 
68   SVOP (0x138c6a0) const  PV (0xbbab50) "foo" 
69   OP (0x13bbae0) pushmark 
70   SVOP (0x13bcee0) gv  GV (0xbbb250) *Devel::REPL::Plugin::B::Concise::foo 
71   UNOP (0x13890a0) entersub [1] 
72   LISTOP (0x13ba020) anonhash 
73   UNOP (0x5983d0) leavesub [1] 
74
75 =head1 DESCRIPTION
76
77 This plugin provides a C<concise> command that uses L<B::Concise> to dump
78 optrees of expressions.
79
80 The code is not actually executed, which means that when used with
81 L<Deve::REPL::Plugin::OutputCache> there is no new value in C<_>.
82
83 The command takes the same options as L<B::Concise/compile>, e.g. C<-basic> or
84 C<-exec> to determine the dump order, C<-debug>, C<-concise> and C<-terse> to
85 determine the formatting, etc.
86
87 =head1 AUTHOR
88
89 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
90
91 =cut
92
93