Perl Module 1
Introduction and Data Structures
K. Yue, copyright @2002

1. Introduction and Background

2. Getting Started

#!/usr/bin/perl
#!/usr/local/bin/perl
#!/opt/gnu/bin/perl

Exercise 1:

Type in the following Perl program and execute it:

#!/opt/gnu/bin/perl
#  My first Perl program.
print "Hello, World.\n";

$perl -e 'perl "Hello World.\n";'

$perl -ne 'print;' quiz.html

3. Perl Basics

Perl has three basic data types:

4. Scalar Data Types

Example:

$str ="Hello.";
$num = 5;
$num = "abcde";

Numeric Data Types

String Data Types

Perl's string literals may be:

Example:

$x = "there!";
$y = "Hi, $x"; #  $y is 'Hi, there!'
$z = "Hi, \$x"; #  $z is 'Hi, $x'

# stringsubstitution.pl
$x = "there!";
$xx = "somewhere!";
$y = "Hi, $x";  #  $y is 'Hi, there!'
$z = "Hi, $xx"; #  $z is 'Hi, somewhere!'
$w = "Hi, ${x}x"; #  $w is 'Hi, there!x'

Example:

chop $str;
# remove the last character of $str and return the character.
substr("abcdefg", 3, 2); # return "de"

Example:  context.pl: executing the code will print 20.

$x = 12;    # an integer
$y = "8";   # a string
$z = $x+$y;
print $z, "\n";

Example: (stringconstant.pl)

$msg = <<_LSTR_;
This is a long string.
In more than one line.
_LSTR_

Example: (Echo.pl)

# Read a line from the standard input file.
$line = <STDIN>;
chop($line);
print "A line: <<$line>>\n";

Exercise 2:

Write a piece of Perl code to read in strings (one string per line) from the standard input file.  For each string, the code print the string, ==, and the string again in a line.

5. Array Data Type (List)

@num = (1, 3, 5, 7, 2, 8);
@str = ('one', 'three', 'two', 'eight');
$num[0] = 8; # change $num[0] from 1 to 8.

@num[1,2] = (3,4);  # $num[1] = 3; $num[2] = 4;
@num[1,2] = @num[2,1]; # swap $num[1] and $num[2];
@num[1,2] = @num[3,3];
#   $num[1] = $num[3]; $num[2] = $num[3];
($num[1], $num[2]) = ($num[2], $num[1]);
#   swap $num[1] and $num[2];
@num = (1,2,3,4,5)[3,2,1]; # @num = (4,3,2);
($first, @num) = @num; # remove the first element of @num
(@num, $last) = @num;
#   unexpected result.
$length = @num;
#   $length gets the length of the array @num.
($length) = @num;  # $length = $num[0];

(1,3, 5..9)  # same as (1, 3, 5, 6, 7, 8, 9); Some important list operations: Exercise 3:

Find out and correct all errors of the following code.

# Read in lines and print out in sorted orders.
$a = <STDIN>
sort(@a)
print @a

Exercise 4:

Write a Perl program to read in and print out a list of strings.  After all strings are read, the list of strings are printed out again first in the read in order and then in the reverse order.

6. Hashes (Associative Arrays)

Example:

$population{'San Antonio'} = 2200900;
print $population{'Houston'};
# print the population of Houston.
$population{'Houston'} += 9999;      

# population of Houston increased by 9999.
$Population{'Dallas'} = 3245672;
$population{'Houston'} = 4434545;

%Population = ('Dallas', 3245672, 'Houston', 4434545,
                     'San Antonio', 2200900);
if ($ENV{DISPLAY})
{   print "X is (probably) running.\n";
}
keys(%a)
# return a list of all current keys in %a.
# Note that the order of the keys returned are arbitrary. values(%a)
# return a list of all current values in %a.
# Note that the order of the values returned are arbitrary.

each(%a)
# Iterate over %a and return the current key-value pair as
# a list.  If %a becomes empty, return an empty list.

delete($a{$b})
# remove the key-value pair with key $b from %a.

Example:

# Print all key-value pairs of %a.
while (($key, $value) = each(%a)) {
   print "Value of $key = $value\n";
}

Exercise 5:

Write a Perl program to read in an input file with one word per line and print all these words in ascending string order.  A word may appear more than once in the input file but your program should only print out every word once.
 

7. Suggestion Solutions to Exercises

2. For example:

# exercise2.pl
while ($line = <STDIN>)
{  chop $line;
   print "$line==$line\n";
}

3. For example:

#!/opt/gnu/bin/perl
# Read in lines and print out in sorted orders.
@a = <STDIN>;
@a = sort(@a);
print @a;
 

4. For example:

# exercise3.pl
@all_lines = ();
while ($line = <STDIN>)
{  push (@all_lines, $line);
}
print @all_lines;
while ($line = pop(@all_lines))
{  print $line;
}

Alternatively:

# exercise3alt.pl
@all_lines = <STDIN>;
print @all_lines;
@all_lines = reverse(@all_lines);
print @all_lines;

5. For example:

#!/opt/gnu/bin/perl
#  exercise4.pl of module 1
@lines = <STDIN>;
foreach $line (@lines)
{   chop($line);
    $wordcounts{$line}++;
}

@words = keys(%wordcounts);
@words = sort(@words);
foreach $word (@words)
{  print "$word ==> $wordcounts{$word}\n";
}