免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5202 | 回复: 8
打印 上一主题 下一主题

逗号,和点号.在输出语句中起连接作用时的差异 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-12 13:34 |只看该作者 |倒序浏览
@books = qw\first second third forth fifth\;

print "There are ".@books." books: @books."."\n";  #两个都是点号
print "There are ",@books," books: @books."."\n";  #两个都是逗号
print "There are ",@books." books: @books."."\n";  #前一个逗号,后一个点号
print "There are ".@books," books: @books."."\n";  #前一个点号,后一个逗号

输出:
There are 5 books: first second third forth fifth.
There are firstsecondthirdforthfifth books: first second third forth fifth.
There are 5 books: first second third forth fifth.
There are 5 books: first second third forth fifth.

在上面的程序里,用@books本意是计算数组books的元素个数,但是当@books左右两边都是逗号时,@books并没有计算出数组的元素个数,而是直接被当成了整个数组元素的字符串集合。但在其他三种情况里,@books都计算出来数组books的元素个数。请问为什么会有这样的差异呢?

我刚开始学小骆驼,目前刚看完第三章。

[ 本帖最后由 bequan 于 2009-8-12 15:06 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-08-12 14:26 |只看该作者
原帖由 bequan 于 2009-8-12 13:34 发表
@books = qw\first second third forth fifth\;

print "There are ".@books." books: @books."."\n";  #两个都是冒号
print "There are ",@books," books: @books."."\n";  #两个都是逗号
print "There ar ...


取决于变量所在的上下文环境:
print "There are ".@books." books: @books."."\n";  
print "There are ",@books." books: @books."."\n";  
print "There are ".@books," books: @books."."\n";
以上三个语句中的第一个@books处于标量环境,纠正下,这个"."不是冒号,是点号,用于连接字符串,
在标量环境中数组变量返回数组大小;
print "There are ",@books," books: @books."."\n";
该语句中第一个@books处于列表环境,因为@books两边都是逗号,而print提供了列表环境,这个语句可看作:
print "There are ", $books[0], $books[1],  $books[2], $books[3], $books[4], " books: @books."."\n";

上下文在perl中是很基本的概念,恩,也是很玄乎的一个东西,有时候觉得只可意会,不可言传。。。。。

[ 本帖最后由 iceberg77 于 2009-8-12 14:31 编辑 ]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
3 [报告]
发表于 2009-08-12 14:52 |只看该作者
哪个是冒号?

context!

论坛徽章:
0
4 [报告]
发表于 2009-08-12 14:56 |只看该作者
原帖由 iceberg77 于 2009-8-12 14:26 发表


取决于变量所在的上下文环境:
print "There are ".@books." books: @books."."\n";  
print "There are ",@books." books: @books."."\n";  
print "There are ".@books," books: @books."."\n";
以上 ...


——这两句,为什么又都解释成了标量呢?
print "There are ",@books." books: @books."."\n";  #前一个逗号,后一个点号
print "There are ".@books," books: @books."."\n";  #前一个点号,后一个逗号

上下文这东西就这么玄乎?这么没规律?

[ 本帖最后由 bequan 于 2009-8-12 14:58 编辑 ]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
5 [报告]
发表于 2009-08-12 15:24 |只看该作者
太玄了,有这么玄吗?
.的优先级别比,高,所以两个都是scalar context。

应该是这个道理。

[ 本帖最后由 blackold 于 2009-8-12 15:26 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-08-12 15:37 |只看该作者
原帖由 bequan 于 2009-8-12 14:56 发表


——这两句,为什么又都解释成了标量呢?
print "There are ",@books." books: @books."."\n";  #前一个逗号,后一个点号
print "There are ".@books," books: @books."."\n";  #前一个点号,后一个逗号
...


这两句其实没多大区别,因为点号比逗号的优先级高, @books处于点号提供的标量环境中;

perl的上下文就像我们平时说话一样,也有上下文,同一句话在不同环境中可能有不同的含义,
并不是无规律可循,但也不是三言两语就能涵盖清楚

论坛徽章:
0
7 [报告]
发表于 2009-08-12 15:39 |只看该作者

回复 #1 bequan 的帖子

print "There are ".@books." books: @books."."\n";  #两个都是点号

.(连接操作符)需要scalar context,它会把@books放在scalar context中来解析,然后再连接前后的字符串,所以@books在这里输出5
至于.操作符为啥是scalar context,这个一直没找到相关的资料,写了段代码证明.操作符只需要scalar context,贴上来供大家参考
  1. use strict;
  2. use warnings;

  3. sub provider { wantarray ? 1 : 2; }
  4. my @a = provider() . '!';
  5. print @a;
复制代码

print "There are ",@books," books: @books."."\n";  #两个都是逗号

,(逗号操作符)既能用于list context,也能用于scalar context,详细介绍请参考perlop中的comma operator一节,在这里因为print需要list context,所以逗号操作符会在list context中来解析@books,即展开数组输出字符串

print "There are ",@books." books: @books."."\n";  #前一个逗号,后一个点号

因为连接操作符比逗号操作符的优先级别高(参考perlop中的Operator Precedence and Associativity一节),所以@books会先在scalar context中和后面的字符串一块解析,所以这里的@books输出5

print "There are ".@books," books: @books."."\n";  #前一个点号,后一个逗号

同上解释

论坛徽章:
0
8 [报告]
发表于 2009-08-12 17:32 |只看该作者
Binary "," is the comma operator. In scalar context it evaluates its left argument in void context, throws that value away, then evaluates its right argument in scalar context and returns that value. This is just like C's comma operator. For example:

$a = (1, 3);
assigns 3 to $a. Do not confuse the scalar context use with the list context use. In list context, a comma is just the list argument separator, and inserts both its arguments into the LIST. It does not throw any values away
For example, if you change the previous example to:

@a = (1, 3);
you are constructing a two-element list, while:
atan2(1, 3);
is calling the function atan2 with two arguments.

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
9 [报告]
发表于 2009-08-12 18:01 |只看该作者

回复 #8 Perl_Er 的帖子

不学习不行。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP