--- /dev/null
+use strictures 2;
+use autodie;
+
+my ($cmd, $file, @args) = @ARGV;
+
+my $input = do { local (@ARGV, $/) = $file; <> };
+
+close STDOUT;
+open STDOUT, '>', $file;
+
+open $out, '|-', $cmd, @args;
+
+print $out $input;
+
+close $out;
--- /dev/null
+use strictures 2;
+use Data::Dumper::Compact;
+use SQL::Abstract::Formatter;
+use SQL::Abstract::ExtraClauses;
+
+my $ddc = Data::Dumper::Compact->new(max_width => 72);
+my $sqla = SQL::Abstract::ExtraClauses->new(
+ lazy_join_sql_parts => 1,
+);
+my $sqlaf = SQL::Abstract::Formatter->new(max_width => 72);
+
+while (1) {
+ my $line = <STDIN>;
+ exit 0 unless defined $line;
+ print $line;
+ last if $line =~ /\A__END__/;
+}
+
+my $slurp = do { local $/; <STDIN> };
+
+my ($expr_re, $aqt_re, $query_re) =
+ map qr/(?sm:(.*?)( +)(# ${_}\n)(?:\n|(.*?)\n\n))/, qw(expr aqt query);
+
+sub reformat {
+ my ($thing, $indent) = @_;
+ my $thing_ddc = $ddc->dump($thing);
+ $thing_ddc =~ s/^/$indent/mg;
+ return $thing_ddc;
+}
+
+sub seval { eval('+('.$_[0].')') or die "seval: $_[0]: $@" }
+
+while ($slurp =~ m/\G$expr_re/gc) {
+ my ($pre, $indent, $type, $expr_str) = ($1, $2, $3, $4);
+ print $pre.$indent.$type;
+ print reformat(my $expr = seval($expr_str), $indent);
+ print "\n";
+ if ($slurp =~ m/\G$aqt_re/gc) {
+ my ($apre, $aindent, $atype) = ($1, $2, $3);
+ print $apre.$aindent.$atype;
+ print reformat($sqla->expand_expr($expr), $aindent);
+ print "\n";
+ }
+ die unless $slurp =~ m/\G$query_re/g;
+ my ($qpre, $qindent, $qtype) = ($1, $2, $3);
+ print $qpre.$qindent.$qtype;
+ my ($sql, @bind) = $sqla->render_statement($expr);
+ my $fsql = $sqlaf->format(@$sql);
+ $fsql =~ s/^/$indent/mg;
+ print $fsql."\n";
+ print reformat(\@bind, $qindent);
+ print "\n";
+}
+
+$slurp =~ /\G(.*)$/sm;
+
+print $1;