11.4 A Quick Example


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

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

 % 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.

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

Figure 11.1. Mason example

graphics/11fig01.gif



Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net