CGI Programming
What if we want to make sure that users have filled out certain fields? This is particularly important when we are collecting data about visitors to a site, and want to make sure that we receive their names, addresses and other vital data. A simple way to do that is to create a list, @required_fields, in which the required fields are listed:
my @required_fields = ("name",
"email",
"address");
If you simply want a generic message indicating that one or more required fields haven't been filled out, you can add the following subroutine at the bottom of the program file:
sub missing_field
{
# Print an a appropriate MIME header
print $query->header("text/html");
# Print a title for the page
print $query->start_html(-title=>
"Missing field(s)");
# Tell the user what the error is
print "<P>At least one required ";
print "field is missing.</P>\n";
# Finish up the HTML
print $query->end_html;
}
We can then insert the following code into the program itself, just before we open the file—since there isn't any reason to open the file if we are simply going to close it again:
foreach $field (@required_fields)
{
# Make sure that the field contains more than
# just whitespace
&missing_field if
($query->param($field) !~m/\w/);
exit;
}
The above code will indeed do the trick, but gives a generic error message. Wouldn't it be better to tell the user which field contains the error? We can do that by modifying missing_field such that it takes an argument, as follows:
sub missing_field
{
# Get our local variables
my (@missing_fields) = @_;
# Print an a appropriate MIME header
print $query->header("text/html");
# Print a title for the page
print $query->start_html
(-title=>"Missing field(s)");
print "<P>You are missing the following ";
print "required fields:</P>\n";
print "<ul>\n";
# Iterate through the missing fields, printing
# them foreach $field (@missing_fields)
{
print "<li> $field\n";
}
print "</ul>\n";
# Finish up the HTML
print $query->end_html;
exit;
}
We then modify the loop that checks for required fields:
foreach $field (@required_fields)
{
# Add the name of each missing field
push (@missing_fields, $field) if
($query->param($field) !~ m/\w/);
}
# If @missing_fields contains any elements, then
# invoke the error routine
&missing_field(@missing_fields)
if @missing_fields;
If we want to get really fancy, we can provide English names for each of the required fields, so that users don't have to suffer through the names we used with the HTML form. We can do that by using associative arrays:
$FULLNAME{"name"} = "your full name";
$FULLNAME{"email"} = "your e-mail address";
$FULLNAME{"address"} = "your mailing address";
Then we modify the foreach loop in &missing_fields such that it prints the full name of the missing field, rather than the name associated with it on the HTML form:
# Iterate through the missing fields, printing
# them foreach $field (@missing_fields)
{
print "<li> $FULLNAME{$field}\n";
}
print "</ul>\n";
Remember that die statement we put in our original program? Well, think about what will happen if that part of the program is ever truly invoked—die will produce an error message, which is a good thing. But that error message will be sent to our web browser, before the HTML header, giving us the dreaded “Server error” message, indicating that something (but not saying what that something is) has gone wrong with our script.
More useful would be a routine that printed the error message to the screen. For example, we could add the following subroutine:
sub error_opening_file
{
my ($filename) = @_;
# Print an a appropriate MIME header
print $query->header("text/html");
# Print a title for the page
print $query->start_html(-title=>"Error
opening file");
# Print the error
print "Could not open the file
\"$filename\".</P>\n";
# Finish up the HTML
print $query->end_html;
exit;
}
And now, we can rewrite the “open” statement as follows:
open (FILE, ">>:$filename") || &error_opening_file($filename);
You probably don't want to tell your users your program couldn't open a particular file—not only do your users not care, but you don't need to tell them which files you are using. A more user-friendly version of error_opening_file could tell the user that the server is experiencing some trouble, or is undergoing maintenance or give a similar message that doesn't broadcast catastrophe to the world.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- Swap Your Laptop for an iPad + Linode
- Building an Open-Source House
- An Automated Reliable Backup Solution
- Tech Tip: Really Simple HTTP Server with Python
- Stupid tar Tricks
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Acquia Founder, Drupal Project Creator Dries Buytaert Named a Young Global Leader by the World Economic Forum
- Getting Rid of Spam
- QuickStart: Replication & Recovery 1.2
- 1998 Atlanta Linux Showcase
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




3 hours 34 min ago
6 hours 7 min ago
7 hours 24 min ago
7 hours 59 min ago
8 hours 21 min ago
13 hours 10 min ago
13 hours 56 min ago
15 hours 30 min ago
17 hours 7 min ago
19 hours 5 min ago