6.10 Programming Codeeperl

6.10 Programming Code”eperl

A nice feature of WML is that executable Perl code can be embedded (embedded Perl code in WML= eperl!= HTML::Embperl ) [8] within the HTML document, allowing the performance of arbitrary computations to be output in HTML.

[8] Covered in Chapter 10.

Perl code can be inserted into the document using the <: ... :> syntax. For a simple example, check out /var/www/html/wml/eperl1.wml :

 <:      print "Hello, world!";  :> 

To build the HTML use:

 $  wmk eperl1.wml  wml -n -o eperl1.html eperl1.wml 

The resulting HTML is in /var/www/html/wml/eperl1.html :

 Hello, world! 

We are sure you can guess what this looks like, but to prove that you are right, check out www.opensourcewebbook.com/wml/eperl1.html or http://localhost/wml/eperl1.html . This example illustrates that all the stuff printed within the eperl block is output by wmk .

Some people (not us) would argue that having to print() text in the eperl block to have it displayed is a pain”why not just have what is in the block displayed? To address this, WML allows the following syntax, which is an implied print:

 <:=      "Hello, world!";  :> 

We prefer not to use this syntax”we want to tell WML when we want our text to be outputted, but you may disagree . That's fine”TMTOWTDI to the rescue.

Eperl allows arbitrary computations. This example creates a Perl hash variable and processes it, generating links to web sites. It can be found in /var/www/html/wml/eperl2.wml :

 <:       %websites = (Google   => http://www.google.com/,           OSWB     => http://www.opensourcewebbook.com/,           HLE      => http://www.hackinglinuxexposed.com/,           BLVPNs   => http://www.buildinglinuxvpns.net/);       foreach $site (sort keys %websites) {           print "<a href=\"$websites{$site}\">$site</a><br>\n";       }  :> 

This code assigns a hash variable, %websites , information about four web sites. Then the hash is sorted by its keys, and the HTML is printed.

The HTML is generated with:

 $  wmk eperl2.wml  wml -n -o eperl2.html eperl2.wml 

The resulting HTML is in /var/www/html/wml/eperl2.html :

 <a href="http://www.buildinglinuxvpns.net/">BLVPNs</a><br>  <a href="http://www.google.com/">Google</a><br>  <a href="http://www.hackinglinuxexposed.com/">HLE</a><br>  <a href="http://www.opensourcewebbook.com/">OSWB</a><br> 

There are now four links that connect to four important web sites. To follow these links, go to www.opensourcewebbook.com/wml/eperl2.html or http://localhost/wml/eperl2.html .

This can be quite useful in templates”for instance, to generate a bread crumb trail (a list of links that show where you are in the web site and that link to other pages on the web site”for an example, see the part of www.opensourcewebbook.com/contents/wml/ that begins "Page Path"). This can be done by creating a template that accepts a variable called $(page) that is defined in this form:

 URI1:Text1:URI2:Text2:URI3:Text3:Other text 

For instance, using this value:

 /book1/:Book 1:/book1/chapter3/:Chapter 3:Section 2 

the first pair, /book1/ and Book 1 , ends up as a link that looks like this:

 <a href="/book1/">Book 1</a> 

The second pair, /book1/chapter3/ and Chapter 3 , ends up as a link that looks like this:

 <a href="/book1/chapter3">Chapter 3</a> 

The last element of $(page) , Section 2 , is not part of a pair, so it is printed simply as plain text.

Moreover, this example demonstrates the use of $(ROOT) , which was defined in the . wmlrc file as -DROOT~. .

Recall that this means that the directory that contains the .wmlrc file, here /var/www/html/ , will be the root of the HTML document tree. Then, any WML file that uses this variable will have the link magically created so that it is relative to the directory that contains the .wml file. For instance, if the file is in the directory /contents/ , this:

 <a href="$(ROOT)/sourcecode/"> 

becomes this:

 <a href="http://sourcecode/"> 

If the file is in the directory /contents/wml/ , the result becomes:

 <a href="../http://sourcecode/"> 

Look at the template in /var/www/html/wml/breadcrumb.wml :

 <html>  <head>  <title>Bread Crumb Example</title>  </head>  <body bgcolor="#ffffff">  <:      # grab the page information passed in      my $page_string = "$(page)";      # if the page info is not blank, process it      if ($page_string ne "") {          # first, print some red text          print "<font color=\"#FF0000\"><b>Page Path - </b></font>";          # split the line on the colon in case we are passing more          # than one piece of information as in:          #   href1:text1:href2:text2:final text          # loop through, building the link          my @page = split(/:/, $page_string);          while (@page > 1) {              my $href="$(ROOT)" . shift(@page);              my $text = shift(@page);              print "<a href=\"$href\"><font color=\"#999966\">                            <b>$text</b></font></a> - ";          }          print "$page[0]";      } else {          print "No bread crumbs!\n";      }  :>  </body>  </html> 

The first thing done in this eperl block is to take the WML variable $(page) and convert it to a Perl variable with $page_string = "$(page)" . The double quotes are mandatory”if they aren't used, Perl will not interpret the contents correctly.

The code then checks to ensure that the string is not empty. If not, it is processed . If it is an empty string, the code simply prints "No bread crumbs." This won't be necessary for a real template, but it will give us a warm fuzzy feeling while testing the code.

Processing the page string starts by printing some HTML to display the red text Page Path - . Then the string is split on the colon, and while there is still more than one element in the list of stuff, the template outputs the HTML to build the link. Finally, the last text is printed, which is the text with no partner, here stored in $page[0] .

The code that use s this template is /var/www/html/wml/eperl3.wml (this should all be on one line, but it wrapped here so that it would fit on the page):

 #use wml::breadcrumb page="/contents/:Contents:/contents/wml/                            :WML:Bread Crumbs" 

It's exceptionally simple”just a use statement. The variable page is assigned a string that has five pieces separated by colons. Therefore, we should end up with two links and some text at the end.

Use this to build the HTML:

 $  wmk eperl3.wml  wml -n -o eperl3.html eperl3.wml 

The resulting code is in /var/www/html/wml/eperl3.html . It should be noted that the line beginning with <font color= and ending prior to </body> is all one line and is broken for clarity:

 <html>  <head>  <title>Bread Crumb Example</title>  </head>  <body bgcolor="#ffffff">  <font color="#FF0000"><b>Page Path - </b></font>    <a href="http://contents/"><font color="#999966">             <b>Contents</b></font></a> -   <a href="http://contents/wml/"><font color="#999966">             <b>WML</b></font></a> - </body>  </html> 

The result can be seen in Figure 6.10. To see for yourself, go to www.opensourcewebbook.com/wml/eperl3.html or http://localhost/wml/eperl3.html .

Figure 6.10. Bread crumbs


One important thing to note”check out these link href s:

 href="http://contents/"  href="http://contents/wml/" 

They are relative links based on the variable $(ROOT) .

Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net