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