Building a Form

Now let’s turn our attention to the &show_student_form routine. When this routine runs, it produces an HTML page that looks something like Figure 17-1.

Form produced by the make_page.cgi script

Figure 17-1. Form produced by the make_page.cgi script

Examining the subroutine code in more detail, we see that after some preliminaries it has the following:

    $content = join "\n",
        "<P ALIGN=\"center\"><STRONG><A HREF=\"$web_root/maint/make_page.cgi\">Return to 
the maintenance page</A></STRONG></P>",
        '<H1 ALIGN="center">Make a student page</H1>',
        start_form, 
        '<TABLE><TR><TD ALIGN="right">',
        b('Teacher:'),
        '</TD><TD>',

And so on. This demonstrates another Perl idiom that seems to show up a lot in my CGI scripts: long chains of arguments to the join function. I often use long join statements, connecting (via \n, or some other suitable joining string) a long list of form elements and HTML embellishment. I tend to use single quotes to enclose the HTML, which saves me from having to backslash all the embedded double quotes surrounding the HTML attributes. This means I can’t interpolate variables into the strings, but that’s okay with me because I just terminate the single-quoted string, throw in a comma, and add the variable as another element in that long chain of join arguments. This works nicely for CGI.pm functions, which can’t easily be interpolated inside double-quoted strings the way variables can.

I also sometimes ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.