Basic operation
check version : perl -v
perldoc
perldoc perl
man perl
CPAN
-w enable warning, in command line
use warnings enable warning, 5.6.x以后perfer
q{}='' won't interpolate
qq{}="" interpolate variables
qx{}=``
qw{}
m// pattern matching
s/// replace
tr/// interpolate $ a d @ variables
ord get the numeic value of a character, e.g. ord("#")
[-564] 从后往前数第564个元素
List (1,2,3)
Array @a=(1,2,3)
增加元素 @a=(@a,4);
$count=@a $count等于array的长度
使用array做index访问array @index = (0,2);
print @a[@b];
reverse
push add last element in array
pop remove last element in array
shift remove a element from start of an array
unshift place an element at the start of an array
hash %link = (
A=>1,
B=>2,
C=>3,
);
delete
keys
values
each
==========================================================
Regular expression
=~ binding operator
!~ negetive binding operator
\Q \E 不转义metacharacter
anchoring matches ^ beginning $ end
[abc] 包含any one of a,b,c
[^abc] caret symbol ^ 不包含
hyphen - 从 。。到。。
(?i) prevent Perl from interpreting the search pattern as case sensitive, e.g. $a=~ /(?i)(Heath\w?)/
Perl5.6.0支持
[[:alpha:]] alphabetic characters
[[:alnum:]] alphabetic characters or numeric characters
[[:digit:]] 0 to 9
[[:lower:]] lowercase letters
[[:upper:]] upcase letters
[[:punct:]] punctuation character标点符号
(a|b|c) 相当于 [abc] alternatives operator |
但是 | 更灵活 e.g. (aa|ws|edc)
Quantifiers
*? to find the first match that occurs
+? to find a match using as few repeats as possible, but allow at least one repeat
?? to find a match using zero repeats if possible
Backreference
找到符合的pattern后,perl将value存到$1,$2,$3....
Search-and-replace
s/e/a/
/i 不区分大小写
/g 全局替换
/m think m for multiple lines,看做多行,这时锚定针对每一行,而非整个字符串
/s Let . match \n , 忽略新行
/\G match 最后一个
/x Ignore most whitespace and allow comments
Transliteration
tr/abcd/efgh/ replace a->e b->f c->g d->h
/d 删除无对应的字符, e.g. $a=~tr/Cre/Db/d e没有对应的字符,e将被删除
split /分隔符/,str
join
?# inline comments
?: 如果没有match,指定的backreference将不会为空,而是存放下一个match。 Prenvent Perl from backreferencing the empty value.
lookaheads ?= only match if the preceding word is the one you have specified , 指定后面跟着的文字
negative lookaheads ?!
lookbehinds ?< only match if the following word is the one you have specified , 指定前面的文字, e.g. $a =~ s/(?<=wedding) day/ vows/ 将wedding day中的day换为vows
negative lookbehinds ?<!
==========================
Variables and parameters
$_ global scalar for pattern-matching and subsitution operation, and also used by filehandlers when reading in and wrinitng to files.
@_ 子程序的输入参数
@ARGV store the inout command-line arguments for use in scripts.
$ARGV contains the name of the current file when reading from the null filehandle <>
$(number) backreference, contains the regular expression pattern memory.
$& contains the string that was last successfully mathced in a pattern
$` contains the string that preceded the last successful pattern match
$' contains the string that was after the last successful pattern match
$+ contains the last string that was matched to a grouped subpattern in the last search.
@+ it is array, refer to the back pointer position of the last successful match in the referenced string
@- list the front pointer's position
input/output variables
$. 最后读取的filehandle的行数
$/ input record separator
$\ out put reord separator
$| internal flag, usually set to zero
$, output filed separator
$" output field separator when a array variable has been interpolated into a double-quoted string
$- contains the number of lines still left to print for the default filehandle.
$? 进程的退出状态
$@ eval()的错误信息
$! 当前的errno的值, 系统的错误信息, error message
File handle
STDIN
STDOUT
STDERR
open FIEHANDLE, "c:\\test\\abc.txt"
<...> diamond operator, readline operator
reading from te command line
while (<>)
reading from entire files
my $file= <FILEHANDLE>
reading in a while loop
while(<FILEHANDLE>)
reading with an array
@book=<FILEHANDLE>
select 改变默认的输出句柄,(默认会使用 STDOUT)