Perl Module 1:  An Introduction and Data Structures
by K. Yue (copyright 2000)
Revised: August 22, 2000

Background

Invented and maintained by Larry Wall.  One of the poster child of the Open Source movement.

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
#(print the content of quiz.html)

Perl Basics

Perl has three basic data types: 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.

Array (List) Data Types

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

Associativee Arrays (Hashes)

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.
 

Suggested Solution to Classwork 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";
}