不能使用未定义的值作为HASH参考 - Perl Mojolicious

时间:2017-02-13 10:51:34

标签: perl mojolicious hashref

所以我在perl上有点像菜鸟,但实质上这段代码是我模型的一部分(插入到sql数据库中)。

运行此代码时出现的错误是“无法使用未定义的值作为HASH参考”。

我认为明确的解决方案是定义一个包含名为host_id的密钥的哈希。

我将如何处理或修复我的代码。

    ####delete subfunctions
    ######
    sub deletehostservices
    {
      my ($self, $host_service_id) = @_;

      my $host_id = $self->pg->db->query('DELETE from hosts_services where hosts_services_id = ? returning host_id', $host_service_id)->hash->{host_id};
      return $host_id;
    }

1 个答案:

答案 0 :(得分:0)

当Mojo :: Pg' ->hash方法返回undef时,原因是没有返回任何行。

您可以将结果存储在具有默认

的临时变量中
my $res = $self->pg->db->query(
  'DELETE from hosts_services where hosts_services_id = ? returning host_id', $host_service_id
)->hash || {};
return $res->{host_id};

或处理undef的其他几种模式。以上是我喜欢的这种情况。