简体   繁体   English

Perl数组哈希-严格的参考

[英]Perl array hash - strict refs

this is probably simple for most, but I have a Perl script where I am ot using strict, and use this syntax: 对于大多数人来说,这可能很简单,但是我有一个Perl脚本,在其中我使用严格,并使用以下语法:

$welcome_data[$x]{email}       = $data[0];

It works fine. 工作正常。 Now, when I enable strict: 现在,当我启用严格时:

$welcome_data[$x]{email}       = $data[0];
Can't use string ("") as a HASH ref while "strict refs" in use

Can anyone help with what I am doing wrong?? 谁能帮我解决我做错的事情?

Many thanks 非常感谢

$welcome_data[$x]{email}

is short for 是短的

$welcome_data[$x]->{email}

In other words, $welcome_data[$x] is expected to be a reference (or undef [1] ). 换句话说, $welcome_data[$x]应该是一个引用(或undef [1] )。 However, in your case, it contained an empty string. 但是,在您的情况下,它包含一个空字符串。 It's as is you were doing 就像你在做的一样

${""}{email}

That's obviously not what you wanted to do. 显然那不是您想要的。 But fortunately for you, this is exactly the kind of bug strict refs is designed to catch. 但是对您来说幸运的是,这正是严格的ref设计用来捕获的错误。 Now you can go fix wherever you are assigning an empty string to $welcome[$x] . 现在,您可以在为$welcome[$x]分配空字符串的任何地方修复。


  1. If it's undef, Perl will autovivifiy a reference to a new anonymous hash for you as if you had used 如果不确定 ,Perl会自动为您提供对新匿名哈希的引用,就像您曾经使用过一样。

     ( $welcome_data[$x] //= {} )->{email} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM