简体   繁体   中英

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:

$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] ). 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. Now you can go fix wherever you are assigning an empty string to $welcome[$x] .


  1. If it's undef, Perl will autovivifiy a reference to a new anonymous hash for you as if you had used

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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