[an error occurred while processing this directive]

Perl

Menu

In-Class Assignment #12

For this final ICA of the semester, you have three different options. Complete any one of them successfully for full credit. Complete any two of them successfully for a bonus half credit. Complete all three of them successfully for a bonus full credit.

Option A - Database Access

While I can't get a real database set up for the class to use, we can simulate one using CSV (comma-separated values) files. I have downloaded and installed the DBD::CSV module, which let's us use the DBI module to pretend that CSV files are really database tables. Download or copy the file ~lallip/perl/ica12A.pl to use as a framework. Change the my $dir = line to refer to a subdirectory of your own directory in which to store the tables.

Create and prepare several SQL statements: one to insert a new student into the students table, one to insert a new homework grade into the homeworks table, one to insert a new ica grade into the icas table, one select statement to obtain the student's id number given his grade, and finally one (or more, depending on the level of knowledge you have about SQL) select statement(s) that will allow you to find the current course grade of a student given the student's name.

Continuously read a line of input from the user. The user will input a line in one of four formats:

  • new <name>
  • hw <name> <num> <grade>
  • ica <name> <num> <grade>
  • <name>

For each line of input above, your program should add the new student to students, add the new hw grade to homeworks, add the new ica grade to icas, and display the student's course grade, respectively. Course grade is 90% of the HW avg plus ICA points.

Sample I/O

new Paul
hw Paul 1 90
ica Paul 1 1
new John
hw Paul 2 100
ica Paul 2 0.5
ica Paul 3 1
hw John 1 80
Paul
Paul's grade is 88
John
John's grade is 72

Option B - Inter-Process Communication

Write two programs. The first, child.pl will be used to see how quickly Perl can increment a number. It takes two numbers on the command line. The first is the starting number. The second is the number of seconds to run. Before starting, set up two signal handlers - one to print the current value of the number and exit the program after the alarm has gone off, and one to just print the current value of the incrementing number when the signal HUP is received. Once the signal handlers are set, enter an infinite loop in which the number starting with the base is just incremented over and over.

The second program, parent.pl, will fork off three separate children processes, each time printing the PID of the child just created. The three children will execute child.pl with arguments of (0, 30), (1000, 10), and (500, 20), respectively. After creating the children, the parent will enter a loop getting input from the user. The user will enter a PID of one of the chidren, and the parent will send a SIGHUP to that child. When the user enters done, the parent will then wait for all the children. Each time one ends, it should print the pid of the child that just ended. When all children have ended, the parent should also end.

Option C - Moose Objects

Download or copy the file ~lallip/perl/moose/pizza.t. This contains a series of tests for a module named Pizza. Write this class using Moose. Your class must have two attributes: size and toppings. Size is a string, either 8-cut, 12-cut, or 16-cut, passed in via the argument list when the object is created. Toppings is an array, that could contain any or all of pepperoni, mushrooms, olives, pineapple, or buffalo chicken. Your class has the following public methods:

  • new - constructor, takes a hash, containing keys size and (possibly) toppings
  • size - sets a new size of the pizza (if given), and returns the size of the pizza
  • add_topping - takes one topping and adds it to the topping list.
  • toppings - returns a reference to an array of all toppings on the pizza.
  • price - returns the total cost for this pizza

Keep in mind Moose will write some of these methods for you. When calculating the total price, use the following cost structure:

  • 8-cut: $5
  • 12-cut: $7.50
  • 16-cut: $10
  • Pepperoni: $1
  • Mushrooms: 50¢
  • Olives: 75¢
  • Pineapple: 50¢
  • Buffalo Chicken: $1.50

Submission Instructions

To submit, run ~lallip/public/submit.pl on solaris.remote.cs.rpi.edu and follow the prompts. Remember that only your final submission is looked at. So if you choose to complete all options for the bonus points, make sure your final submission contains all of ica12A.pl, parent.pl, child.pl, and Pizza.pm.

Your final submission is due at 4pm today, Thursday, May 6, 2010.

Perl Quotes
Perl Quotes