Perl5.010中的智能匹配操作符~~的问题。

18 views
Skip to first unread message

伊现富

unread,
Jan 16, 2011, 11:34:06 PM1/16/11
to shlug
此邮件曾经发送到PerlChina邮件列表,但是没有回复,所以在此重发一下。

大家好!
我在测试Perl5.010中的智能匹配操作符~~
的时候发现下面这么一个问题:
在《Perl语言入门》(第五版)的第236页的表格中,有下面几句话:
@a ~~ /Fred/    有一个元素匹配给定的模式
@a ~~ 123       至少有一个元素转化为数字后是123
@a ~~ 'Fred'     至少有一个元素转化为字符串后是‘Fred'

但是我用下面的代码测试了一下,却发现上面的第2、3句有问题:
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my @a = ( 1, 2, 3 );
foreach my $t (@a) {
    print "$t\t";
}
print "\n";
if ( @a ~~ /2/ ) {
    print "Match YES!\n";
}
else {
    print "Match No\n";
}
if ( @a ~~ 2 ) {
    print "Number YES!\n";
}
else {
    print "Number No\n";
}
if ( @a ~~ "2" ) {
    print "String YES!\n";
}
else {
    print "String No\n";
}

输出结果:
1    2    3   
Match YES!
Number No
String No

不知道谁能给解释一下?谢谢!

Jeova Sanctus Unus

unread,
Jan 17, 2011, 1:43:44 AM1/17/11
to sh...@googlegroups.com
~~操作符变了,5.010和5.012的不一样了
Any Array match against an array element
是说匹配的。不过书上说符合交换律,现在不符合了。
即@a ~~ 2和 2 ~~ @a不一样
你换下就知道了。不过我也不知道2 ~~ @a是啥意思,我再想想
你先2 ~~ @a写着吧
我记得这个问题我在这里问过。newsmth也有。
ps:感谢fvw以前的帮助XD

Jeova Sanctus Unus

unread,
Jan 17, 2011, 2:10:26 AM1/17/11
to sh...@googlegroups.com
@a ~~ 2貌似是把\@a转换成string,比如ARRAY(0x624278),然后和2比较

perl -e 'use 5.010; $c=sprintf "%s",\@a;print "y" if @a ~~ $c'

perl -MO=Deparse -e 'use 5.010; @a ~~ 2'
你应该懂的:)

Jeova Sanctus Unus

unread,
Jan 17, 2011, 2:43:04 AM1/17/11
to sh...@googlegroups.com

伊现富

unread,
Jan 17, 2011, 2:51:31 AM1/17/11
to sh...@googlegroups.com
谢谢!确实你交换律的问题。

在 2011年1月17日 下午2:43,Jeova Sanctus Unus <jeova.san...@gmail.com>写道:

伊现富

unread,
Jan 17, 2011, 2:52:34 AM1/17/11
to sh...@googlegroups.com
perl  -e 'use 5.010; $c=sprintf "%s",\@a;print "y" if @a ~~ $c'
需要改成
perl  -e 'use 5.010; $c=sprintf "%s",\@a;print "y\n" if @a ~~ $c'


perl -MO=Deparse -e 'use 5.010; @a  ~~ 2'
这句不适很明白,不知能不能详细解释一下。谢谢!

伊现富

unread,
Jan 17, 2011, 2:53:16 AM1/17/11
to sh...@googlegroups.com
就是习题答案中遇到的这个问题。

Jeova Sanctus Unus

unread,
Jan 17, 2011, 2:59:19 AM1/17/11
to sh...@googlegroups.com
#我zsh里最后不输出\n也行的...
-M是参数perl --help看下
那句等同于
perl -e 'use O Deparse;use 5.010; $c=sprintf "%s",\@a;print "y" if @a ~~ $c'

ghosTM55

unread,
Jan 17, 2011, 3:00:58 AM1/17/11
to sh...@googlegroups.com
2011/1/17 Jeova Sanctus Unus <jeova.san...@gmail.com>:

Perl这门语言现在是什么样的一个状态?

--
Thomas
Shanghai Linux User Group

http://www.ghosTunix.org
Twitter: @ghosTM55

cnhack TNT

unread,
Jan 17, 2011, 3:20:51 AM1/17/11
to sh...@googlegroups.com
在 Perl 5.10 中的 smart match 已经在 5.10.1 及 5.12 之后的版本有所改变,正如之前楼上说的交换律已经不适用了。

当前的 smart match 行为定义在: http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail
我们在翻译该书的时候,还未有这个问题(亦或是我们没注意,只按照原文来处理了)请以 perldoc.perl.org 的定义为准,给你带来理解上的困扰十分不好意思 :-)

楼主如果是想看看某个标量是否在数组中存在,$a ~~ @b 是正确的做法,而 @b ~~ $a 这种写法在5.10.1及5.12等版本中是不存在的。

另外还要注意的是,请勿用 smart match 来尝试对列表中的元素进行逐一匹配,因为 smart match 工作在标量上下文环境, 在标量上下文环境中,列表返回的是它最右边的一个元素 :-)


2011/1/17 ghosTM55 <ghost...@gmail.com>

Jeova Sanctus Unus

unread,
Jan 17, 2011, 3:29:31 AM1/17/11
to sh...@googlegroups.com
翻译该书?是指learning perl 5th么?
貌似官方已经不管勘误了?
http://oreilly.com.cn/book.php?m=errata&bn=978-7-5641-1763-4

Jeova Sanctus Unus

unread,
Jan 17, 2011, 3:30:47 AM1/17/11
to sh...@googlegroups.com
据我所知:
还有新特性在引入.
老问题没解决.(今天还看见有人说不要用多线程...)

cnhack TNT

unread,
Jan 17, 2011, 3:41:14 AM1/17/11
to sh...@googlegroups.com
是的,我是 learning perl 5th 大陆中文版的译者之一(还有chunzi 和 joejiang)

Perl 的 thread 是比较悲催的,默认我们编译 perl 都不用 thread 支持,打开 thread 支持会导致perl整体性能损耗,如果有多任务的需求,可以使用多进程,或者 POE/Coro/EV,做高性能高并发的东东估计使用 EV 的人会占去一大票,个人认为 perl 中的 thread 很多时候是被大伙儿所忽略的东东。

可以参考以下模块:

Jeova Sanctus Unus

unread,
Jan 17, 2011, 3:49:56 AM1/17/11
to sh...@googlegroups.com
那么,我猜,你是 王晖 咯,呵呵
书的官网(中文)不管勘误.比较悲剧.
今晚我把我遇到的去贴到水木和forum.ubuntu.org.cn去.

伊现富

unread,
Jan 17, 2011, 3:54:47 AM1/17/11
to sh...@googlegroups.com
谢谢楼上几位的热心解答!

cnhack TNT

unread,
Jan 17, 2011, 4:05:14 AM1/17/11
to sh...@googlegroups.com
汗。。。被人肉了。。

Jeova Sanctus Unus

unread,
Jan 17, 2011, 4:14:12 AM1/17/11
to sh...@googlegroups.com
谁让我有买书呢(炫耀下XD),看下封面就知道咯

谁谁谁谁

Reply all
Reply to author
Forward
0 new messages