There are several different ways to embed Perl code into the Mason pages: <% ... %> sections, "%" lines, and <%perl> ... </%perl> blocks. The next sections explore these.
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.
This type of Perl code is most useful for creating global variables and for embedding conditional and looping constructs.
Put this example in /var/wwwl/mason/percentl:
% my $client = $ENV{REMOTE_ADDR}; <l> <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> <l>
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 addressif 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/percentl or www.opensourcewebbook.com/mason/percentl. You should see something that resembles Figure 11.2.
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/wwwl/mason/condl as an example of these two conditional constructs:
<l> <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() =~ /^(Sat|Sun)/) { and it is NOT a weekend. % } else { and it is a weekend! % } </table> </body> <l>
To view the result of this block of code, load one of these URLs: http://localhost/mason/condl or www.opensourcewebbook.com/mason/condl. This should produce a page that resembles what is shown in Figure 11.3.
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/wwwl/mason/loopl:
<l> <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> <l>
To view the result, load either of these URLs: http://localhost/mason/loopl or www.opensourcewebbook.com/mason/loopl. This produces a page similar to Figure 11.4.
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 assignmentsessentially anything Perl.
This example is in /var/wwwl/mason/perll:
<l> <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> <l>
To see the result of this code, load one of the following URLs into your browser: http://localhost/mason/perll or www.opensourcewebbook.com/mason/perll. This page generates an output that resembles Figure 11.5.