Art of Mobile
Home GeneralHardwareJavaMMSPerlPHPSMSWAP


There is a CPAN library that helps to parse the User-Agent. You can download the Perl library at:

http://search.cpan.org/~cmanley/Mobile-UserAgent-1.05/lib/Mobile/UserAgent.pm

After you download the library, perform the usual installation procedures:

perl Makefile.PL
make
make install

The following is the source code:

#!/usr/local/bin/perl
use strict;

use Mobile::UserAgent;
my $uaobj = new Mobile::UserAgent();

The codes above is the standard declaration so that you can call “Mobile::UserAgent” library. The following 3 lines are used for testing purposes. Uncomment the following 3 lines and comment the above 1 line to do some testing.

#my $useragent = 'Nokia6600/1.0 (4.09.1) SymbianOS/7.0s Series60/2.0 '.
#   'Profile/MIDP-2.0 Configuration/CLDC-1.0';
#my $uaobj = new Mobile::UserAgent($useragent);

The following lines check the User-Agent string and pass the output to the subroutine “printWml” if the device suppose WML or the subroutine “printHtml” if the device doesn’t.

if ($uaobj->success()) {
   my $out='Vendor: ' . $uaobj->vendor()     . "<br />\\n";
   $out.='Model:    ' . $uaobj->model()      . "<br />\\n";
   $out.='Version:  ' . $uaobj->version()    . "<br />\\n";
   $out.='Series60: ' . $uaobj->isSeries60() . "<br />\\n";
   &printWml($out);
} else {
   &printHtml("Connecting Client is not a mobile user-agent \\n");
}


The subroutine “printWml” is used to print the standard WML content on the device.

sub printWml {
   my $string= shift;
   print "Content-Type: text/vnd.wap.wml\\r\\n\\r\\n";
print <<EOFWML;
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
   "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
  <card id="init" newcontext="true" title="Art Of Mobile">
    <p align="left">
      <small>
        $string
      </small>
    </p>
    <p align="left">
      Test script by Art of Mobile<br/>
    </p>
  </card>
</wml>
EOFWML
}

The subroutine “printHtml” is used to print the standard HTML content on the browser.

sub printHtml{
   my $string= shift;
   print "Content-Type: text/html\\r\\n\\r\\n";
print <<EOFHTML;
<html>
<header>
  <title>Art of Mobile</title>
</header>
<body>
<p>
$string
</p>
</body>
</html>
EOFHTML
}

You can download the full source code at mobileUserAgent.pl.

You can test the example by pointing your mobile phone’s WAP broswer to this url:
http://www.artofmobile.com/cgi-bin/mobile.pl

Comments are closed.

Powered by WordPress. Theme created by Art Of Mobile