How to Use Perl to Write UNIX Scripts

Overview: Perl is a high-level, object-oriented programming language that supports many programming concepts found in languages such as C and C++. Moreover, for many Unix system administrators, Perl is the tool they depend the most on when it comes to getting the job done.

This post introduces the Perl language, the benefits of the language, how it fits into the Unix system administrator’s tool chest, and, most importantly, how to put Perl to use by writing and troubleshooting Perl scripts.

The real use of Perl

The original development goals for Perl, an acronym for Practical Extraction and Report Language, focused on developing an interpretive scripting language for file and text manipulation facilities.

From this it has found a wide base of users who find it particularly well-suited for tasks involving quick prototyping and system management.

Perl also used in many cases as a bridge between systems by providing programming tools for bringing together a wide variety of applications, making Perl especially popular with programmers, system administrators, mathematicians, journalists, and even business managers.

Open Distribution Policy

Perl’s open distribution policy and large support base of fellow users have also helped the popular rise of the language and its interpreters designed to process Perl syntax on various platforms.

But it is various Unix systems, such as FreeBSD, Linux, and Solaris, that have really taken in the standard Perl library modules and documentation.
The following exercise lets you try out a little Perl to get started.

How to write Hello World in Perl – Use a text editor to create a file called with the following contents:

#!/usr/bin/perl -w
<span style="color: #000000;"># Classic "Hello World" as done with Perl</span>
print "Hello World!\n";

After saving the file and quitting the editor, change the permissions on the file and run the following script:

% chmod 755
 % ./
 Hello World!

How It Perl Works

Like a shell script, the first line in this example tells the Unix system how to run this script, using the Perl interpreter. In most Unix systems, such as the Mac OS X system the scripts in this post were written and tested on, the Perl interpreter can be found at /usr/bin/perl; however, this is not always the case.

If, when executing this example script, an error such as “command not found” or “bad interpreter: No such file or directory” was displayed instead of the expected “Hello World!” chances are the Perl interpreter is located elsewhere on the system.

To quickly locate or verify where the Perl interpreter is, use the which command as follows:

<strong>which perl</strong>

In any case, besides providing a location for where the Perl interpreter can be found, this line allows options for how to invoke Perl:

#!/usr/bin/perl -w

In the case of the “Hello World” example, the -w switch communicates to the Perl interpreter that it would provide an additional level of reporting of warnings along with critical warnings or fatal syntax errors that are encountered while processing the script into machine code.

Another method for making sure Perl provides a robust collection of warning messages, and one recommended for the latest version of the language, is to use the following method, instead of the -w switch:

use warnings;

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.