Let's show an example. We create the file /var/www/html/mason/test.html and place the following contents within it: [1]
% my $name = John Doe; <html> <head> <title>Mason Example</title> </head> <body bgcolor="#ffffff"> Hello, <% $name %>! <br>2 + 3 = <% 2 + 3 %> </body> </html> You will notice right away that our first Mason example is not "hello, world!". We apologize for the inconsistency. At the top of this file is % my $name = John Doe ; . The character " % " indicates that this line is Perl code to be executed. [2] The variable $name is defined using the my() function. Global variables in Mason must be my() variables ”if they are not declared with my() , a syntax error is generated.
The Perl code within <% ... %> is executed, and what that code evaluates to is replaced in the HTML file (much like Embperl's [+ ... +] ). Therefore, <% $name% > is replaced with the value John Doe , and <% 2 + 3 %> is replaced with the result 5. Try this page by loading either of the following URLs: http://localhost/mason/test.html or www.opensourcewebbook.com/mason/test.html. This should display a page similar to Figure 11.1. Figure 11.1. Mason example
|