Why does XML :: LibXML keep printing errors even if you disable them?

I am using XML::LibXML to parse a document.

The HTML file behind it has some minor errors, and the parser tells them:

 http://is.gd/create.php?longurl=http://google.com:15: validity error : ID smallink already defined nal URL was <a href="http://google.com">http://google.com</a><span id="smallink" ^ http://is.gd/create.php?longurl=http://google.com:15: validity error : ID smallink already defined and use <a href="http://is.gd/fNqtL-">http://is.gd/fNqtL-</a><span id="smallink" ^ 

However, I turned off the error report:

 my $parser = XML::LibXML->new(); $parser->set_options({ recover => 2, validation => 0, suppress_errors => 1, suppress_warnings => 1, pedantic_parser => 0, load_ext_dtd => 0, }); my $doc = $parser->parse_html_file("http://is.gd/create.php?longurl=$url"); 

My only option to fix these errors is to run the script with 2>/dev/null , which I don't want. Can someone help me please get rid of these errors?

+4
source share
2 answers

A possible solution is to install the $SIG{__WARN__} , which filters messages or simply disables all warnings:

 local $SIG{__WARN__} = sub { /* $_[0] is the message */ }; 
+2
source

I have no idea if you request XML :: LibXML so as not to print your warnings. I assume that you are, and this is an error in XML :: LibXML (which you should also tell the author), and only an address, how to suppress warnings.

Each time a warning is printed, perl will look for the value of $SIG{__WARN__} and, if it contains a link to the code, is called instead of printing the warning itself.

You can use this to stop the warnings you want to ignore for printing on STDERR . However, you must be careful with this. Be sure to suppress false alarms, not all warnings. Warnings are usually helpful. Also, be sure to localize your use of $SIG{__WARN__} to the smallest possible amount to avoid odd side effects.

 # warnings happen just as always my $parser = ...; $parser->set_options(...); { # in this scope we filter some warnings local $SIG{__WARN__} = sub { my ($warning) = @_; print STDERR $warning if $warning !~ /validity error/; }; $parser->parse_html_file(...); } # more code, now the warnings are back to normal again 

Also note that all of this implies that these warnings come from perl space. It is possible that libxml2, the C XML :: LibXML library uses under the hood, writes warnings directly to stderr itself. $SIG{__WARN__} will not be able to prevent this.

+6
source

All Articles