Friday, March 12, 2010

Use of caller in Perl

Caller or Caller Expr
Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or eval or require, and the undefined value otherwise. In list context, returns
#      0            1            2
($package, $filename, $line ) = caller;


With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to go back before the current one
# 0                       1       2           3             4
($package, $filename, $line, $subroutine, $hasargs,

 #    5                6              7             8           9           10
$wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
= caller($i);

(caller 1)[3] will give the name of the calling subroutine.
($package, $filename, $line ) = caller; or

($package, $filename, $line ) = caller 1; # Both of these are equivalent

@caller = caller 0; #will return whole information about the calling subroutine

Creating Copy of File handles in Perl/Redirecting STDOUT to a file and screen

To Create an independent copy of file descriptor for the file handle: Use open with & mode
open(OLDOUT, ">&STDOUT" ) or die "Couldn't dup STDOUT: $!";
open(OLDIN, "<&STDIN" ) or die "Couldn't dup STDIN : $!";

Use open with the &= mode to create an alias for that filehandle's file descriptor:
open(OUTALIAS, ">&=STDOUT") or die "Couldn't alias STDOUT: $!";
open(INALIAS, "<&=STDIN") or die "Couldn't alias STDIN : $!";
open(BYNUMBER, ">&=5") or die "Couldn't alias file descriptor 5: $!";

If we try to create a copy of any file handle descriptor then actually we are calling dup (2) system call, by this we get two independent file descriptors whose file position, locks, and flags are shared, but which have independent stdio buffers. We can close any of file descriptor, which will not affect the other one.

But If we try to create a alias of any file handle then actually we are calling fdopen (3) stdio function. We get a single file descriptor with two stdio buffers accessed through two filehandles. Closing one filehandle closes the file descriptor of any aliases, but not their filehandles

# take copies of the file descriptors
open(OLDOUT, ">&STDOUT");
open(OLDERR, ">&STDERR");

# redirect stdout and stderr
open(STDOUT, "> /tmp/program.out")  or die "Can't redirect stdout: $!";
open(STDERR, ">&STDOUT")            or die "Can't dup stdout: $!";

# run the program
system($joe_random_program);

# close the redirected filehandles
close(STDOUT)                       or die "Can't close STDOUT: $!";
close(STDERR)                       or die "Can't close STDERR: $!";

# restore stdout and stderr
open(STDERR, ">&OLDERR")            or die "Can't restore stderr: $!";
open(STDOUT, ">&OLDOUT")            or die "Can't restore stdout: $!";

# avoid leaks by closing the independent copies
close(OLDOUT)                       or die "Can't close OLDOUT: $!";
close(OLDERR)                       or die "Can't close OLDERR: $!";

Tuesday, March 2, 2010

How to kill a process on Android device

To find the list of processes running on device just issue the command "adb shell ps", which will give list of all running processes and their PID's.

Issue "adb shell kill <PID>" where PID is the process ID to kill the process.

Running android application from command line using "am start ..." command

If you are a programmer you would like to run your application on android device form command line instead of clicking on the icon. So here is the solution for invoking your applications.

Use "am" command on command prompt. You can go to device's shell terminal by issuing "adb shell" command or you can club the am command with adb shell as "adb shell am start...". Following is the syntax for "am command".


> adb shell am
usage: am [start|instrument]
       am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                [-n <COMPONENT>] [-D] [<URI>]
       am instrument [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>]
                [-w] <COMPONENT>
For example, to start the Contacts application you can use
> adb shell am start -n com.google.android.contacts/.ContactsActivity

Refer to Instrumentation testing on android open source site