DBI and CGI

In this section, we’ll show Perl DBI and MySQL in what is probably the most popular context for their use: combined with the popular Perl CGI module to serve dynamic web pages.

As a protocol, CGI is the universal method by which web servers accept input from users and generate results on the fly. The CGI module in Perl makes it easy for programmers to retrieve user input and create output. In order to dispel any sense of magic surrounding CGI, let’s look at how data travels from web form to Perl program.

An HTML form may contain a text field such as:

<input type="text" name="titleword" />

If a user enters the word Linux into the field and presses the Submit button, the field is sent to the web server. So if the form containing the input field started out like this:

<form method="post" action="/cgi-bin/book_view.cgi"
      enctype="application/x-www-form-urlencoded">

the user’s browser would request the URL:

/cgi-bin/book_view.cgi?titleword=Linux

In other words, the browser requests the URL specified in the form’s action field, with the field name and the field value tacked on at the end. The server invokes the book_view.cgi program, using a special protocol to pass the field name and field value. So now we have to shift our attention to the Perl program.

Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.