mattpettis has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Can anyone tell me where I can find code that translates the useragent field of weblogs into simpler, meaningful interpretations? Is there a general parser that turns that string into a browser, version, and O/S in general? Thanks, Matt
  • Comment on Translating the the useragent field of weblogs

Replies are listed 'Best First'.
•Re: Translating the the useragent field of weblogs
by merlyn (Sage) on Dec 23, 2002 at 16:27 UTC
Re: Translating the the useragent field of weblogs
by Cody Pendant (Prior) on Dec 23, 2002 at 21:28 UTC
    This is very ugly, (experienced monks please avert your eyes) and you should of course use the module, but I found it useful as a quick-and-dirty browser parser.

    Anything not detected by this just comes back as the original user-agent string.

    sub munge_browser { $string = shift () || die "$!"; chomp($string); $string =~ s/^\s*//; ($first, $second, $third) = split ('\(|\)', $string); ($x, $version) = split ('/| ', $first); $version =~ s/[^\d.]//g; if ($first =~ /mozilla/i && $second !~ /compatible/i) { $browser = 'Netscape'; } if ($browser == 'Netscape' && $version =~ /5/) { $version = 6; } if ($second =~ /MSIE/i) { $browser = 'Explorer'; } if ($third =~ /opera ([\d.]+)/i) { $browser = 'Opera'; $version = $1; } if ($second =~ /icab ([\d.]+)/i) { $browser = 'iCab'; $version = $1; } if ($second =~ /MSIE\s([\d.]+)/i) { $version = $1; } if ($second =~ /win/i) { $system = 'PC'; } if ($second =~ /mac/i) { $system = 'Mac'; } if ($browser) { return "$browser $version ($system)"; } else { return "$string"; } }

    --
    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;