Lab 9 - Social Skills and Mapping |
Overview |
Today, we'll extend the social networking script from last week. We want to do two
things:
|
Storing addresses |
Right now, each person's key in the hash just stores a list of their friends. That means if we have two people in our database, Alice Smith and Bob Davis, it will look like this if they are friends: %db = ( 'Alice Smith' => [ 'Bob Davis' ], 'Bob Davis' => [ 'Alice Smith' ] ); If we add a third person, Cindy Jones, and make her friends with Bob, our database will look like this: %db = ( 'Alice Smith' => [ 'Bob Davis' ], 'Bob Davis' => [ 'Alice Smith', 'Cindy Jones' ], 'Cindy Jones' => [ 'Bob Davis' ] ); We would like to change our database so that it can store other information about each person, not just their friends list. In particular, we'd like to store each person's address in two fields - a street address, and a city and state (as one field). We'll store it this way because this is the way that Yahoo Maps expects it. Right now each key in the hash is associated with a list - more precisely, with a reference to a list. We'll update this data structure so that each key in the hash is associated with a hash (well, a hash reference) that stores information about the person. One of the fields of the hash will be friends, which will be associated with the old list (reference) of friends. So now we're going to have a hash of hash references, which in turn contain list references. Scared? Don't be, a step at a time makes it easy. In the hypothetical example above, and with some imaginary addresses around Bloomington, our above example database might look like this: %db = ( 'Cindy Jones' => { 'friends' => [ 'Bob Davis' ], 'street' => '515 E. 4th St', 'citystate' => 'Bloomington, IN' }, 'Bob Davis' = { 'friends' => [ 'Cindy Jones', 'Alice Smith' ], 'street' => '732 E. Smith Ave', 'citystate' => 'Bloomington, IN' }, 'Alice Smith' => { 'friends' => [ 'Bob Davis' ], 'street' => '751 Alpine Trail', 'citystate' => 'Bloomington, IN' } ); Take a minute to think about what needs to be changed. If you have developed your socialnet.pl file carefully, we'll only need to change a few functions to let us store the new data:
Hint: your new add_person method will look like this: ############################################################ # adds a new person to the database # sub add_person { my ($db_ref, $who) = @_; $db_ref->{$who} = { street => "", citystate => "", friends => [ ] }; } This method associates the person's name with a hash containing all the keys we need, and initializing those keys to sensible default values. That way, if we make a person but don't set her address or friends, we can still use the print functionality without it blowing up on us. Do this:
Replace your add_person method with the code above, and then change the other methods listed above so that they don't give error messages when compiled. Of course you can't test that the address works yet, but adding and removing friends should work again when you are done. Remember that all you have to do is:
Your The next step is to make it so we can actually set the address. Because specifying the address on the command line would be annoying, let's do it like this:
When this part is all done, your program should look like this when run: jpr@sulu (i211/lab9) % ./socialnet2.pl addr Alice Smith Setting address for Alice Smith. Enter street address => 751 Alpine Trail Enter city, state => Bloomington, IN jpr@sulu (i211/lab9) % When you print everything, you should see addresses now: jpr@sulu (i211/lab9) % ./socialnet2.pl pr Cindy Jones is friends with: Bob Davis Cindy Jones lives at 515 E. 4th St, Bloomington, IN. Bob Davis is friends with: Cindy Jones, Alice Smith Bob Davis lives at 732 E. Smith Ave, Bloomington, IN. Alice Smith is friends with: Bob Davis Alice Smith lives at 751 Alpine Trail, Bloomington, IN. jpr@sulu (i211/lab9) % If that all looks right, congratulations, you're done with this part! |
Exporting to XML/RSS |
The next step is to export the database to XML/RSS in a format that Yahoo Maps can read. We want to end up with something like this: <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:ymaps="http://api.maps.yahoo.com/Maps/V1/AnnotatedMaps.xsd"> <channel> <item> <title>Cindy Jones</title> <ymaps:Address>515 E. 4th St</ymaps:Address> <ymaps:CityState>Bloomington, IN</ymaps:CityState> </item> <item> <title>Bob Davis</title> <ymaps:Address>732 E. Smith Ave</ymaps:Address> <ymaps:CityState>Bloomington, IN</ymaps:CityState> </item> <item> <title>Alice Smith</title> <ymaps:Address>751 Alpine Trail</ymaps:Address> <ymaps:CityState>Bloomington, IN</ymaps:CityState> </item> </channel> This is relatively simple - we just want to create another version of
the Do this:
Create a method called Here's a twist - use
a file test
operator to check if the file exists before writing. If the file exists,
your function should Next, change your main function so that a user can type Finally, let's make one more improvement. Imagine we want to export only a certain person's friends to the XML file, instead of everyone. Let's make that possible. Do this:
Change your You might be tempted to put all the code currently in
your Next, change the main part of your program so that the Finally, test your program to make sure everything is working correctly. If all the exports look good, it's time to move on to interfacing with Yahoo Maps. |
Interface with Yahoo Maps |
If you have an application ID from the project, this is easy. The first step is to make sure that your web space is set up correctly: jpr@sulu (i211/lab9) % chmod a+x ~ jpr@sulu (i211/lab9) % chmod a+x ~/public_html Next, copy your XML file (let's assume it's called jpr@sulu (i211/lab9) % cp friends.xml ~/public_html jpr@sulu (i211/lab9) % chmod a+r ~/public_html/friends.xml Finally, point your web browser to the apppriate URL. This depends on your application ID (which you get from Yahoo) and your username on Sulu: http://api.maps.yahoo.com/Maps/V1/annotatedMaps?appid=YOURKEY &xmlsrc=http://sulu.informatics.indiana.edu/~username/friends.xml Really that should be on one line, but then it would be too wide to fit on the screen. |
Turn in your assignment |
Strictly speaking, there's only one file in this week's lab, so a tarball isn't really required. However, let's make one anyway. As a reminder, here's the command you can use: jpr@sulu (i211/lab9) $ cd .. jpr@sulu (i211) $ tar czv lab9 > lab9.tar.gz socialnet2.pl jpr@sulu (i211) $ You can use the You should then download this Okay, that's it! Have a good weekend. |