【问题标题】:Use of uninitialized value $Row[3] in concatenation (.) or string在连接 (.) 或字符串中使用未初始化的值 $Row[3]
【发布时间】:2023-03-26 15:17:01
【问题描述】:

我目前在我的 OTRS 系统上遇到此错误,我无法弄清楚发生了什么。

系统日志错误:

在 Kernel::System::Console::Command::Maint::Survey::RequestsDelete 中执行 Execute() 时出错:在连接 (.) 或 / 处的字符串中使用未初始化的值 $Row[3] opt/otrs/Kernel/System/Console/Command/Maint/Survey/RequestsDelete.pm 第 132 行。

部分代码出错:

# fetch the result        
while ( my @Row = $DBObject->FetchrowArray() ) {

                my $Result = join(
                    ' ', "Survey:" . $Row[0] . "\t",
                    "TicketNumber:" . $Row[1] . "\t",
                    "SendTime:" . $Row[2] . "\t",
                    "VoteTime:" . $Row[3] . "\t",
                    "CreateTime:" . $Row[4] . "\t"
                );

                $Self->Print("$Result\n");
            }

【问题讨论】:

  • 表示变量$Row[3]没有赋值或者赋值为undef。可能是warnings 已启用,这就是弹出此消息的原因(这很好)。
  • 一个安静的奇怪,因为我在前端看不到这一行。此外,我在 ssh 上运行 RequestsDelete 并且没有发生错误并且日志仍然是空的。在此处打印:imgur.com/73tEW8E
  • 不管这个警告,$Result 值应该打印在控制台中。
  • 我该怎么做?

标签: perl otrs


【解决方案1】:

您能否验证通过$DBObject->FetchrowArray() 访问的表的数据库架构?首先是确保这些字段确实存在。

无论您是否可以验证架构,最好对字段执行验证并提供默认值(如果不能),例如

$Row[3] = '' if not defined $Row[3];

$Row[3] = '-' if not defined $Row[3];

$Row[3] = 'not defined' if not defined $Row[3];

$Row[3] //= 'not defined';

那么您的代码就会受到保护,不会丢失数据。

只需使用对输出有意义的默认值。

【讨论】:

  • @ikegami 太棒了!我倾向于回避更简洁的示例,因为它们有时会使 Perl 新手感到困惑,但我个人喜欢这种语法。
猜你喜欢
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 2022-11-04
相关资源
最近更新 更多