王朝网络
分享
 
 
 

Perl/TkFAQ-12.3引号的作用如何?

王朝perl·作者佚名  2008-05-18
宽屏版  字体: |||超大  

原文:

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');

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有