锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
@chars = map(chr, @numbers);
榪斿洖涓緇勬暟瀛楃殑騫蟲柟
my @squares = map { $_ > 5 ? ($_ * $_) : () } @numbers;
榪斿洖澶т簬5鐨勬暟瀛楃殑騫蟲柟
my @squares = map { $_ > 5 ? ($_ * $_) : () } @numbers;
鐢變簬 map 鎬繪槸榪斿洖涓涓垪琛紝鍥犳鍙互璧嬪肩粰鍝堝笇綾誨瀷鐨勫彉閲忥細
%hash = map { get_a_key_for($_) => $_ } @array;
涔熷彲浠ヨ繖鏍峰啓錛?/br>
%hash = ();
foreach (@array) {
$hash{get_a_key_for($_)} = $_;
}
-X FILEHANDLE
鏂囦歡嫻嬭瘯鍑芥暟錛?/br>
-r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-e File exists.
-z File has zero size (is empty).
-s File has nonzero size (returns size in bytes).
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO), or Filehandle is a pipe.
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-t Filehandle is opened to a tty.
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
-T File is an ASCII text file (heuristic guess).
-B File is a "binary" file (opposite of -T).
-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time (Unix, may differ for other platforms)
delete EXPR
EXPR 涓哄搱甯屽彉閲忕殑 slice 鎴栬呮槸鍗曚釜鍏冪礌銆?/br>
榪斿洖鍊間負鍒犻櫎鎺夊厓绱犵殑鍊鹼紝鍙互鏄垪琛ㄣ?/br>
濡傛灉榪斿洖鍊艱瑕佹眰涓?scalar 綾誨瀷錛屽垯榪斿洖琚垹闄ょ殑鏈鍚庝竴涓箋?/br>
delete 涔熷彲浠ョ敤浜庢暟緇勭被鍨嬬殑鍙橀噺錛屼笉榪囧畠鐨勮涓哄彲鑳戒笉鏄綘鎵棰勬兂鐨勯偅鏍楓?/br>
渚嬪瓙錛?/br>
%hash = (foo => 11, bar => 22, baz => 33);
$scalar = delete $hash{foo}; # $scalar is 11
$scalar = delete @hash{qw(foo bar)}; # $scalar is 22
@array = delete @hash{qw(foo bar baz)}; # @array is (undef,undef,33)
each HASH
each ARRAY
each EXPR
榪斿洖鍝堝笇鐨勬瘡涓?(key, value) 鎵緇勬垚鐨勬暟緇勩?/br>
渚嬪瓙錛?/br>
while (($key, $value) = each %hash) {
print $key, "\n";
delete $hash{$key}; # This is safe
}
eof FILEHANDLE
eof ()
eof
娉ㄦ剰 eof() 涓?eof 鐨勫尯鍒細
eof()錛氬鏋滃埌杈句簡 <> 鐨勬渶鍚庝竴涓枃浠剁殑鏈熬錛屽垯榪斿洖1
eof錛氬鏋滃埌杈句簡褰撳墠鏂囦歡鐨勬湯灝撅紝鍒欒繑鍥?
# reset line numbering on each input file
while (<>) {
next if /^\s*#/; # skip comments
print "$.\t$_";
} continue {
close ARGV if eof; # Not eof()!
}
# insert dashes just before last line of last file
while (<>) {
if (eof()) { # check for end of last file
print "--------------\n";
}
print;
last if eof(); # needed if we're reading from a terminal
}
]]>
print "The array is: @array\n";
print "The array is: " . join($", @array) . "\n";
瀹冧滑鏄浉絳夌殑銆?/br>
@a = (1, 3, 4);
print "@a\n";
$" = ",";
print "@a\n";
print "1,3,4" =~ /@a/;
杈撳嚭涓?/br>
1 3 4
1,3,4
1
$PROCESS_ID
$PID
$$
榪愯 perl 鑴氭湰鐨勫綋鍓嶈繘紼嬬殑 pid 鍊箋傜瓑鍚屼簬 getpid()銆?/br>
$REAL_GROUP_ID
$GID
$(
璇ヨ繘紼嬬殑 real gid 銆傜敱 getgid() 鑾峰緱銆?/br>
濡傛灉榪愯鍦ㄤ竴涓敮鎸佺敤鎴峰悓鏃跺睘浜庡涓粍鐨勬搷浣滅郴緇熶笂錛?( 灝嗚繑鍥炵敱絀烘牸鍒嗛殧鐨勫涓?gid銆傜敱 getgroups() 鑾峰緱銆?/br>
鍙兘緇?$( 璧嬩簣鍗曚釜鏁存暟銆?/br>
$EFFECTIVE_GROUP_ID
$EGID
$)
璇ヨ繘紼嬬殑 effective gid銆?/br>
$< = $>; # set real to effective uid
($<,$>) = ($>,$<); # swap real and effective uids
$PROGRAM_NAME
$0
褰撳墠紼嬪簭鐨勫悕瀛椼?/br>
$SUBSCRIPT_SEPARATOR
$SUBSEP
$;
濡傛灉榪欐牱璁塊棶涓涓搱緋昏〃錛?/br>
$foo{$a,$b,$c}
絳夊悓浜庯細
$foo{join($;, $a, $b, $c)}
渚嬪瓙錛?/br>
%h = ('123', 'test');
$; = '';
print $h{1,2,3}; # 杈撳嚭 test
%ENV
瀛樻斁鐜鍙橀噺鍝堝笇琛ㄣ?/br>
@F
褰撴寚瀹氫簡 -a 鍙傛暟鐨勬椂鍊欙紝@F 涓繚瀛樼潃姣忎竴琛岃鍒嗛殧鍚庣殑鏁扮粍錛?/br>
perl -ane 'print pop(@F), "\n";'
絳夊悓浜?/br>
while (<>) {
@F = split(' ');
print pop(@F), "\n";
}
$OSNAME
$^O
褰撳墠鎿嶄綔緋葷粺鐨勫悕瀛椼?/br>
# perl -e "print $^O"
# linux
%SIG
鍝堝笇 %SIG 涓繚瀛樹簡淇″彿鐨勫鐞嗗嚱鏁般備緥瀛愬涓嬶細
sub handler { # 1st argument is signal name
my($sig) = @_;
print "Caught a SIG$sig--shutting down\n";
close(LOG);
exit(0);
}
$SIG{'INT'} = \&handler;
$SIG{'QUIT'} = \&handler;
...
$SIG{'INT'} = 'DEFAULT'; # restore default action
$SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT
璧嬪間負 'IGNORE' 鍒欏拷鐣ヨ淇″彿錛岄櫎浜?CHLD 淇″彿銆?/br>
涓嬮潰鏄寚瀹氬鐞嗗嚱鏁扮殑鍏朵粬鏂瑰紡錛?/br>
$SIG{"PIPE"} = "Plumber"; # 鎸囧畾涓?main::Plumber (涓嶆帹鑽?
$SIG{"PIPE"} = \&Plumber; # 鎺ㄨ崘鐨勬柟寮?
$SIG{"PIPE"} = *Plumber; # 鏈夌偣濂囨?
$SIG{"PIPE"} = Plumber(); # 閿欒鐨勫仛娉曪紝Plumber() 浼氳繑鍥炰粈涔堬紵錛?
閮ㄥ垎鍐呴儴鐨?hook 涔熷彲浠ラ氳繃璁劇疆 SIG 鏉ユ寚瀹氥傛瘮濡傝錛?/br>
local $SIG{__WARN__} = sub { die $_[0] };
eval $proggie;
褰撲竴涓?warning 琚姏鍑虹殑鏃跺欙紝$SIG{__WARN__} 鎵鎸囧悜鐨勫嚱鏁板皢琚皟鐢ㄣ?/br>
warning 娑堟伅鐨勫唴瀹逛負鍑芥暟鐨勭涓涓弬鏁般?/br>
$BASETIME
$^T
紼嬪簭寮濮嬭繍琛岀殑鏃墮棿(鏃墮棿鎴?
# perl -e "print $^T"
1314234806
$PERL_VERSION
$^V
perl 鐨勭増鏈彿
warn "Hashes not randomized!\n" if !$^V or $^V lt v5.8.1
== 鍜屾鍒欒〃杈懼紡鐩稿叧鐨勫彉閲?==
$<digits> ($1, $2, ...)
鍖歸厤涓?() 鍖歸厤鐨勯儴鍒嗐?/br>
$&
$MATCH
鍓嶄竴嬈℃垚鍔熷尮閰嶇殑瀛楃涓層?/br>
$`
$PREMATCH
浣嶄簬鍓嶄竴嬈℃垚鍔熷尮閰嶅瓧絎︿覆鍓嶉潰鐨勫瓧絎︿覆銆?/br>
$'
$POSTMATCH
浣嶄簬鍓嶄竴嬈℃垚鍔熷尮閰嶅瓧絎︿覆鍚庨潰鐨勫瓧絎︿覆銆?/br>
local $_ = 'abcdefghi';
/def/;
print "$`:$&:$'\n"; # 杈撳嚭 abc:def:ghi
$LAST_PAREN_MATCH
$+
鍓嶄竴嬈℃垚鍔熷尮閰嶄腑鏈鍚庝竴涓?() 涓殑鍐呭銆?/br>
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
@LAST_MATCH_END
@+
鍓嶄竴嬈℃垚鍔熷尮閰嶄腑姣忎釜 () 鍖歸厤鐨勫亸縐婚噺銆?/br>
$+[0] 涓烘暣涓尮閰嶆湯灝劇殑鍋忕Щ閲?/br>
$+[1] 涓?$1 鏈熬鐨勫亸縐婚噺
$+[2] 涓?$2 鏈熬鐨勫亸縐婚噺
...
$#+ 涓哄墠涓嬈℃垚鍔熷尮閰嶇殑 () 鏁伴噺
%LAST_PAREN_MATCH
%+
鍓嶄竴嬈℃垚鍔熷尮閰嶄腑鍛藉悕鍖歸厤錛堝嵆涓?(?<name>...) 鐨勫艦寮忥級鐨勫搱緋昏〃銆?/br>
濡傦細
'foo' =~ /(?<foo>foo)/; # $+{foo} 鍜?$1 涓兘瀛樻斁鐫 'foo'
鍙﹀涓涓緥瀛愶細
$_ = "testing1234end";
/^testing(.)(.)(..)end/;
print "@+"; # 杈撳嚭 14 8 9 11
/^(?<foo>[a-z]+)(?<num>\d+)/;
print "$+{foo}"; # 杈撳嚭 testing
print "$+{num}"; # 杈撳嚭 1234
@LAST_MATCH_START
@-
鍜?@+ 綾諱技錛?/br>
$-[0] 涓烘暣涓尮閰嶅ご閮ㄧ殑鍋忕Щ閲?/br>
$-[1] 涓?$1 澶撮儴鐨勫亸縐婚噺
$-[2] 涓?$2 澶撮儴鐨勫亸縐婚噺
...
$` 絳夊悓浜?substr($var, 0, $-[0])
$& 絳夊悓浜?substr($var, $-[0], $+[0] - $-[0])
$' 絳夊悓浜?substr($var, $+[0])
$1 絳夊悓浜?substr($var, $-[1], $+[1] - $-[1])
$2 絳夊悓浜?substr($var, $-[2], $+[2] - $-[2])
$3 絳夊悓浜?substr($var, $-[3], $+[3] - $-[3])
%LAST_MATCH_START
%-
綾諱技浜?%+錛岄氳繃 %- 鍙互璁塊棶鍒頒笂涓嬈℃垚鍔熷尮閰嶄腑鎵鏈夌殑鍛藉悕鍖歸厤銆?/br>
姣忎釜鍛藉悕鍖歸厤鐨勫悕瀛楅兘涓庝竴涓暟緇勭浉鍏寵仈銆?/br>
姣斿璇達細
if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) {
foreach my $bufname (sort keys %-) {
my $ary = $-{$bufname};
foreach my $idx (0..$#$ary) {
print "\$-{$bufname}[$idx] : ",
(defined($ary->[$idx]) ? "'$ary->[$idx]'" : "undef"),
"\n";
}
}
}
浼氳緭鍑猴細
$-{A}[0] : '1'
$-{A}[1] : '3'
$-{B}[0] : '2'
$-{B}[1] : '4'
== 鍜屾枃浠跺彞鏌勭浉鍏崇殑鍙橀噺 ==
$ARGV
<> 涓鍦ㄨ璇誨彇鐨勬枃浠剁殑鍚嶅瓧
渚嬪瓙錛?/br>
# cat c
1
2
# cat d
1
2
3
4
# cat a.pl
while (<>) {
print "$ARGV\n";
}
# perl a.pl c d
c
c
d
d
d
d
@ARGV
鍛戒護琛屽弬鏁幫紝$ARGV[0] 浠h〃絎竴涓弬鏁幫紙鑰屼笉鏄▼搴忕殑鍚嶅瓧錛夈?/br>
$OUTPUT_FIELD_SEPARATOR
$OFS
$,
print 鍑芥暟鐨勮緭鍑哄垎闅旂錛岄粯璁ゅ間負 undef銆?/br>
渚嬪瓙錛?/br>
print "a", "c"; # 杈撳嚭 ac
$, = "|";
print "a", "c"; # 杈撳嚭 a|c
$INPUT_LINE_NUMBER
$NR
$.
涓婁竴嬈¤鍙栨枃浠剁殑琛屽彿銆?/br>
$. 鍦ㄦ枃浠跺彞鏌勫叧闂殑鏃跺欓噸緗?/br>
鐢變簬 <> 鎿嶄綔絎︿笉浼氬叧闂枃浠訛紝鍥犳鍦ㄨ繛緇粠 ARGV 璇誨彇鏂囦歡鐨勬椂鍊欙紝$. 浼氫笉鏂鍔犮?/br>
渚嬪瓙錛?/br>
# cat a.pl
while (<>) {
print "$.\n";
}
# perl a.pl c d
1
2
3
4
5
6
鍏朵腑 c d 鐨勯暱搴﹀垎鍒負 2, 4 琛屻?/br>
$INPUT_RECORD_SEPARATOR
$RS
$/
綾諱技浜?awk 鐨?RS 鍙橀噺銆傚畠鍐沖畾浜?perl 濡備綍鍒嗗壊琛屻?/br>
渚嬪瓙錛?/br>
# cat c
1|2|3
# cat a.pl
$/ = "|";
while (<>) {
print "line:$_\n";
}
# perl a.pl c
line:1|
line:2|
line:3
$OUTPUT_RECORD_SEPARATOR
$ORS
$\
print 鍑芥暟杈撳嚭鐨勫垎闅旂銆傞粯璁ゅ間負 undef錛屽鏋滃畾涔変簡錛屽垯鍦?print 瀹屾墍鏈夊弬鏁板悗杈撳嚭 $\銆?/br>
渚嬪瓙錛?/br>
$\ = "|";
print "a";
print "b";
# 灝嗚緭鍑?a|b|
$OUTPUT_AUTOFLUSH
$|
濡傛灉璁劇疆涓洪潪0鐨勫鹼紝灝嗗己鍒剁殑 flush 褰撳墠 selected 鐨勬枃浠跺彞鏌勩?/br>
錛堢被浼間簬 setbuf(xx, NULL) 錛燂級
== 鍜岄敊璇俊鎭湁鍏崇殑鍙橀噺 ==
$ERRNO
$!
絳夊悓浜?libc 涓殑 errno銆傚彲浠ヨ漿鎹負 int 鍜?string 涓ょ褰㈠紡錛?/br>
open '/a';
print int($!) . ": $!\n"; # 杈撳嚭 2: 娌℃湁閭d釜鏂囦歡鎴栫洰褰?
$CHILD_ERROR
$?
綾諱技浜?shell 涓殑 $?銆傚彲浠ヤ負涓嬪垪鎿嶄綔鐨勮繑鍥炲鹼細
- 鏈鍚庝竴嬈$閬撳叧闂?/br>
- `` 鎴栬?system() 璇彞
- wait() 鎴栬?waitpid()
$? >> 8 涓哄瓙榪涚▼鐨勮繑鍥炲?/br>
$EVAL_ERROR
$@
涓婁竴嬈?eval() 鎿嶄綔鐨勮娉曢敊璇俊鎭?/br>
渚嬪瓙錛?/br>
# cat a.pl
eval "my a";
print "$@";
# perl a.pl
No such class a at (eval 1) line 1, near "my a"
syntax error at (eval 1) line 2, at EOF
]]>
褰?.."鎿嶄綔絎﹁繑鍥瀊ool鍨嬪彉閲忕殑鏃跺欙紝鏈夊涓嬪嚑涓緥瀛愶細
while (<>) {
print if (2 .. 6);
}
灝嗕細鎵撳嵃鏂囦歡鐨?2 ~ 6 琛屻?br />
鍚屾牱鐨勶細
next if (1 .. /^$/); # 璺寵繃鍓嶉潰鐨勭┖鐧借
s/^/> / if (/^$/ .. eof); # 鍦ㄦ鏂囧墠闈㈠姞涓?>
榪樺彲浠ヨ繖鏍蜂嬌鐢細
# parse mail messages
while (<>) {
$in_header = 1 .. /^$/;
$in_body = /^$/ .. eof;
if ($in_header) {
# do something
} else { # in body
# do something else
}
}
涓嬮潰鐨勪緥瀛愬睍紺轟簡".."鍜?..."鐨勫尯鍒細
@lines = (" - Foo",
"01 - Bar",
"1 - Baz",
" - Quux");
foreach (@lines) {
if (/0/ .. /1/) {
print "$_\n";
}
}
榪欎釜紼嬪簭鍙緭鍑築ar閭h銆傚鏋滃皢".."鎹㈡垚"..."錛屽垯榪樹細杈撳嚭Baz閭h銆?br />
涓嬮潰灞曠ず".."鎿嶄綔絎﹁繑鍥炲垪琛ㄥ瀷鍙橀噺鏃跺欑殑鍑犱釜渚嬪瓙錛?br />
for (101 .. 200) { print; } # print $_ 100 times
@foo = @foo[0 .. $#foo]; # an expensive no-op
@foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
鍚屾牱鍙互鐢ㄤ簬瀛楃涓詫細
@alphabet = ("A" .. "Z");
$hexdigit = (0 .. 9, "a" .. "f")[$num & 15];
@z2 = ("01" .. "31"); print $z2[$mday];
榪樺彲浠ョ敤浜庡崟涓瓧絎︼細
use charnames "greek";
my @greek_small = ("\N{alpha}" .. "\N{omega}");
use charnames "greek";
my @greek_small = map { chr } ord "\N{alpha}" .. ord "\N{omega}";
]]>