Save the program into your cgi-bin directory (e.g. as redir.pl), then set your server to use it as the default index page. For Apache, the directive is:
DirectoryIndex /cgi-bin/redir.pl
Place an 'index.html' and 'index.wml' file in the document root directory, and you will then be able to use the same URL (http://yoursite) for both HTML and WML (WAP) clients.
#! /usr/bin/perl print"Location: index.".(($ENV{HTTP_ACCEPT}=~/html/g)[0]||'wml')."\n\n +";
Update:
Don't you just love browser compatibility issues? The Motorola browser doesn't like the Location: header, and MSIE doesn't list text/html in its HTTP_ACCEPT.

Amended code is shown below:

#! /usr/bin/perl $ENV{HTTP_ACCEPT}=~/vnd\.wap\.(wml)/; open FILE,"$ENV{DOCUMENT_ROOT}$ENV{REQUEST_URI}index.${\($1||'html')}" + or die($!); print "Content-type: text/${\($&||'html')}\n\n"; print while (<FILE>);
Update (12-Oct-2001) How this works:
All WAP clients (which I have tried) will send the string text/vnd.wap.wml as part of the HTTP_ACCEPT header, however not all HTML clients send text/html (MSIE...!). Therefore this provides quite an accurate way to determine if your site is being accessed via a WAP phone.

A notable exception to this is Opera, which sends the WML content type if the URL is in the form http://wap.site.tld, but not for URLs like http://www.site.tld or http://site.tld, so if you are using this code then make sure your host name does not start http://wap. or Opera users will not be able to view the HTML version of your site.

I have used the above method at http://jonallen.info, and it has been tested on the following platforms:

WAP phones:

Web browsers: