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

📄 ch31_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Pragmatic Modules (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="Pragmatic Modules"><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="ch30_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part5.htm">Part 5: Reference Material</a></td><td align="right" valign="top" width="172"><a href="ch31_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 31.  Pragmatic Modules</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch31_01.htm#useatt">use attributes</a><br><a href="ch31_02.htm">use autouse</a><br><a href="ch31_03.htm">use base</a><br><a href="ch31_04.htm">use blib</a><br><a href="ch31_05.htm">use bytes</a><br><a href="ch31_06.htm">use charnames</a><br><a href="ch31_07.htm">use constant</a><br><a href="ch31_08.htm">use diagnostics</a><br><a href="ch31_09.htm">use fields</a><br><a href="ch31_10.htm">use filetest</a><br><a href="ch31_11.htm">use integer</a><br><a href="ch31_12.htm">use less</a><br><a href="ch31_13.htm">use lib</a><br><a href="ch31_14.htm">use locale</a><br><a href="ch31_15.htm">use open</a><br><a href="ch31_16.htm">use overload</a><br><a href="ch31_17.htm">use re</a><br><a href="ch31_18.htm">use sigtrap</a><br><a href="ch31_19.htm">use strict</a><br><a href="ch31_20.htm">use subs</a><br><a href="ch31_21.htm">use vars</a><br><a href="ch31_22.htm">use warnings</a><br></p></div><p>A <em class="emphasis">pragma</em> is a special kind of module that affects the compilationphase of your program.   Some pragmatic modules (or <em class="emphasis">pragmata</em>, forshort (or <em class="emphasis">pragmas</em>, for shorter)) may also affect the execution phaseof your program.  Think of these as hints to the compiler.  Becausethey need to be seen at compile time, they'll only work when invoked by a<tt class="literal">use</tt> or a <tt class="literal">no</tt>, because by the time a <tt class="literal">require</tt> or a <tt class="literal">do</tt> is run,compilation is long since over.</p><p>By convention, pragma names are written in all lowercase becauselowercase module names are reserved for the Perl distribution itself.When writing your own modules, use at least one capital letter in themodule name to avoid conflict with pragma names.</p><p>Unlike regular modules, most pragmas limit their effects to the rest ofthe innermost enclosing block from which they were invoked.  In otherwords, they're lexically scoped, just like <tt class="literal">my</tt> variables.Ordinarily, the lexical scope of an outer block covers any inner blockembedded within it, but an inner block may countermand a lexicallyscoped pragma from an outer block by using the <tt class="literal">no</tt> statement:<blockquote><pre class="programlisting">use strict;use integer;{    no strict 'refs';       # allow symbolic references    no integer;             # resume floating-point arithmetic    # ....}</pre></blockquote>More so than the other modules Perl ships with, the pragmas form anintegral and essential part of the Perl compilation environment.  It'shard to use the compiler well if you don't know how to pass hints toit, so we'll put some extra effort into describing pragmas.</p><p>Another thing to be aware of is that we often use pragmas to prototypefeatures that later get encoded into "real" syntax.  So in some programsyou'll see deprecated pragmas like <tt class="literal">use attrs</tt> whose functionality isnow supported directly by subroutine declaration syntax.  Similarly,<tt class="literal">use vars</tt> is in the process of being replaced by <tt class="literal">our</tt> declarations. And <tt class="literal">use subs</tt> may someday be replaced by an <tt class="literal">override</tt> attribute on ordinary subroutine declarations. We're not in a terrible hurry to break the old waysof doing things, but we do think the new ways are prettier.</p><a name="useatt"><h2 class="sect1">31.1. use attributes</h2></name><blockquote><pre class="programlisting">sub afunc : method;my $closure = sub : method { ... };use attributes;           @attrlist = attributes::get(\&amp;afunc);</pre></blockquote><p>The <tt class="literal">attributes</tt> pragma has two purposes.  The first is to provide aninternal mechanism for declaring <em class="emphasis">attribute lists</em>, which are optionalproperties associated with subroutine declarations and (someday soon) variable declarations.  (Since it's an internalmechanism, you don't generally use this pragma directly.)  The secondpurpose is to provide a way to retrieve those attribute lists at run time using the <tt class="literal">attributes::get</tt> function call.  In this capacity,<tt class="literal">attributes</tt> is just a standard module, not a pragma.</p><p>Only a few built-in attributes are currently handled by Perl.Package-specific attributes are allowed by an experimental extensionmechanism described in the section "Package-specific AttributeHandling" of the <em class="emphasis">attributes</em>(3) manpage.</p><p>Attribute setting occurs at compile time; attempting to set anunrecognized attribute is a compilation error.  (The error istrappable by <tt class="literal">eval</tt>, but it still stops the compilation within that <tt class="literal">eval</tt>block.)</p><p>Only three built-in attributes for subroutines are currentlyimplemented: <tt class="literal">locked</tt>, <tt class="literal">method</tt>, and<tt class="literal">lvalue</tt>.  See <a href="ch06_01.htm">Chapter 6, "Subroutines"</a>, and <a href="ch17_01.htm">Chapter 17, "Threads"</a>, for further discussion of these.  Thereare currently no built-in attributes for variables as there are forsubroutines, but we can think of several we might like, such as<tt class="literal">constant</tt>.</p><p>The <tt class="literal">attributes</tt> pragma provides two subroutines for general use.They may be imported if you ask for them.</p><dl><dt><b>get</b></dt><dd><p>This function returns a (possibly empty) list of attributes givena single input parameter that's a reference to a subroutine orvariable.  The function raises an exception by invoking <tt class="literal">Carp::croak</tt>if passed invalid arguments.</p></dd><dt><b>reftype</b></dt><dd><p>This function acts somewhat like the built-in <tt class="literal">ref</tt> function, but it always returns the underlying, built-in Perl data type of thereferenced value, ignoring any package into which it might havebeen blessed.</p></dd></dl><p></p><p>Precise details of attribute handling remain in flux, so you'dbest check out the online documentation included with your Perlrelease to see what state it's all in.</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="ch30_02.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="ch31_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">30.2. A Tour of the Perl Library</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">31.2. use autouse</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 + -