Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to turn off warning generated in 'use Module' statement in Perl

Tags:

perl

I am currently using the XML::LibXML module in my Perl script. The XML::LibXML module we're currently using on our machines was compiled against a newer version of the libxml2 library, and the use statement generates the following warning:

Warning: XML::LibXML compiled against libxml2 20708, but runtime libxml2 is older 20706

The warning does not affect the performance of my script. I've spoken to my supervisor and she says that the error can be completely ignored for our current purposes. Is there any way to temporarily turn off this warning without turning off ALL warnings? I tried to enclose the use statement inside of a code block {} and turned off warnings with no warnings;, but I still got the warning.

like image 421
YonkeyDonk64 Avatar asked Oct 12 '25 16:10

YonkeyDonk64


1 Answers

You can inhibit warnings, even those like this one that are issued as a result of an explicit call to the warn function, by setting $SIG{__WARN__}.

A simple working example (improved thanks to ysth's comment):

mod.pm:

package mod;

sub method {
    print "This is mod::method\n";
}

warn "WE DO NOT WANT THIS WARNING\n";

1;

foo.pl:

#!/usr/bin/perl

use strict;
use warnings;

BEGIN {
    local $SIG{__WARN__} = sub {};
    require mod;
    mod::->import();
}

warn "Normal warnings work\n";
mod::method();

The output is:

Normal warnings work
This is mod::method

and I've confirmed that commenting out the setting of $SIG{__WARN__} causes the "DO NOT WANT" warning to appear.

like image 162
Keith Thompson Avatar answered Oct 15 '25 03:10

Keith Thompson