Handout08-How to Program in Perl
Handout08-How to Program in Perl
Following the steps introduced in Handout1 to log onto the host metrostate.mooo.com, then try the content
discussed below. Please create a directory lab8 for the following exercises.
The Perl (Practical Extraction and Report Language) programming is mainly used for working with text.
Perl was created by Larry Wall, a computer programmer and author, in 1987.
Perl uses syntax and concepts from awk, sed, C, the Bourne Shell, Smalltalk, Lisp, and English.
Perl is a scripting language.
Perl is often used to scan and extract information from text files and generate reports based on that information.
Perl is also heavily used for system administration, software development, and general-purpose programming.
Since Perl has been implemented on many operating systems, Perl programming code is portable.
Perl is an informal, practical, robust, easy-to-use, efficient, complete, and down-and-dirty language that
supports procedural and object-oriented programming.
Perl is also introduced in Chapter 11 of your Linux book.
Perl can be invoked in the following ways:
1) From the command line (the –e option is used to lead a single argument program on the command line; the
perl program is enclosed within single quotation marks)
$ perl –e ‘print “Hello, Metro State!\n”’
Hello, Metro State!
$
2) Also from the command line, type perl to start an interactive Perl session, then end the session with Ctrl-D.
Pay attention the “;” at the end of each line.
$ perl
print "Hello, ";
print "Metro ";
print "State!";
print "\n";
^D
Hello, Metro State!
$
3) A Perl script is invoked by perl from the command line (the script itself is not executable)
$ cat > myperl2.pl
print “Hello, Metro State!\n”;
^D
$
$ perl myperl2.pl
Hello, Metro State!
$
4) To make a Perl script itself as an executable file. (This is the preferred mothed in this handout.)
$ cat > myperl3.pl
#!/usr/bin/perl -w
print “Hello, Metro State!\n”
^D
$
$ chmod +x myperl3.pl
$ ./myperl3.pl
Hello, Metro State!
$
As used in other scripting languages, the #! at the start of the first line of the file directs the shell to
pass the rest of the file to /usr/bin/perl for execution;
Option –w tells Perl to issue warning messages when it identifies potential errors in the code.
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 2 of 4
Handout 8 - How to Program in Perl
_____________________________________________________________________________________
Example 1: How to read a system file and then display its contents using a loop
$ cat > myperl4.pl
#!/usr/bin/perl -w
while ( $line = <> ) {
print $line;
}
^D
$
$ chmod +x myperl4.pl
$ ./myperl4.pl < /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
$
<> is a short writing for <STDIN>, the standard input.
Example 2: If you only want to display the information for “bash” related shells, myperl4.pl can be modified
as follow:
$ cat > myperl5.pl
#!/usr/bin/perl -w
while ( $line = <> ) {
if ( $line =~ /bash/ )
{
print $line;
}
}
^D
$ chmod +x myperl5.pl
$ ./myperl5.pl < /etc/shells
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
$
The =~ operator is used to search for the pattern “bash” in the string variable $line. If a match is found,
a true value will be returned.
By default, regular expressions are delimited by slashes (/), like “/bash/”.
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 3 of 4
Handout 8 - How to Program in Perl
_____________________________________________________________________________________
Example 3: If you want to modify the code in myperl5.pl to display all the lines that do not contain “bash,”
you need to use the operator “!~” instead. So, we can modify the code in myperl5.pl as follow:
$ cat > myperl6.pl
#!/usr/bin/perl -w
while ( $line = <> ) {
if ( $line !~ /bash/ )
{
print $line;
}
}
^D
$ chmod +x myperl6.pl
$ ./myperl6.pl < /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/usr/bin/tmux
/usr/bin/screen
$
Example 4: To substitute something with something else in a file, an “s” option can be used. In this example,
we will substitute “Metro State” with “CFS 264” in a testing file called “testfile”
$ cat > testfile
Hello, Metro State!
Good evening, Metro State!
Bye, Metro State!
^D
$
$ cat > myperl7.pl
#!/usr/bin/perl -w
while ( $line = <> ) {
$line =~ s/Metro State/CFS 264/;
print $line;
}
^D
$ chmod +x myperl7.pl
$ ./myperl7.pl < testfile
Hello, CFS 264!
Good evening, CFS 264!
Bye, CFS 264!
$
Example 5: This example will show you how to get two numbers from the keyboard and then to output the
summation of the two numbers.
$ cat > myperladd2.pl
#!/usr/bin/perl -w
print "Please enter the first number: ";
$first = <>;
print "Please enter the second number: ";
$second = <>;
$total = $first + $second;
chomp($first);
chomp($second);
print "$first + $second = $total\n";
^D
$ chmod +x myperladd2.pl
$ ./myperladd2.pl
Please enter the first number: 4
Please enter the second number: 6
4 + 6 = 10
$
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L
CFS264 ‐ Computer and Operating Systems Fundamentals II Page 4 of 4
Handout 8 - How to Program in Perl
_____________________________________________________________________________________
Example 6-1: This example will show you how to compare numbers
$ cat > if2.pl
#!/usr/bin/perl -w
print "Enter 28: ";
$entry = <>;
if ($entry == 28) { # use == for a numeric comparison
print "Thank you for entering 28.\n";
}
print "End.\n";
^D
$ chmod +x if2.pl
$ ./if2.pl
Enter 28: 28.0
Thank you for entering 28.
End.
$
Example 6-2: This example will show you how to compare strings
$ cat if2a.pl
#!/usr/bin/perl -w
print "Enter the word 'five': ";
$entry = <>;
chomp ($entry);
if ($entry eq "five") { # use eq for a string comparison
print "Thank you for entering 'five'.\n";
}
else {
print "Error: you either didn't enter 'five' or entered number 5.\n";
}
print "End.\n";
^D
$ chmod +x if2a.pl
$ ./if2a.pl
Enter the word 'five': five
Thank you for entering 'five'.
End.
$ ./if2a.pl
Enter the word 'five': 5
Error: you either didn't enter 'five' or entered number 5.
End.
_____________________________________________________________________________________________
Please complete the Linux Lab in D2L