Burak Gürsoy

NAME

Parse::HTTP::UserAgent - Parser for the User Agent string

SYNOPSIS

   use Parse::HTTP::UserAgent;
   my $ua = Parse::HTTP::UserAgent->new( $str );
   die "Unable to parse!" if $ua->unknown;
   print $ua->name;
   print $ua->version;
   print $ua->os;
   # or just dump for debugging:
   print $ua->dumper;

DESCRIPTION

This document describes version 0.39 of Parse::HTTP::UserAgent released on 2 December 2013.

Quoting http://www.webaim.org/blog/user-agent-string-history/:

   " ... and then Google built Chrome, and Chrome used Webkit, and it was like
   Safari, and wanted pages built for Safari, and so pretended to be Safari.
   And thus Chrome used WebKit, and pretended to be Safari, and WebKit pretended
   to be KHTML, and KHTML pretended to be Gecko, and all browsers pretended to
   be Mozilla, (...) , and the user agent string was a complete mess, and near
   useless, and everyone pretended to be everyone else, and confusion
   abounded."

User agent strings are a complete mess since there is no standard format for them. They can be in various formats and can include more or less information depending on the vendor's (or the user's) choice. Also, it is not dependable since it is some arbitrary identification string. Any user agent can fake another. So, why deal with such a useless mess? You may want to see the choice of your visitors and can get some reliable data (even if some are fake) and generate some nice charts out of them or just want to send an HttpOnly cookie if the user agent seems to support it (and send a normal one if this is not the case). However, browser sniffing for client-side coding is considered a bad habit.

This module implements a rules-based parser and tries to identify MSIE, FireFox, Opera, Safari & Chrome first. It then tries to identify Mozilla, Netscape, Robots and the rest will be tried with a generic parser. There is also a structure dumper, useful for debugging.

METHODS

new STRING [, OPTIONS ]

Constructor. Takes the user agent string as the first parameter and returns an object based on the parsed structure.

The optional OPTIONS parameter (must be a hashref) can be used to pass several parameters:

  • extended: controls if the extended probe will be used or not. Default is true. Set this to false to disable:

       $ua = Parse::HTTP::UserAgent->new( $str, { extended => 0 } );

    Can be used to speed up the parser by disabling detection of non-major browsers, robots and most mobile agents.

trim STRING

Trims the string.

as_hash

Returns a hash representation of the parsed structure.

dumper

See Parse::HTTP::UserAgent::Base::Dumper.

accessors

See Parse::HTTP::UserAgent::Base::Accessors for the available accessors you can use on the parsed object.

OVERLOADED INTERFACE

The object returned, overloads stringification (name) and numification (version) operators. So that you can write this:

    print 42 if $ua eq 'Opera' && $ua >= 9;

instead of this

    print 42 if $ua->name eq 'Opera' && $ua->version >= 9;

ERROR HANDLING

  • If you pass a false value to the constructor, it'll croak.

  • If you pass a non-hashref option to the constructor, it'll croak.

  • If you pass a wrong parameter to the dumper, it'll croak.

SEE ALSO

Similar Functionality

Resources

Module Reviews

AUTHOR

Burak Gursoy <burak@cpan.org>.

COPYRIGHT

Copyright 2009 - 2013 Burak Gursoy. All rights reserved.

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.16.2 or, at your option, any later version of Perl 5 you may have available.