您的位置:首页 > 其它

PERL 语言中的q,qw,qr,qx,qq......符号用法总结

2011-06-03 23:01 856 查看
在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.



补充:

当你use某个模块时,默认会导入一些函数或变量,他们通过所在包的@EXPORT这个特殊数组输出.

也有些不常用的方法或方法组没有默认输出,通常放在@EXPORT_OK数组里,他们要被手工导入.

use CGI qw(:standard)这里表示手工导入CGI.pm里的standard方法组,它其实是个函数集合.

这样看下就明白了:

use CGI qw();不导入任何符号及符号集

use CGI qw(:all);导入所有方法

use CGI qw(:standard);导入standard这个方法集合

use CGI;导入默认的方法,可能是什么也没有

use CGI qw(redirect);导入redirect这个方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: