Download presentation
Presentation is loading. Please wait.
Published byBridget Beasley Modified over 9 years ago
1
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Four Instructor: Byrne Reese X401
2
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Review from Last Week… 1.Web Automation 2.Writing a “Remember Me” web app using Cookies 3.Discussion of “Sessions” and Cookie/Session Security 4.Quick SQL Primer 5.Homework: Writing a Link Checker
3
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Today’s Agenda 1.Homework Review 2.Fast SQL Primer 3.MySQL and other free databases 4.DBI Module
4
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Homework Review Write a link checker: –Takes as input a single URL –For each href located on that page, verify that the link is valid. –Print out the links to validate and their status as you go. Hints: –Regular expressions to extract URLs –URI::Find
5
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. SQL Primer
6
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. About SQL Structured Query Language Standard
7
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Our Database Table 1: “users” –user_id (primary key) –username –password Table 2: “bookmarks” –user_id (foreign key) –link –label
8
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Setup the Database scp perlclass@majordojo.com:./*.sql. Password: “iloveperl” mysqladmin create –u root perlclass mysql –u root perlclass < schema_data.sql mysql –u root perlclass
9
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. INSERT Statement Adds a single row to the selected database table Example: INSERT INTO users (username, password) VALUES (‘byrne’,’reese’);
10
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. UPDATE Statement Modifies a row or rows in the selected database table Example: UPDATE users SET password=‘reese’ WHERE username=‘byrne’
11
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. DELETE Statement Deletes a row or rows in the selected database table Example: DELETE FROM users WHERE username=‘byrne’
12
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. SELECT Statement Reads a row or rows in the selected database table Example: SELECT username,password FROM users WHERE username=‘john’
13
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. JOINs Selects and associates data located in two different tables into a single result set. Primary keys and Foreign keys Example: SELECT b.label, b.link FROM users u, bookmarks b WHERE u.username=‘byrne’ AND u.user_id = b.user_id
14
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 15 Minute Break
15
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Using Perl’s DBI Installing DBI and DBD::mysql Connecting to the Database Preparing a Statement Executing a Statement Executing a Query and parsing the results
16
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Connecting to the Database Database “Handles” Example: connect_to_db(): use DBI; sub connect_to_db { my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST", "$DBUSER","$DBPASS",{RaiseError => 1}) or die "connecting : $DBI->errstr\n"; return $dbh; }
17
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Executing a Statement Example: my $dbh = connect_to_db(); my $sql = "DELETE FROM users"; my $sth = $dbh->prepare($sql); $sth->execute; $sth->finish; $dbh->close;
18
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Executing a Query Example: my $dbh = connect_to_db(); my $sql = "SELECT username,password FROM users"; my $sth = $dbh->prepare($sql); $sth->execute; while (($username,$password) = $sth->fetchrow) { print “username=$username, password=$password\n”; }
19
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. XML
20
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. XML Primer eXtensible Markup Language Why use XML? –It’s Simple –It’s Extensible –It’s a Standard –It’s Descriptive –It’s Describable
21
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. XML in the Real World Web Services –Amazon –eBay –PayPal –Salesforce.com Blogs –Atom and RSS Feedburner Google Maps
22
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. XML Primer XML Syntax –Well formed-ness –Valid Parsers –DOM –SAX
23
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Sample XML File Example: intperl.xml: Intermediate Perl Byrne Reese 18:30Z-09:00 Paul Elaine Vladimir
24
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. XML Modules XML::Parser XML::SAX XML::Simple
25
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. XML::Simple – Reading XML Code Sample: #!/usr/bin/perl use XML::Simple; my $xmldata = XMLin(“intperl.xml”); print $xmldata->students->student->[1];
26
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. XML::Simple – Writing XML Code Sample: #!/usr/bin/perl use XML::Simple; my $xmldata = { name => ‘Intermediate Perl’, instructor => ‘Byrne Reese’, schedule => { time => ’18:30Z-09:00’ }, students => { student => [ ‘Paul’,’Elaine’,’Vladimir’ ] } my $xml = XMLout($xmldata);
27
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Debugging Data::Dumper –Stringifies Perl data structures Code sample: #!/usr/bin/perl use XML::Simple; use Data::Dumper; my $xmldata = XMLin(“intperl.xml”); print Dumper($xmldata);
28
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Exercise 1 In class: parse and display the contents of an RSS feed. –http://www.majordojo.com/atom.xml At home: take a URL to an RSS feed as a command line argument, scrape that page and then display the contents. Hint: “man XML::Simple”
29
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Homework Your choice of one of two exercises: –Exercise 1 - XML Exercise –Exercise 2 - Database Exercise
30
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Exercise 2 Create an address book: –Write subroutines capable of creating, reading, updating and deleting (CRUD) a single row in the database Extra Credit: –Make it command line accessible
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.