Download presentation
Presentation is loading. Please wait.
Published byEmil Perry Modified over 9 years ago
1
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class Three Instructor: Byrne Reese X401
2
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Review from Last Week… 1.Introduction to CPAN 2.CGI Programming 3.Using Template Toolkit 4.Writing a “Remember Me” web app using Cookies
3
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Today’s Agenda 1.Homework Review 2.Web Automation 3.Writing a Link Checker 4.SQL Primer 5.MySQL and other free databases 6.DBI Module
4
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Homework Review Add a “Remember Me” checkbox to the Hello World form: –If the user has a cookie set that identifies them as a returning user, then do not prompt them for a name to say hello to. –The cookie should only be “session based.” Extra Credit: –Give the user a way of clearing the cookie without restarting the browser.
5
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Web Automation
6
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. LWP What’s in a name? –“LibWww-Perl” Automate form posting “Web Scraping”
7
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Exercise Write a command line script that: –Takes as input a single URL –Then prints to the screen the HTML content of that URL Hint: Use the “LWP” Perl Module
8
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Automating a Form Post # Create a user agent object use LWP::UserAgent; $ua = LWP::UserAgent->new; # Create a request my $req = HTTP::Request->new( GET => 'http://localhost/cgi-bin/hello.cgi‘ ); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print $res->content; } else { print $res->status_line, "\n"; }
9
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Exercise Write a script that automates a Google Search: –Takes as input a single keyword –Executes query at google.com –Displays search results in HTML to screen Hints: –man LWP;
10
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Exercise 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
11
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. SQL Primer
12
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. About SQL Structured Query Language Standard
13
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
14
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
15
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’);
16
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’
17
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’
18
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’
19
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
20
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. 15 Minute Break
21
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
22
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; }
23
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;
24
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”; }
25
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Exercise 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
© 2024 SlidePlayer.com. Inc.
All rights reserved.