The autotag.pl application begins with some initialization routines.Listing 3. Initialization
The SEARCH_ALL constant is what I use when the user wants to search for a word everywhere -- track names, artist names, etc. I made it a constant in case anyone wants to change it to something else, but it could have been hard-coded as "all" as well.
%freedb_searches hash maps FreeDB fields to information about
them, including ID3v2 tag elements. For instance, it says that what
FreeDB calls "artist" is known as "TPE1" in an MP3 tag. The "abbrev" field
in the hash entry is used to define command-line switches, so later I can
-artist switch that can be abbreviated to
-i based on the
%info2freedb hash maps FreeDB fields common across all tracks in a
disc to ID3v2 fields. These are not the fields in
is a different mapping that says that "cdname" and "artists," also known as
"TALB" and "TPE1," respectively, are the same for all tracks in an album.
%supported_frames hash and the
@supported_frames list will be used
to figure out what ID3v2 tag elements I support. I could have generated
the hash from the list instead of getting the list from the hash, but I
feel the difference is irrelevant. The supported frames are used for mass
tagging and when writing ID3v2 tags (I only modify the supported frames).
Finally, I create a
Term::ReadLine object for user input throughout the application.
Next, I initialize the
AppConfig options. Bear with me, this is
Yes, all that code just initialized the command-line options. With
AppConfig, those options can be used and modified throughout the program;
there are many benefits to using
AppConfig that are outside the scope of
this article (see Resources for more information
Also, I use the entries in the
%freedb_searches hash to create the
appropriate configuration options, which makes life easier for the user
and for the programmer. I can also use the entries in the
%freedb_searches hash to create the appropriate configuration options.
After loading a configuration file, if the user specified it, I populate the character replacement and bad character arrays with a sensible default.
Finally, I handle the
-help switch. Note how the default values for
various options are printed inside the help text, using variable
interpolation. This makes for a very readable help message. I always
update my help message right after I added a feature, and sometimes even
before. I believe that help should be synchronized with the functionality
of the program, otherwise the program is confusing and the help is
misleading. The autotag.pl program in particular needs more documentation
-- a POD-style documentation would be nice, and that may be in place by the
time you read this article. POD documentation is a part of the script, so
downloading autotag.pl (see Resources) will
include the POD documentation if I have written it already.