⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06_05.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
字号:
<?label 6.5. Mason?><html><head><title>Mason (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch06_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm">CGI Programming with Perl</a></td><td width="172" valign="top" align="right"><a href="ch07_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">6.5. Mason</h2><p>The <a name="INDEX-1489" /> <a name="INDEX-1,490" /><a name="INDEX-1491" /> <a name="INDEX-1,492" />HTML::Mason Perl module, often referredto as <em class="firstterm">Mason</em>, is another <a name="INDEX-1493" />template solution. Like Embperl, itallows you to embed full Perl expressions within HTML documents.However, unlike the other template solutions already mentioned, Masonfocuses on supporting <a name="INDEX-1494" />components that can be embedded withinone another. This goes beyond creating modular CGI code. For many websites, especially large ones, multiple elements on each page as wellas overall page layout is shared across many pages. Mason allows youto modularize HTML as well as code and reuse both throughout your website.</p><p>For example, a web page may be composed of a header and footer thatis shared across your web site and possibly a navigation sidebar thatis shared across many pages. With Mason you can create components foreach of these that can easily be included in documents. Mason doesnot distinguish between static components and dynamic components; anycomponent may include code and include other components. Mason alsoallows components to act as filters.</p><p>Although it also supports a <a name="INDEX-1495" />CGI mode, Mason -- more thanEmbperl -- essentially requires<em class="emphasis">mod_perl</em><a name="INDEX-1496" />. First, due to thecomponent nature of Mason, it makes much more sense to have it handlefiles directly instead of having requests pass through a CGI script.Second, because Mason is written entirely in Perl (unlike Embperl,which contains compiled C), it is much less efficient when not usedwith <em class="emphasis">mod_perl</em>, because with<em class="emphasis">mod_perl</em> the Perl source is only loaded,interpreted, and compiled once instead of with each request.</p><p>Thus, Mason is not truly CGI technology. On the other hand, givenMason's increasing popularity, it seems inappropriate to omitMason in a discussion of HTML template solutions. We will limit ourdiscussion to an overview that allows you to compare against othersolutions. For more information on Mason, visit the site at<a href="http://www.masonhq.com/">http://www.masonhq.com/</a>.</p><a name="ch06-23-fm2xml" /><div class="sect2"><h3 class="sect2">6.5.1. A Component Approach</h3><p>Mason's <a name="INDEX-1497" />component approach differs from theother solutions we've discussed; those solutions differed fromeach other primarily by the degree of power and complexity of theirtemplate commands. You can create a component architecture with theother template solutions as well, but not to the same degree that youcan with Mason. Here is how the other solutions compare in thisregard:</p><ul><li><p>As we saw in our footer example earlier (see <a href="ch06_01.htm">Chapter 6, "HTML Templates"</a>), <a name="INDEX-1498" />SSI's <tt class="command">include</tt>command works great but SSI commands are limited to static documents:you can include CGI script output in HTML documents but not viceversa.</p></li><li><p><a name="INDEX-1499" />HTML::Templatehas a similar TMPL_INCLUDE command, but you can only include literalfile contents that are interpreted as part of the same template andexecuted in the context of the current CGI script. HTML::Template wasnot designed to allow the dynamically generated output of one CGIscript in the output of another. It is possible, but it's messy(refer to the FAQ section of the HTML::Template documentation).</p></li><li><p><a name="INDEX-1500" />Embperl is powerfulenough that you can make it do just about whatever you need, but itsemphasis is not on components, and it does not have all of thefeatures that Mason has for filtering or automatically executingcomponents according to preset rules.</p></li><li><p>Mason does not draw a distinction between<a name="INDEX-1501" /><a name="INDEX-1502" />files containing code, filescontaining HTML, or even files containing data. Any text file can beincluded as a component within any other file. Mason also supports<a name="INDEX-1503" /><a name="INDEX-1504" /><a name="INDEX-1505" />filters and autohandlers that allowyou to modify the<a name="INDEX-1506" />output of existingcomponents without editing them directly.</p></li><li><p>For the sake of comparison, <a name="INDEX-1507" />CGI.pm does not offer a way to include theoutput of one dynamic request within another without using anothermodule such as LWP. Typically, however, you would move the code thatneeds to be shared into a module that can be used by several CGIscripts. Static content can of course be manually included by readingand printing the file in Perl.</p></li></ul><p>Does this mean that Mason is a better solution than the others? Notat all -- it is only better suited for those sites with manyshared components that can benefit from it. Mason can make such siteseasier to maintain if they are designed well, but it does addcomplexity because maintainers must understand all of theinteractions between the different components. What looks like asingle page to a browser may be comprised of several separatecomponents, so maintainers need to know where to look to makechanges. It also requires HTML designers to work closely with CGIdevelopers because the line between the two can get fuzzy with Mason.However, for large sites where it makes sense, it <a name="INDEX-1508" /> <a name="INDEX-1,509" />can certainly be<a name="INDEX-1510" />anelegant <a name="INDEX-1511" /><a name="INDEX-1512" /><a name="INDEX-1513" /><a name="INDEX-1514" />solution.</p></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch06_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch07_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">6.4. Embperl</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">7. JavaScript</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -