数组 – perl中的${…}究竟是什么意思?
想着下一个.有一个列表
qw(a b c); 现在,将LIST分配给无名(匿名)ARRAY [ qw(a b c) ] 所以下一个 use 5.016; use warnings; use diagnostics; my $x = [ qw(a b c) ]; say ref $x; #ARRAY - the $x is an ARRAY reference,and say $x->[1]; #prints "b",and say [ qw(a b c) ]->[1]; #works too 但现在发生了什么? use 5.016; use warnings 'all'; use diagnostics; say ${[ qw(a b c) ]}[1]; 它打印b,但是 my $y = ${[ qw(a b c) ]}; 是一个错误, Not a SCALAR reference at pepe line 6 (#1) (F) Perl was trying to evaluate a reference to a scalar value,but found a reference to something else instead. You can use the ref() function to find out what kind of ref it really was. See perlref. Uncaught exception from user code: Not a SCALAR reference at pepe line 17. 那么,什么意思是建筑${….} >它在说“工作”(打印匿名数组的第二个元素),但不明白为什么 来自诊断的提示并不是很有帮助,因为当我无法分配时我应该如何使用ref?我错过了perlref? 解决方法
${EXPR1} [EXPR2]是一个数组索引取消引用.它返回由EXPR1返回的引用引用的数组的EXPR2返回的索引处的元素.
${$array_ref} […]是数组引用,因为$array […]是数组. ${EXPR}未跟随[或{是标量解除引用.它返回EXPR返回的引用引用的标量. ${$scalar_ref}是标量引用,因为$scalar是标量. 正如您所看到的,在处理引用时,您可以使用与通常相同的语法,除非您使用{$ref}替换变量的名称(保留前导符号). 因此,@ {$array_ref}是数组引用,因为@array是数组. say @{[ qw(a b c) ]}; 这是我早期Mini-Tutorial: Dereferencing Syntax帖子中图表的本质.另见: > References quick reference 哎呀,我以为你有 say ${[ qw(a b c) ]}; # Want to print a b c 你有 my $y = ${[ qw(a b c) ]}; 你要 my $y = [ qw(a b c) ]; []创建一个数组和对该数组的引用,并返回后者,有点像 my $y = do { my @anon = qw(a b c); @a }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |