Listing 2: add-person.html

<!-- -*- mmm-classes: mason -*- -->
<HTML>

% # Make sure that we got all required name-value pairs
% unless ($first_name and $last_name and $country and $email) {

    <Head>
        <Title>Missing information!</Title>
    </Head>

    <Body>
    <H1>Missing information!</H1>

    <P>Sorry, but you need to provide us with (at a minimum) the
    person's first name, last name, country, and e-mail address.</P>

    </Body>

% # Make sure that the e-mail address looks somewhat valid
% } elsif ($email !~ /[\w.]+@\w+\.\w{2,3}/) {
  
    <Head>
        <Title>Bad e-mail address!</Title>
    </Head>

    <Body>
        <H1>Bad e-mail address!</H1>

    <P>Sorry, but the provided e-mail address doesn't look legal.</P>

    </Body>

% # If we made it this far, try to create the new person
% } else {

<%perl>
my $success = $people->new_person(first_name => $first_name,
                                  last_name => $last_name,
                                  country => $country,
                                  email => $email);
</%perl>

% if ($success) {

    <Head>
    <Title>Sucess!</Title>
    </Head>

    <Body>
    <H1>Success!</H1>

    <P>We successfully added a new person to the database.</P>
    </Body>

% } else {

    <Head>
    <Title>Error!</Title>
    </Head>

    <Body>
    <H1>Error!</H1>

    <P>There was an error trying to add a new person to the database.
    Here is the method that was invoked:</P>
                                     
    <pre>
    $people->new_person(first_name => <% $first_name %>,
                        last_name => <% $last_name %>,
                        country => <% $country %>,
                        email => <% $email %>);
    </pre>

    </Body>

% }

% }

</HTML>

<%init>
use People;
my $people = new People;
</%init>

<%args>
$first_name              
$last_name
$address1
$address2
$email
$city
$state
$postal_code
$country
$comments
</%args>
                                                      
