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

📄 ch29_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 5 页
字号:
just because it happens to have a leading "0".<blockquote><pre class="programlisting">$DEF_MODE = 0644;   # Can't use quotes here!PROMPT: {    print "New mode? ";    $strmode = &lt;STDIN&gt;;        exit unless defined $strmode;   # test for eof    if ($strmode =~ /^\s*$/) {          # test for blank line        $mode = $DEF_MODE;    }    elsif ($strmode !~ /^\d+$/) {        print "Want numeric mode, not $strmode\n";        redo PROMPT;    }    else {        $mode = oct($strmode);          # converts "755" to 0755    }    chmod $mode, @files;}</pre></blockquote>This function works with numeric modes much like the Unix <em class="emphasis">chmod</em>(2)syscall.  If you want a symbolic interface like the one the<em class="emphasis">chmod</em>(1) command provides, see the <tt class="literal">File::chmod</tt> module on CPAN.<a name="INDEX-4664"></a></p><p>You can also import the symbolic <tt class="literal">S_I*</tt> constants from the <tt class="literal">Fcntl</tt>module:<blockquote><pre class="programlisting">use Fcntl ':mode';chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;</pre></blockquote>Some people consider that more readable than<tt class="literal">0755</tt>.  Go figure.</p><h3 class="sect2">29.2.11. chomp&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarunderscore.gif"> <img src="figs/xro.gif"></h3><p><blockquote><pre class="programlisting">chomp <em class="replaceable">VARIABLE</em>chomp <em class="replaceable">LIST</em>chomp</pre></blockquote><a name="INDEX-4665"></a><a name="INDEX-4666"></a><a name="INDEX-4667"></a><a name="INDEX-4668"></a><a name="INDEX-4669"></a>This function (normally) deletes a trailing newline from the end of astring contained in a variable.  This is a slightly safer versionof <tt class="literal">chop</tt> (described next) in that it has no effect upon a string thatdoesn't end in a newline.  More specifically, it deletes the terminating stringcorresponding to the current value of <tt class="literal">$/</tt>, and not just any lastcharacter.</p><p>Unlike <tt class="literal">chop</tt>, <tt class="literal">chomp</tt> returns the number of characters deleted.If <tt class="literal">$/</tt> is <tt class="literal">""</tt> (in paragraph mode), <tt class="literal">chomp</tt> removes all trailingnewlines from the selected string (or strings, if chomping a <em class="replaceable">LIST</em>).You cannot <tt class="literal">chomp</tt> a literal, only a variable.</p><p>For example:<blockquote><pre class="programlisting">while (&lt;PASSWD&gt;) {    chomp;   # avoid \n on last field    @array = split /:/;    ...}</pre></blockquote>With version 5.6, the meaning of <tt class="literal">chomp</tt> changesslightly in that input disciplines are allowed to override the valueof the <tt class="literal">$/</tt> variable and mark strings as to how theyshould be chomped.  This has the advantage that an input disciplinecan recognize more than one variety of line terminator (such asUnicode paragraph and line separators), but still safely<tt class="literal">chomp</tt> whatever terminates the current line.</p><h3 class="sect2">29.2.12. chop&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarunderscore.gif"> <img src="figs/xro.gif"></h3><p><blockquote><pre class="programlisting">chop <em class="replaceable">VARIABLE</em>chop <em class="replaceable">LIST</em>chop</pre></blockquote><a name="INDEX-4670"></a><a name="INDEX-4671"></a>This function chops off the last character of a string variable andreturns the character chopped.  The <tt class="literal">chop</tt> operator is used primarilyto remove the newline from the end of an input record, and is moreefficient than using a substitution.  If that's all you're doing,then it would be safer to use <tt class="literal">chomp</tt>, since <tt class="literal">chop</tt>always shortens the string no matter what's there, and <tt class="literal">chomp</tt> ismore selective.</p><p>You cannot <tt class="literal">chop</tt> a literal, only a variable.</p><p><a name="INDEX-4672"></a>If you <tt class="literal">chop</tt> a <em class="replaceable">LIST</em> of variables, each string in the list ischopped:<blockquote><pre class="programlisting">@lines = `cat myfile`;chop @lines;</pre></blockquote>You can <tt class="literal">chop</tt> anything that is an lvalue, including anassignment:<blockquote><pre class="programlisting">chop($cwd = `pwd`);chop($answer = &lt;STDIN&gt;);</pre></blockquote>This is different from:<blockquote><pre class="programlisting">$answer = chop($tmp = &lt;STDIN&gt;);  # WRONG</pre></blockquote><a name="INDEX-4673"></a>which puts a newline into <tt class="literal">$answer</tt> because<tt class="literal">chop</tt> returns the character chopped, not theremaining string (which is in <tt class="literal">$tmp</tt>).  One way toget the result intended here is with <tt class="literal">substr</tt>:<blockquote><pre class="programlisting">$answer = substr &lt;STDIN&gt;, 0, -1;</pre></blockquote>But this is more commonly written as:<blockquote><pre class="programlisting">chop($answer = &lt;STDIN&gt;);</pre></blockquote>In the most general case, <tt class="literal">chop</tt> can be expressed in terms of <tt class="literal">substr</tt>:<blockquote><pre class="programlisting">$last_char = chop($var);$last_char = substr($var, -1, 1, "");   # same thing</pre></blockquote>Once you understand this equivalence, you can use it to do biggerchops.  To chop more than one character, use <tt class="literal">substr</tt> as an lvalue,assigning a null string. The following removes the last fivecharacters of <tt class="literal">$caravan</tt>:<blockquote><pre class="programlisting">substr($caravan, -5) = "";</pre></blockquote>The negative subscript causes <tt class="literal">substr</tt> to count from the end of thestring instead of the beginning.  If you wanted to save the charactersso removed, you could use the four-argument form of <tt class="literal">substr</tt>, creatingsomething of a quintuple chop:<blockquote><pre class="programlisting">$tail = substr($caravan, -5, 5, "");</pre></blockquote></p><h3 class="sect2">29.2.13. chown&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarbang.gif"> <img src="figs/xu.gif"> <img src="figs/xt.gif"></h3><p><blockquote><pre class="programlisting">chown <em class="replaceable">LIST</em></pre></blockquote><a name="INDEX-4674"></a><a name="INDEX-4675"></a><a name="INDEX-4676"></a><a name="INDEX-4677"></a><a name="INDEX-4678"></a><a name="INDEX-4679"></a><a name="INDEX-4680"></a>This function changes the owner and group of a list of files. Thefirst two elements of the list must be the <em class="emphasis">numeric</em> UID and GID,in that order.  A value of <tt class="literal">-1</tt> in either position is interpreted bymost systems to leave that value unchanged.  The function returnsthe number of files successfully changed. For example:<blockquote><pre class="programlisting">chown($uidnum, $gidnum, 'file1', 'file2') == 2        or die "can't chown file1 or 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 many files gotchanged (in the sense that the operation succeeded, not in the sensethat the owner was different afterward).  Here's a more typical usage:<blockquote><pre class="programlisting">chown($uidnum, $gidnum, @filenames) == @filenames        or die "can't chown @filenames: $!";</pre></blockquote>Here's a subroutine that accepts a username, looks up the user andgroup IDs for you, and does the <tt class="literal">chown</tt>:<blockquote><pre class="programlisting">sub chown_by_name {    my($user, @files) = @_;    chown((getpwnam($user))[2,3], @files) == @files            or die "can't chown @files: $!";}chown_by_name("fred", glob("*.c"));</pre></blockquote>However, you may not want the group changed as the previous functiondoes, because the <em class="emphasis">/etc/passwd</em> file associateseach user with a single group even though that user may be a member ofmany secondary groups according to <em class="emphasis">/etc/group</em>.An alternative is to pass a <tt class="literal">-1</tt> for the GID, whichleaves the group of the file unchanged.  If you pass a<tt class="literal">-1</tt> as the UID and a valid GID, you can set thegroup without altering the owner.</p><p>On most systems, you are not allowed to change the ownership of thefile unless you're the superuser, although you should be able tochange the group to any of your secondary groups.  On insecuresystems, these restrictions may be relaxed, but this is not aportable assumption.  On POSIX systems, you can detect which ruleapplies like this:<blockquote><pre class="programlisting">use POSIX qw(sysconf _PC_CHOWN_RESTRICTED);# only try if we're the superuser or on a permissive systemif ($&gt; == 0 || !sysconf(_PC_CHOWN_RESTRICTED) ) {    chown($uidnum, -1, $filename)        or die "can't chown $filename to $uidnum: $!";}</pre></blockquote></p><h3 class="sect2">29.2.14. chr&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarunderscore.gif"></h3><p><blockquote><pre class="programlisting">chr <em class="replaceable">NUMBER</em>chr</pre></blockquote><a name="INDEX-4681"></a><a name="INDEX-4682"></a><a name="INDEX-4683"></a><a name="INDEX-4684"></a><a name="INDEX-4685"></a><a name="INDEX-4686"></a><a name="INDEX-4687"></a><a name="INDEX-4688"></a></p><p>This function returns the character represented by that <em class="replaceable">NUMBER</em> inthe character set.  For example, <tt class="literal">chr(65)</tt> is "<tt class="literal">A</tt>" in either ASCIIor Unicode, and <tt class="literal">chr(0x263a)</tt> is a Unicode smiley face.  For thereverse of <tt class="literal">chr</tt>, use <tt class="literal">ord</tt>.</p><p>If you'd rather specify your characters by name than by number (forexample, <tt class="literal">"\N{WHITE SMILING FACE}"</tt> for a Unicode smiley), see<tt class="literal">charnames</tt> in <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a>.</p><h3 class="sect2">29.2.15. chroot&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarunderscore.gif"> <img src="figs/dollarbang.gif"> <img src="figs/xu.gif"> <img src="figs/xt.gif"></h3><p><blockquote><pre class="programlisting">chroot <em class="replaceable">FILENAME</em>chroot</pre></blockquote><a name="INDEX-4689"></a><a name="INDEX-4690"></a><a name="INDEX-4691"></a><a name="INDEX-4692"></a>If successful, <em class="replaceable">FILENAME</em> becomes the new root directory for thecurrent process--the starting point for pathnames beginning with"<tt class="literal">/</tt>".  This directory is inherited across <tt class="literal">exec</tt> calls and byall subprocesses <tt class="literal">fork</tt>ed after the <tt class="literal">chroot</tt> call.  There is noway to undo a <tt class="literal">chroot</tt>.  For security reasons, only the superusercan use this function.  Here's some code that approximates whatmany FTP servers do:<blockquote><pre class="programlisting">chroot((getpwnam('ftp'))[7])    or die "Can't do anonymous ftp: $!\n";</pre></blockquote>This function is unlikely to work on non-Unix systems.  See <em class="emphasis">chroot</em>(2).</p><h3 class="sect2">29.2.16. close&nbsp;&nbsp;&nbsp;&nbsp; <img src="figs/dollarbang.gif"> <img src="figs/dollarquestion.gif"> <img src="figs/xarg.gif"></h3><p><blockquote><pre class="programlisting">close <em class="replaceable">FILEHANDLE</em>close</pre></blockquote><a name="INDEX-4693"></a><a name="INDEX-4694"></a><a name="INDEX-4695"></a><a name="INDEX-4696"></a><a name="INDEX-4697"></a><a name="INDEX-4698"></a><a name="INDEX-4699"></a><a name="INDEX-4700"></a>This function closes the file, socket, or pipe associated with<em class="replaceable">FILEHANDLE</em>.  (It closes the currentlyselected filehandle if the argument is omitted.)  It returns true ifthe close is successful, false otherwise.  You don't have to close<em class="replaceable">FILEHANDLE</em> if you are immediately going todo another <tt class="literal">open</tt> on it, since the next<tt class="literal">open</tt> will close it for you.  (See<tt class="literal">open</tt>.)  However, an explicit<tt class="literal">close</tt> on an input file resets the line counter(<tt class="literal">$.</tt>), while the implicit close done by<tt class="literal">open</tt> does not.</p><p><em class="replaceable">FILEHANDLE</em> may be an expression whose value can be used as anindirect filehandle (either the real filehandle name or a referenceto anything that can be interpreted as a filehandle object).</p><p>If the filehandle came from a piped open, <tt class="literal">close</tt> will return false ifany underlying syscall fails or if the program at the other end ofthe pipe exited with nonzero status.  In the latter case, the <tt class="literal">close</tt>forces <tt class="literal">$!</tt> (<tt class="literal">$OS_ERROR</tt>) to zero.  So if a <tt class="literal">close</tt> on a pipereturns a nonzero status, check <tt class="literal">$!</tt> to determine whether the problemwas with the pipe itself (nonzero value) or with the program atthe other end (zero value).  In either event, <tt class="literal">$?</tt> (<tt class="literal">$CHILD_ERROR</tt>)

⌨️ 快捷键说明

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