[Chapter 18] 18.3 Converting Shell Programs to Perl
Author: Randal Schwartz, Tom Christiansen & Larry Wall
Date: 1999-07-02T01:36:06Z

#

| Chapter 18 Converting Other Languages to Perl |

## 18.3 Converting Shell Programs to Perl

Heh. Thought there'd be a shell-to-Perl translator, eh?

Nope. Many have asked for such a beast, but the real problem is that most of what a shell script does is not done by the shell. Most shell scripts spend practically all of their time calling separate programs to extract pieces of strings, compare numbers, concatenate files, remove directories, and so forth and so on. Converting such a script to Perl would either require understanding the operation of each of the called utilities, or leave Perl calling each of the utilities, which gains nothing.

So, the best you can do is stare at a shell script, figure out what it does, and start from scratch with Perl. Of course, you can do a quick-and-dirty transliteration, by putting major portions of the original script inside `system()` calls or backquotes. You might be able to replace some of the operations with native Perl: for example, replace `system(rm fred)` with `unlink(fred)`, or a shell `for` loop with a Perl `for` loop. But generally you'll find it's a bit like converting a COBOL program into C (with about the same reduction in the number of characters and increase in illegibility).

18.2 Converting sed Programs to Perl |  | 18.4 Exercise