O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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).

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required