1 package Method::Inliner;
4 use Text::Balanced qw(extract_bracketed);
7 sub slurp { do { local (@ARGV, $/) = $_[0]; <> } }
9 open my $out, '>', $_[1] or die "can't open $_[1]: $!";
10 print $out $_[0] or die "couldn't write to $_[1]: $!";
15 my @chunks = split /(^sub.*?^}$)/sm, slurp $file;
16 warn join "\n--\n", @chunks;
18 foreach my $chunk (@chunks) {
19 if (my ($name, $body) =
20 $chunk =~ /^sub (\S+) {\n(.*)\n}$/s
25 foreach my $chunk (@chunks) {
26 my ($me) = $chunk =~ /^sub.*{\n my \((\$\w+).*\) = \@_;\n/ or next;
27 my $meq = quotemeta $me;
31 while ($copy =~ s/^(.*?)${meq}->(\S+)(?=\()//s) {
32 my ($front, $name) = ($1, $2);
33 ((my $body), $rest) = extract_bracketed($copy, '()');
34 warn "spotted ${name} - ${body}";
37 s/^\(//, s/\)$// for $body;
38 $body = "${me}, ".$body;
39 $fixed .= $front.Sub::Quote::inlinify($code{$name}, $body);
41 $fixed .= $front.$me.'->'.$name.$body;
43 #warn $fixed; warn $rest;
46 $fixed .= $rest if $fixed;
47 warn $fixed if $fixed;
48 $chunk = $fixed if $fixed;
50 print join '', @chunks;