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

📄 ch29_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</blockquote><a name="INDEX-4644"></a><a name="INDEX-4645"></a><a name="INDEX-4646"></a><a name="INDEX-4647"></a>This function attaches an address (a name) to an already opened socketspecified by the <em class="replaceable">SOCKET</em> filehandle. The function returns true if itsucceeded, false otherwise.<em class="replaceable">NAME</em> should be a packed address of the proper type for the socket.<blockquote><pre class="programlisting">use Socket;$port_number = 80;      # pretend we want to be a web server$sockaddr = sockaddr_in($port_number, INADDR_ANY);bind SOCK, $sockaddr or die "Can't bind $port_number: $!\n";</pre></blockquote>See <em class="emphasis">bind</em>(2).See also the examples in the section "Sockets" in<a href="ch16_01.htm">Chapter 16, "Interprocess Communication"</a>.</p><h3 class="sect2">29.2.6. binmode&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/xarg.gif"></h3><p><blockquote><pre class="programlisting">binmode <em class="replaceable">FILEHANDLE</em>, <em class="replaceable">DISCIPLINES</em>binmode <em class="replaceable">FILEHANDLE</em></pre></blockquote><a name="INDEX-4648"></a><a name="INDEX-4649"></a><a name="INDEX-4650"></a><a name="INDEX-4651"></a>This function arranges for the <em class="replaceable">FILEHANDLE</em>to have the semantics specified by the<em class="replaceable">DISCIPLINES</em> argument.  If<em class="replaceable">DISCIPLINES</em> is omitted, binary (or"raw") semantics are applied to the filehandle.  If<em class="replaceable">FILEHANDLE</em> is an expression, the value istaken as the name of the filehandle or a reference to a filehandle, asappropriate.</p><p>The <tt class="literal">binmode</tt> function should be called after the <tt class="literal">open</tt> but beforeany I/O is done on the filehandle. The only way to reset the mode on afilehandle is to reopen the file, since the various disciplines mayhave treasured up various bits and pieces of data in various buffers.This restriction may be relaxed in the future.</p><p>In the olden days, <tt class="literal">binmode</tt> was used primarily onoperating systems whose run-time libraries distinguished textfrom binary files.  On those systems, the purpose of<tt class="literal">binmode</tt> was to turn off the default text semantics.However, with the advent of Unicode, all programs on all systems musttake some cognizance of the distinction, even on Unix and Mac systems.These days there is only one kind of binary file (as far as Perl isconcerned), but there are many kinds of text files, which Perl wouldalso like to treat in a single way.  So Perl has a single internalformat for Unicode text, UTF-8.  Since there are many kinds of textfiles, text files often need to be translated upon input into UTF-8,and upon output back into some legacy character set, or some otherrepresentation of Unicode.  You can use disciplines to tell Perl howexactly (or inexactly) to do these translations.<a href="#FOOTNOTE-2">[2]</a></p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2]Moreprecisely, you <em class="emphasis">will</em> be able to use disciplinesfor this, but we're still implementing them as of thiswriting.</p></blockquote><p>For example, a discipline of <tt class="literal">":text"</tt> will tell Perl to do generictext processing without telling Perl which kind of text processing todo.  But disciplines like <tt class="literal">":utf8"</tt> and <tt class="literal">":latin1"</tt> tell Perl whichtext format to read and write.  On the other hand, the <tt class="literal">":raw"</tt>discipline tells Perl to keep its cotton-pickin' hands off the data.For more on how disciplines work (or will work), see the <tt class="literal">open</tt> function.  The rest of this discussion describes what <tt class="literal">binmode</tt> doeswithout the <em class="replaceable">DISCIPLINES</em> argument, that is, the historical meaning of<tt class="literal">binmode</tt>, which is equivalent to:<blockquote><pre class="programlisting">binmode <em class="replaceable">FILEHANDLE</em>, ":raw";</pre></blockquote>Unless instructed otherwise, Perl will assume your freshly opened fileshould be read or written in text mode.  Text mode means that<tt class="literal">\n</tt> (newline) will be your internal line terminator.  All systems use<tt class="literal">\n</tt> as the internal line terminator, but what that really representsvaries from system to system, device to device, and even file to file,depending on how you access the file.  In such legacy systems(including MS-DOS and VMS), what your program sees as a <tt class="literal">\n</tt> may notbe what's physically stored on disk.  The operating system might, forexample, store text files with <tt class="literal">\cM\cJ</tt> sequences that are translatedon input to appear as <tt class="literal">\n</tt> to your program, and have <tt class="literal">\n</tt> from yourprogram translated back to <tt class="literal">\cM\cJ</tt> on output to a file.  The <tt class="literal">binmode</tt> function disables this automatic translation on such systems.</p><p>In the absence of a <em class="replaceable">DISCIPLINES</em> argument, <tt class="literal">binmode</tt> has no effectunder Unix or Mac OS, both of which use <tt class="literal">\n</tt> to end each line andrepresent that as a single character.  (It may, however, be a differentcharacter: Unix uses <tt class="literal">\cJ</tt> and older Macs use <tt class="literal">\cM</tt>.  Doesn't matter.)</p><p>The following example shows how a Perl script might read a GIF imagefrom a file and print it to the standard output.  On systems thatwould otherwise alter the literal data into something other than itsexact physical representation, you must prepare both handles.While you could use a <tt class="literal">":raw"</tt> discipline directly in the GIF open,you can't do that so easily with pre-opened filehandles like <tt class="literal">STDOUT</tt>:<blockquote><pre class="programlisting">binmode STDOUT;open(GIF, "vim-power.gif") or die "Can't open vim-power.gif: $!\n";binmode GIF;while (read(GIF, $buf, 1024)) {    print STDOUT $buf;}</pre></blockquote></p><h3 class="sect2">29.2.7. bless&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/xarg.gif"></h3><p><blockquote><pre class="programlisting">bless <em class="replaceable">REF</em>, <em class="replaceable">CLASSNAME</em>bless <em class="replaceable">REF</em></pre></blockquote><a name="INDEX-4652"></a><a name="INDEX-4653"></a><a name="INDEX-4654"></a>This function tells the referent pointed to by reference <em class="replaceable">REF</em> thatit is now an object in the <em class="replaceable">CLASSNAME</em> package--or thecurrent package if no <em class="replaceable">CLASSNAME</em> is specified.If <em class="replaceable">REF</em> is not a valid reference, an exception is raised.For convenience, <tt class="literal">bless</tt> returns the reference, since it'soften the last function in a constructor subroutine.  For example:<blockquote><pre class="programlisting">$pet = Beast-&gt;new(TYPE =&gt; "cougar", NAME =&gt; "Clyde");# then in Beast.pm:sub new {    my $class  = shift;    my %attrs = @_;    my $self   = { %attrs };    return bless($self, $class);}</pre></blockquote>You should generally bless objects into <em class="replaceable">CLASSNAME</em>s that are mixedcase.  Namespaces with all lowercase names are reserved for internaluse as Perl pragmata (compiler directives).  Built-in types (such as"<tt class="literal">SCALAR</tt>", "<tt class="literal">ARRAY</tt>", "<tt class="literal">HASH</tt>", etc., not to mention the base classof all classes, "<tt class="literal">UNIVERSAL</tt>") all have uppercase names, so you may wishto avoid such package names as well.</p><p>Make sure that <em class="replaceable">CLASSNAME</em> is not false; blessing into falsepackages is not supported and may result in unpredictable behavior.</p><p>It is not a bug that there is no corresponding <tt class="literal">curse</tt> operator.  (Butthere is a <tt class="literal">sin</tt> operator.) See also <a href="ch12_01.htm">Chapter 12, "Objects"</a>, for moreabout the blessing (and blessings) of objects.</p><h3 class="sect2">29.2.8. caller&nbsp;&nbsp;&nbsp;&nbsp;</h3><p><blockquote><pre class="programlisting">caller <em class="replaceable">EXPR</em>caller</pre></blockquote><a name="INDEX-4655"></a><a name="INDEX-4656"></a><a name="INDEX-4657"></a>This function returns information about the stack of current subroutinecalls and such.  Without an argument, it returns the package name,filename, and line number that the currently executing subroutine wascalled from:<blockquote><pre class="programlisting">($package, $filename, $line) = caller;</pre></blockquote>Here's an example of an exceedingly picky function, making use of thespecial tokens <tt class="literal">__PACKAGE__</tt> and <tt class="literal">__FILE__</tt> described in <a href="ch02_01.htm">Chapter 2, "Bits and Pieces"</a>:<blockquote><pre class="programlisting">sub careful {    my ($package, $filename) = caller;    unless ($package  eq __PACKAGE__ &amp;&amp; $filename eq __FILE__) {        die "You weren't supposed to call me, $package!\n";    }    print "called me safely\n";}sub safecall {    careful();}</pre></blockquote>When called with an argument, <tt class="literal">caller</tt> evaluates <em class="replaceable">EXPR</em> as thenumber of stack frames to go back before the current one.  Forexample, an argument of 0 means the current stack frame, 1 meansthe caller, 2 means the caller's caller, and so on.  The functionalso reports additional information as shown here:<blockquote><pre class="programlisting">$i = 0;while (($package, $filename, $line, $subroutine,        $hasargs, $wantarray, $evaltext, $is_require,        $hints, $bitmask) = caller($i++) ){    ...}</pre></blockquote>If the frame is a subroutine call, <tt class="literal">$hasargs</tt> is trueif it has its own <tt class="literal">@_</tt> array (not one borrowed fromits caller). Otherwise, <tt class="literal">$subroutine</tt> may be<tt class="literal">"(eval)"</tt> if the frame is not a subroutine call, butan <tt class="literal">eval</tt>.  If so, additional elements<tt class="literal">$evaltext</tt> and <tt class="literal">$is_require</tt> areset: <tt class="literal">$is_require</tt> is true if the frame is created bya <tt class="literal">require</tt> or <tt class="literal">use</tt> statement, and<tt class="literal">$evaltext</tt> contains the text of the<tt class="literal">eval</tt><em class="replaceable">EXPR</em> statement.  Inparticular, for a <tt class="literal">eval</tt><em class="replaceable">BLOCK</em> statement,<tt class="literal">$filename</tt> is <tt class="literal">"(eval)"</tt>, but<tt class="literal">$evaltext</tt> is undefined.  (Note also that each<tt class="literal">use</tt> statement creates a <tt class="literal">require</tt>frame inside an <tt class="literal">eval</tt><em class="replaceable">EXPR</em> frame.)  The <tt class="literal">$hints</tt>and <tt class="literal">$bitmask</tt> are internal values; please ignorethem unless you're a member of the thaumatocracy.</p><p><a name="INDEX-4658"></a>In a fit of even deeper magic, <tt class="literal">caller</tt> also sets thearray <tt class="literal">@DB::args</tt> to the arguments passed in thegiven stack frame--but only when called from within the<tt class="literal">DB</tt> package.  See<a href="ch20_01.htm">Chapter 20, "The Perl Debugger"</a>.</p><h3 class="sect2">29.2.9. chdir&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarbang.gif"> <img src="figs/xt.gif"></h3><p><blockquote><pre class="programlisting">chdir <em class="replaceable">EXPR</em>chdir</pre></blockquote><a name="INDEX-4659"></a><a name="INDEX-4660"></a><a name="INDEX-4661"></a>This function changes the current process's working directory to<em class="replaceable">EXPR</em>, if possible.  If<em class="replaceable">EXPR</em> is omitted, the caller's homedirectory is used.  The function returns true upon success, false otherwise.<blockquote><pre class="programlisting">chdir "$prefix/lib" or die "Can't cd to $prefix/lib: $!\n";</pre></blockquote>See also the <tt class="literal">Cwd</tt> module, described in <a href="ch32_01.htm">Chapter 32, "Standard Modules"</a>, which lets you keep track of your current directoryautomatically.</p><h3 class="sect2">29.2.10. chmod&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarbang.gif"> <img src="figs/xt.gif"></h3><p><blockquote><pre class="programlisting">chmod <em class="replaceable">LIST</em></pre></blockquote><a name="INDEX-4662"></a><a name="INDEX-4663"></a>This function changes the permissions of a list of files. The firstelement of the list must be the numerical mode, as in the <em class="emphasis">chmod</em>(2) syscall.  Thefunction returns the number of files successfully changed.  For example:<blockquote><pre class="programlisting">$cnt = chmod 0755, 'file1', 'file2';</pre></blockquote>will set <tt class="literal">$cnt</tt> to <tt class="literal">0</tt>,<tt class="literal">1</tt>, or <tt class="literal">2</tt>, depending on how manyfiles were changed.  Success is measured by lack of error, not by anactual change, because a file may have had the same mode before theoperation.  An error probably means you lacked sufficient privilegesto change its mode because you were neither the file's owner nor thesuperuser.  Check <tt class="literal">$!</tt> to find the actual reason forfailure.</p><p>Here's a more typical usage:<blockquote><pre class="programlisting">chmod(0755, @executables) == @executables        or die "couldn't chmod some of @executables: $!";</pre></blockquote>If you need to know which files didn't allow the change, use somethinglike this:<blockquote><pre class="programlisting">@cannot = grep {not chmod 0755, $_} 'file1', 'file2', 'file3';die "$0: could not chmod @cannot\n" if @cannot;</pre></blockquote>This idiom makes use of the <tt class="literal">grep</tt> function to selectonly those elements of the list for which the <tt class="literal">chmod</tt>function failed.</p><p>When using nonliteral mode data, you may need to convert an octalstring to a number using the <tt class="literal">oct</tt> function.  That's becausePerl doesn't automatically assume a string contains an octal number

⌨️ 快捷键说明

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