CT320

Three headers follow: ! Long name of class !! Semester !!! R 6, replaced by the name of the current page.

CT320: Network and System Administration

Fall 2013

R 6

Links to the various pages for this class:

Wish I could do this: * Schedule

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:

  1. Starts with the line #! /usr/bin/perl
  2. Add comment lines with your names, email addresses, date, class, assignment.
  3. Add a comment line that shows the start of Part 1.
  4. Open the standard input and output streams.
  5. Outputs a prompt to the console: “What is your name? “.
  6. Inputs your name into a variable called $name
  7. Strips the line feed from $name
  8. Outputs the message “Hello <name>!” to the console.

Part 2 – Scalar, Math, Strings, Operators

Extend the Perl script to do simple math and string operations using scalars:

  1. Add a comment line that shows the start of Part 2.
  2. Declare three scalars and assign them integer values between 0 and 20.
  3. Declare three scalars and assign them real values between 0.0 and 10.0.
  4. Show examples of integer, float, and mixed arithmetic using (+, -, *, /, %, **).
  5. Declare three scalars and assign them strings with between 5 and 15 characters.
  6. Show examples of string concatenation (.) and string replication (x).
  7. Show examples of numerical (==, !=, <, >) and string (lt, gt, eq, ne) comparison.
  8. Declare two scalars and assign them hexadecimal values and binary.
  9. Show examples of the binary operators (<<, >>, &, |, ^).
  10. Show conversion of hexadecimal and binary numbers to decimal and vice versa.

Part 3 – Arrays

Extend the Perl script to do array manipulation:

  1. Add a comment line that shows the start of Part 3.
  2. Declare an array with 8 integer values.
  3. Declare an array with 8 string values.
  4. Can you declare an array in Perl with both integer and strings?
  5. Show how array access works by printing the third element of both arrays.
  6. Print the size of both arrays.
  7. Push an entry onto both arrays.
  8. Print the size of the arrays again.
  9. Pop an entry from both arrays into a scalar.
  10. Print the size of the arrays again.
  11. Create another array with 3 strings and add it to the string array.
  12. 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:

  1. Add a comment line that shows the start of Part 4.
  2. To demonstrate loop structures, print out some value from the arrays in Part 3.
  3. Show loop structures (for/foreach, while, until).
  4. Show the difference between C-style and Perl-style for loops.
  5. 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:

  1. Add a comment line that shows the start of Part 5.
  2. Create a file by redirecting a manpage, e.g.: man ls > ls.man
  3. Open (read) the ls.man file into an array of lines.
  4. Open (write) a file called ls.man.bak and write the array of lines.
  5. Close both files.
  6. Open (append) ls.man.bak and append a line of text.
  7. Close the file again.

Part 6 – Functions

Extend the Perl script to show examples of functions:

  1. Add a comment line that shows the start of Part 6.
  2. Write a function that parses a line of text into an array of strings.
  3. Write a function that concatenates an array of strings into a line of text.
  4. 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:

  1. Add a comment line that shows the start of Part 7.
  2. Write code that calls the find . -print command in /bin.
  3. Store the output of the command in an array of strings.

Part 8 – Regular Expressions

Extend the Perl script to use regular expressions:

  1. Add a comment line that shows the start of Part 8.
  2. Create a set of strings with characters, digits, and special characters.
  3. Write code that uses regular expressions to match different substrings.
  4. Write code that uses regular expressions to substitute different substrings.
  5. Show that your regular expressions work by printing the results.

Part 9 – A Useful Script

Extend the Perl script to do something useful:

  1. Add a comment line that shows the start of Part 9.
  2. Write Perl code that matches the bash script from R1:
    1. Accept the same commands: unprotect, list, delete, archive.
    2. All commands accept a directory.
    3. Handle the unprotect command through chmod 777 of all files.
    4. Handle the list command as a non-recursive complete listing.
    5. Handle the delete command by deleting the specified directory.
    6. Handle the archive command by copying to /tmp/archive.
    7. Test as before by creating a directory structure.
    8. Optional: Have the Perl script create the directory structure.

Part 10 – Checkin

Check in your script to the R6 dropbox on RamCT.

Modified: 2013-10-10T16:41

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building