[Israel.pm] Perl on Android in Haifa

Gabor Szabo szabgab at gmail.com
Wed May 5 01:58:11 PDT 2010

On Sun, May 2, 2010 at 12:42 AM, Shmuel Fomberg <semuelf at 012.net.il> wrote:
> Hi Gabor.
> Gabor Szabo wrote:
>> Thanks Shmuel for organizing the meeting and Qualcomm for
>> providing the venue for the meeting and for all the people who
>> came. I hope you got enough input to kick start you on your way
>> to write Perl for Android devices even if you don't yet own one.
> Can you publish the notes that your show was based on?

Now that SawyerX has received commit bit on the ASE repository
some of the examples I used will be included in some improved form.

Anyway, here are the comments I had in raw format.
Some of them just instructions for myself what to show.
I hope it might be useful to someone.



The directory layout of the Andorid device:

Scripts are located in   /sdcard/ase/scripts/

Perl is installed in     /data/data/com.google.ase/perl/

The executable is this:  /data/data/com.google.ase/perl/perl

When the device is mounted as USB I can see the scripts in

/ase/extras/perl/site_perl  holds Android.pm and JSON.pm

/Downloads/perl/    contains  /Downloads/perl/perl   the exe

On the desktop:

Starting the emulator:

$ emulator -avd xx      where xx is the name of the emulator

$ adb devices       lists the connected emulators and devices

                    I get the following result when the emulator is running:
List of devices attached
emulator-5554	device

To upload a file (script.pl) to the emulator:

$ adb push script.pl /sdcard/ase/scripts/

to download from the emulator

$ adb pull /sdcard/ase/scripts/script.pl .


Sample scripts

Scripts usually need the Android API:

use Android;
my $d = Android->new();


Methods of the Android usually (or always?) return a hash with 3 entries:

 error  => which is undef if no error was found
 id     => which is a sequence number indicating the number of api
calls in the current process
 result => some data

$d->makeToast("hello world");

# Showing a popup that disappears after a second or so.
# result is undef


the Android object itself has two keys
     conn => Symbol::GEN, IO::Socket::INET,
     id => 0,


We have perl 5.10.1 on the device but lots of pieces are still missing

- features
- autodie
- File::Spec::Unix  (hence no File::Find)

the current working directory of the perl scripts is  /
I could not manage to upload .pm file or create subdirectories in the
scripts directory

In many cases the "result" is a JSON::PP::Boolean object that
stringifies to "true" or "false"
and that gives you real true and false in boolean context.


say($d->checkRingerSilentMode->{result});                   # true
say($d->checkRingerSilentMode->{result} ? 'ON' : 'OFF');    # ON

say($d->checkRingerSilentMode->{result});                   # false
say($d->checkRingerSilentMode->{result} ? 'ON' : 'OFF');    # OFF

See: scripts/ringer_silent_mode.pl

Creating an alert dialog with 3 buttons:

$d->dialogCreateAlert("the title", "text");

say("Still running");


# This call waits for someone to press a button
# the result is a hash with one pair. The key is "which".
# The value is one of the following:
# "neutral", "negative", "positive"


To get user input use the getInput method

$d->getInput("title", "text");

The result will contain the string the user typed in


Progress bar:

use Android;
my $d = Android->new();

my $max = 32;

   $max,    # maximum progress (shows a number 0/$max)
   0,       # boolean, cancellable (Bug: I don't see any difference if
I set it to 1)
my $n = 0;
while ($n < $max) {
	sleep 1;
	$n += int rand 7;
	$n = $n > $max ? $max : $n;


# also:
# $d->dialogCreateSpinnerProgress(


use Android;
my $d = Android->new();

$d->speak("hello Android from Perl");

# ... but it does not speak for me :-(


Single choice and Multi choice:

$d->dialogSetSingleChoiceItems([qw(Foo Bar Baz)]);
say("waiting for input");
$d->dialogGetResponse; # just gives the type of the button clicked
# the 'result' field is set to an ARRAY ref with the index of the selected item

# $d->dialogSetSingleChoiceItems([qw(Foo Bar Baz)], 2);   # defaults to "Baz"

# $d->dialogSetMultiChoiceItems([qw(Foo Bar Baz)], [1]);    # setting
the defaults here needs an arrayref
# $d->dialogSetMultiChoiceItems([qw(Foo Bar Baz)], [0, 2]);

# I am not sure what is this for:
# $d->dialogSetItems([qw(A B C)]);






Download and install
 - the Android SDK from http://developer.android.com/sdk/index.html
 - Java JDK
 - Apache Ant

Setup the PATH to make sure this java will be the first in the PATH.

Installing the Emulator
Setting up an environment
   ~/android-sdk-linux_86/tools/android create project --target 1
--name and --path /home/gabor/work/and/ --package and.perl.com
--activity And

Installing ASE and the Perl
Setup SDK, try to compile perl
   Check out the source code from
   http://code.google.com/p/android-scripting/source/checkout  to
   Add   android-scripting/tools/agcc to the PATH

Show how to package a Java based application?

Download the source of Android from http://source.android.com/download
and build it using "make".

More information about the Perl mailing list