Creating Dynamic Pages With Embperl

By: Paul Vining

Now that we have an Apache server setup to parse through our pages we can use Embperl to create dynamic pages. These are just samples of interesting ways to use Embperl that I have found; there are literally unlimited ways to use it to generate web pages.

Browser detection.

Let's say that you just revamped a website and you have the most amazing home page that uses all the latest technologies, but you want to allow older browsers to still see the older version of the site. You could use javascript and basically generate the page that the browser needs. This would solve the problem but it could get real messy depending on the complexity of the site. The cleanest and easiest to maintain solution is to use Embperl.

[$ if $ENV{HTTP_USER_AGENT} =~ m/4\.\d{0,3}/ $]
<HTML><HEAD></HEAD><BODY>New 4.0 browser page</BODY></HTML>
[$ else $]
<HTML><HEAD></HEAD><BODY>Old 3.0 browser page</BODY></HTML>
[$ endif $]
This allows you to essentially have two "pages" in one file. This is easier to maintain and edit in the future. Another use of browser detection is to change the background image for better alignment of images. I have found that rather than have images that don't exactly align in either browser; I can make two images (one for each browser) and use the correct one dynamically.
<HTML><HEAD></HEAD>
<BODY BGCOLOR="#85BFE4" BACKGROUND="images/
  [$if ($ENV{HTTP_USER_AGENT} =~ /MSIE/i) $] 
	iebackground.gif
  [$else$] 
	nsbackground2.gif[$endif$]" 
leftmargin=0 topmargin=0>
</BODY></HTML>
The beauty of doing browser detection this way is that if a person views the source they think that the page was written just for them, because it really was.

Generating form elements.

Usually with forms online you have many different elements with very similar code. For instance, you have many different shirts and would like a user to be able to select a few different ones. Rather than write highly repetitive code and have difficult updates in the future, we can use perl to write them for us.

<FORM>
<TABLE>
[$ foreach $five_amounts (1..5) $]
<TR>
    <TD>
        <SELECT NAME="HawaiianShirt[+ $five_amounts +]">
        <OPTION VALUE="">Select Style
        <OPTION VALUE="Mauna Lau">Mauna Lau
        <OPTION VALUE="Flowers">Flowers
        <OPTION VALUE="Vegas">Vegas
        <OPTION VALUE="Cards">Cards
        <OPTION VALUE="Sharks">Sharks
        </SELECT>
    </TD>
    <TD>Size:
        <SELECT NAME="SizeHawaiianShirt[+ $five_amounts +]">
        <OPTION VALUE="">
        <OPTION VALUE="small">small
        <OPTION VALUE="medium">medium
        <OPTION VALUE="large">large
        <OPTION VALUE="Xlarge">Xlarge
        <OPTION VALUE="XXlarge">XXlarge
        </SELECT>  
    </TD>
    <TD>Quantity:
        <SELECT NAME="QuantityHawaiianShirt[+ $five_amounts +]">
        <OPTION VALUE="">
        <OPTION VALUE="1">1
        <OPTION VALUE="2">2
        <OPTION VALUE="3">3
        </SELECT>
</TD>
</TR>
[$ endforeach $]
</TABLE>
</FORM>
This way to allow another shirt selection you simply change the looping variable, or if you want to add a different style you need only add it once not five times.

Form processing, self-contained style.

If you find yourself poking around in the cgi-bin after looking at the form to remember where each form goes for processing, let me suggest that you can put your processing script "in" your page. By using a looping variable you can determine if the visitor is in the "input mode" or in the "processing mode".

[- $pass = $fdat{pass} -]
[$ if $pass == 1 $]
[- . . . perl processing script . . .-]
<HTML><HEAD></HEAD><BODY>Thank You for your order</BODY></HTML>
[$ else $]
<HTML><HEAD></HEAD><BODY>
<FORM ACTION="form.htm">
<INPUT TYPE="Text" NAME="phone_number" value="" size="14">
<INPUT TYPE="Hidden" NAME="pass" VALUE="1">
<INPUT TYPE="Submit">
</FORM>
</BODY></HTML>
[$ endif $]
By putting the processing script in with the form it is easier to maintain and debug in the future. You can also create a customized thank you page quite trivially.

Using modules

You can even use perl modules in your html pages. For instance, you would like to display the date like a newspaper does at the top of your page. You need to know what day of the week the current day is and display it accordingly.

[- use Date::Calc qw(:all) -]
<HTML><HEAD></HEAD>
<BODY>
[- ($year,$month,$day) = Date::Calc::Today() -]
[+ Date::Calc::Month_to_Text($month) +] [+ $day +], [+ $year +]
</BODY>
</HTML>
This example displays the date in the format "January 1, 1999". Anything you can do with perl you can do on demand with embedded perl. I have done full telnet sessions, ftp, and generation of images with embedded perl.

Wrapup

These examples are just the tip of the iceberg in embedding perl in your web pages. You can use all the server's environment variables and perl modules in your coding quite easily. Next month I will discuss how to connect to databases and use the output in a dynamic website.

If you have any questions about the examples, or would like something discussed in a future column, feel free to email me.

-Paul Vining paul@gina.net