perl随笔-20070706.doc_第1页
perl随笔-20070706.doc_第2页
perl随笔-20070706.doc_第3页
perl随笔-20070706.doc_第4页
perl随笔-20070706.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

perl随笔-200707061. 变量ID项目描述1.$ARGV传递进来的参数数组2.$0perl程序文件名3.$一般变量4.数组5.%散列表6.$#var_name数组的最大下标7.STDIN, STDOUT, STDERR内置句柄,可以使用、关闭,重新open等8.$ARG $_缺省的输入/循环变量The default input and pattern-searching space. The following pairs are equivalent: while () . while (defined($_ = ) . /Subject:/ $_ = /Subject:/ tr/a-z/A-Z/ $_ = tr/a-z/A-Z/ chomp chomp($_)9._ contains the parameters passed to that subroutine.10.$Contains the subpattern from the corresponding set of capturing parentheses from the last pattern match, not counting patterns matched in nested blocks that have been exited already. (Mnemonic: like digits.) These variables are all read-only and dynamically scoped to the current BLOCK.11.$MATCH $&local $_ = abcdefghi;/def/;print $:$&:$n; # prints abc:def:ghi12.$PREMATCH $见上面例子13.$POSTMATCH $见上面例子14.2. 流程控制ID项目描述1.if( ! -f $ARGV0 )判断文件是否存在2.3.4.5.6.7.8.9.3. 正则表达式3.1. OPERATORS = determines to which variable the regex is applied. In its absence, $_ is used. $var = /foo/; ! determines to which variable the regex is applied, and negates the result of the match; it returns false if the match succeeds, and true if it fails. $var ! /foo/; m/pattern/igmsoxc searches a string for a pattern match, applying the given options. i case-Insensitive g Global - all occurrences m Multiline mode - and $ match internal lines s match as a Single line - . matches n o compile pattern Once x eXtended legibility - free whitespace and comments c dont reset pos on failed matches when using /g If pattern is an empty string, the last I matched regex is used. Delimiters other than / may be used for both this operator and the following ones. qr/pattern/imsox lets you store a regex in a variable, or pass one around. Modifiers as for m/ and are stored within the regex. s/pattern/replacement/igmsoxe substitutes matches of pattern with replacement. Modifiers as for m/ with one addition: e Evaluate replacement as an expression e may be specified multiple times. replacement is interpreted as a double quoted string unless a single-quote () is the delimiter. ?pattern? is like m/pattern/ but matches only once. No alternate delimiters can be used. Must be reset with L.3.2. SYNTAX Escapes the character immediately following it . Matches any single character except a newline (unless /s is used) Matches at the beginning of the string (or line, if /m is used) $ Matches at the end of the string (or line, if /m is used) * Matches the preceding element 0 or more times + Matches the preceding element 1 or more times ? Matches the preceding element 0 or 1 times . Specifies a range of occurrences for the element preceding it . Matches any one of the characters contained within the brackets (.) Groups subexpressions for capturing to $1, $2. (?:.) Groups subexpressions without capturing (cluster) | Matches either the subexpression preceding or following it 1, 2 . The text from the Nth group3.3. ESCAPE SEQUENCESThese work as in normal strings. a Alarm (beep) e Escape f Formfeed n Newline r Carriage return t Tab 037 Any octal ASCII value x7f Any hexadecimal ASCII value x263a A wide hexadecimal value cx Control-x Nname A named character l Lowercase next character u Titlecase next character L Lowercase until E U Uppercase until E Q Disable pattern metacharacters until E E End case modificationFor Titlecase, see Titlecase.This one works differently from normal strings: b An assertion, not backspace, except in a character class3.4. CHARACTER CLASSES amy Match a, m or y f-j Dash specifies range f-j- Dash escaped or at start or end means dash f-j Caret indicates match any character _except_ theseThe following sequences work within or without a character class. The first six are locale aware, all are Unicode aware. The default character class equivalent are given. See the perllocale manpage and the perlunicode manpage for details. d A digit 0-9 D A nondigit 0-9 w A word character a-zA-Z0-9_ W A non-word character a-zA-Z0-9_ s A whitespace character tnrf S A non-whitespace character tnrf C Match a byte (with Unicode, . matches a character) pP Match P-named (Unicode) property p. Match Unicode property with long name PP Match non-P P. Match lack of Unicode property with long name X Match extended unicode sequencePOSIX character classes and their Unicode and Perl equivalents: alnum IsAlnum Alphanumeric alpha IsAlpha Alphabetic ascii IsASCII Any ASCII char blank IsSpace t Horizontal whitespace (GNU extension) cntrl IsCntrl Control characters digit IsDigit d Digits graph IsGraph Alphanumeric and punctuation lower IsLower Lowercase chars (locale and Unicode aware) print IsPrint Alphanumeric, punct, and space punct IsPunct Punctuation space IsSpace sck Whitespace IsSpacePerl s Perls whitespace definition upper IsUpper Uppercase chars (locale and Unicode aware) word IsWord w Alphanumeric plus _ (Perl extension) xdigit IsXDigit 0-9A-Fa-f Hexadecimal digitWithin a character class: POSIX traditional Unicode :digit: d pIsDigit:digit: D PIsDigit3.5. ANCHORSAll are zero-width assertions. Match string start (or line, if /m is used) $ Match string end (or line, if /m is used) or before newline b Match word boundary (between w and W) B Match except at word boundary (between w and w or W and W) A Match string start (regardless of /m) Z Match string end (before optional newline) z Match absolute string end G Match where previous m/g left off3.6. QUANTIFIERSQuantifiers are greedy by default - match the longest leftmost. Maximal Minimal Allowed range - - - n,m n,m? Must occur at least n times but no more than m times n, n,? Must occur at least n times n n? Must occur exactly n times * *? 0 or more times (same as 0,) + +? 1 or more times (same as 1,) ? ? 0 or 1 time (same as 0,1)There is no quantifier ,n - that gets understood as a literal string.3.7. EXTENDED CONSTRUCTS (?#text) A comment (?imxs-imsx:.) Enable/disable option (as per m/ modifiers) (?=.) Zero-width positive lookahead assertion (?!.) Zero-width negative lookahead assertion (?=.) Zero-width positive lookbehind assertion (?.) Grab what we can, prohibit backtracking (? code ) Embedded code, return value becomes $R (? code ) Dynamic regex, return value used as regex (?(cond)yes|no) cond being integer corresponding to capturing parens (?(cond)yes) or a lookaround/eval zero-width assertion3.8. VARIABLES $_ Default variable for operators to use $* Enable multiline matching (deprecated; not in 5.9.0 or later) $& Entire matched string $ Everything prior to matched string $ Everything after to matched stringThe use of those last three will slow down all regex use within your program. Consult the perlvar manpage for LAST_MATCH_START to see equivalent expressions that wont cause slow down. See also the Devel:SawAmpersand manpage. $1, $2 . hold the Xth captured expr $+ Last parenthesized pattern match $N Holds the most recently closed capture $R Holds the result of the last (?.) expr - Offsets of starts of groups. $-0 holds start of whole match + Offsets of ends of groups. $+0 holds end of whole match Captured groups are numbered according to their opening paren.3.9. FUNCTIONS lc Lowercase a string lcfirst Lowercase first char of a string uc Uppercase a string ucfirst Titlecase first char of a string pos Return or set current match position quotemeta Quote metacharacters reset Reset ?pattern? status study Analyze string for optimizing matching split Use regex to split a string into partsThe first four of these are like the escape sequences L, l, U, and u. For Titlecase, see Titlecase.4. 内置函数ID项目描述1.scalar(var_name)取数组的长度2.indexindex STR,SUBSTR,POSITION index STR,SUBSTR3.substrsubstr EXPR,OFFSET,LENGTH substr EXPR,OFFSET4.scalar EXPRForces EXPR to be interpreted in scalar context and returns the value of EXPR.5.lc EXPR lcReturns a lowercased version of EXPR. This is the internal function implementing the L escape in double-quoted strings.6.uc EXPR ucReturns an uppercased version of EXPR. This is the internal function implementing the U escape in double-quoted strings.7.ucfirst EXPR ucfirstReturns the value of EXPR with the first character in uppercase (titlecase in Unicode). This is the internal function implementing the u escape in double-quoted strings.8.length EXPR lengthReturns the length in characters of the value of EXPR. If EXPR is omitted, returns length of $_. Note that this cannot be used on an entire array or hash to find out how many elements these have. For that, use scalar array and scalar keys %hash respectively.9.caller EXPR caller10.defined EXPR defined判断变量是否定义。例: if (an_array) print has array elementsn if (%a_hash) print has hash membersn You may also use defined(&func) to check whether subroutine &func has ever been defined.11.delete EXPRGiven an expression that specifies a hash element, array element, hash slice, or array slice, deletes the specified element(s) from the hash or array.例: %hash = (foo = 11, bar = 22, b

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论