my $query;
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$query = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query = $ENV{'QUERY_STRING'} . "&" . $query
if ($ENV{'QUERY_STRING'});
}
foreach $_ (split(/&/, $query)) {
tr/+/ /;
s/\%(..)/pack(C, hex($1))/ge;
($_, $param) = split (/=/, $_);
if ($param{$_}) {
$param{$_} .= ",$param";
} else {
$param{$_} = $param;
}
}
my $query;
# Read data from GET
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$query = $ENV{'QUERY_STRING'};
}
# Read data from POST
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $query, $ENV{'CONTENT_LENGTH'});
$query = $ENV{'QUERY_STRING'} . "&" . $query
if ($ENV{'QUERY_STRING'});
}
# Parse the query
foreach $_ (split(/&/, $query)) {
tr/+/ /;
s/\%(..)/pack(C, hex($1))/ge;
($_, $param) = split (/=/, $_);
if ($param{$_}) {
$param{$_} .= ",$param";
} else {
$param{$_} = $param;
}
}
1;
require "ParseInput.lib";
# Print mime header
print "Content-type: text/html\n\n";
# Check input parameters
unless ($param{'user'} && $param{'email'}) {
print "Must fill out user and email";
exit;
}
# Process form
print <<END_OF_UPDATE;
<h1>Success</h1>
Thanks for your input. The following update has been made:
<p>
<table border>
<tr>
<td>User:
<td>$param{'user'}
<tr>
<td>Email:
<td>$param{'email'}
</table>
END_OF_UPDATE
<h1>Update</h1> <form action="update.cgi" method="GET"> Please fill in your profile: <table> <tr> <td>User: <td><input type="text" name="user"> <tr> <td>Email: <td><input type="email" name="email"> <tr> <td> <td><input type="submit" value="Update"> </table> </form>
use CGI;
my $q = new CGI;
my $name = $q->param("name");
print $q->header("text/html"),
$q->start_html("Welcome"),
$q->p("Welcome back, $name!"),
$q->end_html;
use CGI qw( :standard );
my $name = param("name");
print header("text/html"),
start_html("Welcome"),
p("Welcome back, $name!"),
end_html;
content_type CONTENT_TYPE path_info PATH_INFO query_string QUERY_STRING request_method REQUEST_METHOD script_name SCRIPT_NAME Accept HTTP_ACCEPT
my $path = $q->path_info;
foreach $name ($q->param) {
print "$name";
}
$email = $q->param("email");
print "Email: $email";
$color = $q->param("color"); # just the first one
@colors = $q->param("color"); # list of all colors
$q->param(sku => "102030");
$q->param(interests => "music", "dining", "sport");
$q->delete("age");
$q->delete_all;
foreach ($q->param) {
$q->param($1 => 1) if /(.*)\.x/;
}
use CGI;
my $q = new CGI;
my $timestamp = localtime();
print $q->header("text/html"),
$q->start_html(-title => "Current Time", -bgcolor => "blue"),
$q->h1("Current Time"),
$q->hr,
$q->p("The current time is:", $q->b($timestamp)),
$q->end_html;
print $q->header(-type => "text/html",
-status("404 Not Found"));
print $q->header(-type => "text/html",
-expires => "+30m");
print $q->header(-type => "text/html",
-target => "main");
print $q->redirect("/thanks.html");
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML> <HEAD><TITLE>Current Time</TITLE></HEAD> <BODY BGCOLOR="blue">
print $q->hr;
print $q->p("One paragraph");
print $q->p("Thanks for the update,", $user);
print $q->p("Thanks for the update,", $q->em($user));
print $q->a({ -href => "http://www.yahoo.com" },
"Yahoo!");
print $q->ol($q-li(["One", "Two", "Three"]));
print $q->table(
{ -border => 1,
-width => 200 },
$q->Tr( [
$q->th(["Name", "Age"]),
$q->td(["Bob", 29]),
$q->td(["Alice", 22]),
$q->td(["Mary", 24])
])
);
print $q->start_form( -method => "get", -action => "/cgi-bin/script.cgi"); print $q->end_form;
print $q->textfield( -name => "user", -default => "Enter your name"); print $q->password_field( -name => "secret");
print $q->hidden( -name => "action", -value => "checkout");
print $q->submit( -value => "Edit"); print $q->reset( -value => "Undo"); print $q->button( -value => "Recalculate", -onClick => "updateShoppingCart();");
print $q->checkbox( -name => "help", -value => "yes", -label => "Display context-sensitive help", -checked => 1),
print $q->image_button( -name => "order", -src => "/shop/images/order.jpg"),
print $q->textarea( -rows => 10, -cols => 60, -default => "Enter a comment");
use CGI::Pretty;
my $q = new CGI::Pretty;
print $q->header("text/html"),
$q->start_html("Register"),
$q->h1("Register"),
$q->hr;
print $q->start_form(
-method => "post",
-action => "update.cgi"),
$q->textfield(
-name => "user",
-value => "Enter your name"),
$q->p(
$q->textfield(
-name => "email",
-value => "Enter your email")),
$q->p(
$q->checkbox(
-name => "junk",
-value => "yes",
-label => "Sign me up for junk mail",
-checked => 1)),
$q->p(
$q->submit(
-value => "Register"),
$q->reset(
-value => "Clear")),
$q->end_form;
print $q->end_html;
print $q->radio_group(
-name => "movie",
-values => [ "a", "b", "c" ],
-default => "a",
-labels => {
"a" => "Movie A",
"b" => "Movie B",
"c" => "Movie C" } );
use CGI::Carp qw( fatalsToBrowser ); $result = 1/0; # buggy script
use CGI::Carp qw( fatalsToBrowser );
BEGIN {
sub handleError {
my $error = shift;
my $q = new CGI;
print $q->start_html("Error"),
$q->h1("Error"),
$q->p("An error has occurred:"),
$q->p($q->em($error)).
$q->end_html;
}
CGI::Carp::set_message( \&handleError );
}