我的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 : ................
所以它是关于我想要摆脱的两个Use of uninitialized value
警告。其他警告是我自己的记录。
答案 0 :(得分:12)
您可以尝试拦截警告:
$SIG{'__WARN__'} = sub { warn $_[0] unless (caller eq "Device::SerialPort"); };
答案 1 :(得分:3)
在我的主目录中制作了该模块的副本,并添加了两行:
$ diff SerialPort.pm /usr/lib/perl5/Device/SerialPort.pm
2207,2208d2206
< unless ( defined $count_in ) { $count_in = 0; }
< unless ( defined $string_in ) { $string_in = ""; }
这似乎有效。通知作者。