Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Need to turn off prints from module

by Mushka (Acolyte)
on Nov 17, 2010 at 16:50 UTC ( #872018=perlquestion: print w/replies, xml ) Need Help??
Mushka has asked for the wisdom of the Perl Monks concerning the following question:

Hi All!

Im using Parse::HTTP::UserAgent module that keeps FLOODING me with messages like:
Version string '4.06&#91;en&#93;' contains invalid data; ignoring: '&# +91;en&#93;' at /usr/local/share/perl/5.8.8/Parse/HTTP/ li +ne 210, <FILES> line 8.
I want to tell it "im ok with that, shut up and do your job without bothering me". :o) I dont want to comment out strict and warnings (high five to On Commenting Out 'use strict;')!

Any suggestions?

Replies are listed 'Best First'.
Re: Need to turn off prints from module
by Corion (Pope) on Nov 17, 2010 at 17:08 UTC

    It would seem to me that this is a bug in Parse::HTTP::UserAgent. It contains a regular expression to suppress those messages, but the RE is

    use constant RE_WARN_INVALID => qr{\QVersion string .+? contains inva +lid data; ignoring:\E}xms;

    ... which will quotemeta all metachars, especially .+?, which seems wrong. Also, the mixed usage of x and \Q happens to work but usually when using /x, you should use \s (or [ ]) for matching spaces.

    My approach would be to either preload and monkeypatch Parse::HTTP::UserAgent::Constants or to fix the module upstream.

    Also, I'm not sure what actually raises these warnings.

Re: Need to turn off prints from module
by kcott (Canon) on Nov 17, 2010 at 17:39 UTC

    If you can identify the point in the code where this is happening, you can selectively turn off this specific warning type with:

    no warnings 'misc'; # problem code here use warnings 'misc';

    Be aware this turns off all 'misc' warnings between no warnings 'misc'; and use warnings 'misc';.

    If you look in perldiag, you can do a search for 'Version string' which finds:

    Version string '%s' contains invalid data; ignoring: '%s'
    (W misc) The version string contains invalid characters at the end, which are being ignored.

    The "(W misc)" tells you which warning to turn off.

    perllexwarn describes all the warning types.

    -- Ken

      Thank you all!

      Ken, i love your suggestion - this is exactly what i'm looking for. I tried putting no warnings 'misc'; just before i start using the module in my $ua = Parse::HTTP::UserAgent->new($useragent); and said to use warnings after all the parsing is done, but that doesnt change anything - i still see those pesky lines. :o( I also tried no warnings; and that didnt help either.

      Looking forward to more ideas!

        warnings are lexically scoped so, for that solution to work you'd need to modify JavaFan was spot on (below) - if you're interested, check the Parse::HTTP::UserAgent source code: in sub _numify you'll find the line my $rv = version->new("$v")->numify;.

        Anyway, not being someone who likes to be beaten by zeros and ones, here's a workaround. Before the line:

        my $ua = Parse::HTTP::UserAgent->new($useragent);


        $useragent =~ s{ ( [+] ) [[] [^]]+ []] ( [+] ) }{$1$2}msx;

        I ran a few tests with this and it seems to work fine. The test code and output is below (click on Read more...).

        -- Ken

Re: Need to turn off prints from module
by JavaFan (Canon) on Nov 17, 2010 at 17:56 UTC
    The warning seems to come from It seems Parse::HTTP::UserAgent passes '4.06[en]' to be versionified. But doesn't like that.
Re: Need to turn off prints from module
by biohisham (Priest) on Nov 17, 2010 at 18:29 UTC
    From constantly dealing with messy files, at times the file being parsed maybe the problem, You may wanna check what is there on line 8 in your file handled by <FILE> and see if it is something different from the rest of the lines (It may contain invalid strings hidden somewhere in the line or at the end of the line for example). or check on whether these errors are repeating more often for certain lines over the others and see how that can be fixed/tweaked just in case because the User Agent strings seem to have no standard format to them.

    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
      This is how the "offender" user agent strings look like: Mozilla/4.06+[en]+(WinNT;+I)
Re: Need to turn off prints from module
by thargas (Deacon) on Nov 18, 2010 at 13:27 UTC

    My take on this is that any module which spits out warnings is broken and should be fixed. There is no good way to suppress these warnings external to the module. You can use $SIG{__WARN__} or re-route STDERR or even hack the symbol-table to stuff your own replacement for the method which is causing you problems. They all have their problems which will surface to bite you later.

    IMHO you should look at why you are trying to parse the user-agent string anyway. There is no standard for what might be there, browsers are all regularly impersonating each other, and the end-user can tell them to use some random string as the user-agent. Anything you try for parsing the user-agent is a hack and may or may not work today, but you can pretty much guarantee that it will break someday.

    Please consider if whatever you are trying to accomplish would not be better done in the browser itself by feature-detection, rather than browser-detection. Feature-detection is much more reliable and doesn't depend on figuring out which browser you've got.

Re: Need to turn off prints from module
by Mushka (Acolyte) on Nov 18, 2010 at 17:32 UTC
    Thank you all again!

    Ken, thanks for pointing out that your user agents have spaces instead of plusses. Im grabbing my strings from the server logs, and it looks like the user agent strings are being modified by the script that's producing these logs. Changing plusses to spaces helped greatly, but there are still occasional errors like:
    Use of uninitialized value in split at /usr/local/share/perl/5.8.8/Par +se/HTTP/ line 98, <FILES> line 14.
    Version string '3.5.9-1.fc11' contains invalid data; ignoring: '-1.fc11' at /usr/local/share/perl/5.8.8/Parse/HTTP/ line 210, <FILES> line 181.

    However, making the plusses into spaces allowed the two other modules that i mentioned to make more sense out of the user agent strings that i was passing. And neither of them are giving me the annoying notifications :o) So i will probably end up using one of these as opposed to Parse::HTTP::UserAgent since there seems to be no easy way to shut the module up. And yes, like i said at the beginning, turning off all errors and warnings is not an option for a sensible programmer.

    Thargas, thank you for bringing up a good point that user agent strings are not a science, that im never gonna get a 100% accurate result, and that the accuracy is going to drop as the time goes by. To answer your question, we want to see a statistics of which browsers our clients are using to reach our website so we could make business decisions on which browsers we should start/continue to provide support for. Luckily, our management understands that the report is going to have an approximation of what browsers are being used and they still see a value in such report. If you see a different approach to better achieve this goal i would love to hear your ideas!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://872018]
Approved by Old_Gray_Bear
[stevieb]: I'll see how to get in touch with someone and advise that there's an issue. Thanks for helping me confiirm pryrt!
[pryrt]: also mismatches; but matches their sha1
[pryrt]: The mismatched ones have a Jan 23 2017 Last-Modified header -- I wonder if they rezipped them and forgot to update the sha1

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-03-29 21:06 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.