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

📄 ch32_47.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>User::pwent (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="User::pwent"><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="ch32_46.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch33_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">32.47. User::pwent</h2><blockquote><pre class="programlisting">use User::pwent;                 # Default overrides built-ins only.$pw = getpwnam("daemon")   or die "No daemon user";if ($pw-&gt;uid == 1 &amp;&amp; $pw-&gt;dir =~ m#^/(bin|tmp)?$# ) {    print "gid 1 on root dir";}$pw = getpw($whoever);           # Accepts both string or number.$real_shell = $pw-&gt;shell || '/bin/sh';for (($fullname, $office, $workphone, $homephone) =       split /\s*,\s*/, $pw-&gt;gecos){   s/&amp;/ucfirst(lc($pw-&gt;name))/ge;}use User::pwent qw(:FIELDS);     # Sets globals in current package.getpwnam("daemon")         or die "No daemon user";if ($pw_uid == 1 &amp;&amp; $pw_dir =~ m#^/(bin|tmp)?$# ) {    print "gid 1 on root dir";}use User::pwent qw/pw_has/;if (pw_has(qw[gecos expire quota])) { .... }if (pw_has("name uid gid passwd"))  { .... }printf "Your struct pwd supports [%s]\n", scalar pw_has();</pre></blockquote><p>By default, this module's exports override the core <tt class="literal">getpwent</tt>,<tt class="literal">getpwuid</tt>, and <tt class="literal">getpwnam</tt> functions, replacing them with versionsthat return a <tt class="literal">User::pwent</tt> object (or <tt class="literal">undef</tt> on failure).  It isoften better to use the module than the core functions it replaces,because the built-ins overload or even omit various slots in the returnlist in the name of backward compatibility.</p><p>The returned object has methods that access the similarly namedstructure field name from the C's <tt class="literal">passwd</tt> structure from <em class="emphasis">pwd.h</em>,stripped of their leading "<tt class="literal">pw_</tt>" parts, namely <tt class="literal">name</tt>, <tt class="literal">passwd</tt>,<tt class="literal">uid</tt>, <tt class="literal">gid</tt>, <tt class="literal">change</tt>, <tt class="literal">age</tt>, <tt class="literal">quota</tt>, <tt class="literal">comment</tt>, <tt class="literal">class</tt>,<tt class="literal">gecos</tt>, <tt class="literal">dir</tt>, <tt class="literal">shell</tt>, and <tt class="literal">expire</tt>.  The <tt class="literal">passwd</tt>, <tt class="literal">gecos</tt>,and <tt class="literal">shell</tt> fields are tainted.  You may also import the structurefields into your own namespace as regular variables using the"<tt class="literal">:FIELDS</tt>" import tag, although this still overrides your corefunctions.  Access these fields as scalar variables named with a"<tt class="literal">pw_</tt>" prepended to the  method name.  The <tt class="literal">getpw</tt> function is asimple frontend switch that forwards a numeric argument to <tt class="literal">getpwuid</tt>and a string argument to <tt class="literal">getpwnam</tt>.</p><p>Perl believes that no machine ever has more than one of <tt class="literal">change</tt>,<tt class="literal">age</tt>, or <tt class="literal">quota</tt> implemented, nor more than one of either<tt class="literal">comment</tt> or <tt class="literal">class</tt>.  Some machines do not support <tt class="literal">expire</tt>,<tt class="literal">gecos</tt>, or allegedly, even <tt class="literal">passwd</tt>.  You may call these methodsno matter what machine you're on, but they'll return <tt class="literal">undef</tt> ifunimplemented.  See <em class="emphasis">passwd</em>(5) and <em class="emphasis">getpwent</em>(3) for details.</p><p>You can determine whether these fields are implemented by asking theimportable <tt class="literal">pw_has</tt> function about them.  It returns true if allparameters are supported fields on the build platform or false if one ormore were not, and it raises an exception if you ask about a field whose name it doesn't recognize.  If you pass no arguments, it returnsthe list of fields your C library thinks are supported.</p><p>Interpretation of the <tt class="literal">gecos</tt> field varies between systems butoften holds four comma-separated fields containing theuser's full name, office location, work phone number, and home phonenumber.  An <tt class="literal">&amp;</tt> in the <tt class="literal">gecos</tt> field should be replaced by the user'sproperly capitalized login <tt class="literal">name</tt>.  The <tt class="literal">shell</tt> field, if blank,must be assumed to be <em class="emphasis">/bin/sh</em>, although Perl does not do thisfor you.  The <tt class="literal">passwd</tt> is one-way hashed gobbledygook, not cleartext, and may not be unhashed save by brute-force guessing.  Securesystems often use a more secure hashing than DES.  On systemssupporting shadow password systems, Perl automatically returns theshadow password entry when called by a suitably empowered user,even if your underlying vendor-provided C library was too short-sightedto realize it should do this.</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="ch32_46.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="ch33_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.46. User::grent</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">33. Diagnostic Messages</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 + -