📄 perlfunc.1
字号:
.IP "continue \s-1BLOCK\s0" 8.IX Xref "continue".IX Item "continue BLOCK".PD 0.IP "continue" 8.IX Item "continue".PD\&\f(CW\*(C`continue\*(C'\fR is actually a flow control statement rather than a function. Ifthere is a \f(CW\*(C`continue\*(C'\fR \s-1BLOCK\s0 attached to a \s-1BLOCK\s0 (typically in a \f(CW\*(C`while\*(C'\fR or\&\f(CW\*(C`foreach\*(C'\fR), it is always executed just before the conditional is about tobe evaluated again, just like the third part of a \f(CW\*(C`for\*(C'\fR loop in C. Thusit can be used to increment a loop variable, even when the loop has beencontinued via the \f(CW\*(C`next\*(C'\fR statement (which is similar to the C \f(CW\*(C`continue\*(C'\fRstatement)..Sp\&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR may appear within a \f(CW\*(C`continue\*(C'\fRblock. \f(CW\*(C`last\*(C'\fR and \f(CW\*(C`redo\*(C'\fR will behave as if they had been executed withinthe main block. So will \f(CW\*(C`next\*(C'\fR, but since it will execute a \f(CW\*(C`continue\*(C'\fRblock, it may be more entertaining..Sp.Vb 9\& while (EXPR) {\& ### redo always comes here\& do_something;\& } continue {\& ### next always comes here\& do_something_else;\& # then back the top to re\-check EXPR\& }\& ### last always comes here.Ve.SpOmitting the \f(CW\*(C`continue\*(C'\fR section is semantically equivalent to using anempty one, logically enough. In that case, \f(CW\*(C`next\*(C'\fR goes directly backto check the condition at the top of the loop..SpIf the \*(L"switch\*(R" feature is enabled, \f(CW\*(C`continue\*(C'\fR is also afunction that will break out of the current \f(CW\*(C`when\*(C'\fR or \f(CW\*(C`default\*(C'\fRblock, and fall through to the next case. See feature and\&\*(L"Switch statements\*(R" in perlsyn for more information..IP "cos \s-1EXPR\s0" 8.IX Xref "cos cosine acos arccosine".IX Item "cos EXPR".PD 0.IP "cos" 8.IX Item "cos".PDReturns the cosine of \s-1EXPR\s0 (expressed in radians). If \s-1EXPR\s0 is omitted,takes cosine of \f(CW$_\fR..SpFor the inverse cosine operation, you may use the \f(CW\*(C`Math::Trig::acos()\*(C'\fRfunction, or use this relation:.Sp.Vb 1\& sub acos { atan2( sqrt(1 \- $_[0] * $_[0]), $_[0] ) }.Ve.IP "crypt \s-1PLAINTEXT\s0,SALT" 8.IX Xref "crypt digest hash salt plaintext password decrypt cryptography passwd encrypt".IX Item "crypt PLAINTEXT,SALT"Creates a digest string exactly like the \fIcrypt\fR\|(3) function in the Clibrary (assuming that you actually have a version there that has notbeen extirpated as a potential munitions)..Sp\&\fIcrypt()\fR is a one-way hash function. The \s-1PLAINTEXT\s0 and \s-1SALT\s0 is turnedinto a short string, called a digest, which is returned. The same\&\s-1PLAINTEXT\s0 and \s-1SALT\s0 will always return the same string, but there is no(known) way to get the original \s-1PLAINTEXT\s0 from the hash. Smallchanges in the \s-1PLAINTEXT\s0 or \s-1SALT\s0 will result in large changes in thedigest..SpThere is no decrypt function. This function isn't all that useful forcryptography (for that, look for \fICrypt\fR modules on your nearby \s-1CPAN\s0mirror) and the name \*(L"crypt\*(R" is a bit of a misnomer. Instead it isprimarily used to check if two pieces of text are the same withouthaving to transmit or store the text itself. An example is checkingif a correct password is given. The digest of the password is stored,not the password itself. The user types in a password that is\&\fIcrypt()\fR'd with the same salt as the stored digest. If the two digestsmatch the password is correct..SpWhen verifying an existing digest string you should use the digest asthe salt (like \f(CW\*(C`crypt($plain, $digest) eq $digest\*(C'\fR). The \s-1SALT\s0 usedto create the digest is visible as part of the digest. This ensures\&\fIcrypt()\fR will hash the new string with the same salt as the digest.This allows your code to work with the standard crypt andwith more exotic implementations. In other words, do not assumeanything about the returned string itself, or how many bytes in thedigest matter..SpTraditionally the result is a string of 13 bytes: two first bytes ofthe salt, followed by 11 bytes from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR, and onlythe first eight bytes of the digest string mattered, but alternativehashing schemes (like \s-1MD5\s0), higher level security schemes (like C2),and implementations on non-UNIX platforms may produce differentstrings..SpWhen choosing a new salt create a random two character string whosecharacters come from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR (like \f(CW\*(C`join \*(Aq\*(Aq, (\*(Aq.\*(Aq,\&\*(Aq/\*(Aq, 0..9, \*(AqA\*(Aq..\*(AqZ\*(Aq, \*(Aqa\*(Aq..\*(Aqz\*(Aq)[rand 64, rand 64]\*(C'\fR). This set ofcharacters is just a recommendation; the characters allowed inthe salt depend solely on your system's crypt library, and Perl can'trestrict what salts \f(CW\*(C`crypt()\*(C'\fR accepts..SpHere's an example that makes sure that whoever runs this program knowstheir password:.Sp.Vb 1\& $pwd = (getpwuid($<))[1];\&\& system "stty \-echo";\& print "Password: ";\& chomp($word = <STDIN>);\& print "\en";\& system "stty echo";\&\& if (crypt($word, $pwd) ne $pwd) {\& die "Sorry...\en";\& } else {\& print "ok\en";\& }.Ve.SpOf course, typing in your own password to whoever asks youfor it is unwise..SpThe crypt function is unsuitable for hashing large quantitiesof data, not least of all because you can't get the informationback. Look at the Digest module for more robust algorithms..SpIf using \fIcrypt()\fR on a Unicode string (which \fIpotentially\fR hascharacters with codepoints above 255), Perl tries to make senseof the situation by trying to downgrade (a copy of the string)the string back to an eight-bit byte string before calling \fIcrypt()\fR(on that copy). If that works, good. If not, \fIcrypt()\fR dies with\&\f(CW\*(C`Wide character in crypt\*(C'\fR..IP "dbmclose \s-1HASH\s0" 8.IX Xref "dbmclose".IX Item "dbmclose HASH"[This function has been largely superseded by the \f(CW\*(C`untie\*(C'\fR function.].SpBreaks the binding between a \s-1DBM\s0 file and a hash..IP "dbmopen \s-1HASH\s0,DBNAME,MASK" 8.IX Xref "dbmopen dbm ndbm sdbm gdbm".IX Item "dbmopen HASH,DBNAME,MASK"[This function has been largely superseded by the \f(CW\*(C`tie\*(C'\fR function.].SpThis binds a \fIdbm\fR\|(3), \fIndbm\fR\|(3), \fIsdbm\fR\|(3), \fIgdbm\fR\|(3), or Berkeley \s-1DB\s0 file to ahash. \s-1HASH\s0 is the name of the hash. (Unlike normal \f(CW\*(C`open\*(C'\fR, the firstargument is \fInot\fR a filehandle, even though it looks like one). \s-1DBNAME\s0is the name of the database (without the \fI.dir\fR or \fI.pag\fR extension ifany). If the database does not exist, it is created with protectionspecified by \s-1MASK\s0 (as modified by the \f(CW\*(C`umask\*(C'\fR). If your system supportsonly the older \s-1DBM\s0 functions, you may perform only one \f(CW\*(C`dbmopen\*(C'\fR in yourprogram. In older versions of Perl, if your system had neither \s-1DBM\s0 norndbm, calling \f(CW\*(C`dbmopen\*(C'\fR produced a fatal error; it now falls back to\&\fIsdbm\fR\|(3)..SpIf you don't have write access to the \s-1DBM\s0 file, you can only read hashvariables, not set them. If you want to test whether you can write,either use file tests or try setting a dummy hash entry inside an \f(CW\*(C`eval\*(C'\fR,which will trap the error..SpNote that functions such as \f(CW\*(C`keys\*(C'\fR and \f(CW\*(C`values\*(C'\fR may return huge listswhen used on large \s-1DBM\s0 files. You may prefer to use the \f(CW\*(C`each\*(C'\fRfunction to iterate over large \s-1DBM\s0 files. Example:.Sp.Vb 6\& # print out history file offsets\& dbmopen(%HIST,\*(Aq/usr/lib/news/history\*(Aq,0666);\& while (($key,$val) = each %HIST) {\& print $key, \*(Aq = \*(Aq, unpack(\*(AqL\*(Aq,$val), "\en";\& }\& dbmclose(%HIST);.Ve.SpSee also AnyDBM_File for a more general description of the pros andcons of the various dbm approaches, as well as DB_File for a particularlyrich implementation..SpYou can control which \s-1DBM\s0 library you use by loading that librarybefore you call \fIdbmopen()\fR:.Sp.Vb 3\& use DB_File;\& dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")\& or die "Can\*(Aqt open netscape history file: $!";.Ve.IP "defined \s-1EXPR\s0" 8.IX Xref "defined undef undefined".IX Item "defined EXPR".PD 0.IP "defined" 8.IX Item "defined".PDReturns a Boolean value telling whether \s-1EXPR\s0 has a value other thanthe undefined value \f(CW\*(C`undef\*(C'\fR. If \s-1EXPR\s0 is not present, \f(CW$_\fR will bechecked..SpMany operations return \f(CW\*(C`undef\*(C'\fR to indicate failure, end of file,system error, uninitialized variable, and other exceptionalconditions. This function allows you to distinguish \f(CW\*(C`undef\*(C'\fR fromother values. (A simple Boolean test will not distinguish among\&\f(CW\*(C`undef\*(C'\fR, zero, the empty string, and \f(CW"0"\fR, which are all equallyfalse.) Note that since \f(CW\*(C`undef\*(C'\fR is a valid scalar, its presencedoesn't \fInecessarily\fR indicate an exceptional condition: \f(CW\*(C`pop\*(C'\fRreturns \f(CW\*(C`undef\*(C'\fR when its argument is an empty array, \fIor\fR when theelement to return happens to be \f(CW\*(C`undef\*(C'\fR..SpYou may also use \f(CW\*(C`defined(&func)\*(C'\fR to check whether subroutine \f(CW&func\fRhas ever been defined. The return value is unaffected by any forwarddeclarations of \f(CW&func\fR. Note that a subroutine which is not definedmay still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR method thatmakes it spring into existence the first time that it is called \*(-- seeperlsub..SpUse of \f(CW\*(C`defined\*(C'\fR on aggregates (hashes and arrays) is deprecated. Itused to report whether memory for that aggregate has ever beenallocated. This behavior may disappear in future versions of Perl.You should instead use a simple test for size:.Sp.Vb 2\& if (@an_array) { print "has array elements\en" }\& if (%a_hash) { print "has hash members\en" }.Ve.SpWhen used on a hash element, it tells you whether the value is defined,not whether the key exists in the hash. Use \*(L"exists\*(R" for the latterpurpose..SpExamples:.Sp.Vb 6\& print if defined $switch{\*(AqD\*(Aq};\& print "$val\en" while defined($val = pop(@ary));\& die "Can\*(Aqt readlink $sym: $!"\& unless defined($value = readlink $sym);\& sub foo { defined &$bar ? &$bar(@_) : die "No bar"; }\& $debugging = 0 unless defined $debugging;.Ve.SpNote: Many folks tend to overuse \f(CW\*(C`defined\*(C'\fR, and then are surprised todiscover that the number \f(CW0\fR and \f(CW""\fR (the zero-length string) are, in fact,defined values. For example, if you say.Sp.Vb 1\& "ab" =~ /a(.*)b/;.Ve.SpThe pattern match succeeds, and \f(CW$1\fR is defined, despite the fact that itmatched \*(L"nothing\*(R". It didn't really fail to match anything. Rather, itmatched something that happened to be zero characters long. This is allvery above-board and honest. When a function returns an undefined value,it's an admission that it couldn't give you an honest answer. So youshould use \f(CW\*(C`defined\*(C'\fR only when you're questioning the integrity of whatyou're trying to do. At other times, a simple comparison to \f(CW0\fR or \f(CW""\fR iswhat you want..SpSee also \*(L"undef\*(R", \*(L"exists\*(R", \*(L"ref\*(R"..IP "delete \s-1EXPR\s0" 8.IX Xref "delete".IX Item "delete EXPR"Given an expression that specifies a hash element, array element, hash slice,or array slice, deletes the specified element(s) from the hash or array.In the case of an array, if the array elements happen to be at the end,the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -