5 sub _clean_eval { eval $_[0] }
11 our @EXPORT = qw(quote_sub unquote_sub quoted_from_sub);
13 our %QUOTE_OUTSTANDING;
18 my ($from, $captures, $indent) = @_;
23 or die "capture key should start with \@, \% or \$: $_";
24 (' ' x $indent).qq{my ${_} = ${1}{${from}->{${\perlstring $_}}};\n};
30 my ($code, $args, $extra, $local) = @_;
31 my $do = 'do { '.($extra||'');
32 if (my ($code_args, $body) = $code =~ / +my \(([^)]+)\) = \@_;(.*)$/s) {
33 if ($code_args eq $args) {
36 $do.'my ('.$code_args.') = ('.$args.'); '.$body.' }';
39 $do.($local ? 'local ' : '').'@_ = ('.$args.'); '.$code.' }';
43 sub _unquote_all_outstanding {
44 return unless %QUOTE_OUTSTANDING;
45 my ($assembled_code, @assembled_captures, @localize_these) = '';
46 # we sort the keys in order to make debugging more predictable
47 foreach my $outstanding (sort keys %QUOTE_OUTSTANDING) {
48 my ($name, $code, $captures) = @{$QUOTE_OUTSTANDING{$outstanding}};
50 push @localize_these, $name if $name;
54 if (keys %$captures) {
55 my $ass_cap_count = @assembled_captures;
56 $make_sub .= capture_unroll("\$_[1][${ass_cap_count}]", $captures, 2);
57 push @assembled_captures, $captures;
60 my $o_quoted = perlstring $outstanding;
63 # disable the 'variable $x will not stay shared' warning since
64 # we're not letting it escape from this scope anyway so there's
65 # nothing trying to share it
66 ? " no warnings 'closure';\n sub ${name} {\n"
67 : " \$Sub::Quote::QUOTED{${o_quoted}}[3] = sub {\n"
70 $make_sub .= " }".($name ? '' : ';')."\n";
72 $make_sub .= " \$Sub::Quote::QUOTED{${o_quoted}}[3] = \\&${name}\n";
75 $assembled_code .= $make_sub;
77 my $debug_code = $assembled_code;
78 if (@localize_these) {
80 "# localizing: ".join(', ', @localize_these)."\n"
82 $assembled_code = join("\n",
83 (map { "local *${_};" } @localize_these),
84 'eval '.perlstring($assembled_code).'; die $@ if $@;'
87 $ENV{SUB_QUOTE_DEBUG} && warn $assembled_code;
89 $assembled_code .= "\n1;";
90 unless (_clean_eval $assembled_code, \@assembled_captures) {
91 die "Eval went very, very wrong:\n\n${debug_code}\n\n$@";
93 $ENV{SUB_QUOTE_DEBUG} && warn $debug_code;
94 %QUOTE_OUTSTANDING = ();
98 # HOLY DWIMMERY, BATMAN!
99 # $name => $code => \%captures => \%options
100 # $name => $code => \%captures
102 # $code => \%captures => \%options
105 (ref($_[-1]) eq 'HASH' and ref($_[-2]) eq 'HASH')
108 my $captures = pop if ref($_[-1]) eq 'HASH';
109 undef($captures) if $captures && !keys %$captures;
113 my $deferred = defer_sub +($options->{no_install} ? undef : $name) => sub {
114 unquote_sub($outstanding);
116 $outstanding = "$deferred";
117 $QUOTE_OUTSTANDING{$outstanding} = $QUOTED{$outstanding} = [
118 $name, $code, $captures
123 sub quoted_from_sub {
130 _unquote_all_outstanding;
138 Sub::Quote - efficient generation of subroutines via string eval
144 use Sub::Quote qw(quote_sub unquote_sub quoted_from_sub);
146 quote_sub 'Silly::kitty', q{ print "meow" };
148 quote_sub 'Silly::doggy', q{ print "woof" };
150 my $sound; $$sound = 0;
152 quote_sub 'Silly::dagron',
153 q{ print ++$$sound % 2 ? 'burninate' : 'roar' },
154 { '$sound' => \$sound };
160 Silly->dagron; # burninate
161 Silly->dagron; # roar
162 Silly->dagron; # burninate
166 This package provides performant ways to generate subroutines from strings.
172 my $coderef = quote_sub 'Foo:bar', q{ print $x++ . "\n" }, { '$x' => \0 };
174 Arguments: ?$name, $code, ?\%captures, ?\%options
176 C<$name> is the subroutine where the coderef will be installed.
178 C<$code> is a string that will be turned into code.
180 C<\%captures> is a hashref of variables that will be made available to the
181 code. See the L</SYNOPSIS>'s C<Silly::dagron> for an example using captures.
189 B<Boolean>. Set this option to not install the generated coderef into the
190 passed subroutine name on undefer.
196 my $coderef = unquote_sub $sub;
198 Forcibly replace subroutine with actual code. Note that for performance
199 reasons all quoted subs declared so far will be globally unquoted/parsed in
200 a single eval. This means that if you have a syntax error in one of your
201 quoted subs you may find out when some other sub is unquoted.
203 If $sub is not a quoted sub, this is a no-op.
205 =head2 quoted_from_sub
207 my $data = quoted_from_sub $sub;
209 my ($name, $code, $captures, $compiled_sub) = @$data;
211 Returns original arguments to quote_sub, plus the compiled version if this
212 sub has already been unquoted.
214 Note that $sub can be either the original quoted version or the compiled
215 version for convenience.
219 my $prelude = capture_unroll {
224 my $inlined_code = inlinify q{
227 print $x + $y . "\n";
228 }, '$x, $y', $prelude;
230 Takes a string of code, a string of arguments, a string of code which acts as a
231 "prelude", and a B<Boolean> representing whether or not to localize the
234 =head2 capture_unroll
236 my $prelude = capture_unroll {
241 Generates a snippet of code which is suitable to be used as a prelude for
242 L</inlinify>. The keys are the names of the variables and the values are (duh)
243 the values. Note that references work as values.