CT320: Perl                

Perl Programming Lab                
The purpose of this assignment is to learn how write basic scripts using the Perl programming language.                 
Documentation                
- Make sure that the
perldocprogram is installed:sudo apt install perl-doc - To find out about the
sqrtfunction:perldoc -f sqrt - General Perl syntax:
man perlsyn
Part 1 — Simple Perl Script                
Write a simple perl script called PerlCode that does the following:
                
- Starts with the line
#! /usr/bin/perl -w - Next line must be:
use 5.16.1; - Add comment lines with your names, email addresses, date, class, assignment.
- Add a comment line that shows the start of Part 1.
- Output a prompt to the console: “What is your name? “.
- Input your name into a variable called
$name - Strip the newline from
$name - Output the message “Hello, <name>!” to the console.
- Figure out what version of perl you are running with
perl --version. - Make the script executable via
chmod +x PerlCode - Run the script using the command
./PerlCodeand make sure it works.
Part 2 — Scalar, Math, Strings, Operators                
Extend the Perl script to do simple math and string operations using scalars:                 
- Add a comment line that shows the start of Part 2.
- Declare three scalars and assign them integer values between 0 and 20.
- Declare three scalars and assign them real values between 0.0 and 10.0.
- Show examples of integer, floating-point, and mixed arithmetic using
(
+,-,*,/,%,**). - Declare three scalars and assign them strings with between 5 and 15 characters.
- Show examples of string concatenation (
.) and string replication (x). - Show examples of numerical (
==,!=,<,>) and string (lt,gt,eq,ne) comparison. - Declare two scalars and assign them hexadecimal and binary values.
- Show examples of the binary operators (
<<,>>,&,|,^). - Show conversion of hexadecimal and binary numbers to decimal and vice versa.
Part 3 — Arrays                
Extend the Perl script to do array manipulation:                 
- Add a comment line that shows the start of Part 3.
- Create an array with eight integer values.
- Create an array with eight string values.
- Can you create an array in Perl with both integer and strings?
- Show how array access works by printing the third element of both arrays. (Which one is the third element, again?)
- Print the size of both arrays.
- Push an entry onto both arrays.
- Print the size of the arrays again.
- Pop an entry from both arrays into a scalar.
- Print the size of the arrays again.
- Create another array with three strings and add it to the string array.
- How do you delete the third entry in the integer and string arrays?
Hint: Look at the
splicefunction in Perl.
Part 4 — Control                
Extend the Perl script to show control structures:                 
- Add a comment line that shows the start of Part 4.
- To demonstrate loop structures, print out some value from the arrays in Part 3.
- Show loop structures (
for/foreach,while,until).- What’s the difference between
forandforeach?
- What’s the difference between
- Show the difference between C-style and Perl-style
forloops. - Show an
if/elseandif/elsif/elseconditional and the same forunless.
Part 5 — Files                
Extend the Perl script to show file input and output:                 
- Add a comment line that shows the start of Part 5.
- Create a file by redirecting a manpage, e.g.:
man ls > ls.man - Open (read) the
ls.manfile into an array of lines. - Open (write) a file called
ls.man.bakand write the array of lines. - Close both files.
- Open (append)
ls.man.bakand append a line of text. - Close the file again.
Part 6 — Functions                
Extend the Perl script to show examples of functions:                 
- Add a comment line that shows the start of Part 6.
- Write a function that split a line of text, by whitespace character(s), into an array of strings.
- Write a function that concatenates an array of strings, into a line of text, separated by vertical bars.
- Use the functions to count the words and lines in the file
/etc/resolv.conf.
Part 7 — System Calls                
Extend the Perl script to call system functions:                 
- Add a comment line that shows the start of Part 7.
- Write code that calls the
find . -printcommand in/etc/profile.d. - Store the output of the command in an array of strings.
Part 8 — Regular Expressions                
Extend the Perl script to use regular expressions:                 
- Add a comment line that shows the start of Part 8.
- Create several strings with characters, digits, and special characters.
- Write code that uses regular expressions to match different substrings.
- Write code that uses regular expressions to substitute different substrings.
- Show that your regular expressions work by printing the results.
Part 9 — A Useful Script                
Write a Perl script called pscript that imitates the bash script from
the Bash I lab.
                
- Accept the same commands: unprotect, list, delete, archive.
- All commands accept a directory.
- Handle the unprotect command through
chmod 0777of all files. - Handle the list command as a non-recursive complete listing.
- Handle the delete command by deleting the specified directory.
- Handle the archive command by copying to
/tmp/archive. - Test as before by creating a directory structure.
- Optional: Have the Perl script create the directory structure.
Part 10 — Credit                
Show your work to the TA.