#!/usr/bin/perl # This tells the program to use a 'Class' (set of subroutines/functions/methods) # that knows how to read and write the HTTP CGI methods. use CGI; # This creates an instance of that class, called an 'object', # and its output gets assigned to a variable called 'query'. # 'query' is now how we can access all of the CGI class's functionality. $query = new CGI; # This is the logic/ruleset governing what the page displays. # If the script receives data using the HTTP GET method, # a subroutine called 'show_form' (that generates the # HTML displaying the form) gets accessed. # Otherwise, if something is sent to the script using the # POST method, the 'process_form' subroutine (that # contains the mailing instructions) is run. How does # the program know it can rely on receiving things from POST? # Otherwise, access the 'error' subroutine, which prints an error. if ($query->request_method eq 'GET') { &show_form; } elsif ($query->request_method eq 'POST') { &process_form; } else { &error('Unsupported request method.'); } ########################################################## # Here begin the subroutine definitions. # Don't worry about understanding everything that they do. # Just understand the template: # - logic at the top of the script, makes decisions on # how the page should react given certain user input; # - then encapulated lists of actions hold HTML fragments # that get pieced together to make the webpage code # seen by the user. ########################################################## sub error { my ($message) = @_; print $query->header, $query->start_html(-bgcolor=>'white'), qq{ $message }, $query->end_html; exit; } sub process_form { my $email, $message; $message = $query->param('message'); $email = $query->param('email'); $email =~ s/\s//g; if ($email =~ /^[a-zA-Z]+\@indiana.edu$/i) { } elsif ($email =~ /^[a-zA-Z]+$/i) { $email .= "\@indiana.edu"; } else { &error('Unsupported e-mail address format.'); } open MAIL, "| mailx $email jowarren\@indiana.edu "; print MAIL $message; close MAIL; print $query->header, $query->start_html(-bgcolor=>'white'), qq{ Your message<blockquote>$message</blockquote> has been sent to the webmaster. A copy has been sent to the e-mail address that you indicated. }, $query->end_html; } sub show_form { print $query->header, $query->start_html(-bgcolor=>'white', -title=>'feedback'), $query->start_form(-method=>'POST', -action=>$query->url), qq{ Email address: }, $query->textfield(-name=>'email', -size=>20, -maxlength=>40), $query->p, qq{Message: }, $query->textarea(-name=>'message', -rows=>5, -columns=>60, -default=>'Replace me with your comments...'), $query->p, $query->submit(-name=>'Proceed'), $query->end_form, $query->end_html; }