- tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644 if $caching;
- memoize 'flacinfo', NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] if $caching;
- memoize 'mp3info' , NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] if $caching;
-
- my @files;
- for my $file (@ARGV) {
- push @files, thaw flacinfo $file if $file =~ /.flac$/i;
- push @files, thaw mp3info $file if $file =~ /.mp3$/i;
- }
-
- my $ht=HTML::Template::Compiled->new(
- default_escape => 'HTML',
- $template eq '' ? (scalarref => \$default_template) : (filename => $template),
- );
- $ht->param(files=>[sort { $a->{title} cmp $b->{title} } @files]);
- print $ht->output;
+ my %info = %info;
+ if ($cache) {
+ tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644;
+ $info{$_} = memoize $info{$_}, INSTALL => undef, NORMALIZER => \&normalizer, LIST_CACHE => 'FAULT', SCALAR_CACHE => [HASH => \%cache] for keys %info;
+ }
+
+ my %files;
+ for my $file (@ARGV) {
+ my ($basename, undef, $suffix) = fileparse $file, keys %info;
+ $files{$basename} //= [];
+ push @{$files{$basename}}, thaw scalar $info{$suffix}->($file);
+ }
+
+ my $ht=HTML::Template::Compiled->new(
+ default_escape => 'HTML',
+ global_vars => 2,
+ $template eq '' ? (scalarref => \$default_template) : (filename => $template),
+ );
+
+ my @files;
+ for (sort keys %files) {
+ my @versions = @{$files{$_}};
+ my %entry = (formats => [], map { $_ => '?' } qw/title artist year album tracknumber tracktotal genre/);
+ for my $ver (@versions) {
+ push @{$entry{formats}}, {format => $ver->{format}, file => $ver->{file}};
+ for my $key (keys %$ver) {
+ $entry{$key} = $ver->{$key} if $ver->{$key} && $ver->{$key} ne '?';
+ }
+ }
+ delete $entry{$_} for qw/format file/;
+ $entry{fragment} = make_fragment @entry{qw/artist title/};
+ push @files, \%entry
+ }
+
+ @files = sort { $a->{title} cmp $b->{title} } @files;
+ $ht->param(files => \@files, prefix => $prefix);
+ print $ht->output;