During compatibilty testing of a new site I'm working on, I needed a way to detect browsers and act accordingly. This short and sweet script does just that, and removes redundancy. (I'm also teaching myself hash and array references)
my @user_agent_list=
({title=>'Internet Explorer',
string=>'MSIE',
url=>'http://www.microsoft.com/windows/ie/defa
+ult.asp'},
{title=>'Konqueror',
string=>'Konqueror',
url=>'http://www.konqueror.org/'},
{title=>'Galeon',
string=>'Galeon',
url=>'http://galeon.sourceforge.net/'},
{title=>'Opera',
string=>'Opera',
url=>'http://www.opera.com/'},
{title=>'Dillo',
string=>'Dillo',
url=>'http://dillo.cipsga.org.br/'},
{title=>'Mozilla',
string=>'Mozilla\/5',
url=>'http://www.mozilla.org/'},
{title=>'Netscape',
string=>'Mozilla\/4',
url=>'http://wp.netscape.com/download/'},
);
print div({-id=>'browser'}, p("Browser: $ENV{'HTTP_USER_AGENT'}"),);
for my $browser (@user_agent_list) {
if (($ENV{'HTTP_USER_AGENT'}) =~ /$browser->{string}/) {
print p("You seem to be using",
a({-href=>"$browser->{url}"},
$browser->{title}, ));
}
}
Update
I just found a slight flaw in this design. Microsoft Internet Explorer on Win32 platforms displays a $ua->agent string of:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
..and that also matches what Netscape displays. Running this against those two browsers will print out two lines for each of them (IE matches Netscape and IE, and vice-versa for Netscape).
I'll try to find a way around this without having to use !~ matching.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.