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.