Perl logo

Perl Refreshments ⌘

Treat - Calling Other Programs from Perl

I recently worked on a project that I discovered could be done easier by using a couple of other programs. However, my main script was done in Perl (and command-line at that). I wanted to see how far I could get.

While Perl is an excellent tool for most text-parsing projects (which my project included a lot of), there are times when another tool would work better. This is where you need to be able to call other 'tools' or programs from your own.

Not a particularly exotic thing to to do - most, if not all, languages let you do that somehow. With my project the tools I needed were other applications I had installed already on my Mac.

I ended up using 'exiftool', 'cat', 'ftp', 'curl', 'egrep', 'mysql', plus a shell script I had acquired (legally).

Some of these are already installed - cat, ftp and curl come with any Mac. They are 'command-line' tools, so can only be run from a Terminal window.

To see all the tools available to a Mac user, take a look here.

'mysql' is the command-line client to manage the MySQL database I use. It comes with the MySQL / MariaDB package I have.

'exiftool' is a real nugget. Most images, audio, and video files come packed with metadata - data 'about' the file. This tool extracts the information out, and for some types of file, lets you edit the data. If you have a chance to use it, you will be amazed. If you have ever done some work with audio files, you might have heard of 'ID3' tags.

Before continuing let me briefly explain my project, so you have an idea where this is going...

I have over 3,000 CDs by over 1000 artists, all in digital format on my Mac. I am making a web-enabled site to make them available. Some of the data for artists and cds is already in a local database on my Mac. My project is to reproduce this data on a hosted server.

Not a difficult job, if the local database was complete - but it isn't. Short story is I still have a LOT of work to do.

One of the jobs I wanted to do was get the playing time for each track. Yes, I could go through a media player and somehow grab the data, but that would be an incredibly boring and time-consuming job.

Enter 'exiftool'. Running the command on 1 mp3 file gives me over 40 lines of data!

Running it in a directory using 'exiftool *.mp3' would give the same amount of data on each mp3 file.

Since this is command-line operation, I can redirect the output to a file, which I can then use later.

But, all I want out of all that are only a few pieces of information: Title, Artist, Album, Genre, Track Number, Duration. These are all things I can use when building my database. Pulling out just that data requires the use of 'egrep'.

And again, since this is CL (Command Line), I can pipe the output of exiftool into egrep and save it to a file to only show me certain things:

exiftool "$f" | egrep 'Album|Artist|Genre|Title|^Track|Duration' > $newf.txt;

I end up with a file like this:

Title                           : Aftermath # 01
Artist                          : Aes Dana
Album                           : Aftermath 2.0 (Archives of Peace)
Genre                           : Ambient
Track                           : 1/8
Duration                        : 0:07:49 (approx)

Once I have done this for each track, I want to put them all together in 1 file (to make it easier to manage later). To do that a simple 'cat *.txt > all.txt' does the job.

All the above steps are included in my Perl script. The magic is using the 'system' command.

This is a Perl command which lets you run any command or program on the current computer - a very powerful command.

Follow my adventure here.


-30-