加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何抑制从加载的模块的代码中发出的Perl警告?

发布时间:2020-12-13 19:49:17 所属栏目:Linux 来源:网络整理
导读:我的Perl程序正在从通过USB连接的串行设备读取数据.伪Perl中的脚本标题: use warnings;use strict;use Device::SerialPort;my $PortObj = tie( *$handle,"Device::SerialPort",$PortName ) or die "Cannot open serial port: $!n";while ( 1 ) { my $readL
我的Perl程序正在从通过USB连接的串行设备读取数据.伪Perl中的脚本标题:
use warnings;
use strict;

use Device::SerialPort;
my $PortObj = tie( *$handle,"Device::SerialPort",$PortName ) or die "Cannot open serial port: $!n";
while ( 1 ) {
  my $readLength = read( $handle,my $frameData,$frameLength )
}

一切工作正常,即使我从USB拔出设备我可以从这种情况恢复,当设备文件消失并重新出现.我可以捕获从我自己的脚本产生的所有错误,但加载的模块(Device :: SerialPort)也产生警告,我不希望它们出现在我的日志记录中.

我可以在我的代码中添加一些标志,所以我看不到这些特定的警告?对我来说重要的是只有模块的警告被抑制,而不是我自己的脚本中的警告.目前看起来像这样:

[/dev/ttyUSB1]   0x0020 : 00 00 00 00 00 00 00 00 00 AA 93 82 73 68 5E 58 : ............sh^X
[/dev/ttyUSB1]   0x0030 : 55 54 52 52 4F 4E 50 51 50 00 00 00 00 00 00 00 : UTRRONPQP.......
Use of uninitialized value $count_in in addition (+) at /usr/lib/perl5/Device/SerialPort.pm line 2214.
Use of uninitialized value $string_in in concatenation (.) or string at /usr/lib/perl5/Device/SerialPort.pm line 2232.
[/dev/ttyUSB1] Restart required!
[/dev/ttyUSB1] Cannot open serial port: No such file or directory
[/dev/ttyUSB1] Cannot open serial port: No such file or directory
[/dev/ttyUSB1] Cannot open serial port: No such file or directory

[/dev/ttyUSB1]   0x0000 : 41 42 01 40 71 01 1C E4 80 99 80 80 80 80 00 00 : AB.@q...........
[/dev/ttyUSB1]   0x0010 : 00 03 00 00 83 00 01 01 00 00 00 00 00 00 00 00 : ................

所以这是关于两个使用未初始化的值警告,我想摆脱.其他警告是我自己的日志记录.

> libdevice-serialport-perl 1.04-2build1
> perl v5.12.4

解决方法

你可以尝试拦截警告:
$SIG{'__WARN__'} = sub { warn $_[0] unless (caller eq "Device::SerialPort"); };

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读