KAL

Lab #3

Introduction to Perl Programming

Perl means Practical Extraction and Report Language. Those of you who know awk, sed, and shell scripts will think Perl is very much like these.

Actually, Perl does much more than each of these. Perl syntax also looks like these as well as like C. This lab presumes only that you know C.

There is a good Perl book for beginners by Eleen Quigley, published by Prentice-Hall called Perl by Example. This lab uses the methods found in this book.

DIRECTIONS: Make a file for each of the following examples. Then execute it. Make sure each program works, and that you understand it. Try making small changes to each.

To run a Perl program:

Step 1: Create a file name.pl with the program in the example

Step 2: Type: perl name.pl

Example 1 - "Hello World" This is the standard first example for all UNIX programs

#!/usr/local/bin/perl

print "Hello, world!\n";


Example 1 Explanation:

Line 0 Normally, # would indicate a comment in Perl, but here it expresses the location of the Perl interpreter. Often, you don't really need this. In the following explanations, this line (line 0) won't be mentioned.

Line 1 The string "Hello, world!" is printed on the screen. Notice that each statement end with a ";".

Example 2 - Perl Scalar Variables (Numbers or strings) begin with $ followed by a letter and then letters, digits or underscores.

#!/usr/local/bin/perl

$Num1 = 3;

$Num2 = $Num1++;

$Pi = 3.14;

$Famous_Name = "Karen A. Lemone";

print "The numbers are: $Num1, and $Num2, and $Pi \n";

print "$Famous_Name \n";


Example 2 Explanation

Line 1 The integer 3 is assigned to $Num1.

Line 2 $Num2 is assigned the value 3; then $Num1 is incremented

Line 3 $Pi is assigned 3.14

Line 4 $Famous_Name is assigned the string "Karen A. Lemone"

Line 5 The numbers are printed.

Line 6 The string is printed.

Example 3 Arrays - begin with @ - can contain mixed data types

#!/usr/local/bin/perl

$Num1 = 3;

@Num1 = (1, "one", 1.5, $Num1);

print "The array is: @Num1 \n";

print "The number is $Num1 \n";

$Num1[0] = 5;

print "The array is: @Num1 \n";


Example 3 Explanation

Line 1 The scalar $Num1 is assigned 3.

Line 2 The array $Num1 is assigned the 5 values shown. $Num1[0] is assigned the number 1, $Num1[1] is assigned the string "one", etc.

Line 3 The values of the array @Num1 are printed.

Line 4 The value of the scalar $Num1 is printed.

Line 5 The first entry in the array is changed to a 5.

Line 6 The values of the array @Num1 are printed.

Example 4 - Some built-in operators. chop

 

#!/usr/local/bin/perl

#chop - chops the last character of each element in the array.

#Usually chop is used to get rid of the end-of-line character

# when reading input.

@Num1 = (101, "one", 1.51, 1);

chop (@Num1);

print "CHOP - The array chopped off is: @Num1 \n";


Example 4 - chop Explanation

Line 1 Array Num1 is assigned the 4 values shown.

Line 2 Looking at each element of Num1 as a character, the rightmost character is chopped off, leaving the array as [10, on, 1.5, _ ].


grep

#grep - matches the first argument with the 2nd

@Num2 = (10111, "one", 1.52, 1);

$count = grep(/1/, @Num2);#grep returns the number of elements with a match

@newNum2 = grep(/1/,@Num2); #grep returns a new array with just the

#matched elements

print "GREP - The number of times 1 was found: $count \n";

print "GREP - The array with just the elements matching 1 is: @newNum2 \n";


push

#push - pushes the 2nd arg onto the end of the array in the 1st arg

@Num3 = (101, "one", 1.51, 1);

push(@Num3, hi);

print "PUSH - The array with hi pushed on the end is: @Num3 \n";


pop

#pop - pops the last array element

@Num4 = (101, "one", 1.51, 1);

pop(@Num4);

print "POP - The array with the last item popped off is: @Num1 \n";

$gotcha = pop(@Num4);

print "The popped off item is: $gotcha \n";


join

#join - joins elements of a list using the delimiter which is the 1st arg

@Names = ("Karen", "A", "Lemone");

print "JOIN - ", join(":", "Karen", "A", "Lemone"), "\n";


shift

#shift - shifts off the first element of an array

@Names = ("Karen", "A", "Lemone");

print "SHIFT - ", shift(@Names), "\n";


splice

#splice replaces the elems starting with index (the 2nd arg) in an

# array (the 1st arg)

@Num5 = (101, "one", 1.51, 1);

print "SPLICE - ", splice(@Num5, 2, 2, "two", 2), "\n";

print "@Num5 \n";


split

#split - splits up a string (the 2nd arg) by the delimiter (the 1st arg)

$mynames = "Karen A. Lemone";

@name = split(' ',$mynames);

print "SPLIT - My first name is $name[0] \n";


reverse

#!/usr/local/bin/perl

#reverse - reverses the elements in an array

@Num6 = (101, "one", 1.51, 1);

print "Array is: @Num6 \n";

print "Array reversed is:", reverse(@Num6), "\n";


if

# if: an expression is executed if another expression is true

$Two = 2;

print "true\n" if $Two ==2;


substr

# substr(string1, offset, length) - a substring at $offset

# for $length bytes

$Myname = "Karen A. Lemone":

print substr($Myname,9,6), "\n";


length

# Length returns the number of characters in an expression

$Myname = "Karen A. Lemone";

print length($Myname),"\n";


offset

Note Offset doesn't work with all versions of Perl.

# Offset(string1, string2) returns the offset of string2 in string1

$Myname = "Karen A. Lemone";

print offset($Myname, "Lemone"),"\n";

If you have gotten this far, try the examples at:

Intro to Perl

or

SV-PAL

Nik Silver's Perl Manual

Prewritten Perl Scripts

Perl Manual


Send questions and comments to: Karen Lemone

What do these buttons mean?