Download Week 4 - Byrne

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Intermediate
Perl Programming
Class Four
Instructor: Byrne Reese
X401
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
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Today’s Agenda
1.
2.
3.
4.
Homework Review
Fast SQL Primer
MySQL and other free databases
DBI Module
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
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
SQL Primer
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
About SQL
• Structured Query Language
• Standard
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
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Setup the Database
 scp [email protected]:./*.sql .
Password: “iloveperl”
 mysqladmin create –u root perlclass
 mysql –u root perlclass < schema_data.sql
 mysql –u root perlclass
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’);
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’
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’
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’
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
FROM
WHERE
AND
b.label, b.link
users u, bookmarks b
u.username=‘byrne’
u.user_id = b.user_id
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
15 Minute Break
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
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;
}
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;
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”;
}
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
XML
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
XML Primer
• eXtensible Markup Language
• Why use XML?
–
–
–
–
–
It’s
It’s
It’s
It’s
It’s
Simple
Extensible
a Standard
Descriptive
Describable
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
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
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Sample XML File
Example: intperl.xml:
<class code=“X401”>
<name>Intermediate Perl</name>
<instructor>Byrne Reese</instructor>
<schedule>
<time>18:30Z-09:00</time>
</schedule>
<students>
<student>Paul</student>
<student>Elaine</student>
<student>Vladimir</student>
</students>
</class>
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
XML Modules
• XML::Parser
• XML::SAX
• XML::Simple
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];
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);
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);
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” 
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
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
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Related documents