Perl Web Development: From Novice To Professional by B. Brown Jason

Perl Web Development: From Novice To Professional by B. Brown Jason

Author:B. Brown, Jason [B. Brown, Jason]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2020-10-19T16:00:00+00:00


Compare this output with that shown in Figure 11-12. This output was produced by placing the output from the args() method into a hash, as shown in Listing 11-6. Notice that only the final value for the action parameter is actually sent to output.

Figure 11-12. Output from a multivalued query string

Although you can iterate through multivalued parameters, if your CGI program will be using multivalued parameters, I recommend using the CGI.pm’s methods for accessing these values, rather than coding around the args() method.

So far, these methods retrieve values from GET requests. For POST requests, use the content() method, as shown in Listing 11-7 (Post.cgi).

Listing 11-7. Using the content() Method

use strict;

my $r = shift;

$r->send_http_header('text/plain');

my %data = $r->content;

foreach my $name (keys %data) {

print "$name = $data{$name}
";

}

This code looks and is largely similar to Listing 11-6, which retrieved arguments using the args() method. Mainly, the variable names have been changed to protect the innocent. The content() in this case.

The Apache::Request class provides the most flexible method for working with form data through the param() method, as in the example in Listing 11-8 (Param.cgi).

Listing 11-8. Using the param() Method to Look at Parameters use Apache::Request;

my $r = Apache::Request->new(shift);

$r->send_http_header('text/plain');

foreach my $param ($r->param) {

print "Param: $param = " , $r->param($param), "
";

}

The param() method works like CGI.pm, but is implemented in the C programming language as opposed to Perl. The param() method works with both GET and POST data.

■ Note The examples in this section also demonstrate how to work with HTML form fields through

mod_perl. They show how to parse the parameters from the GET or POST request.

Other methods related to the incoming request include header_only(), for determining whether or not the incoming request was aHEAD type request, and proxyreq(), for determining whether or not the request is a proxy request. Both of these methods return true if they are positive, meaning that the method will return true if it’s aHEAD request or if it’s a proxy request, respectively.

Accessing Request Headers

So far, you’ve seen the actual request and methods for working with the request. As noted earlier, the client may also send headers related to the request.

You can access request headers through the headers_in() method, which contains a set of name =value pairs. The headers_in() method is part of the Apache::Table class, which also contains other class methods, including the following:

• err_headers_out()

• headers_out()

• info()

• notes()

• subprocess_env()

Each of the class methods of Apache::Table has its own set of methods, including the following:

• add()

• clear()

• do()

• get()

• merge()

• new()

• set()

• unset()

Since name =value pairs are sent from the headers_in() method, the output naturally lends itself to being represented in a hash. Listing 11-9 (Headersin.cgi) prints the output from the headers_in() method.

Listing 11-9. The headers_in() Method to See Name=Value Pairs use strict;

my $r = shift;

$r->send_http_header('text/plain');

my %headers = $r->headers_in();

foreach my $header (keys %headers) { print "$header = $headers{$header}
";

}

The output is shown in Figure 11-13.

Figure 11-13. The headers for a request retrieved using the headers_in() method

You could access individual headers by calling them within the hash. For example, Listing 11-10 (Printuseragent.cgi) shows the code to see the user agent used for the request.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.