Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources] - نسخه متنی

James Lee, Brent Ware

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










6.10 Programming Codeeperl


A nice feature of WML is that executable Perl code can be embedded (embedded Perl code in WML= eperl!= HTML::Embperl)
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 eperl1l eperl1.wml

The resulting HTML is in /var/www/html/wml/eperl1l:


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/eperl1l or http://localhost/wml/eperl1l. 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 painwhy 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 syntaxwe want to tell WML when we want our text to be outputted, but you may disagree. That's fineTMTOWTDI 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 eperl2l eperl2.wml

The resulting HTML is in /var/www/html/wml/eperl2l:


<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/eperl2l or http://localhost/wml/eperl2l.

This can be quite useful in templatesfor 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 sitefor 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:


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

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


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

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


<a href="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 , 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=">

becomes this:


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

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


<a href=">

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=" . 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 mandatoryif 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 uses 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 simplejust 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 eperl3l eperl3.wml

The resulting code is in /var/www/html/wml/eperl3l. 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 www.opensourcewebbook.com/wml/eperl3l or http://localhost/wml/eperl3l.


Figure 6.10. Bread crumbs


One important thing to notecheck out these link hrefs:


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

They are relative links based on the variable .


/ 136