sub _generate_trigger {
my ($self, $name, $obj, $value, $trigger) = @_;
if (my $quoted = quoted_from_sub($trigger)) {
- die "Captures? ARGH!" if $quoted->[2];
my $code = $quoted->[1];
+ my $at_ = 'local @_ = ('.join(', ', $obj, $value).');';
+ if (my $captures = $quoted->[2]) {
+ my $cap_name = qq{\$trigger_captures_for_${name}};
+ $self->{captures}->{$cap_name} = \$captures;
+ return "do {\n".' '.$at_."\n"
+ .Sub::Quote::capture_unroll($cap_name, $captures, 6)
+ ." ${code}\n }";
+ }
return 'do { local @_ = ('.join(', ', $obj, $value).'); '.$code.' }';
}
my $cap_name = qq{\$trigger_for_${name}};
our %QUOTED;
+sub capture_unroll {
+ my ($from, $captures, $indent) = @_;
+ join(
+ '',
+ map {
+ /^([\@\%\$])/
+ or die "capture key should start with \@, \% or \$: $_";
+ (' ' x $indent).qq{my ${_} = ${1}{${from}->{${\perlstring $_}}};\n};
+ } keys %$captures
+ );
+}
+
sub _unquote_all_outstanding {
return unless %QUOTE_OUTSTANDING;
my ($assembled_code, @assembled_captures, @localize_these) = '';
if (keys %$captures) {
my $ass_cap_count = @assembled_captures;
- $make_sub .= join(
- "\n",
- map {
- /^([\@\%\$])/
- or die "capture key should start with \@, \% or \$: $_";
- qq{ my ${_} = ${1}{\$_[1][${ass_cap_count}]{${\perlstring $_}}};\n};
- } keys %$captures
- );
+ $make_sub .= capture_unroll("\$_[1][${ass_cap_count}]", $captures, 2);
push @assembled_captures, $captures;
}
run_for 'Bar';
+{
+ package Baz;
+
+ use Sub::Quote;
+ use Class::Tiny;
+
+ has one => (
+ is => 'rw',
+ trigger => quote_sub(q{ push @{$tr}, $_[1] }, { '$tr' => \\@::tr })
+ );
+}
+
+run_for 'Baz';
+
done_testing;