Friday, 27 June 2014

How to write PERL module in simple and easy way?

Link :

Below is my code -
 Package name -


require Foo;

Foo::bar( "a" );
Foo::blat( "b" );
yatendra@yatendra:~/Research$ cat

package Foo;
sub bar {
  print "Hello $_[0]\n"

sub blat {
  print "World $_[0]\n"
File name -

require Foo;

Foo::bar( "a" );
Foo::blat( "b" );

Create the Perl Module Tree
  h2xs -AX -n Foo

What are Packages?

  • A package is a collection of code which lives in its own namespace
  • A namespace is a named collection of unique variable names (also called a symbol table).
  • Namespaces prevent variable name collisions between packages
  • Packages enable the construction of modules which, when used, won't clobbber variables and functions outside of the modules's own namespace

The Package Statement

  • package statement switches the current naming context to a specified namespace (symbol table)
  • If the named package does not exists, a new namespace is first created.

$i = 1; print "$i\n"; # Prints "1"
package foo;
$i = 2; print "$i\n"; # Prints "2"
package main;
print "$i\n"; # Prints "1"
  • The package stays in effect until either another package statement is invoked, or until the end of the end of the current block or file.
  • You can explicitly refer to variables within a package using the :: package qualifier


For Example:
$i = 1; print "$i\n"; # Prints "1"
package foo;
$i = 2; print "$i\n"; # Prints "2"
package main;
print "$i\n"; # Prints "1"

print "$foo::i\n"; # Prints "2"

BEGIN and END Blocks

You may define any number of code blocks named BEGIN and END which act as constructors and destructors respectively.
BEGIN { ... }
END { ... }
BEGIN { ... }
END { ... }
  • Every BEGIN block is executed after the perl script is loaded and compiled but before any other statement is executed
  • Every END block is executed just before the perl interpreter exits.
  • The BEGIN and END blocks are particularly useful when creating Perl modules.

What are Perl Modules?

A Perl module is a reusable package defined in a library file whose name is the same as the name of the package (with a .pm on the end).
A Perl module file called "" might contain statements like this.

package Foo;
sub bar { 
   print "Hello $_[0]\n" 

sub blat { 
   print "World $_[0]\n" 
Few noteable points about modules
  • The functions require and use will load a module.
  • Both use the list of search paths in @INC to find the module (you may modify it!)
  • Both call the eval function to process the code
  • The 1; at the bottom causes eval to evaluate to TRUE (and thus not fail)

The Require Function

A module can be loaded by calling the require function

require Foo;

Foo::bar( "a" );
Foo::blat( "b" );
Notice above that the subroutine names must be fully qualified (because they are isolated in their own package)
It would be nice to enable the functions bar and blat to be imported into our own namespace so we wouldn't have to use the Foo:: qualifier.

The Use Function

A module can be loaded by calling the use function

use Foo;

bar( "a" );
blat( "b" );
Notice that we didn't have to fully qualify the package's function names?
The use function will export a list of symbols from a module given a few added statements inside a module
require Exporter;
@ISA = qw(Exporter);
Then, provide a list of symbols (scalars, lists, hashes, subroutines, etc) by filling the list variable named @EXPORT: For Example
package Module;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(bar blat);

sub bar { print "Hello $_[0]\n" }
sub blat { print "World $_[0]\n" }
sub splat { print "Not $_[0]\n" }  # Not exported!


Create the Perl Module Tree

When you are ready to ship your PERL module then there is standard way of creating a Perl Module Tree. This is done using h2xs utility. This utility comes alongwith PERL. Here is the syntax to use h2xs
$h2xs -AX -n  Module Name

# For example, if your module is available in file
$h2xs -AX -n Person

This will produce following result
Writing Person/lib/
Writing Person/Makefile.PL
Writing Person/README
Writing Person/t/Person.t
Writing Person/Changes
Writing Person/MANIFEST
Here is the descritpion of these options
  • -A omits the Autoloader code (best used by modules that define a large number of infrequently used subroutines)
  • -X omits XS elements (eXternal Subroutine, where eXternal means external to Perl, i.e. C)
  • -n specifies the name of the module
So above command creates the following structure inside Person directory. Actual result is shown above.
  • Changes
  • Makefile.PL
  • MANIFEST (contains the list of all files in the package)
  • t/ (test files)
  • lib/ ( Actual source code goes here
So finally you tar this directory structure into a file Person.tar and you can ship it. You would have to update README file with the proper instructions. You can provide some test examples files in t directory.

Installing Perl Module

Installing a Perl Module is very easy. Use the following sequence to install any Perl Module.
perl Makefile.PL
make install

Simple steps to automate Android Application test cases using UIAutomator

  • Can be used on device displays with different resolution
  • Events can be linked with Android UI controls. For example, click on a button with text that says “Ok,” instead of clicking a coordinate location (x=450, y=550).
  • Can reproduce a complex sequence of user actions
  • Always performs the same sequence of actions, allowing us to collect performance metrics on different devices.
  • Can run several times and on different devices without changing any Java* code
  • Can use hardware buttons on devices
  • Hard to use with OpenGL* and HTML5 applications because these apps have no Android UI components.
  • Time consuming to write JavaScript*

Ui Automator is an Android Test Framework used for functional Testing, black box Testing. This is an easy to understand and execute. It’s open source. UI Automator identifies the ui element using their Text, Decription, Index, Class Names etc. UI Automator can be used to write Test Cases for any android App either default or the third party apps. This Doesn’t require the Tester to change the signature of the Apps thus can be used for the native apps like Settings.apk
UI Automator is also a JUnit Based Test Suite. So we can use the Assert Class to get the specific test case pass/fail Reason.
The biggest limitation of UI Automator framework is that it only works on API level16 or higher (Android 4.1 or later).

      Set Up for the UI Automator:
1) Android application apk file for Testing. Ex: ApplicationToTest.apk
2) Eclipse for building Test project
3) ADT (Android Development Tools)
4) SDK (Software Development Kit)
If you are using Eclipse for just your android Development then download eclipse from
This includes SDK‘s for android also.
5) JDK (Java Development Kit)
6) Go to the location where your SDK is present Check for android.jar and uiautomator.jar. It should be present under the sdk>Platform> (any Folder with) API level 16 or higher.

Creating the Test Project:

1. Create a new Java project in Eclipse, and give your project a name that is relevant to the tests you’re about to create (for example, "MyAppNameTests"). In the project, you will create the test cases that are specific to the application that you want to test.
2. From the Project Explorer, right-click on the new project that you created, then select Properties > Java Build Path, and do the following:
Click Add Library > JUnit then select JUnit3 to add JUnit support.
Click Add External JARs... and navigate to the SDK directory. Under the platforms directory, select the latest SDK version and add both the uiautomator.jar and android.jar files.
  1. Goto the project create a Package name as per standards and create a class file .
  2. Create New android application project.
And copy paste the below code:
package com.yatendra;
public class writeMsg extends UiAutomatorTestCase{
public void testDemo2() throws UiObjectNotFoundException {
// this create a uiDevice instance an perss on the Home button
//this will create a instance of the “Apps” on the home screen
UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
//this will click on the apps and wait for the new window to open
//this will create a ui object of the app tab using the String “Apps”
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
//This will click on the tab;
//this will create a scroller object which we can use to perform some actions
UiScrollable appViews = new UiScrollable(
new UiSelector().scrollable(true));
//now it scrolls horizontal
//now find the app you want to click using the Text i.e “Messaging” and create a instance of it

UiObject msgApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()),
//now Click on the object found using name
//Now it will opne a new activity identify it using its package name
UiObject msgUI = new UiObject(new UiSelector()
UiObject ui = msgUI.getChild(new UiSelector().className("android.widget.TextView").descriptionContains("New message"));
//check if the window is present or now or give a assert
assertTrue("Unable to find the message Ui ",ui.exists());
if (ui.exists()!= true){
//type the send to number
UiObject sendTo = new UiObject(new UiSelector().className("android.widget.MultiAutoCompleteTextView"));

//here I am handling a pop that sometimes appear
UiObject addToPop = new UiObject(new UiSelector().className("android.widget.EditText").textContains("Add to People"));
if (addToPop.exists()== true){
//find the message text body ,write the message .
UiObject sendTomsg = new UiObject(new UiSelector().className("android.widget.EditText"));
sendTomsg.setText("Hello this i to test Meassaging apk");

Executing and Creating the Test automation JAR:
  1. Using Terminal enter the location where your automation project is present.

Path/yatendra$ android create uitest-project -n yatendra -t android-17 -p /Path/yatendra

  • Here the android-17 is the folder under sdk/platform where the uiautomator and android JARs are present
    • 1st yatendra is my project name
    • This will create the build file .xml
Path/yatendra$ ant build
    • This will create the Jar file .
    • Make sure the ant path is also set properly.
    • If ant is not install : sudo apt-get install ant1.8
    • Or sudo apt-get update or sudo apt-get install ant
    • if ant build showing error like below - use update-alternative --config Java
      2. /home/yatendra/adt-bundle-linux-x86_64-20131030/sdk/tools/ant/uibuild.xml:183: java.lang.UnsupportedClassVersionError: com/sun/tools/javac/Main : Unsupported major.minor version 51.0
    • If ant is showing error like below – use sudo apt-get install ant-optional
Or Check the sdk path and whether u have installed for 32 bit or 64 bit.

      1. Buildfile: build.xml
      3. /home/yatendra/Workspace_android/testAPK/build.xml:90: The following error occurred while executing this line:
      4. /home/yatendra/adt-bundle-linux-x86_64-20131030/sdk/tools/ant/uibuild.xml:105: No supported regular expression matcher found: java.lang.ClassNotFoundException:
      5. Total time: 0 seconds

  1. Now we need to push the jar file created using above command to the device under test.
Path/yatendra$ adb push bin/yatendra.jar /data/local/tmp
  • We are pushing yatendra.jar to /data/local/tmp

  1. Now to run the particular test case we need to execute one more command :
Path/yatendra$ adb shell uiautomator runtest yatendra.jar -c com.yatendra.writeMsg
  • Here com.yatendra.writeMsg is the particular testcase we are running.
  • Com.staish is package name and write_Msg is class name