report variables unformatted on trailing #
[barcat.git] / barcat
diff --git a/barcat b/barcat
index 189c8ff72b3688ce8fcd8984313d30503da199d4..a86f74f4343bf3676e8b771a1e7ea4804a55c831 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -365,8 +365,8 @@ sub show_stat {
        }
        if (@order) {
                my $total = sum @order;
-               my $fmt = '${sum;color(1)} total in ${count} values';
-               $fmt .= ' over ${lines} lines' if @order != @lines;
+               my $fmt = '${sum;color(1)} total in ${count#} values';
+               $fmt .= ' over ${lines#} lines' if @order != @lines;
                $fmt .= " ($_)" for $opt{report} || ();
                print varfmt($fmt, {
                        sum => $total,
@@ -383,11 +383,22 @@ sub show_stat {
 
 sub varfmt {
        my ($fmt, $vars) = @_;
-       $fmt =~ s[\$\{ (\w+) (?<cmd>; (?: [^{}]+ | \{.*?\} )*)? \}]{
-               local $_ = $vars->{$1}; #TODO //
-               $_ = $opt{'value-format'}->($_) if $opt{reformat};
-               eval $+{cmd} if $+{cmd}; #TODO $@
-               $_;
+       $fmt =~ s[\$\{( (?: [^{}]++ | \{(?1)\} )+ )\}]{
+               my ($name, $cmd) = split /\s*;/, $1, 2;
+               my $format = $name !~ s/\h*\#// && $opt{reformat};
+               local $_ = $vars->{$name};
+               if (defined) {
+                       $_ = $opt{'value-format'}->($_) if $format;
+                       if ($cmd) {
+                               eval $cmd;
+                               warn "Error in \$$name report: $@" if $@;
+                       }
+                       $_;
+               }
+               else {
+                       warn "Unknown variable \$$name in report\n";
+                       "\$$name";
+               }
        }eg;
        return $fmt;
 }