Perl logo

Perl Refreshments ⌘

Treat - Calling Other Programs from Perl

Previously, we introduced the idea of calling other programs (shell scripts, or applications) from your Perl script. Using the 'system' command is how this is done - now we get to the gritty nitty.

Our first example used the 'exiftool' and 'egrep' programs to pull some metadata (ID3 tags) out of some .mp3 files.

A simple example would be:

exiftool somefile.mp3
     or for several files ...
exiftool *.mp3
     or
exiftool *.flac

Depending on whether any ID3 or metadata exists already in each file, you will end up with a screenful of data. Here's what I got from one of my files:

ExifTool Version Number         : 10.37
File Name                       : 01 Alignments.flac
Directory                       : .
File Size                       : 41 MB
File Modification Date/Time     : 2015:03:09 21:19:05-04:00
File Access Date/Time           : 2017:01:06 04:17:32-05:00
File Inode Change Date/Time     : 2016:10:18 11:33:29-04:00
File Permissions                : rw-r--r--
File Type                       : FLAC
File Type Extension             : flac
MIME Type                       : audio/flac
Block Size Min                  : 4096
Block Size Max                  : 4096
Frame Size Min                  : 16
Frame Size Max                  : 13150
Sample Rate                     : 44100
Channels                        : 2
Bits Per Sample                 : 16
Total Samples                   : 21053440
Picture Type                    : Front Cover
Picture MIME Type               : image/jpeg
Picture Description             :
Picture Width                   : 500
Picture Height                  : 500
Picture Bits Per Pixel          : 24
Picture Indexed Colors          : 0
Picture Length                  : 96724
Picture                         : (Binary data 96724 bytes, use -b option to extract)
Vendor                          : reference libFLAC 1.2.1 20070917
Notes                           : Medieval CUE Splitter (www.medieval.it)
Title                           : Alignments
Artist                          : Aes Dana
Album                           : Leylines
Date                            : 2009
Genre                           : Downtempo
Track Number                    : 01
Album Artist                    : Aes Dana
Duration                        : 0:07:57

Wow, that is a LOT of data. But most of it I don't need. I only want to pull out certain pieces as mentioned previously (Title, Artist, Album, Genre, Track Number, Duration).

Now we need the 'egrep' tool. 'grep' is a standard Unix / Mac program (grep actually stands for 'Globally search Regular Expression Print'). The 'e' represents an 'extended' version which has to be installed. It lets us include some other things in our search pattern.

So all the terms I want to pull out are available, so how do I tell egrep to just show me those? We use the 'or' feature. Between each pattern we want, we add a vertical bar (pipe) like this:

Hold on - how do we use these 2 commands together? Ah, another feature of most terminal shells is the ability to pipe the output of one program into the input of another program.

another feature of most terminal shells is the ability to pipe the output of one program into the input of another program.

And this allows us to do this:

exiftool somefile.flac | egrep 'Album|Artist|Genre|Title|Track|Duration' > somefile.txt

That starts with the 'exiftool' command to pull out data from 'somefile.flac'. Then 'egrep' lets us filter that output (now input) to look for particular pattern/s. All the data the 'exiftool' finds is then filtered by our 'egrep' command, which we have told to look only for particular patterns. Finally, we use the 'redirection' symbol to save the output to a file.

Voila, what we end up with is this in a text file:

Title                           : Alignments
Artist                          : Aes Dana
Album                           : Leylines
Genre                           : Downtempo
Track Number                    : 01
Album Artist                    : Aes Dana
Duration                        : 0:07:57

Saving this data will allow us to use this file later to extract that data when we build our web site. But if we have a dozen audio tracks, we will have a dozen text files to work through. It's my experience that the less time your program spends opening files and doing something with them, the better.

To do that, we use the 'cat' command (short for conCATenate). We use the redirection symbol again to put everything into just 1 file:

cat *.txt > all-data.txt

Now we have all the information about all our audio files in 1 file, which we can 'walk' through at any time to extract data. We will learn how that's done later.

But first we have to show you how to do this from your Perl script - after all that's what this is all about.


-30-