Introduction

The other day I was working on a Windows computer, and I really missed my good friend, the Unix cat command. I was trying to merge a bunch of files together, and couldn't think of how to do it easily on Windows system (the DOS type command certainly didn't do it). Easily distracted, I set off to write a cat command with Perl that I could use on any system.

A little background - how cat should work

For those who are not familiar with Unix, the cat command stands for concatenate. You can use cat to display the contents of a file named myfile by typing:

cat myfile

You can also display multiple files, one after the other, like this:

cat file1 file2 file3

In Unix, you can redirect these files to another file like this:

cat file1 file2 file3 > file4

This last example is what I was trying to do on my Windows system.

First, the algorithm

In this first article, we'll keep it simple. Our algorithm will work like this:

  1. We'll create a foreach loop that will run one loop for each filename the user types on the command line.
  2. Then, we'll open each file in succession, print the file contents, close the file, and move on to the next file.
  3. If an error occurs, we'll print a warning message to STDERR, but we won't abort the entire process; we'll just try to read the next file.

That's the simple plan. On to the code...

The Perl cat program source code

Without any further ado, Listing 1 shows what these steps look like in Perl code:

#!/usr/bin/perl -w
#
#  PROGRAM: cat

#  for each arg, open the file and print it

   FILE: foreach (@ARGV) {

      open(FILE, $_) || ((warn "Can't open file $_\n"), next FILE);

      while (<FILE>) {
         print;
      }
      close(FILE);
   }

Listing 1 (above): Our simple cat program opens each file in succession, and prints the file contents. A warning message is printed to STDERR if a problem occurred trying to open a file.

Quick discussion

The only thing I've done here that's very tricky is (a) the use of the warn statement and (b) the FILE label. The warn function just prints my message out to STDERR, but unlike the die function, the program goes on. If a user wants to cat out four files, for example, and the second file doesn't work for some reason, I don't want the program to come to a complete stop. I'd rather just put an error message on STDERR and keep working on the rest of the files.

I use the FILE label as a reference for the next statement. I use it because I think it makes the program easier to read, which is helpful when I come back to look at this program some time in the future. I've seen other people that really don't like to use these types of labels, but in my opinion, I think the code is easier to read.

Running the Perl cat program

Assuming you wanted to open three files named moe, larry, and curly, you would run the program on a Unix system like this:

cat more larry curly

On a DOS/Windows system, you'd invoke the command like this:

perl cat more larry curly

or preferably create a DOS batch file, named cat.bat, to do the dirty work of invoking Perl for you.

What's Related


devdaily logo