Appendix D. mod_perl Example Code

The following are a few code examples that were omitted from Chapter 17, “mod_perl,” in the interests of brevity.

mod_perl Form Handler Code

In Chapter 17, code was presented that would parse the contents of a HTML form, or the contents of URL “command-line” arguments in ?key=value&key=value syntax.

The portion of this code that does the actual form parsing is the single line that follows:

my %form = $r->method eq 'POST' ? $r->content : $r->args;

This line uses the Perl ? : syntax, which functions exactly like an if/else clause. This syntax allows for a simple if/else clause to be expressed in one line, as shown in the example. If written out fully, the statement would look like the following block:

my %form; # Declare the form variable
if ($r->method eq 'POST') {  # was the data posted?
    %form = $r->content;    # Get the form contents
}  else {
    %form = $r->args;       # Otherwise, read arguments from the URL
}

It is very important to note that the example code does not work for forms that contain multiple-select lists, and so should not be used for any production code. The following subroutine solves this problem, and you will probably want to use this instead.

A detailed explanation of the code is not provided in this appendix.

The following function returns the contents of the form as a hash reference. The keys of the hash are the names of the form fields, whereas the values are the values of those fields. The only exception to this is a multiple-select list, where more than one element of the list was selected. In this case, the value that is put into the hash is actually a reference to a list of the selected value. An example of this is provided following the example function.

sub form {
    my $r = Apache->request();
    my @form = $r->method eq 'POST' ? $r->content : $r->args;
    my %form;
    foreach my $i (0..@form) {
        my $key = $form[$i];
        my $value = $form[++$i];
        next unless $value;

        if ($form{ $key} ) {
            my $temp  = $form{ $key} ;
            if(ref $temp) {
                push @{ $form{ $key} } ,$value;
            }  else {
                $form{ $key}  = [ $temp, $value ];
            }
        } else {
            $form{ $key}  = $value;
        }
    }
    return \%form;
}

In your handler code, you will access this method as follows:

my $form = form();
my $color = $form->{ favorite_color} ;

If accessing a value that was returned from a multiple select list, you will need to test the value to determine if more than one element was selected, as shown in this example:

if ( ref $form->{ favorite_food}  ) {
    @food = @{  $form->{ favorite_food}  } ; # Dereference the array
} else {
    $food = $form->{ favorite_food} ; # There's just one value
}

Please note that the examples provided here are just that[md]examples, not fully functioning programs, and, therefore, are not intended to be blindly copied into your code.

Numerous modules on CPAN can help you do this stuff and should be used in preference to examples printed in this, or any other, book. CPAN can be found at http://www.cpan.org/, as well as at hundreds of mirrors worldwide.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset