Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress Perl warnings emitted from within a loaded module's code?

My Perl program is reading data from a serial device attached through USB. Headlines of my script in pseudo-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 )
}

All works fine and even when I unplug the device from USB I'm able to recover from that situation, when the device file disappears and reappears. I can catch all errors spawned from my own script, but the loaded modules (Device::SerialPort) spawns warnings too and I don't want them to appear in my logging.

Can I add some sort of flag to my code so I don't see these specific warnings? It is important for me that only warnings from the module(s) are suppressed, not the warnings from my own script. Currently it looks like this:

[/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 : ................

So it is about the two Use of uninitialized value warnings that I want to get rid of. The other warnings are my own logging.

  • libdevice-serialport-perl 1.04-2build1
  • perl v5.12.4
like image 971
jippie Avatar asked Apr 29 '12 08:04

jippie


1 Answers

You could try and intercept the warnings:

$SIG{'__WARN__'} = sub { warn $_[0] unless (caller eq "Device::SerialPort"); };
like image 103
kmkaplan Avatar answered Sep 21 '22 19:09

kmkaplan