modules
play

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


  1. Modules Intermediate Perl Programming – Part 2 1 of 42 What is a module? MyModule Intermediate Perl Programming – Part 2 2 of 42

  2. What is a module? $MyModule::x @MyModule::a MyModule::do_this() MyModule::do_that() MyModule Intermediate Perl Programming – Part 2 3 of 42 What is a module? package main; $main::x # Package vars use vars qw($x @a); @main::a # Subroutines sub do_this { ... } main::do_this() sub do_that { ... } main::do_that() main Intermediate Perl Programming – Part 2 4 of 42

  3. What’s the point? Protect one part of your program from another Make your program easier to understand Code reuse (laziness is a virtue!) Intermediate Perl Programming – Part 2 5 of 42 Objects and classes A thumbnail sketch Intermediate Perl Programming – Part 2 6 of 42

  4. Class A module you can use to create objects Just tell it to create one use Some::Module; my $object = Some::Module->new; Intermediate Perl Programming – Part 2 7 of 42 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') ; Intermediate Perl Programming – Part 2 8 of 42

  5. HTTP, CGI, and DBI Intermediate Perl Programming – Part 2 9 of 42 HTTP User’s computer Web server Intermediate Perl Programming – Part 2 10 of 42

  6. search.pl HTTP CGI Apache checkout.pl Web server Intermediate Perl Programming – Part 2 11 of 42 search.pl HTTP CGI DBI Database Apache checkout.pl Web server Intermediate Perl Programming – Part 2 12 of 42

  7. HTTP 2 min. overview Intermediate Perl Programming – Part 2 13 of 42 HTTP request/response exchange Request GET /cgi-bin/hello.pl HTTP/1.0 User-Agent: Safari/1.1 Response HTTP/1.0 200 OK Date: Tue, 17 Oct 2006 12:56:29 GMT Server: Apache/1.3.37 User’s Last-Modified: Fri, 13 Oct 2006 19:42:06 GMT computer Content-Type: text/plain Content-Length: 14 Web Hello, world! server Intermediate Perl Programming – Part 2 14 of 42

  8. CGI Common Gateway Interface Intermediate Perl Programming – Part 2 15 of 42 CGI exchange Environment variables (Standard input) Request GET /cgi-bin/hello.pl HTTP/1.0 User-Agent: Safari/1.1 Apache search.pl Response 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! Standard output Intermediate Perl Programming – Part 2 16 of 42

  9. hello.html – the user interface Name (optional): Yolanda Greet me http://eowyn.simmons.edu/ceperl/hello.html Intermediate Perl Programming – Part 2 17 of 42 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"; Intermediate Perl Programming – Part 2 18 of 42

  10. 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 opportunity to perform any initialization it requires. Intermediate Perl Programming – Part 2 19 of 42 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. Intermediate Perl Programming – Part 2 20 of 42

  11. 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. Intermediate Perl Programming – Part 2 21 of 42 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” . Intermediate Perl Programming – Part 2 22 of 42

  12. 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. Intermediate Perl Programming – Part 2 23 of 42 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. Intermediate Perl Programming – Part 2 24 of 42

  13. 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. Intermediate Perl Programming – Part 2 25 of 42 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. Intermediate Perl Programming – Part 2 26 of 42

  14. 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. Intermediate Perl Programming – Part 2 27 of 42 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 other nitpicky details. Intermediate Perl Programming – Part 2 28 of 42

  15. hello.pl – the result Hello, Yolanda! Intermediate Perl Programming – Part 2 29 of 42 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. Intermediate Perl Programming – Part 2 30 of 42

  16. KidCat Intermediate Perl Programming – Part 2 31 of 42 Databases SQL and RDBMSes in a nutshell Intermediate Perl Programming – Part 2 32 of 42

  17. Tables Works Items UserItems Users id id user_id id title work_id item_id firstname author part checked_out lastname edition library_id due category place call_number renewals status publisher barcode email year barcode isbn Libraries id name Intermediate Perl Programming – Part 2 33 of 42 Columns and rows id firstname lastname category status email barcode 3 Yolanda Ipswich S A yoyo@mama.org 5019 1 Xerxes Axelrod T A xaxelrod@goatisland.edu 3928 6 Wilhelmina Happenstantz S A hellion604@yahoo.com 1085 Intermediate Perl Programming – Part 2 34 of 42

  18. DBI Perl database interface Intermediate Perl Programming – Part 2 35 of 42 The DBI mantra Connect Prepare Execute Fetch Error checking! Intermediate Perl Programming – Part 2 36 of 42

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend