阅读perl来源我多次看到下一个结构:
printf qq[%s\n], getsomestring( $_ );
但通常是,它被写为
printf "%s\n", getsomestring( $_ );
问题:
qq[...]
与"..."
perlop没有提及此事。
答案 0 :(得分:6)
您可以使用qq()
作为替代双引号方法,例如在字符串中有双引号时。例如:
"\"foo bar\""
写作时看起来更好
qq("foo bar")
当在使用双引号的windows cmd shell中时,我经常在需要插值时使用qq()
。例如:
perl -lwe "print qq($foo\n)"
qq()
运算符 - 与许多其他perl运算符(如s///
,qx()
一样 - 在演示时也很方便,因为它可以使用几乎任何字符作为其分隔符:
qq[this works]
qq|as does this|
qq#or this#
当字符串中有许多不同的分隔符时,这很方便。例如:
qq!This is (not) "hard" to quote!
至于最佳实践,我会说使用更具可读性的东西。
答案 1 :(得分:2)
当字符串中有引号时,我总是使用qq[...]
,例如:
qq["here you are", he said]
如果没有,对我来说使用""