Recitation 6 (R6)
CT 320: Network and Systems Administration
Group Project
Perl Programming Lab
The purpose of this assignment is to learn how write basic scripts using the
Perl programming language.
Part 1 – Simple Perl Script
Write a simple perl script called PerlCode
that does the following:
- Starts with the line
#! /usr/bin/perl
- Add comment lines with your names, email addresses, date, class, assignment.
- Add a comment line that shows the start of Part 1.
- Open the standard input and output streams.
- Outputs a prompt to the console: “What is your name? “.
- Inputs your name into a variable called
$name
- Strips the line feed from
$name
- Outputs 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
./PerlCode
and 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, float, 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 values and binary.
- 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.
- Declare an array with 8 integer values.
- Declare an array with 8 string values.
- Can you declare an array in Perl with both integer and strings?
- Show how array access works by printing the third element of both arrays.
- 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 3 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
splice
function 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
).
- Show the difference between C-style and Perl-style
for
loops.
- Show an
if
/else
and if
/elsif
/else
conditional
and the same for unless
.
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.man
file into an array of lines.
- Open (write) a file called
ls.man.bak
and write the array of lines.
- Close both files.
- Open (append)
ls.man.bak
and 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 parses a line of text into an array of strings.
- Write a function that concatenates an array of strings into a line of text.
- 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 . -print
command in /bin
.
- 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 a set of 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
Extend the Perl script to do something useful:
- Add a comment line that shows the start of Part 9.
- Write Perl code that matches the bash script from R1:
- Accept the same commands: unprotect, list, delete, archive.
- All commands accept a directory.
- Handle the unprotect command through
chmod 777
of 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 – Checkin
Check in your script to the R6 dropbox on RamCT.