Developer's Daily Perl Education
  front page | java | perl | unix | dev directory | web log
   
Front Page
Perl
Education
Perl Education Articles
   
 
A simple cat program, written with Perl
 

Introduction

The other day I was working on a Windows95 laptop, 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:

You can also display multiple files, one after the other, like this: In Unix, you can redirect these files to another file like this: This last example is what I was trying to do on my Windows95 system.
 

First, a little 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...
 

Show me the 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:  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 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:

On a DOS/Windows system, you'd invoke the command like this or preferably create a DOS batch file, named cat.bat, to do the dirty work of invoking Perl for you.

 
That's it, that's all

As usual with Perl, There's More Than One Way To Do It.  This was just the first approach that came to mind, and I think it reads well.  We're always open to suggestions, though, so if you think you have a better way to do it, write us a quick note.  We'll incorporate your thoughts with this article as quickly as we're able.

Best wishes!
 
   


Read the BLOG

Copyright © 1998 DevDaily Interactive, Inc.
All Rights Reserved.