【问题标题】:suppress stderr from perl DBI execute从 perl DBI 执行中抑制 stderr
【发布时间】:2021-04-30 23:21:40
【问题描述】:
my $sth = $dbh->prepare(q{
  INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
}) or die $dbh->errstr;
while (<>) {
    chomp;
    my ($product_code, $qty, $price) = split /,/;
    $sth->execute($product_code, $qty, $price) or die $dbh->errstr;
}
$dbh->commit or die $dbh->errstr;

我不想在控制台上显示来自 $sth->execute 的 stderr。如何抑制它?

【问题讨论】:

  • 这能回答你的问题吗? Perl: How to "die" with no error message?
  • 删除 die $sth->execute($product_code, $qty, $price) 后,我想从 $sth->execute 抑制 stderr,控制台上不应打印任何错误
  • 可以通过编写$SIG{__WARN__} 挂钩在很大程度上控制流向STDERR 的内容。请参阅 perlvar 中的 %SIG 哈希。 (但我不确定你到底在问什么......)
  • 请考虑其他人可能想要这些错误,尽管在您的用例中您不需要。这可能意味着不理会它并将标准输出重定向到空设备。但是,当您需要时,警告就会出现。

标签: perl dbi


【解决方案1】:

这由 DBI 句柄的 PrintWarn 和 PrintError 设置控制。我通常在建立初始数据库连接 ($dbh = DBI-&gt;connect($data_source, $user, $pass, { PrintWarn =&gt; 0, PrintError =&gt; 0 });) 时全局关闭它们,因为我更喜欢自己进行错误处理和报告,但也可以在每个语句级别设置它们,将它们关闭一段时间使用set_err 方法,单个execute 然后再返回等等。

【讨论】:

    猜你喜欢
    • 2016-10-31
    • 1970-01-01
    • 2013-10-27
    • 2019-02-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2012-11-15
    • 2014-01-28
    相关资源
    最近更新 更多