#!/share/bin/perl -w use strict; use DBI; #Global variable declarations my $data_source = "DBI:ODBC:epalfinder"; my $dbh; my $searchHandle; my $insertHandle; my %interesttable; my @interestlist = ( "Abseiling", "Aerobics", "AmateurDramatics", "Archery", "ArmyReserve", "Astronomy", "Athletics", "AFL", "Ballet", "Baseball", "Basketball", "Biking", "BirdWatching", "BodyBuilding", "Buddhism", "BungeeJumping", "Camping", "Cars", "Cats", "Caving", "ChoralSociety", "Christianity", "ClassicalMusic", "Clubs", "Cookery", "Computing", "CountryMusic", "Craftwork", "Cricket", "Democrat", "Dieting", "DiscoDance", "Diving", "Dogs", "Drinking", "ECommerce", "Environment", "Fashion", "Fishing", "FolkMusic", "Gambling", "Gliding", "Golf", "GourmetFood", "Guitar", "GridIron", "Gymnastics", "HangGliding", "Hiking", "HinduReligion", "Hockey", "Horses", "IRC", "Islam", "Jazz", "Judaism", "Kayaking", "Labour", "Liberal", "LocalHistory", "LocalPolitics", "Marxism", "ModelMaking", "MotorBikes", "MotorRacing", "MovieMaking", "Movies", "MP3Music", "Naturism", "Novels", "Olympics", "Opera", "Orienteering", "Painting", "Parenting", "Philosophy", "Photography", "Piano", "Poetry", "PopMusic", "Psychology", "RockClimbing", "RolePlayingGames", "Rollerblades", "Running", "RugbyLeague", "Scouts&Guides", "Skiing", "Snowboarding", "Soccer", "Socialism", "SteamTrains", "StockMarket", "Sunbathing", "Surfing", "Swimming", "Tennis", "Theatre", "TVCartoons", "TVCommedies", "TVSoaps", "WebDesign", "WineTasting", "Yoga", "Zen" ); sub initialize { my ($id, $interest); $id = 0; foreach $interest (@interestlist) { $interesttable{$interest} = $id; $id++; } $dbh = DBI->connect($data_source) || die "Couldn't connect to db\n"; $searchHandle = $dbh->prepare("SELECT * FROM epal");; $insertHandle = $dbh->prepare("INSERT INTO epal VALUES ( ?, ?, ?, ?, ?, ?, ?, ? ) "); } sub owntype { my $type; my @allowed; while(1) { print "Your type : "; $type = ; chomp $type; @allowed = qw( MALE FEMALE EPERSON); if(grep { /$type/i } @allowed) { last; } print "Unrecognized type; allowed values are @allowed\n"; } return uc $type; } sub getinterests { my @list; my @nlist; my $temp; while(1) { print "Enter five personal interests : "; $temp = ; @list = split /\s/, $temp; my $length; $length = @list; if($length != 5) { print "5 interests required\n"; next; } $temp = 1; my $item; foreach $item (@list) { unless (exists $interesttable{$item}) { print "Interest $item not recognized\n"; $temp = 0; next; } push(@nlist, $interesttable{$item}); } if($temp) { last; } } return @nlist; } sub wanttype { my $type; my @allowed; while(1) { print "Your desire : "; $type = ; chomp $type; @allowed = qw( MALE FEMALE EPERSON ANY); if(grep { /$type/i } @allowed) { last; } print "Unrecognized type; allowed values are @allowed\n"; } return uc $type; } sub doSearch { my ($you, $desire, @interests, @row); my ($theiremail, $theirtype, $theirdesire, @theirinterest); my $score; $you = owntype; $desire = wanttype; @interests = getinterests; $searchHandle->execute || die "Select request failed because $DBI::errstr"; $score = 0; while(@row = $searchHandle->fetchrow_array) { ( $theiremail, $theirtype, $theirdesire, @theirinterest) = @row; next unless (($desire eq "ANY") || ($desire eq $theirtype)); next unless (($theirdesire eq "ANY") || ($theirdesire eq $you)); my @common = (); my $interest; foreach $interest (@interests) { push(@common, $interest) if (grep {$interest == $_ } @theirinterest); } next if ((scalar @common) == 0); $score++; print "Contact mail $theiremail:\tCommon interests: "; foreach (@common) { print "\t$interestlist[$_]"; } print "\n"; } unless ($score > 0) { print "Sorry, we currently don't have any contacts for you\n"; } } sub doAdd { my ($you, $desire, $email, @interests); $you = owntype; $desire = wanttype; @interests = getinterests; print "Your email address : "; $email = ; chomp($email); $insertHandle->execute( $email, $you, $desire, @interests) || die "Failed to insert record because $DBI::errstr"; } #------------------------------------------------------------ #Example program for database initialize; while(1) { my $cmd; print "Enter command (add,search, list (interests), quit): "; $cmd = ; if($cmd =~ /quit/i) { last; } elsif($cmd =~ /search/i) { doSearch; } elsif($cmd =~ /add/i) { doAdd; } elsif($cmd =~ /list/i) { print "Interest list @interestlist\n"; } else { print "Command not recognized\n"; } }