250 likes | 388 Views
Chapter 20 Email, Files, CGI.pm. The Computer Survey page contains an example of every form element. Chapter 20 Hash of Arrays, I. Encoded Fields Storage Representation title=Ms $hash{'title'}[0]="Ms"; First=Penny $hash{'First'}[0]="Penny";
E N D
Chapter 20 Email, Files, CGI.pm The Computer Survey page contains an example of every form element.
Chapter 20 Hash of Arrays, I Encoded Fields Storage Representation title=Ms$hash{'title'}[0]="Ms"; First=Penny$hash{'First'}[0]="Penny"; Last=Opossum$hash{'Last'}[0]="Opossum"; Address=323+River+Rd.\n $hash{'Address'}[0]="323 River Muskrat+MoundsRd.<BR>Muskrat Mounds"; Mac=on $hash{'system'}[0]="Mac"; Linux=on$hash{'system'}[1]="Linux";
Chapter 20 Hash of Arrays Encoded Fields Storage Representation languages=Perl $hash{'languages'}[0]="Perl"; language=CPP $hash{'languages'}[1]="C++"; languages=HTML $hash{'languages'}[2]="HTML"; languages=Java $hash{'languages'}[3]="Java"; languages=JavaScript $hash{'languages'}[4] ="JavaScript"; years=Six+to+ ten+years $hash{'years'}[0]= "Six to ten years";
Chapter 20 Email Version Delivered-To: Recipient@Email.address Date Fri, 13 Oct 2000 10:52:59 -0500 From: Sender@Email.address Mime-Version: 1.0 The completed form information is: Ms Penny Opossum 323 River Rd. Muskrat Mounds Years of Experience: Six to ten years Language Knowledge: C++ HTML Java JavaScript Perl System Experience: Mac Linux
Chapter 20 Email Version 1. Open a File Handle.open(MAIL, "|mail mmusk\@musk.rat.com"); 2. Print to the Email File.print MAIL, “My name is Penny.\n”; 3. Close the File.close(MAIL);
Chapter 20 Reading Files The poem.txt file is written toa page.
Chapter 20 Reading Files 1. Open a File Handle.open(FILE, $name); 2. Read the File.@FileInfo = <FILE>; 3. Close the File.close(FILE); The file reading operator, <>, reads the file into an array.
Chapter 20 The read_file() Subroutine sub read_file {#read the information from the file my($name) = @_; my (@FileInfo); open(FILE, $name); #1 @FileInfo = <FILE>; #2 close(FILE); #3 return @FileInfo; }
Chapter 20 Writing Files The Suggestion Box page writes the new suggestion to a file and then reads and displays the file.
Chapter 20 Writing Files 1. Open a File Handle.open(FILE, “>$name”); Oropen(FILE, “>>$name”); 2. Print to the File.foreach $line(@contents_ref) {print FILE $line;} 3. Close the File.close(FILE); Create a new file. Append to an existing file.
Chapter 20 CGI.pm Basics Invoking the CGI.pm Module Methods Group Approach use CGI qw/methods group/; $title = param("title"); Object-Oriented Approach use CGI; $form = CGI::new(); $title = $form->param("title");
Chapter 20 Method Groups Method Group Purpose :all Complete package :cgi CGI methods :form HTML form methods :html All HTML methods :html2 All HTML 2.0 methods :html3 All HTML 3.0 methods :netscape All Netscape Extensions :standard HTML 2.0, Form, and CGI methods
Chapter 20 Using CGI.pm #!/usr/local/bin/perl use CGI qw/:standard/; ... print param("title"); print param("First"); print " "; print param("Last"); print “<BR>”; print param("Address"); ... This code prints a portion of the Computer Survey page using CGI.pm
Chapter 20 CGI.pm Form Functions startform(-name=>'form name', -method=>'GET' | 'POST', -action=>'script name', -target=>'frame name', -encoding=>'encoding scheme'); Put the form elements here endform();
Chapter 20 CGI.pm: Text Fields textfield(-name=>'name', -default =>'value', -size=>'number', … -onChange=>'function', -onFocus=>'function' -onBlur=>'function' -onSelect=>'function');
Chapter 20 CGI.pm: Text Fields HTML CGI.pm <INPUT TYPE="text" print $form->textfield( NAME="First" -name=>'First', SIZE="7">-size=>'7'); <INPUT TYPE="text" print $form->textfield( NAME="Last" -name=>'Last', SIZE="12"> -size=>'12');
Chapter 20 CGI.pm: Text Areas textarea(-name=>'name', -default =>'value', -size=>'number', -rows=>'number', -columns=>'number', -wrap=>'wrap style', -onChange=>'function', -onFocus=>'function' -onBlur=>'function' -onSelect=>'function');
Chapter 20 CGI.pm: Radio Buttons radio_group(-name=>'name', -values=>\@values, -default=>'selected value', -labels=>\%hash);
Chapter 20 CGI.pm: Radio Buttons honorific = ("Mr","Mrs", "Miss","Ms", "Dr"); print $form->radio_group( -name =>'title', -values=>\@honorific);
Chapter 20 CGI.pm: Checkboxes checkbox(-name=>'name', -checked=>'checked value', -value=>'value', -label=>'label name');
Chapter 20 CGI.pm: Checkboxes @system = ("PC", "Mac", "Linux", "Unix", "Other"); foreach $element(@system) { print $form->checkbox( -name=>$element, -label=>$element); }
Chapter 20 CGI.pm: Buttons button(-name=>'name', -value=>'button label', -onClick=>'function'); submit(); reset();
Chapter 20 CGI.pm: Select Lists scrolling_list(-name=>'name', -values=>\@option names, -default=>'selected option', -size=>'number', -multiple=>'true', -labels=>\%label names, -onFocus=>'function' -onBlur=>'function' -onSelect=>'function');
Chapter 20 CGI.pm: Multiple-Select Lists @language_list =("Ada", "Algol", "C", "CPP", "Fortran", "HTML","Java", "JavaScript", "Logo", "Lisp", "Modula", "Pascal", "Perl", "Prolog", "Scheme", "Snobol", "Other"); $label{"CPP"} = "C++"; print $form->scrolling_list( -name=>'languages', -values=>\@language_list, -size=>'8', -multiple=>'true', -labels=>\%label, -default=>"HTML");
Chapter 20 CGI.pm: Single-Select Lists @years = ("Less than one year", "One to five years", "Six to ten years", "More than ten years"); print $form->scrolling_list( -name=>'years', -values=>\@years);