加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl钻石运算符是否迭代非魔术数组(不是@ARGV)?

发布时间:2020-12-15 21:46:18 所属栏目:大数据 来源:网络整理
导读:我不认为以下内容应该有效,但确实如此: $perl -e '@a = qw/1222 2 3/; while (@a) { print $_ ."n";}'122223$ 据我所知,Perl的运算符应该对文件句柄,全局等工作,除了文字 (而不是 FILEHANDLE),它神奇地遍历@ARGV. 有谁知道它是否应该像我在测试中那样工作
我不认为以下内容应该有效,但确实如此:
$perl -e '@a = qw/1222 2 3/; while (<@a>) { print $_ ."n";}'
1222
2
3
$

据我所知,Perl的<>运算符应该对文件句柄,全局等工作,除了文字<> (而不是< FILEHANDLE>),它神奇地遍历@ARGV.

有谁知道它是否应该像我在测试中那样工作?

解决方法

魔术在工作!

从’perldoc perlop‘:

If what’s within the angle brackets is neither a filehandle nor a simple scalar variable
containing a filehandle name,typeglob,or typeglob reference,it is
interpreted as a filename
pattern to be globbed,and either a list of filenames or the next
filename in the list is
returned,depending on context.

这是您使用此代码触发的规则.这是发生了什么:

>< @ a> (在语法上,在编译时)确定为glob扩展
>因此<>将@a转换为字符串“1222 2 3”(数组的字符串插值)
列表上下文中的> glob(“1222 2 3”)返回(‘1222′,’2′,’3’)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读