Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP

As we saw earlier, Embperl commands start with a " [ " and end with a " ] ". Because " [ " has a special meaning, to put a " [ "on your web page, you must write it as " [[ ".

10.5.1 [+ Perl Code +]

The code within [+ ... +] is executed, and the result is that the command is replaced with what the code evaluates to. Thus [+ "hello!" +] is the same as print "hello!" in a normal CGI program. For example:

<p>Hello [+ $name +]</p>

If the value of $name is "Gerald Richter", the result of the previous command after Embperl is through processing it is this:

<p>Hello Gerald Richter</p>

A command can contain any Perl expression, including array elements, hash elements, and function calls:

[+ $i+1 +] [+ $a[$i] +] [+ $info{name} +] [+ generate_link() +]

Embperl can be used to create links. [4] Check out /var/www/html/embperl/link.html :

[4] "Ha! Big deal!" you might think. "I can do that with regular HTML." Of course you can, but with Embperl, you can generate links dynamically.

[- $url = http://www.opensourcewebbook.com/; -] <html> <head> <title>The World of Open Source Web Development</title> </head> <body bgcolor="#ffffff"> The fun begins here: <a href="[+ $url +]">[+ $url +]</a> </body> </html>

The first part of this HTML file is similar to the previous example, but the URL shown on the web page is inserted via the variable $url in this file.

The line beginning with <a href executes the Embperl code. The code [+ $url +] is evaluated within the double quotes first and evaluated within the >...< a second time. This creates the following valid HTML link:

<a href="http://www.opensourcewebbook.com/"> http://www.opensourcewebbook.com/</a>

This example can be seen by loading one of these URLs into your browser: http://localhost/embperl/link.html or www.opensourcewebbook.com/embperl/link.html. This generates Figure 10.2. Of course, much more fun is to be had if you click the link on the page and go down that rabbit hole.

Figure 10.2. Generating a link in Embperl

10.5.2 [- Perl Code -]

In the previous examples, we've used the construct [- ... -] repeatedly. This command executes the Perl code, but the return value of the code isn't printed into the HTML, as it is with the [+ ... +] construct. However, if you print() within the [- ... -] , that printed text does find its way onto the HTML page. The easy way to remember this is that the results of whatever you put inside the [+ ... +] end up in the browser, while that inside [- ... -] doesn't (unless you print() ).

Here is one example we have already seen:

[- $msg = hello, world!; -]

This assigns a value to the variable $msg but doesn't generate any output that would be displayed by the browser.

This command can be used to execute arbitrary code, such as:

[- # connect to a MySQL database use DBI; $user = someuser; $pass = somepassword; $dbh = DBI->connect(DBI:mysql:somedb, $user, $pass); -]

That code does a lot of database things that might need to be done, but it does not generate any HTML that is sent to the browser.

10.5.3 [! Perl Code !]

This command is the same as [- ... -] except that the Perl code within is executed only the first time the page is requested (or when the page is modified). Recall that in Embperl, the page is compiled the first time it is requested , and then it is cached for quick execution for all following requests .

This command is useful for defining subroutines or initializing variables . Here is an example:

[! sub create_link { my($url) = @_; return "<a href=\"$url\">$url</a>"; } $HOME_URL = http://www.opensourcewebbook.com/; !]

10.5.4 [# Some Text #]

This is a comment block. Everything between [# ... #] is removed from the output.

Because the pages we discuss here are being processed by Embperl, [# ... #] can be used instead of the HTML comment marks <!-- ... --> .

10.5.5 [ $ Command Arguments $ ]

The [$ ... $] metacommand contains Perl commands such as the flow control constructs if , while , and foreach . This is the syntax for the if metacommand:

[$ if ( condition )$]... [$ endif $]

It includes the contents of the if section if the condition is true. For example:

[$ if ($day eq Monday)$] Today is <b>Monday</b>, so my condolences.<br> [$ endif $]

The [$ elsif $] and [$ else $] are optional and work similarly. An example is in /var/www/htdocs/embperl/if.html :

[- # assign date the current date/time (as if we # executed `date`) $date = localtime(); -] <html> <head> <title>Embperl if</title> </head> <body bgcolor="#ffffff"> Today's date is: <b>[+ $date +]</b>. <hr> [# check to see if $date begins with `Mon #] [# if so, it is Monday #] [$ if ($date =~ / ^ Mon/) $] Today is <b>Monday</b>, so my condolences. [# check to see if $date begins with `Sat #] [# or `Sun, if so, it is the weekend #] [$ elsif ($date =~ / ^ (SatSun)/) $] Today is a <b>weekend day</b>, so take the day off! [# otherwise, just a normal work day #] [$ else $] Today is an <b>acceptable weekday</b>, so get some work done. [$ endif $] </body> </html>

To view the result, load one of these URLs into your browser: http://localhost/embperl/if.html or www.opensourcewebbook.com/embperl/if.html. You should see something similar to Figure 10.3.

Figure 10.3. An Embperl if metacommand

In addition to the condition if construct, Embperl has looping constructs. This is the syntax for the while metacommand:

[$ while ( condition )$]... [$ endwhile $]

The while loop behaves like Perl's while loop ”it includes the contents of the while section as long as condition is true. In the following example, the while loop prints all of Apache's environment variables stored within %ENV . Note that the code within [- ... -] is embedded in the middle of the HTML document ”Embperl commands can be anywhere within the HTML document.

<html> <head> <title>Embperl while</title> </head> <body bgcolor="#ffffff"> <h1>Environment Variables</h1> [- # initialize $i, the loop control variable # used within the while loop below $i = 0; # grab the keys and sort them, storing them # in @k @k = sort(keys(%ENV)); -] [# loop through @k, printing the variable names #] [# and their values #] [$ while ($i <= $#k) $] [+ $k[$i] +] = [+ $ENV{$k[$i]} +]<br> [- $i++; -] [$ endwhile $] </body> </html>

Again, note that there are [- ... -] commands in the middle of the page, including [- $i++; -] inside a [$ ... $] command.

To see the result, look at either http://localhost/embperl/while.html or www.opensourcewebbook.com/embperl/while.html. You should see something similar to Figure 10.4.

Figure 10.4. Displaying environment variables with Embperl's while command

Embperl has a foreach loop similar to Perl's foreach loop, passing the control_var through list and executing the body for each of the elements. This is the syntax:

[$ foreach control_var ( list )$]...[$endforeach $]

The while loop in the previous example, which prints the environment variables, is better written with foreach :

<html> <head> <title>Embperl foreach</title> </head> <body bgcolor="#ffffff"> <h1>Environment Variables Redux</h1> [# loop through the sorted keys of %ENV, printing #] [# the keys and their values #] [$ foreach $key (sort keys %ENV) $] [+ $key +] = [+ $ENV{$key} +]<br> [$ endforeach $] </body> </html>

Executing this code produces the same result as the while loop, as seen in Figure 10.4.

You can see it by going to either of these URLs: http://localhost/embperl/foreach.html or www.opensourcewebbook.com/embperl/foreach.html.

Embperl Language Notes

There is not a [$ for ... $] metacommand in Embperl. The [$ while ... $] command can be used instead (as it can in every other language with those constructs also).

There is a [$ do $] ... [$ until condition $] metacommand. We don't use it much ”we rarely find much application for do ... while or do ... until loops in HTML pages. If you care to, you can read about these things at the Embperl web site, http://perl.apache.org/embperl/Embperl.pod.cont.html, or perldoc HTML::Embperl .

Категории