Modules Intermediate Perl Programming Part 2 1 of 42 What is a - - PDF document

modules
SMART_READER_LITE
LIVE PREVIEW

Modules Intermediate Perl Programming Part 2 1 of 42 What is a - - PDF document

Modules Intermediate Perl Programming Part 2 1 of 42 What is a module? MyModule Intermediate Perl Programming Part 2 2 of 42 What is a module? $MyModule::x @MyModule::a MyModule::do_this() MyModule::do_that() MyModule


slide-1
SLIDE 1

1 of 42 Intermediate Perl Programming – Part 2

Modules

2 of 42 Intermediate Perl Programming – Part 2

What is a module? MyModule

slide-2
SLIDE 2

3 of 42 Intermediate Perl Programming – Part 2

What is a module? MyModule

MyModule::do_this() MyModule::do_that() $MyModule::x @MyModule::a

4 of 42 Intermediate Perl Programming – Part 2

What is a module? main

main::do_this() main::do_that() $main::x @main::a

package main; # Package vars use vars qw($x @a); # Subroutines sub do_this { ... } sub do_that { ... }

slide-3
SLIDE 3

5 of 42 Intermediate Perl Programming – Part 2

What’s the point? Protect one part of your program from another Make your program easier to understand Code reuse (laziness is a virtue!)

6 of 42 Intermediate Perl Programming – Part 2

Objects and classes

A thumbnail sketch

slide-4
SLIDE 4

7 of 42 Intermediate Perl Programming – Part 2

Class A module you can use to create objects use Some::Module; my $object = Some::Module->new; Just tell it to create one

8 of 42 Intermediate Perl Programming – Part 2

Object Another black box Has properties you can tell it to get or set Has behavior – if you tell it do something, it reacts in a certain way use Some::Module; my $object = Some::Module->new; print $object->name; $object->jump('up');

slide-5
SLIDE 5

9 of 42 Intermediate Perl Programming – Part 2

HTTP, CGI, and DBI

10 of 42 Intermediate Perl Programming – Part 2

User’s computer

Web server HTTP

slide-6
SLIDE 6

11 of 42 Intermediate Perl Programming – Part 2

Web server

Apache

HTTP

search.pl checkout.pl

CGI

12 of 42 Intermediate Perl Programming – Part 2

Web server

Apache

HTTP

search.pl checkout.pl

CGI

Database

DBI

slide-7
SLIDE 7

13 of 42 Intermediate Perl Programming – Part 2

HTTP

2 min. overview

14 of 42 Intermediate Perl Programming – Part 2

HTTP request/response exchange

GET /cgi-bin/hello.pl HTTP/1.0 User-Agent: Safari/1.1

Web server User’s computer

HTTP/1.0 200 OK Date: Tue, 17 Oct 2006 12:56:29 GMT Server: Apache/1.3.37 Last-Modified: Fri, 13 Oct 2006 19:42:06 GMT Content-Type: text/plain Content-Length: 14 Hello, world!

Request Response

slide-8
SLIDE 8

15 of 42 Intermediate Perl Programming – Part 2

CGI

Common Gateway Interface

16 of 42 Intermediate Perl Programming – Part 2

CGI exchange

GET /cgi-bin/hello.pl HTTP/1.0 User-Agent: Safari/1.1

Request

HTTP/1.0 200 OK Date: Tue, 17 Oct 2006 12:56:29 GMT Server: Apache/1.3.37 Last-Modified: Fri, 13 Oct 2006 19:42:06 GMT Content-Type: text/plain Content-Length: 14 Hello, world!

Response

Apache search.pl

Environment variables (Standard input) Standard output

slide-9
SLIDE 9

17 of 42 Intermediate Perl Programming – Part 2

hello.html – the user interface

Name (optional):

Yolanda

Greet me

http://eowyn.simmons.edu/ceperl/hello.html

18 of 42 Intermediate Perl Programming – Part 2

hello.pl – the CGI program

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

slide-10
SLIDE 10

19 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • The program relies upon the CGI module. This statement

checks to make sure it’s available and – if it is – gives it an

  • pportunity to perform any initialization it requires.

20 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • We use a generic greeting by default.
slide-11
SLIDE 11

21 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • Ask the CGI module to create a new object containing the data

that the web server program has passed to us.

22 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • Look for a “name” parameter. This is the text that the user typed

into the HTML input field named “name” .

slide-12
SLIDE 12

23 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • Don’t assume that there was such a field! The ”user” could be a

malicious program in disguise.

24 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • Check to see if the user left the ”name” field blank.
slide-13
SLIDE 13

25 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • OK, use the name they supplied.

Normally, we would be careful to check to make sure the value supplied wasn’t malformed, but we don’t bother here because we’re only going to send the name back to the user.

26 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • The content that we’re generating on the web server program’s

behalf is plain text.

slide-14
SLIDE 14

27 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • A blank line separates the HTTP header from the content.

28 of 42 Intermediate Perl Programming – Part 2

hello.pl

use CGI; my $user_name = 'whoever you are'; my $query = CGI->new; my $name_supplied = $query->param('name'); if (defined $name_supplied and $name_supplied ne '') { $user_name = $name_supplied; } print "Content-Type: text/plain\n"; print "\n"; print "Hello, $user_name!\n";

  • Send our greeting. The web server program will take care of all
  • ther nitpicky details.
slide-15
SLIDE 15

29 of 42 Intermediate Perl Programming – Part 2

hello.pl – the result

Hello, Yolanda!

30 of 42 Intermediate Perl Programming – Part 2

hello.pl – try it out http://eowyn.simmons.edu/ceperl/cgi-bin/hello.pl http://eowyn.simmons.edu/ceperl/cgi-bin/hello.pl?name=Yolanda http://eowyn.simmons.edu/ceperl/cgi-bin/badger/hello.pl http://eowyn.simmons.edu/ceperl/cgi-bin/loris/hello.pl — etc.

slide-16
SLIDE 16

31 of 42 Intermediate Perl Programming – Part 2

KidCat

32 of 42 Intermediate Perl Programming – Part 2

Databases

SQL and RDBMSes in a nutshell

slide-17
SLIDE 17

33 of 42 Intermediate Perl Programming – Part 2

Tables

isbn year publisher place edition author title id Works renewals due checked_out item_id user_id UserItems barcode call_number library_id part work_id id Items name id Libraries barcode email status category lastname firstname id Users

34 of 42 Intermediate Perl Programming – Part 2

Columns and rows

Happenstantz hellion604@yahoo.com 6 A Wilhelmina 1085 S T 3928 xaxelrod@goatisland.edu A Xerxes Axelrod 1 A 5019 S Ipswich Yolanda 3 yoyo@mama.org barcode email status category lastname firstname id

slide-18
SLIDE 18

35 of 42 Intermediate Perl Programming – Part 2

DBI

Perl database interface

36 of 42 Intermediate Perl Programming – Part 2

The DBI mantra

Connect Prepare Execute Fetch Error checking!

slide-19
SLIDE 19

37 of 42 Intermediate Perl Programming – Part 2

Connect

$dbh = DBI->connect( $data_source, $user, $password );

38 of 42 Intermediate Perl Programming – Part 2

Prepare

$dbh = DBI->connect( $data_source, $user, $password ); $sth = $dbh->prepare($query);

slide-20
SLIDE 20

39 of 42 Intermediate Perl Programming – Part 2

Execute

$dbh = DBI->connect( $data_source, $user, $password ); $sth = $dbh->prepare($query); $sth->execute;

40 of 42 Intermediate Perl Programming – Part 2

Fetch

$dbh = DBI->connect( $data_source, $user, $password ); $sth = $dbh->prepare($query); $sth->execute; while (@row = $sth->fetchrow_array) { ... }

slide-21
SLIDE 21

41 of 42 Intermediate Perl Programming – Part 2

Error checking

$dbh = DBI->connect( $data_source, $user, $password ) or die; $sth = $dbh->prepare($query)

  • r die;

$sth->execute or die; while (@row = $sth->fetchrow_array) { ... } die if $DBI::err;

42 of 42 Intermediate Perl Programming – Part 2

Error checking simplified

$dbh = DBI->connect( $data_source, $user, $password, { 'RaiseError' => 1 } ); $sth = $dbh->prepare($query); $sth->execute; while (@row = $sth->fetchrow_array) { ... }