вопрос к perl-guru. можно ли упростить этот код?
#! /usr/bin/perl -w
use Getopt::Long;
my $format=q(%artist% - %title%);
my $player;
GetOptions('player=s' => \$player, 'format=s' => \$format);
exit(1) if (!defined($player));
my $cmd = qq(dbus-send --print-reply --type=method_call --dest=org.mpris.MediaPlayer2.$player /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' |);
my %tags;
open (DBUS, $cmd) or die "Can't run $cmd: $!\n";
while (<DBUS>) {
my $tag;
my $value;
chomp;
s/^\s*(.*)\s*$/$1/;
if (m/dict entry\(/) {
while (<DBUS>) {
chomp;
s/^\s*(.*)\s*$/$1/;
last if (m/\)$/);
if (m/^string.*\"mpris:/) {
last;
} elsif (m/^string.*\"xesam:(.*)\"$/) {
$tag = $1;
next;
} elsif (m/^variant.*string.*\"(.*)\"$/) {
$value = $1;
next;
} elsif (m/^variant.*array/) {
while (<DBUS>) {
chomp;
s/^\s*(.*)\s*$/$1/;
last if (m/\]$/);
if (m/^string.*\"(.*)\"$/) {
$value .= $1 . ", ";
next;
}
}
$value =~ s/\,\s*$//;
}
}
$tags{$tag} = $value if defined($value);
}
}
close (DBUS);
# print results
if (defined($tags{'artist'})) {
$format =~ s/\%artist\%/$tags{'artist'}/g;
} else {
$format =~ s/\%artist\%//g;
}
if (defined($tags{'title'})) {
$format =~ s/\%title\%/$tags{'title'}/g;
} else {
$format =~ s/\%title\%//g;
}
if (defined($tags{'album'})) {
$format =~ s/\%album\%/$tags{'album'}/g;
} else {
$format =~ s/\%album\%//g;
}
if (defined($tags{'genre'})) {
$format =~ s/\%genre\%/$tags{'genre'}/g;
} else {
$format =~ s/\%genre\%//g;
}
print $format, "\n";