patternMinor
Perl script for generating iTunes xml Metadata for MP3 CDs/DVDs
Viewed 0 times
scriptdvdscdsitunesperlxmlgeneratingformp3metadata
Problem
Many years ago, I wrote a script in Perl that was meant to create a cached metadata of MP3 files (in Apple's plist/XML format) which iTunes uses when you insert a CD/DVD full of MP3 files (the same would be true for AAC files, but I limited the scope of my project, due to lack of usable libraries back then).
Since my Perl is rusty, I would love to have some feedback and constructive criticism on how to make the script better, especially regarding readability.
Since the script is trivial, but long, I don't know how much I should put it here. The whole script is in one of my github repositories, if you want to see it.
An abridged version of it is:
```
#!/usr/bin/perl -W
# Copyright (C) 2009 Rogério Brito
# This program is Free Software and is distributed under the terms of
# the GNU General Public License version 2 or, at your option, any
# latter version.
use strict 'vars';
use warnings;
use utf8;
use Encode qw(encode decode);
use MP3::Tag;
# ==============================================================================
# Auxiliary functions for generation of the output
sub pr_header {
print FH
Current Version1
Compatible Version1
Applicationm3s v0.0
Burner Info$_[0]
Disc ID$_[1]
Disc Name$_[2]
tracks
EOF
}
sub pr_footer { print FH "\t\n\n\n"; }
# ==============================================================================
# Functions to generate the proper XML tags for iTunes.
sub pr_open_dict { print FH "\t\t\n"; }
sub pr_key { print FH "\t\t\t", my_utf8_encode($_[0]), ""; }
sub pr_string { print FH "", my_utf8_encode($_[0]), "\n"; }
sub pr_integer { print FH "", my_utf8_encode($_[0]), "\n"; }
sub pr_date { print FH "", my_utf8_encode($_[0]), "\n"; }
sub pr_boolean { print FH ($_[0])?"\n":"\n"; }
sub pr_close_dict { print FH "\t\t\n"; }
# ==============================================================================
# Auxiliary functions
sub gen_serial_no {
return sprintf("%04X%04X%04X%04X",
Since my Perl is rusty, I would love to have some feedback and constructive criticism on how to make the script better, especially regarding readability.
Since the script is trivial, but long, I don't know how much I should put it here. The whole script is in one of my github repositories, if you want to see it.
An abridged version of it is:
```
#!/usr/bin/perl -W
# Copyright (C) 2009 Rogério Brito
# This program is Free Software and is distributed under the terms of
# the GNU General Public License version 2 or, at your option, any
# latter version.
use strict 'vars';
use warnings;
use utf8;
use Encode qw(encode decode);
use MP3::Tag;
# ==============================================================================
# Auxiliary functions for generation of the output
sub pr_header {
print FH
Current Version1
Compatible Version1
Applicationm3s v0.0
Burner Info$_[0]
Disc ID$_[1]
Disc Name$_[2]
tracks
EOF
}
sub pr_footer { print FH "\t\n\n\n"; }
# ==============================================================================
# Functions to generate the proper XML tags for iTunes.
sub pr_open_dict { print FH "\t\t\n"; }
sub pr_key { print FH "\t\t\t", my_utf8_encode($_[0]), ""; }
sub pr_string { print FH "", my_utf8_encode($_[0]), "\n"; }
sub pr_integer { print FH "", my_utf8_encode($_[0]), "\n"; }
sub pr_date { print FH "", my_utf8_encode($_[0]), "\n"; }
sub pr_boolean { print FH ($_[0])?"\n":"\n"; }
sub pr_close_dict { print FH "\t\t\n"; }
# ==============================================================================
# Auxiliary functions
sub gen_serial_no {
return sprintf("%04X%04X%04X%04X",
Solution
Agree with what Vedran wrote. I'd also add some prototypes to go with that, so that perl can give you warnings or errors when you miss a parameter; e.g., something like
to specify that this sub takes two scalars as parameters.
There are some limitations to what you can do with perl's prototypes like this, but it's still useful.
sub foo($) {
}to specify that this sub takes two scalars as parameters.
There are some limitations to what you can do with perl's prototypes like this, but it's still useful.
Code Snippets
sub foo($$) {
}Context
StackExchange Code Review Q#29602, answer score: 2
Revisions (0)
No revisions yet.