[ Team LiB ] |
10.5 Embperl CommandsAs 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:
If the value of $name is "Gerald Richter", the result of the previous command after Embperl is through processing it is this:
A command can contain any Perl expression, including array elements, hash elements, and function calls:
Embperl can be used to create links.
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:
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 Embperl10.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:
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:
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:
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:
It includes the contents of the if section if the condition is true. For example:
The [$ elsif $] and [$ else $] are optional and work similarly. An example is in /var/www/htdocs/embperl/if.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 metacommandIn addition to the condition if construct, Embperl has looping constructs. This is the syntax for the while metacommand:
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.
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 commandEmbperl 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:
The while loop in the previous example, which prints the environment variables, is better written with foreach:
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 NotesThere 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. |
[ Team LiB ] |
No comments:
Post a Comment