From 3f12a3221f4b752a93dc4bb3780708b0201ddd6e Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 1 Aug 2015 18:46:36 +0300 Subject: [PATCH] Simplify code --- lib/App/MusicExpo.pm | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/App/MusicExpo.pm b/lib/App/MusicExpo.pm index 3bdef01..a55b53c 100644 --- a/lib/App/MusicExpo.pm +++ b/lib/App/MusicExpo.pm @@ -119,6 +119,16 @@ sub mp4info{ }; } +my %info = ( + '.flac' => \&flacinfo, + '.mp3' => \&mp3info, + '.ogg' => \&vorbisinfo, + '.oga' => \&vorbisinfo, + '.mp4' => \&mp4info, + '.aac' => \&mp4info, + '.m4a' => \&mp4info, +); + sub normalizer{ "$_[0]|".(stat $_[0])[9] } @@ -126,22 +136,17 @@ sub normalizer{ sub make_fragment{ join '-', map { lc =~ y/a-z0-9/_/csr } @_ } sub run { + my %info = %info; if ($cache) { tie my %cache, 'DB_File', $cache, O_RDWR|O_CREAT, 0644; - memoize $_, NORMALIZER => \&normalizer, LIST_CACHE => 'MERGE', SCALAR_CACHE => [HASH => \%cache] for qw/flacinfo mp3info vorbisinfo mp4info/; + $info{$_} = memoize $info{$_}, INSTALL => undef, NORMALIZER => \&normalizer, LIST_CACHE => 'FAULT', SCALAR_CACHE => [HASH => \%cache] for keys %info; } my %files; for my $file (@ARGV) { - my $info; - $info = thaw scalar flacinfo $file if $file =~ /\.flac$/i; - $info = thaw scalar mp3info $file if $file =~ /\.mp3$/i; - $info = thaw scalar vorbisinfo $file if $file =~ /\.og(?:g|a)$/i; - $info = thaw scalar mp4info $file if $file =~ /\.mp4|\.aac|\.m4a$/i; - next unless defined $info; - my $basename = fileparse $file, '.flac', '.mp3', '.ogg', '.oga', '.mp4', '.aac', '.m4a'; + my ($basename, undef, $suffix) = fileparse $file, keys %info; $files{$basename} //= []; - push @{$files{$basename}}, $info; + push @{$files{$basename}}, thaw scalar $info{$suffix}->($file); } my $ht=HTML::Template::Compiled->new( -- 2.39.2