Perl数组哈希-严格的参考

时间:2018-09-26 03:35:42

标签: arrays perl hash strict

对于大多数人来说这可能很简单,但是我有一个Perl脚本,在其中我使用了严格的语法,并使用以下语法:

struct MyQuestionAnswerer {
    func responseTo(question: String) -> String {

        let question = question.lowercased()

        let defaultNumber = question.count % 3

        if question == "hello there" {
            return "Why hello there"
        } else if question == "where should I go on holiday?" {
            return "To the North!"
        } else if question == "where can I find the north pole?" {
            return "To the North!"
        } else if question == "where are the cookies?" {
            return "In the cookie jar!"
        } else if defaultNumber == 0 {
            return "That really depends"
        } else if defaultNumber == 1 {
            return "Ask me again tomorrow"
        } else {
            return "Could be anything"
        }
    }
}

工作正常。现在,当我启用严格时:

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

有人可以帮我解决我做错的事情吗?

非常感谢

1 个答案:

答案 0 :(得分:5)

$welcome_data[$x]{email}

的缩写
$welcome_data[$x]->{email}

换句话说,$welcome_data[$x]应该是引用(或undef [1] )。但是,在您的情况下,它包含一个空字符串。就像你在做

${""}{email}

这显然不是您想要做的。但是对您来说幸运的是,这正是严格的ref设计用来捕获的错误。现在,您可以在为$welcome[$x]分配空字符串的任何地方修复问题。


  1. 如果未定义,Perl将为您自动引用新匿名哈希的引用

    ( $welcome_data[$x] //= {} )->{email}
    
相关问题