原文:
12.3. What happened to the quotation marks?
Perl 4 programmers especially may be surprised to find a serious dearth of quotation marks around strings in perl 5 scripts such as in perl/Tk. The "rules have been relaxed" somewhat for the use of quotation marks. Basically it is OK to leave them out if the context of the string in question is unambiguous. However, it never hurts to leave them in and may help readability.
Here is Larry Wall's synopsis of the string situation: Newsgroups: comp.lang.perl.misc Subject: Re: To string or not to string... In article <4e49fv$j0u@panix3.panix.com>,Andy Finkenstadt <genie@panix.com> wrote:! Back when I was learning perl (after receiving a review copy of! learning perl, and buying the real perl book, each from ORA),! I always got bit by when I needed to use "strings" and when! I could get away with bare_words within braces for associative! arrays. (Yes, this is under 4.036 if it matters.)! ! the most typical example would be:! ! When must I use $assoc{"trailer"} and when can I get away with! $assoc{trailer}? Similarly, $ENV{CONTENT_LENGTH} versus! $ENV{"CONTENT_LENGTH"}? Unfortunately sometimes my strings! end up being numbers in their own right, i.e.: $message{"0"}! or $msg=0; $message{$msg}. Which is more appropriate,! which are merely stylistic, and which are stricly perl5! features now that I'm upgrading most of my installations! of perl.
Perl 4 let you use a "bareword" for a string if it had no other interpretation. It would warn you under -w if you used a word consisting entirely of lower-case characters, since such a word might gain an interpretation someday as a keyword.
Perl 5 still works the same way, but with several twists.
1. ) Since you can now call predeclared subroutines as though they were builtins, you have to worry about collisions with subroutine names too. However...
2. ) You can completely disallow the default interpretation of barewords by saying "use strict subs", which requires any such bareword to be a predeclared subroutine. But...
3. ) Overriding all that, Perl 5 (in recent versions) will FORCE string interpretation of any bare identifier used where a single hash subscript is expected, either within curlies or before a =>. (Those are the places you might usually want the old barewords anyway.)
The upshot of these rules is that you can write Perl 5 with much less punctuation than Perl 4, yet also with less ambiguity. If you so choose.
Larry
Tcl programmers should note that in Perl the single quotation marks '' act much as the curly brace {} enclosure does in Tcl (no escaping special characters $@\ etc.). Whereas the double quotation marks "" allow for substitution of $variables (the rules are a little different between Tcl and Perl however).
Note also that a frequently seen short hand in perl5/Tk scripts is the @list returned by qw(): @list = qw(zoom schwartz bufigliano);
which is equivalent to: @list = split(' ','zoom schwartz bufigliano');
or more simply: @list = ('zoom','schwartz','bufigliano');
i.e. the qw/STRING/ @list is not equivalent to the quotation marks provided by q/STRING/, qq/STRING/, or qq(STRING)...
There are, ironically enough, situations in perl/Tk where one needs to use quotation marks as in the following by post by <a904209@pluto.tiuk.ti.com>: Paul Wickman wrote in article <4b4o0fINNlu8@CS.UTK.EDU>:!! Why does the following statement work fine:!!$day->pack(-before => $year, -side => 'left');!! But the below generates the given error:!!$day->pack(-after => $year, -side => 'left');!!Ambiguous use of after => resolved to "after" => at line 191.!
Because there is a sub after in scope, probably imported from Tk via use Tk;.
There are two workrounds:
use Tk qw(MainLoop exit ...); # just ones you use
or
$day->pack('-after' => $year, -side => 'left');
译文:
12.3 引号的作用如何?
Perl4 的程序员可能会觉得特别的惊讶,因为在Perl5的脚本中(例如Perl/Tk),很多字符串周围都没有引号。显然,对于引号的使用,Perl5将其规则稍微放松了一些。基本上,只要上下文中不会引起歧义,就可以不用引号包围字符串。当然,加上引号是不会有任何害处的,而且还可以帮助阅读。
下面就是作者Larry Wall对于字符串的说明:
新闻组:comp.lang.perl.misc
主题:回复:字符串和非字符串……
文章4e49fv$j0u@panix3.panix.com
Andy Finkenstadt写到:
!回想当年我开始学习Perl的时候(自从我收到了一个学习Perl的赠刊,
!然后又买了一个真正的Perl书——都是来自ORA),我总是因为必须使用
!引号包围字符串,但在关联数组的括号中又可以使用裸词,而感到很不舒
!服。(是的,这是在4.036版本以前的情况。)
!
!最典型的例子是:
!
!在什么情况下必须使用$assoc{“trailer”},而什么情况下又可以使用
!$assoc{trailer}? 简单的说就是ENV{CONTENT_LENGTH}对
!$ENV{"CONTENT_LENGTH"}的问题? 不幸的是,有时我的字符串本身右边
!是以数组结尾的,例如:$message{“0”}或者是$msg=0;$message{$msg}。
!哪一个更合适一些?哪个格式更标准些?或者说,哪一个更符合我正在升
!级而成的Perl5的特性?
Perl4允许你在没有其它的解释的情况下使用“裸词”来表示一个字符串。在使用-w选项的情况下,如果你使用一个完全由小写字母构成的词,将会被警告。因为这个词有可能在以后被用作为一个关键词来解释。
Perl5中的原则基本相同,但也有些区别:
1.)因为现在我们可能像调用一个内置函数一样的调用一个预申明的子程序,所以我们必须考虑我们的字符串可能会与子程序名产生的冲突。但是……
2.)你可以使用“use strict subs”来完全的禁止默认的对于裸词的解释,这样就要求所有这样的裸词都是预申明的子程序。然而……
3.)除此之外,比以上优先级更高的是,Perl5中(近期的版本)在如下的一些情况下将会强制把裸词译为字符串:作为hash的关键词时;在花括号内;或在符号“=>”的前面时。(总之,都是一些你过去希望使用裸词的地方。)
这些新规则的结果就是,使Perl5的脚本会比Perl4少很多标点符号,同时也更加明确。如果你选择这个。
Larry
Tcl的程序员会发现Perl中的单引号’’的作用非常像Tcl语言中的花括号{}(除了特殊字符$@\等等)。但是,双引号运行进行变量值的替换(这一点规则Tcl和Perl有所不同)。
同时请注意在Perl5/Tk的脚本中经常出现的用qw()来返回数组的方法:
@list = qw(zoom schwartz bufigliano);
这实际上等价于:
@list = split(' ','zoom schwartz bufigliano');
或者更简单的写为:
@list = ('zoom','schwartz','bufigliano');
也就是说,qw/STRING/的表示形式不等于其它的引用方式,如q/STRING/, qq/STRING/, 或qq(STRING)……
特别需要注意的,在Perl/Tk中有很多地方有时是必须使用引号的,例如下面的帖子:
来自<a904209@pluto.tiuk.ti.com>
Paul Wickman 在文章<4b4o0fINNlu8@CS.UTK.EDU>中写到:
!
! 为什么这样的语句可以使用:
! $day->pack(-before => $year, -side => 'left');
!
! 但是下面的这个却会产生如下的报错:
!
! $day->pack(-after => $year, -side => 'left');
!
!Ambiguous use of after => resolved to "after" => at line 191.
!
因为在你的脚本中还存在着一个名为after的子程序,可能是通过use Tk;语句从Tk中引入的。
这里有两个解决的方法:
use Tk qw(MainLoop exit …); #也就是只导入你需要用的子程序
或者
$day->pack('-after' => $year, -side => 'left');