Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Need to turn off prints from module

by kcott (Archbishop)
on Nov 17, 2010 at 22:09 UTC ( [id://872064]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Need to turn off prints from module
in thread Need to turn off prints from module

warnings are lexically scoped so, for that solution to work you'd need to modify version.pm. 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);

add

$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...).

Test code:

#!perl use strict; use warnings; use Parse::HTTP::UserAgent; my @useragent_list = ( q{Mozilla/4.06+[en]+(WinNT;+I)}, q{Opera/9.80 (Windows NT 5.1; U; en) Presto/2.6.30 Version/10.61}, q{Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.4) Geck +o/20091016 Firefox/3.5.4}, q{Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525. +28 (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1}, ); for my $useragent (@useragent_list) { print q{UA: }, $useragent, qq{\n}; $useragent =~ s{ ( [+] ) [[] [^]]+ []] ( [+] ) }{$1$2}msx; my $ua = Parse::HTTP::UserAgent->new($useragent); print $ua->name, qq{\n}; print $ua->version, qq{\n}; print $ua->os, qq{\n}; }

Output:

$ ver_overload_prob.pl UA: Mozilla/4.06+[en]+(WinNT;+I) Netscape 4.060 Windows NT UA: Opera/9.80 (Windows NT 5.1; U; en) Presto/2.6.30 Version/10.61 Opera 10.610 Windows XP UA: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.4) Gecko/ +20091016 Firefox/3.5.4 Firefox 3.005004 Windows XP UA: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.28 + (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1 Safari 3.002002 Windows XP

-- Ken

Replies are listed 'Best First'.
Re^4: Need to turn off prints from module
by Mushka (Acolyte) on Nov 17, 2010 at 22:53 UTC
    Thank you Ken! I thought of this workaround too, and it works marvelously for "[en]" issue, but those were not the only user agent strings that the module was notifying me about.

    For example, string
    Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.9.1.15)+Gecko/20101026+Firefox/3.5.15+(.NET+CLR+3.5.30729)
    gives me a warning
    Version string '20101026Firefox.0' contains invalid data; ignoring: '20101026Firefox.0' at /usr/local/share/perl/5.8.8/Parse/HTTP/UserAgent.pm line 210, <FILES> line 26.

    That's why i was asking a more global question how to "shut that module up". :o) I was thinking along the lines how you can say 2>&1 perhaps you could send all output from specified module to /dev/null... I would love to go into the module and tweak it, but i have read-only privileges, and i would try to avoid bothering the person who is responsible for installing these modules for me.

    I was researching other user agent modules, but HTML::ParseBrowser thinks that mostly everything is Netscape (when it's clearly Firefox) and HTTP::BrowserDetect thinks that the only IE browser ppl are using is version 4.0 (and as someone said: No offence to that module's writer, but "Is this IE? Yay! Is this 7? Yay" is a bass-ackwards approach to how to detect useragents.)

      For my tests, I used the problem string you posted then added three more from browsers I had handy on my PC. I notice that the Firefox one you've now posted has "+" characters in every position that mine has spaces (ignoring a few minor variations in version numbers). I took mine directly from the browsers; it occurred to me that may yours are perhaps URL-encoded (while you can represent a space as hex %20, it can also just be converted to a plus-sign).

      So, I added your latest problem string to my existing test list and changed:

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

      to just:

      $useragent =~ y{+}{ };

      And it all worked! :-)

      $ ver_overload_prob.pl UA: Mozilla/4.06+[en]+(WinNT;+I) Netscape 4.060 Windows NT UA: Opera/9.80 (Windows NT 5.1; U; en) Presto/2.6.30 Version/10.61 Opera 10.610 Windows XP UA: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.4) Gecko/ +20091016 Firefox/3.5.4 Firefox 3.005004 Windows XP UA: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.28 + (KHTML, like Gecko) Version/3.2.2 Safari/525.28.1 Safari 3.002002 Windows XP UA: Mozilla/5.0+(Windows;+U;+Windows+NT+5.1;+en-US;+rv:1.9.1.15)+Gecko +/20101026+Firefox/3.5.15+(.NET+CLR+3.5.30729) Firefox 3.005015 Windows XP

      So, give that a go - it might be the solution.

      If not, you can redirect STDERR (see open - example code is about halfway down the page) but you'll lose every error message by doing that and I really wouldn't recommend it.

      In the _numify() routine (referenced in my last post) pluses are actually removed (see final alternation item):

      $v =~ s{ pre | rel | alpha | beta | \-stable | gold | [ab]\d+ | a\-XXXX | \+ }{}xmsig;

      So, if my solution works, you might want to post a bug report with the fix being:

      $v =~ y{+}{ }; $v =~ s{ pre | rel | alpha | beta | \-stable | gold | [ab]\d+ | a\-XXXX }{}xmsig;

      -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://872064]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found