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

📄 ch21_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Internals and Externals (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Internals and Externals"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch20_06.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part3.htm">Part 3: Perl as Technology</a></td><td align="right" valign="top" width="172"><a href="ch21_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 21.  Internals and Externals</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch21_01.htm">How Perl Works</a><br><a href="ch21_02.htm">Internal Data Types</a><br><a href="ch21_03.htm">Extending Perl (Using C from Perl)</a><br><a href="ch21_04.htm">Embedding Perl (Using Perl from C)</a><br><a href="ch21_05.htm">The Moral of the Story</a><br></p></div><a name="INDEX-3664"></a><a name="INDEX-3665"></a><a name="INDEX-3666"></a><p><a name="INDEX-3667"></a><a name="INDEX-3668"></a>As we discussed in <a href="ch18_01.htm">Chapter 18, "Compiling"</a>, <em class="emphasis">perl</em> (the program)contains both a compiler and an interpreter for programs written inPerl (the language).  The Perl compiler/interpreter is itself writtenin C.  In this chapter, we'll sketch how that C program works from theperspective of someone who wants either to extend or to embed Perl.  When you<em class="emphasis">extend</em> Perl, you're putting a chunk of C code (called the<em class="emphasis">extension</em>)  under the control of Perl, and when you <em class="emphasis">embed</em> Perlyou're putting a Perl interpreter<a href="#FOOTNOTE-1">[1]</a> under the control of a larger Cprogram.</p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1] While we are careful todistinguish the compiler from the interpreter when that distinction isimportant, it gets a bit wearisome to keep saying"compiler/interpreter", so we often just shorten that to "interpreter"to mean the whole glob of C code and data that functions like oneinstance of <em class="emphasis">perl</em> (the program); when you're embedding Perl, you canhave multiple instances of the interpreter, but each behaveslike its own little <em class="emphasis">perl</em>.</p></blockquote><p>The brief coverage we provide here is no substitute for the onlinedocumentation of Perl's innards: see the documentation for<em class="emphasis">perlguts</em>, <em class="emphasis">perlxs</em>, <em class="emphasis">perlxstut</em>, <em class="emphasis">perlcall</em>,<em class="emphasis">perlapi</em>, and <em class="emphasis">h2xs</em>, all bundled with Perl.  Again, unless you're extending or embeddingPerl, you will never need to know any of this stuff.</p><p>Presuming you need to know, what you need to know first is a bit aboutPerl's guts.  You'll also need to know C for most of what follows.  You'llneed a C compiler to run the examples.  If your end goal is to create amodule for other people to use, they'll need a C compiler too.  Many ofthese examples will only run on Unix-like systems.  Oh, and thismaterial is subject to change in future releases of Perl.</p><p>In other words, here be dragons.</p><h2 class="sect1">21.1. How Perl Works</h2><a name="INDEX-3669"></a><a name="INDEX-3670"></a><a name="INDEX-3671"></a><p><a name="INDEX-3672"></a>When the Perl compiler is fed a Perl program, the first task itperforms is <em class="emphasis">lexical analysis</em>: breaking down the program into itsbasic syntactic elements (often called <em class="emphasis">tokens</em>).  If the program is:<blockquote><pre class="programlisting">print "Hello, world!\n";</pre></blockquote>the lexical analyzer breaks it down into three tokens: <tt class="literal">print</tt>,<tt class="literal">"Hello, world!\n"</tt>, and the final semicolon.  The token sequence isthen <em class="emphasis">parsed</em>, fixing the relationship between the tokens.  In Perl,the boundary between lexical analysis and parsing is blurred more thanin other languages.  (Other computer languages, that is.  If you thinkabout all the different meanings <tt class="literal">new Critter</tt> might have depending onwhether there's a Critter package or a subroutine named <tt class="literal">new</tt>, you'llunderstand why.  On the other hand, we disambiguate these kinds ofthings all the time in English.)</p><p><a name="INDEX-3673"></a><a name="INDEX-3674"></a><a name="INDEX-3675"></a><a name="INDEX-3676"></a><a name="INDEX-3677"></a>Once a program has been parsed and (presumably) understood, it iscompiled into a tree of <em class="emphasis">opcodes</em> representing low-level operations,and finally that tree of operations is executed--unless you invokedPerl with the <span class="option">-c</span> ("check syntax") switch, which exits upon completingthe compilation phase.  It is during compilation, not execution, that<tt class="literal">BEGIN</tt> blocks, <tt class="literal">CHECK</tt> blocks, and <tt class="literal">use</tt> statements are executed.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch20_06.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch21_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">20.6. The Perl Profiler</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">21.2. Internal Data Types</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><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 shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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