Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
There are several different ways to embed Perl code into the Mason pages: <% ... %> sections, " % " lines, and <%perl> ... </%perl> blocks. The next sections explore these. 11.5.1 The <% ... %> Tag
These tags are useful for generating the values of variables and complex expressions. This example code generates HTML using these tags: Name: <% $name %> Age: <% $age %> Address: <% %address %> Price : <% $price*(1+$tax)%> The HTML generated is the result of the evaluation of the variables within the tags, including the evaluation of the final arithmetic expression. 11.5.2 % Lines
This type of Perl code is most useful for creating global variables and for embedding conditional and looping constructs. Put this example in /var/www/html/mason/percent.html : % my $client = $ENV{REMOTE_ADDR}; <html> <head> <title>Percent Lines with Mason</title> </head> <body bgcolor="#ffffff"> Hello <% $client %>! <hr> % if ($client =~ /127\.0\.0\.1/) { We are called from localhost. % } else { We are called from elsewhere. % } <hr> % my $i=0; % while ($i < 10) { <tt>$i == <% $i %></tt> <br> % $i++; % } </body> </html> The file begins by setting $client to the client IP address stored in %ENV (notice that Mason has access to the Apache environment variables stored in %ENV ). Within the body of the HTML is the code <% $client %> , which is replaced with the client IP address ”if this program is accessed by a client that is the localhost , this address will be 127.0.0.1 . An if statement checks the client IP address. If it is 127.0.0.1 , the localhost , the program says so. Otherwise, it prints a statement saying the program was called from elsewhere. The code then loops from 0 to 9, including the values of the integers (such as $i = 0 ) in the HTML. Again, $i must be declared as a my() variable. To view this file, load either of these URLs: http://localhost/mason/percent.html or www.opensourcewebbook.com/mason/percent.html. You should see something that resembles Figure 11.2. Figure 11.2. Mason % lines
More on Perl Constructs
All of Perl's constructs can be embedded in a Mason HTML file. Next we demonstrate the syntax for each construct and then show a complete example illustrating each construct. The if Statement This is the syntax for the if : % if ( condition ){ % } elsif { % } elsif { % } else { % } As usual, the elsif and the else are optional. The unless Statement This is the syntax for the unless : % unless ( condition ){ % } else { % } The else is optional. Put the following code in /var/www/html/mason/cond.html as an example of these two conditional constructs: <html> <head> <title>Mason Conditional Constructs</title> </head> <body bgcolor="#ffffff"> <h3><tt>if</tt> Statement</h3> % if ($ENV{HTTP_USER_AGENT} =~ /mozilla/i) { Hello Mozilla browser! % } else { You are not the Mozilla browser. % } <h3><tt>unless</tt> Statement</h3> Today is <% scalar(localtime()) %> % unless (localtime() =~ / ^ (SatSun)/) { and it is NOT a weekend. % } else { and it is a weekend! % } </table> </body> </html> To view the result of this block of code, load one of these URLs: http://localhost/mason/cond.html or www.opensourcewebbook.com/mason/cond.html. This should produce a page that resembles what is shown in Figure 11.3. Figure 11.3. Mason conditional statements
The while Loop The syntax for the while loop is: % while ( condition ) { % } The for Loop This is the syntax for the for loop: % for ( init_expression; condition; step_expression ) { % } The foreach Loop The syntax for the foreach loop is: % foreach my variable ( list ) { % } We'll show examples of each these looping constructs. Put the following code into the file /var/www/html/mason/loop.html : <html> <head> <title>Mason Looping Constructs</title> </head> <body bgcolor="#ffffff"> <h3><tt>while</tt> Loop</h3> <table border="1"> <tr><th>number</th><th>squared</td></tr> % my $i = 1; % while ($i <= 5) { <tr><td><% $i %></td><td><% $i ** 2 %></td></tr> % $i++; % } </table> <h3><tt>for</tt> Loop</h3> % for (my $j = 10; $j > 5; $j--) { $j = <% $j %><br> % } <h3><tt>foreach</tt> Loop</h3> <table border="1"> <tr><th>Variable</th><th>Value</th></tr> % foreach my $var (sort keys %ENV) { <tr><td><% $var %></td><td><% $ENV{$var} %></td></tr> % } </table> </body> </html> To view the result, load either of these URLs: http://localhost/mason/loop.html or www.opensourcewebbook.com/mason/loop.html. This produces a page similar to Figure 11.4. Figure 11.4. Mason looping statements
11.5.3 The <%perl> ... </%perl> Tag
The <%perl> ... </%perl> tags can be used to execute arbitrary amounts of Perl code. These tags can occur anywhere within the HTML file and can contain comments, functions, and variable assignments ” essentially anything Perl. This example is in /var/www/html/mason/perl.html : <html> <head> <title>Mason <%perl> Block</title> </head> <body bgcolor="#ffffff"> <%perl> # we can put arbitrary amounts of Perl # code within these tags # these tags can be embedded anywhere within the HTML # remember that all variables must be my() variables my $client = $ENV{REMOTE_ADDR}; my $agent = $ENV{HTTP_USER_AGENT}; my $method = $ENV{REQUEST_METHOD}; my $time = localtime(); # we can also include function definitions - this function # is called in the HTML below sub hello_world { return "hello, world!" } </%perl> Hello <b><% $client %></b>. <br> You are using <b><% $agent %></b>. <br> The local time is <b><% $time %></b>. <br> Your request method is <b><% $method %></b>. <br> A message: <b><% hello_world() %></b>. </table> </body> </html> To see the result of this code, load one of the following URLs into your browser: http://localhost/mason/perl.html or www.opensourcewebbook.com/mason/perl.html. This page generates an output that resembles Figure 11.5. Figure 11.5. Mason <%perl> ... </%perl> tag
|