Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources]

James Lee, Brent Ware

نسخه متنی -صفحه : 136/ 117
نمايش فراداده

11.4 A Quick Example

Let's show an example. We create the file /var/wwwl/mason/testl and place the following contents within it: [1]

[1] Set the permissions to 644, but you already knew that, right?

% my $name = ´John Doe´; 
<l> 
<head> 
<title>Mason Example</title> 
</head> 
<body bgcolor="#ffffff"> 
Hello, <% $name %>! 
<br>2 + 3 = <% 2 + 3 %> 
</body> 
<l> 

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() variablesif they are not declared with my(), a syntax error is generated.

[2] For lines that begin with "%," that "%" must be the first character in the line.

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/testl or www.opensourcewebbook.com/mason/testl. This should display a page similar to Figure 11.1.

Figure 11.1. Mason example