Category Archives: Perl

Perl Programming

Detect WAP browser and redirect to different url in Perl

Do you want to have the same url for your Web and Wap broswer? The following is the code to do so in Perl:

use CGI;

my $query = new CGI;

my @list=$query->Accept();

my $htmlsite="";
my $wmlsite="";

my $wml=0;
my $html=0;

foreach (@list) {
   $html=1 if /text\\/html/i;
   $wml=1 if  /wap/;

print $query->redirect($wmlsite)  if $wml==1;
print $query->redirect($htmlsite) if $html==1;
print $query->redirect($wmlsite);

How to create a WAP push service in Perl

To push a service (or commonly a URL) to a mobile phone from a Push Initiator (PI), normally a web server, WAP defines a protocol to do so in Push Access Protocol (PAP).

This Push Message can be sent either as Service Indication (SI) or Service Loading (SL) kind of service push.

SI signals an event on which the client should get informed when a message is pushed to the mobile device. The user can base of the text message to decide whether to retrieve it or not. SL push service was specified so that a PI can push a URL to the mobile device which is automatically loaded and executed.

The following covers Perl code to do both SI and SL service. The Perl code is the same, you just need to load different raw file to achieve the different kind of Push Message.


use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");

my $req = HTTP::Request->new(
   POST => '');

$req->content_type('multipart/related; type="application/xml"; '.

open(FILE, "raw") or die "$!";
my $content=;
close FILE;

print "----Header-----\n";
# print $hash->as_string;
print "\n----Body-------\n";
print $req->content;

my $res = $ua->request($req);
if ($res) {
   # Check the outcome of the response
   if ($res->is_success) {
      my $ret= $res->content;
      print "$ret\n";
   } else {
      print "\n".$res->status_line, "\n";
      print $res->content;
      print "\n";
} else {
   print "No pointer assigned\n";

print "\n\n";

Input file for SI push (raw_si):

Content-Type: application/xml

--boundary0123456789 Content-Type: text/ Welcome to Art of Mobile! --boundary0123456789--

Input file for SL push (raw_sl):

Content-Type: application/xml

--boundary0123456789 Content-Type: text/ --boundary0123456789--