CT320

CT320: Network and System Administration                

Fall 2015                

Perl                

CT320: Perl                

Perl Programming Lab                

The purpose of this assignment is to learn how write basic scripts using the Perl programming language.                 

Documentation                

Part 1 — Simple Perl Script                

Write a simple perl script called PerlCode that does the following:                 

  1. Starts with the line #! /usr/bin/perl -w
  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. Output a prompt to the console: “What is your name? “.
  5. Input your name into a variable called $name
  6. Strip the newline from $name
  7. Output 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, floating-point, 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. Create an array with eight integer values.
  3. Create an array with eight string values.
  4. Can you create an array in Perl with both integer and strings?
  5. Show how array access works by printing the third element of both arrays. (Which one is the third element, again?)
  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 three 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).
    • What’s the difference between for and foreach?
  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 strfry > strfry.man
  3. Open (read) the strfry.man file into an array of lines.
  4. Open (write) a file called strfry.man.bak and write the array of lines.
  5. Close both files.
  6. Open (append) strfry.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 /etc/profile.d.
  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 several 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                

Write a Perl script called pscript that imitates the bash script from the first Bash lab?.                 

  1. Accept the same commands: unprotect, list, delete, archive.
  2. All commands accept a directory.
  3. Handle the unprotect command through chmod 0777 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 — Credit                

Show your work to the TA.                 

Modified: 2015-10-17T23:19                 

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