Linux Know-How provides a collection of introductory texts on often needed Linux skills.


Perl

perl

Powerful and widely used scripting language, very popular among gurus. Perl looks cryptic yet it is quite straight-forward if you need to achieve simple tasks. Think of perl as a swiss-army knife for simple programming. Perl's syntax parallels that of the "C" language. Excellent implementation of the perl interpreter is available for MS Windows so you code can be cross-platform. Here is how Eric Reymond (famous Linux guru) describes perl: "Perl, of course, is the 800-pound gorilla of modern scripting languages. It has largely replaced shell as the scripting language of choice for system administrators, thanks partly to its comprehensive set of UNIX library and system calls, and partly to the huge collection of Perl modules built by a very active Perl community. The language is commonly estimated to be the CGI language behind about 85% of the ``live'' content on the Net. Larry Wall, its creator, is rightly considered one of the most important leaders in the Open Source community, and often ranks third behind Linus Torvalds and Richard Stallman in the current pantheon of hacker demigods."

How do I write a simple perl script?

I may use pico (or any other text editor of my choice) to type in a simple perl script:

pico try_perl

The example script below does nothing useful, except illustrates some features of perl:

#!/usr/bin/perl -w

# a stupid example perl program

# the lines starting with # are comments except for the first line

# names of scalar variables start with $

$a=2;

$b=3;

# each instruction ends with a semicolon, like in "c"

print $a**$b,"\n";

$hello_world='Hello World';

print $hello_world,"\n";

system "ls";

The first line tells the shell how to execute my text file. The option "-w" causes perl to print some additional warnings, etc. that may be useful for debugging your script. The next 3 lines (starting with #) are comments. The following lines are almost self explanatory: I assign some values to two variables ($a and $b), put $a to power $b and print the result. The "\n" prints a new line, just like in the "c" programming language. Then I define another variable to contain the string "Hello World" and, in the next line, I print it to the screen. Finally, I execute the local operating system command "ls", which on Linux prints the listing of the current directory content. Really stupid script.

After saving the file, I make it executable:

chmod a+x try_perl

Now, I can run the script by typing:

./try_perl

Here is somewhat longer script that does something very simple yet useful to me. I take a long text file which is generated by a data acquisition system. I need to erase every other line (or so) so that the file can be crammed into MS Excel (as required):

#!/usr/bin/perl -w

# Create a text file containing a selection of lines from an original file. This is needed

# so that data for manual postprocessing are fewer.

#

# Prompt the user for the filename, and the selection of lines to preserve in the output.

print STDOUT "Enter the filename: ";

chomp($infile=<STDIN>);

open(INFILE,"<$infile"); # open the file for reading.

print STDOUT "Enter the number of initial lines to preserve: ";

chomp($iskip=<STDIN>); # the first lines may contain column headings etc

print STDOUT "Enter the skip: ";

chomp($skip=<STDIN>);

#

# The name of the output file is created automatically on the basis of the

# input file and the selection of lines. It is always of type CSV, so preserve is so.

$outfile=$infile.'-pro'.$iskip.'-'.$skip.'.csv'; #glue strings together using the dot operator.

open(OUTFILE,">$outfile"); # open file for writing.

#

# write the "initial" lines to the output file.

for($a=0;$a<$iskip;$a++) {

$line=<INFILE>;

print OUTFILE $line;

}

#

# do the rest of the file

$c=0;$w=0;$skip++;

while($line=<INFILE>){

$c++;

if(!($c%$skip)) { #use % for remainder of integer division

print OUTFILE $line;

$w++;

}

}

#

close(OUTFILE);

print STDOUT "Read Lines: ", $c+$iskip," Wrote lines: ", $w+$iskip,"\n";


Last Update: 2010-12-16