Topic: Introduction to Perl and Python.

Some questions to answer, topics to consider:

  1. Describe lists and hash tables in Perl, lists and dictionaries in Python.
  2. How does a CGI program read and parse its data in Perl and Python.
  3. Explain the basic CGI.pm functionality (brief overview of what and how).
  4. Provide a Python implementation of the simple Perl vending machine shown in class.

Please give examples.

The simple vending machine was presented in week two notes:

#!/usr/bin/perl

%contents = ("snickers" => 70, "pretzels" => 50, "soda" => 75);

print "Welcome to the vending machine!\nready> ";

while ($input = <STDIN>) {
  chop($input);
  if ($input eq "quit") { last; }
  elsif ($input =~ /^add\s/) {
    ($add, $what, $price) = split(/ /, $input, 3);
    $contents{$what} = $price;

  } elsif ($input =~ /^\s*show\s+all\s*$/i) {
    foreach $key (keys %contents) {
      $price = sprintf("%4.2f", $contents{$key} / 100);
      print $key, " --> \$", $price, "\n";
    }
  } elsif ($contents{$input}) {
    $price = sprintf("%4.2f", $contents{$input} / 100);
    print $input, " costs \$$price\n";
  } else {
    print "I don't understand $input, sorry.\n";
    print "Try: show all, add <product> <cents>, <product>, or quit\n";
  }
  print "ready> ";
}