轉:PERL 語言中的q,qw,qr,qx,qq......符號用法總結

 PERL 語言中的q,qw,qr,qx,qq......符號用法總結
在perl語言中,有兩個特殊而常用的符號qq qw,分別解釋如下:
qq{foobar}的意思爲意思爲雙引號字符串,可內插變量
相當於 "foobar"
qw{foo bar}的意思爲用空格分解字符串,得到列表,相當於如下語句
split(' ','foo bar') 得到的結果是'foo','bar'

字符串比較等於用 eq

q運算符對'號轉義的方式
$someword = 'i /'ve some money';
可以等價於:
$someword = q~i 've some money~;

qq運算符對"號轉義的方式
$someword = "i say /"ok!/".";
可以等價於:
$someword = qq~i say "ok!".~;

qw代表用空格來分隔元素,得到列表
@list = ("perl","Regular","network","web");
可以等價於:
@list = qw(perl Regular network web);

qr代表創建正則
$myword = "catcat";
$replaceword = qr(catcat);
$finalword = "ok" if ($myword =~ $replaceword);

引號執行運算符(quoted execution operator),qx//
qx/uname -p -r/

q 和 qq 運算符的特點:
※ q 和 qq 必須是一個標識符,而不是標識符的部分。例如:
q (abc)      用 () 作爲分界符
q(abc)       用 () 作爲分界符
q xabcx      用 x 作爲分界符
都是合法的,而
qxabcx 就會被當作是一個標識符來處理,有誰會想到 qxabcx 居然是一個 q 運算式呢?

※ q 和 qq 後面的空格並不會影響語法,但是任何非空字符(不是空格、不是 TAB 字符、
   不是換行符)則會被當作界限符來使用。

※ 如果 q 和 qq 使用一些特殊的符號的時候,則必須配對。比如:< 只能和 >; 在一起
   用來當作界限符,而不能只用兩個 < 作爲界限符。而 | 則只能和它自己作爲一對界
   限符。這些特殊的符號有:()、{}、[]、<>;

$myword = "catcat";
$replaceword = qr(catcat);
$finalword = "ok" if ($myword =~ $replaceword);

@list = ("perl","Regular","network","web");
可以等價於:
@list = qw(perl Regular network web);

$someword = 'i /'v some money';
可以等價於:
$someword = q~i 'v some money~;

q// is generally the same thing as using single quotes - meaning it doesn't interpolate values inside the delimiters.
qq// is the same as double quoting a string. It interpolates.
qw// return a list of white space delimited words. @q = qw/this is a test/ is functionally the same as @q = ('this', 'is', 'a', 'test')
qx// is the same thing as using the backtick operators.
I've never used qr//, but it's got something to do with compiling regex's for later use.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章