5 sub _clean_eval { eval $_[0] }
11 our @EXPORT = qw(quote_sub unquote_sub);
13 our %QUOTE_OUTSTANDING;
17 sub _unquote_all_outstanding {
18 return unless %QUOTE_OUTSTANDING;
19 my ($assembled_code, @assembled_captures, @localize_these) = '';
20 foreach my $outstanding (keys %QUOTE_OUTSTANDING) {
21 my ($name, $code, $captures) = @{$QUOTE_OUTSTANDING{$outstanding}};
23 push @localize_these, $name if $name;
27 if (keys %$captures) {
28 my $ass_cap_count = @assembled_captures;
33 or die "capture key should start with \@, \% or \$: $_";
34 qq{ my ${_} = ${1}{\$_[1][${ass_cap_count}]{${\perlstring $_}}};\n};
37 push @assembled_captures, $captures;
40 my $o_quoted = perlstring $outstanding;
43 # disable the 'variable $x will not stay shared' warning since
44 # we're not letting it escape from this scope anyway so there's
45 # nothing trying to share it
46 ? " no warnings 'closure';\n sub ${name} {\n"
47 : " \$Sub::Quote::QUOTED{${o_quoted}}[3] = sub {\n"
50 $make_sub .= " }".($name ? '' : ';')."\n";
52 $make_sub .= " \$Sub::Quote::QUOTED{${o_quoted}}[3] = \\&${name}\n";
55 $assembled_code .= $make_sub;
57 if (@localize_these) {
58 $ENV{SUB_QUOTE_DEBUG} && warn
59 "# localizing: ".join(', ', @localize_these)."\n"
61 $assembled_code = join("\n",
62 (map { "local *${_};" } @localize_these),
63 'eval '.perlstring $assembled_code
66 $ENV{SUB_QUOTE_DEBUG} && warn $assembled_code;
68 _clean_eval $assembled_code, \@assembled_captures;
70 die "Eval went very, very wrong:\n\n${assembled_code}\n\n$@";
72 %QUOTE_OUTSTANDING = ();
76 # HOLY DWIMMERY, BATMAN!
77 # $name => $code => \%captures => \%options
78 # $name => $code => \%captures
80 # $code => \%captures => \%options
83 (ref($_[-1]) eq 'HASH' and ref($_[-2]) eq 'HASH')
86 my $captures = pop if ref($_[-1]) eq 'HASH';
90 my $deferred = defer_sub +($options->{no_install} ? undef : $name) => sub {
91 unquote_sub($outstanding);
93 $outstanding = "$deferred";
94 $QUOTE_OUTSTANDING{$outstanding} = $QUOTED{$outstanding} = [
95 $name, $code, $captures
100 sub quoted_from_sub {
107 _unquote_all_outstanding;