#!/usr/local/bin/perl # Dilip Barman, http://www.cs.unc.edu/Courses/wwwp-f97/members/barman # Sep 20 1997 # # dbAddRecord2.pl.cgi: As part of my perl programming project, described # at www.cs.unc.edu/Courses/wwwp-f97/members/barman/perlProjectProposal.html, # this perl cgi-bin script is called from the HTML form perlDatabase2.html # to add a new record. We have all the field values (title, artist, etc.) # as STDIN values. # We start off by including a nice library file by Steven Brenner which does # lots of things for us require "/afs/cs.unc.edu/project/courses/190-25f97/programs/perl/lib/cgi-lib.pl"; # GLOBAL VARIABLES $FALSE = 0; $TRUE = 1; # $DBFILENAME = "../musicDatabase/musicDB2"; # name of DBM database file $DBFILENAME = "/afs/cs.unc.edu/project/courses/190-25f97/members/barman/musicDatabase/musicDB2"; $ROACCESS = "0555"; # Read-Only open: RWX, so U=101, G=101, O=101 $RWACCESS = "0777"; # Read-Write open: 111 111 111 # Remember to set AFS directory permissions so system:anyuser (a web # client, for example) has write privilege to this directory -- # fs setat ../musicDatabase system:anyuser write # $RECDELIMSTART = "\*\$\["; # delimit record entries: *$[ ... ]$* # $RECDELIMEND = "\]\$\*"; # Sep 21: to simplify this, I'm going to use the tilde character $NOTITLESTRING = "*Required Name of Recording*"; # Take this from the main HTML file - so if title has this value, that # means user hasn't typed a title in # * * * P R O C E S S U S E R I N P U T * * * &ReadParse(*input); # Store input as attrib-value keys in variable input $title = $input{"title"}; $artist = $input{"artist"}; $genre = $input{"genre"}; $format = $input{"format"}; $time = $input{"time"}; $comments = $input{"comments"}; # If any field has a tilde (~) in it, remove all the tildes $title =~ s/\~//ge; $artist =~ s/\~//ge; $time =~ s/\~//ge; $comments =~ s/\~//ge; # * * * T O P L E V E L H T M L S T U F F * * * # Okay, since this is run from the web, we have to first send back # http header information - and there is a library function to do it print &PrintHeader; print ""; print "\nDilip's Music Database: Add Entry"; print "\n

Music Database: Add Entry

"; # print &PrintVariables(%input); # * * * D A T A B A S E L O G I C * * * # Basic validation - if a title is provided, add the record if ( ($title eq "") || ($title eq $NOTITLESTRING) ) { print "\n

ERROR: No Title

"; print "\n

You did not specify a title. The title is"; print "\n(the only field that is) required."; } else { &addToDB; } # * * * W R A P U P H T M L * * * # Give option to return to try again or return home print "\n

Go back to main screen"; print "\n
"; print "\n

Read"; print "\n"; print "\nproject proposal or system"; print "\n"; print "\nimplementation notes

"; print "\n

Return to Dilip's course "; print""; print "home page

"; print "\n
Background courtesy of "; print "\nMoyra's Web Jewels
"; # End HTML page print "\n "; # * * * S U B R O U T I N E S * * * sub addToDB { # # addToDB subroutine # Sep 21 1997 # We have at least a title, and we add it to the database. # Because this is transaction-based and not a persistent # session, we must open and close the DB for each request. # So this subroutine opens the database (whose name is # defined above as global variable $DBFILENAME), creates # a string representing the value fields, and writes the # record. # # We use two more global variables, RECDELIMSTART and RECDELIMEND, # to build the values. $title is the key and since DBM databases # are 1-key 1-value structured, we build the value as a concatenation # of the individual values with ~ (tilde) as delimiter. For example: # # key="The Eagle's Greatest Hits" # value="Eagles~Rock~CD~48~A great CD from early 70s!" # # There are other ways to have designed this, instead of a rigid # positional way, but again this is one quick way for purposes of # a short project (which isn't so short!). In any case, we build # a positional string with artist, format (again, in real life we # would just store a byte for each of the formats), time, and comments. # if (-e $DBFILENAME) {print "\n$DBFILENAME exists";} # else {print "\n$DBFILENAME does not exist";} $del = "~"; $newEntry = $artist . $del . $genre . $del . $format . $del . $time . $del . $comments; # Open the file and check if key already exists. Otherwise, write record! dbmopen(%RECORDS, $DBFILENAME, $RWACCESS); $newkey = TRUE; # assume it's a new entry # while ( (($key, $value)=each(%RECORDS)) && ($newkey) ) # Above didn't work - ?? So I'm being ineff. and looping all the way # while ( (($key, $value) = each(%RECORDS)) ) # { # if ($key eq $title) {$newkey = FALSE;} # print "\nLooking at title $key"; # } foreach $key (keys %RECORDS) { if ($key eq $title) {$newkey = $FALSE;} } if ($newkey) { $RECORDS{$title} = $newEntry; print "\n

Added New Title \"$title\"

"; print "\n

Artist: $artist"; print "\n
Genre: $genre"; print "\n
Format: $format"; print "\n
Time: $time"; print "\n
Comments: $comments"; print "\n

Database record: \"$newEntry\""; } else { print "\n

ERROR: Duplicate key!

"; print "\n

Data not added; there already is an entry"; print "\nwith title \"$title\"."; } # end else - i.e., duplicate key dbmclose(%RECORDS); } # End addToDB subroutine