detab. again. think I might've fixed it this time.
[gitmo/Moo.git] / lib / Sub / Quote.pm
CommitLineData
a165a07f 1package Sub::Quote;
2
3use strictures 1;
4
5sub _clean_eval { eval $_[0] }
6
7use Sub::Defer;
8use B 'perlstring';
9use base qw(Exporter);
10
625d6219 11our @EXPORT = qw(quote_sub unquote_sub quoted_from_sub);
a165a07f 12
13our %QUOTE_OUTSTANDING;
14
15our %QUOTED;
16
8c6626cf 17sub capture_unroll {
18 my ($from, $captures, $indent) = @_;
19 join(
20 '',
21 map {
22 /^([\@\%\$])/
17a8e3f0 23 or die "capture key should start with \@, \% or \$: $_";
8c6626cf 24 (' ' x $indent).qq{my ${_} = ${1}{${from}->{${\perlstring $_}}};\n};
25 } keys %$captures
26 );
27}
28
a165a07f 29sub _unquote_all_outstanding {
30 return unless %QUOTE_OUTSTANDING;
31 my ($assembled_code, @assembled_captures, @localize_these) = '';
32 foreach my $outstanding (keys %QUOTE_OUTSTANDING) {
33 my ($name, $code, $captures) = @{$QUOTE_OUTSTANDING{$outstanding}};
34
35 push @localize_these, $name if $name;
36
37 my $make_sub = "{\n";
38
39 if (keys %$captures) {
40 my $ass_cap_count = @assembled_captures;
8c6626cf 41 $make_sub .= capture_unroll("\$_[1][${ass_cap_count}]", $captures, 2);
a165a07f 42 push @assembled_captures, $captures;
43 }
44
45 my $o_quoted = perlstring $outstanding;
46 $make_sub .= (
47 $name
934ea2c1 48 # disable the 'variable $x will not stay shared' warning since
49 # we're not letting it escape from this scope anyway so there's
50 # nothing trying to share it
a165a07f 51 ? " no warnings 'closure';\n sub ${name} {\n"
934ea2c1 52 : " \$Sub::Quote::QUOTED{${o_quoted}}[3] = sub {\n"
a165a07f 53 );
54 $make_sub .= $code;
55 $make_sub .= " }".($name ? '' : ';')."\n";
56 if ($name) {
57 $make_sub .= " \$Sub::Quote::QUOTED{${o_quoted}}[3] = \\&${name}\n";
58 }
59 $make_sub .= "}\n";
60 $assembled_code .= $make_sub;
61 }
a16d301e 62 my $debug_code = $assembled_code;
a165a07f 63 if (@localize_these) {
a16d301e 64 $debug_code =
e8b2bab6 65 "# localizing: ".join(', ', @localize_these)."\n"
66 .$assembled_code;
a165a07f 67 $assembled_code = join("\n",
68 (map { "local *${_};" } @localize_these),
a16d301e 69 'eval '.perlstring($assembled_code).'; die $@ if $@;'
a165a07f 70 );
e8b2bab6 71 } else {
72 $ENV{SUB_QUOTE_DEBUG} && warn $assembled_code;
a165a07f 73 }
a16d301e 74 $assembled_code .= "\n1;";
75 unless (_clean_eval $assembled_code, \@assembled_captures) {
76 die "Eval went very, very wrong:\n\n${debug_code}\n\n$@";
a165a07f 77 }
a16d301e 78 $ENV{SUB_QUOTE_DEBUG} && warn $debug_code;
a165a07f 79 %QUOTE_OUTSTANDING = ();
80}
81
82sub quote_sub {
83 # HOLY DWIMMERY, BATMAN!
6f68f022 84 # $name => $code => \%captures => \%options
a165a07f 85 # $name => $code => \%captures
86 # $name => $code
6f68f022 87 # $code => \%captures => \%options
a165a07f 88 # $code
6f68f022 89 my $options =
90 (ref($_[-1]) eq 'HASH' and ref($_[-2]) eq 'HASH')
91 ? pop
92 : {};
a165a07f 93 my $captures = pop if ref($_[-1]) eq 'HASH';
625d6219 94 undef($captures) if $captures && !keys %$captures;
a165a07f 95 my $code = pop;
96 my $name = $_[0];
97 my $outstanding;
6f68f022 98 my $deferred = defer_sub +($options->{no_install} ? undef : $name) => sub {
a165a07f 99 unquote_sub($outstanding);
100 };
101 $outstanding = "$deferred";
102 $QUOTE_OUTSTANDING{$outstanding} = $QUOTED{$outstanding} = [
103 $name, $code, $captures
104 ];
105 return $deferred;
106}
107
108sub quoted_from_sub {
109 my ($sub) = @_;
110 $QUOTED{$sub};
111}
112
113sub unquote_sub {
114 my ($sub) = @_;
115 _unquote_all_outstanding;
116 $QUOTED{$sub}[3];
117}
118
1191;