Feb 26, 2014

Django Tutorial - Part I

Reference: 
Django Project / Django Book

Django web framework / Python web development with Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
The tage line for Django is "The web framework for perfectionists with deadlines"
Django makes it easier to build Web apps more quickly and with less code, mainly used for web development


Main components of The Django framework


Object-relational mapper
Define your data models entirely in Python. You get a rich, dynamic database-access API for free, but you can still write SQL if required.
If your web application is data extensive, better write your SQL instead of object-relational mapper

Automatic admin interface
It comes with Admin interface, you need to enable it in the web framework
There by saving extra effort in creating the admin interface from the scratch

Elegant URL design
Very Flexible URL design using loose coupling
URL definitions and the view functions they call are loosely coupled; that is, the decision of what the URL should be for a given function, and the implementation of the function itself, reside in two separate places. This lets you switch out one piece without affecting the other.
Map the URLs and view methods, if you want to change the URL pattern later, you can do it very easily by changing urls.py
It makes use of regular expression for mapping the URL patterns
It generates dynamic URLs as well

Template system
Use Django's powerful, extensible and designer-friendly template language to separate design, content and Python code.

Cache system
Hook into memcached or other cache frameworks for super performance — caching is as granular as you need.

Internationalization
Django has full support for multi-language applications, letting you specify translation strings and providing hooks for language-specific functionality.


How to Install Django

Download Django (https://www.djangoproject.com/download/) and unzip

django-admin.py should be on your system path if you installed Django via its setup.py utility.
on Windows, you need to update your PATH environment variable.

python setup.py install
python -c 'import sys, pprint; pprint.pprint(sys.path)' #this locate site-packages direcotry

How to check Django Installed

>>> import django
>>> django.VERSION
(1, 4, 2, 'final', 0)

How to create a project

django-admin.py startproject <proj_name>
E.g., django-admin.py startproject mysite

Django Project Structure

The startproject command creates a directory containing five files:
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

How to access Django from shell

python manage.py shell

Django Development Server

Django comes with its own development server

Difference between Django Development Server Vs Web Development Server like Apache is as mentioned below :

Advantage:
Comes as part of Django itself
You can develop your site rapidly without having to deal with configuring your production server (e.g., Apache) until you are ready for production
Django Development server watches your code and automatically reloads it. making it easy for you to change code without restarting server

Disadvantage:
Not suitable for production environment
You have to deploy your code to Apache when moving to production

Django Development Server suitable in development environment
Apache web development server suitable in production environment

How to start Django Development Server

Django develeopment server works on default port 8000
python manage.py runserver 8000

How to test home page on Web Page

Using Django Development Server
http://127.0.0.1:8000/

Using Apache Server (If you configure Apache)
http://127.0.0.1/mysite/

How to configure Apache with Django

In Apache httpd.conf
WSGScriptAlias /mysite "C:/django_workspace/mysite/mysite/wsgi.py"


Thanks for reading this article. I will discuss more examples in the coming sessions, stay tuned :-)


Feb 24, 2014

UIAutomator Tutorial - Part II

In continuation to earlier post on UIAutomator Introduction / UIAutomator Basics lets discuss more UIAutomator Android with few examples

Reference: UIAutomator Documentation / Android Developer Docs

UIAutomator API in brief

It provides six different classes, these classes with interfaces and exceptions allows to capture and manipulate UI components on Android Device.

Following are the classes :
   UiDevice
   UiObject
   UiScrollable
   UiSelector
   UiCollection
   UiConfigurator

UiDevice
  Provides access to state information about the device. We can  also use this class to simulate user actions on the device, such  as pressing the d-pad hardware button or pressing the Home and Menu buttons.
UiObject
 Represents a user interface (UI) element.
UiScrollable
 Provides support for searching for items in a scrollable UI .
UiSelector
 Represents a query for one or more target UI elements on a device screen.
UiCollection
  Used to enumerate  screen elements for the purpose of counting, or targeting a sub elements by a child's text or description.
UiConfigurator
  Allows  to set key parameters for running uiautomator tests

We need to import following classes
import android.widget.LinearLayout;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.android.uiautomator.core.UiDevice;

UiObject/UISelector

Click on Text
new UiObject(new UiSelector().text(text)).click();

Click on Button
We need to use Button Class name for this
new UiObject(new UiSelector().text(btnText).className("android.widget.Button")).click();

Long Click
new UiObject(new UiSelector().text(text)).longClick();

Press Back
getUiDevice().pressBack();

Go Back to Home
getUiDevice().pressHome();

Click and Wait
new UiObject(new UiSelector().description(text)).clickAndWaitForNewWindow();

Check if text exists
new UiObject(new UiSelector().text(value)).exists();

Wait until 
new UiObject(new UiSelector().className(android.widget.ProgressBar.class.getName())).waitUntilGone(1000);


UIDevice

Example : Swipe Down Notification

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObjectNotFoundException;

public static void swipeDownNotificationBar() throws UiObjectNotFoundException {
UiDevice deviceInstance = UiDevice.getInstance();
int dHeight = deviceInstance.getDisplayHeight();
int dWidth = deviceInstance.getDisplayWidth();
int xScrollPosition = dWidth / 2;
int yScrollStop = dHeight / 2;
UiDevice.getInstance().swipe(xScrollPosition, 0, xScrollPosition, yScrollStop, 100);
}

UiScrollable

Example: 
1) Scroll and Click event

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiScrollable;

new UiScrollable(new UiSelector().scrollable(false))
.scrollIntoView(new UiSelector().text(text));
new UiObject(new UiSelector().text(text)).click();

2) Scroll Back
new UiScrollable(new UiSelector().scrollable(true)).scrollBackward();

How to create a project in Eclipse

Create a Java project in Eclipse
After Creating Java Project, add external jar files which are android.jar and uiautomator.jar
android.and uiautomator.jar are present in
“D:\adt-bundle-windows-x86\adt-bundle-windows\sdk\platforms\android-4.2”

Create Java File

Create Test.java in project / eclipse work space, say it is in D:\UIAutomator

public void clickText(String text) throws UiObjectNotFoundException {
    new UiObject(new UiSelector().text(text)).click();
}

How to build UIAutomator project

You need Apache Ant software for building Java project

Syntax:
android create uitest-project -n "project-name" -t 1 -p "project-location"

"-t" referes to Android target (in case of different versions of Android are present, you can define which target to build this JAR)

E.g.,
cd D:\UIAutomator
D:
android create uitest-project -n UIAutomatorTest -t 1 -p D:\UIAutomator
ant build
cd bin
adb push UIAutomatorTest.jar /data/local/tmp
cd ..

The above commands create a JAR file in the bin folder
Then push the JAR file to /data/local/tmp folder of Android phone

How to call UIAutomator methods from Adb Shell

Go to command line and execute the below command

adb shell uiautomator runtest UIAutomatorTest.jar -c com.test.uiautomator.Test#clickText -e text "Phone"

Here Test refers to Test.java class
ClickText refers to the method inside Test.java
-e <Name> <Value> pairs for passing to test classes

If you haven't go through earlier post, Please go through


Feb 22, 2014

UIAutomator Tutorial - Part I


Reference: http://developer.android.com/tools/help/uiautomator


What is UIAutomator / UIAutomator Introduction


  • UIAutomator  is Introduced  by Google and it is available from Jellybean Versions of Android.
  • The UIAutomator is a testing framework provided by Google's Android 

Use of UIAutomator


  • To test Android Applications efficiently by creating automated functional UI test cases that can be run against applications on one or more devices.
  • Works on all the Android applications, including Google’s installed apps such as Settings, Contacts, Phone, etc.

Skills Required for working on UIAutomator


Core Java
Software: ANT, Android SDK, Eclipse

How UIAutomator works

  • Java project is created using android.jar and uiautomator.jar. 
  • uiautomator.jar provided by Android,  has in built methods which are used to perform UI actions  on Android device.
  • Create the Android test project based on your requirement; build a Jar file for the Java project is created and push the JAR file to /data/local/tmp location of Android device. 
  • Using adb shell, one can access the methods inside the Jar

How to build UIAutomator Project


cd D:\UIAutomator
D:
android create uitest-project -n UIAutomatorTest -t 1 -p D:\UIAutomator
ant build
cd bin
adb push UIAutomatorTest.jar /data/local/tmp


The above commands create a JAR file in the bin folder
Then push the JAR file to /data/local/tmp folder of Android phone


How to invoke a method in the JAR file

   adb shell uiautomator runtest UIAutomatorTest.jar -c com.test.uiautomator.Bluetooth#btPair


Debug UIAutomator


  • Using android DDMS tool (present in Eclipse), we can analyze properties of Android screen (Activity)
  • Using android DDMS tool, we can get the screenshot of the android screen and access the layout of the android screen, there implement the logic of what action we want to achieve

Advantages / Pros


  • Free ware, developed by Google, trust-able
  • Simple, uses class object OOPS methodology, less time to implement
  • Handling asynchronous events like Toasts,dialog and alerts occurring  during tests is easy. (JAR approach)

Disadvantages / Cons


  • Works only from Android version 4.1. 



UIAutomator API


  • It provides six different classes, these classes with interfaces and exceptions allows to capture and manipulate UI components on Android Device.

Following are the classes : 
   UiDevice 
   UiObject 
   UiScrollable 
   UiSelector 
   UiCollection 
   UiConfigurator 

UiDevice 

  Provides access to state information about the device. We can  also use this class to simulate user actions on the device, such  as pressing the d-pad hardware button or pressing the Home and Menu buttons. 

UiObject 

 Represents a user interface (UI) element. 

UiScrollable 

 Provides support for searching for items in a scrollable UI . 

UiSelector 

 Represents a query for one or more target UI elements on a device screen. 

UiCollection 

  Used to enumerate  screen elements for the purpose of counting, or targeting a sub elements by a child's text or description. 

UiConfigurator 

  Allows  to set key parameters for running uiautomator tests  



Thanks for reading. I will discuss more samples/examples on UIAutomator in detail in the next post

Feb 20, 2014

python serial

The following example illustrates python serial communication / python read serial port 

What you need to have
Connect the device you want to read the SERIAL logs to the system
To know the port is working, cross check by opening the port and check the logs in TeraTerm
If TeraTerm already using the port, you cannot read the logs from SERIAL port, so make sure before executing the script, stop the TeraTerm

What the script will do
We use python module 'serial'
Create a serial object by passing port, baudrate, bytesize, parity etc.,
To continuously read the logs, use Python Thread concept

from serial import *
from threading import Thread

text_received = ''

def receiveSerialDataFromPort(ser):
    global text_received
    read_buffer = ''

    while True:
        read_buffer += ser.read(ser.inWaiting())
        if '\n' in read_buffer:
            text_received, read_buffer = read_buffer.split('\n')[-2:]
            print text_received

if __name__ ==  '__main__':
    ser = Serial(
        port='COM23',
        baudrate=230400,
        bytesize=EIGHTBITS,
        parity=PARITY_NONE,
        stopbits=STOPBITS_ONE,
        interCharTimeout=None
    )
    
    Thread(target=receiveSerialDataFromPort, args=(ser,)).start()
  

python find

Python find helps us to find a search pattern in a given text
Python method find() determines if a sub-string presents in a given string

Syntax:
str.find(str, beg=0 end=len(string))

For Example:
stra = "python find substring"
strb = "substring"

stra.find(strb)
stra.find(strb, 10) #This will search the required sub-string from index 10
stra.find(strb, 20) #This will search the required sub-string from index 20

print stra.find(strb)             #12
print stra.find(strb, 10)       #12
print stra.find(strb, 16, 30)  #-1
print stra.find(strb, 20, 30)  #-1

Return Value
find() returns -1 if there is no match, else it will return the index of the match

Example:
def getText():
    return "Python find Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure. Defaults for start and end and interpretation of negative values is the same as for slices";

input_text = getText()

print input_text.find("Defaults for start")
print "\n"

if (input_text and input_text.find("Defaults for start") != -1):
    print "\n Found"
else:
    print "\n Not Found"
  

python check process running

We make use of python subprocess module
  • To write/execute the command in the command line
  • To write to stdin and read the output of stdout

Methods Used
1) checkProcessRunning
        To check whether process in running or not

2) writeToCMD
        To execute the command in the command line and read the output of it

Pass the required arguments
checkProcessRunning(cmd = "adb shell ps", processName = "com.android.phone")

cmd -> Command to execute
processName -> Text to search from the command line output

How to Run
python python_check_process_running.py
import subprocess

def checkProcessRunning(cmd, processName):
    print "\n Process to check : " + cmd
    result = writeToCMD(cmd)
    if (result[0].rstrip().find(processName) != -1):
        print processName + " is present/running"
        return True
    else:
        print processName + " is not present/running"
        return False
          
def writeToCMD(cmd):
    proc = None       
    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell="True")
    stdout_value = proc.communicate()
    if stdout_value:
        return stdout_value

checkProcessRunning(cmd = "adb shell ps", processName = "com.android.phone")
  


Feb 14, 2014

Python Delete old files

Many a times, we come across deleting old files like logs, deprecated files from file system


We come across scenarios like 

To delete logs older than x days
To delete txt files older than x days

Let us discuss how we can achieve deleting old files using python script

What does the script do?
It will take the path from where you need to delete the txt files
The below program checks the time stamp of the files and deletes the txt files older than 7 days.
You can change the file extension, folder path and use it.
folder_path = "C:\Files_To_Read"
file_ends_with = ".txt"
how_many_days_old_logs_to_remove = 7

How to call the script?
python delete_old_logs.py

delete_old_logs.py

import os, time, sys

folder_path = "C:\Files_To_Read"
file_ends_with = ".txt"
how_many_days_old_logs_to_remove = 7

now = time.time()
only_files = []

for file in os.listdir(folder_path):
    file_full_path = os.path.join(folder_path,file)
    if os.path.isfile(file_full_path) and file.endswith(file_ends_with):
        #Delete files older than x days
        if os.stat(file_full_path).st_mtime < now - how_many_days_old_logs_to_remove * 86400: 
             os.remove(file_full_path)
             print "\n File Removed : " , file_full_path
 

perl extract urls from file

As a developer, we need to extract URLs from an input file quite often in our day to day work.
Let us discuss how to extract URLs from an input Text or HTML file using a perl snippet.
Let us automate extracting URLs

What does the script do?
The following script will take input file as command line argument.
It will read the file line by line, extract the HTTP urls and push the results into an array.
Finally prints the array object.

How to call the script?
perl get_urls_from_input_file.pl "C:\Files_To_Read\blog_html.txt"


get_urls_from_input_file.pl

use strict;
use warnings;
use warnings;
use Data::Dumper;

my %substitute = ( '%20' => '', '%3A' => '' , '%24' => '');
my @result_urls;

#perl get_urls_from_input_file.pl 'C:\Files_To_Read\blog_html.txt'

my $infile  = $ARGV[0];

open(my $fh, '<', $infile) or die "Could not open logfile: $!";
while ( my $each_line = <$fh> ) {
    chomp $each_line;

 if ($each_line =~ /['"](http:\/\/.*?)['"]/) {
  my $grep_url = $1;
  push(@result_urls, $1);
 }
}
close $fh;

print "\n" . Dumper(\@result_urls);

1;

Jul 10, 2013

Perl Config File

We can make use of Config::Simple module for this.
This library supports parsing, updating and creating configuration files.

Main Features of Config::Simple are as mentioned below:
1) It allows to read config file in different formats/styles like INI-FILE format and HTML format.
2) It allows to read config file in the form of objects and access the variables from the object.
3) It allows to fetch all the variables at into a hash/hashref using "vars" method.

Let's discuss how can we read/modify/write config files easily in perl as mentioned below :
Reading config file in INI-FILE (ini) style
Reading config file in HTTP-LIKE style
Creating config file in INI style

1) Reading/Updating config file in INI-FILE (ini) style

If the configuration file has different blocks, then this style is very useful
Let's explain with the below mentioned example

db_ini.cfg

[mysql]
host=DBI:mysql:host
login=mysql_user
password=mysql_pass
db_name=test
RaiseError=1
PrintError=1

[oracle]
host=DBI:oracle:host
login=oracle_user
password=oracle_pass
db_name=oracle_db
RaiseError=1
PrintError=1
  

Script
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Config::Simple;

my $cfg = new Config::Simple('db_ini.cfg');

#Get Values from Config File
print "\n MySql DB Name     : " . $cfg->param("mysql.db_name");
print "\n MySql DB Password : " . $cfg->param("mysql.password");

print "\n\n Oracle DB Name     : " . $cfg->param("oracle.db_name");
print "\n Oracle DB Password : " . $cfg->param("oracle.password");
 
#Set/Update Values Config File
$cfg->param("mysql.db_name", "new_mysql_db_name");
$cfg->param("mysql.password", "new_mysql_password");

print "\n\n MySql DB Name     : " . $cfg->param("mysql.db_name");
print "\n MySql DB Password : " . $cfg->param("mysql.password");

$cfg->param("oracle.db_name", "new_orcl_db_name");
$cfg->param("oracle.password", "new_orcl_password");

print "\n\n Oracle DB Name     : " . $cfg->param("oracle.db_name");
print "\n Oracle DB Password : " . $cfg->param("oracle.password");

#Adding a new Variable to Config File
$cfg->param("mysql.new_var", "mysql_adding_variable");

print "\n\n MySql New Var     : " . $cfg->param("mysql.new_var");

$cfg->param("oracle.new_var", "oracle_adding_variable");

print "\n\n Oracle New Var     : " . $cfg->param("oracle.new_var");

print "\n\n Deleting Mysql New Var 'new_var' ... ";
$cfg->delete('mysql.new_var'); # deletes 'new_var' from [mysql] block

print "\n\n Deleting Oracle New Var 'new_var' ... ";
$cfg->delete('oracle.new_var'); # deletes 'new_var' from [oracle] block

#Config Vars
#Config::Simple also supports vars() method, which, depending on the context used, returns all the values either as hash or hashref
my %Config = $cfg->vars();
print "\n\n Config Hash Obj : " . Dumper(\%Config);

my $config_ref = $cfg->vars();
print "\n\n Config Hash Ref : " . Dumper($config_ref);
  

db_ini.cfg Output
 MySql DB Name     : test
 MySql DB Password : mysql_pass

 Oracle DB Name     : oracle_db
 Oracle DB Password : oracle_pass

 MySql DB Name     : new_mysql_db_name
 MySql DB Password : new_mysql_password

 Oracle DB Name     : new_orcl_db_name
 Oracle DB Password : new_orcl_password

 MySql New Var     : mysql_adding_variable

 Oracle New Var     : oracle_adding_variable

 Deleting Mysql New Var 'new_var' ... 
 
 Deleting Oracle New Var 'new_var' ... 
 
 Config Hash Obj : $VAR1 = {
          'mysql.PrintError' => '1',
          'mysql.db_name' => 'new_mysql_db_name',
          'oracle.password' => 'new_orcl_password',
          'oracle.host' => 'DBI:oracle:host',
          'mysql.host' => 'DBI:mysql:host',
          'mysql.password' => 'new_mysql_password',
          'oracle.PrintError' => '1',
          'oracle.login' => 'oracle_user',
          'mysql.RaiseError' => '1',
          'oracle.db_name' => 'new_orcl_db_name',
          'oracle.RaiseError' => '1',
          'mysql.login' => 'mysql_user'
        };


 Config Hash Ref : $VAR1 = {
          'mysql.PrintError' => '1',
          'oracle.password' => 'new_orcl_password',
          'mysql.db_name' => 'new_mysql_db_name',
          'oracle.host' => 'DBI:oracle:host',
          'mysql.host' => 'DBI:mysql:host',
          'oracle.PrintError' => '1',
          'mysql.password' => 'new_mysql_password',
          'oracle.login' => 'oracle_user',
          'mysql.RaiseError' => '1',
          'oracle.db_name' => 'new_orcl_db_name',
          'oracle.RaiseError' => '1',
          'mysql.login' => 'mysql_user'
        };
  


2) Reading/Updating config file in HTTP-LIKE style

When we just key and value pairs, this simple HTTP-Like style works.
Let's explain with the below mentioned example

db_http.cfg
host:'DBI:mysql:host'
login:user
password:secret
db_name:test
RaiseError:1
PrintError:1
  

Script
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Config::Simple;

my $cfg = new Config::Simple('db_http.cfg');

#Get Values from Config File
print "\n DB Name     : " . $cfg->param("db_name");
print "\n DB Password : " . $cfg->param("password");
 
#Set/Update Values Config File
$cfg->param("db_name", "new_db_name");
$cfg->param("password", "new_password");

print "\n\n DB Name     : " . $cfg->param("db_name");
print "\n DB Password : " . $cfg->param("password");

#Adding a new Variable to Config File
$cfg->param("new_var", "adding_variable");

print "\n\n New Var     : " . $cfg->param("new_var");

print "\n\n Deleting New Var 'new_var' ... ";
$cfg->delete('new_var'); # deletes 'new_var'

#Config Vars
#Config::Simple also supports vars() method, which, depending on the context used, returns all the values either as hash or hashref
my %Config = $cfg->vars();
print "\n\n Config Hash Obj : " . Dumper(\%Config);

my $config_ref = $cfg->vars();
print "\n\n Config Hash Ref : " . Dumper($config_ref);


db_http.cfg Output
 DB Name     : test
 DB Password : secret

 DB Name     : new_db_name
 DB Password : new_password

 New Var     : adding_variable

 Deleting New Var 'new_var' ... 
 
 Config Hash Obj : $VAR1 = {
          'db_name' => 'new_db_name',
          'password' => 'new_password',
          'RaiseError' => '1',
          'PrintError' => '1',
          'login' => 'user',
          'host' => 'DBI:mysql:host'
        };

 Config Hash Ref : $VAR1 = {
          'db_name' => 'new_db_name',
          'password' => 'new_password',
          'RaiseError' => '1',
          'host' => 'DBI:mysql:host',
          'login' => 'user',
          'PrintError' => '1'
        };
  


3) Creating config file in INI style

Creating a config file is explained as mentioned below.

$cfg = new Config::Simple(syntax=>'ini');
$cfg->write("test.cfg");

Supported "syntax" keywords are "ini", "simple" or "http"

write()
Alternatively, you can pass a name to either write() or save() to indicate the name of the file to create instead of modifying existing configuration file

Since we are trying to create a new config file, we need to pass the file name.

Script

$cfg = new Config::Simple(syntax=>'ini');

$cfg->param("mysql.dsn", "DBI:mysql:db;host=localhost.com");
$cfg->param("mysql.user", "username");
$cfg->param("mysql.pass", 'secret');
$cfg->write("test.cfg");
  

test.cfg Output
[mysql]
dsn:mysql:db;host=localhost.com
user:username
pass:secret
  


Perl Remove Special Characters From File

While reading some kind of log files as mentioned below, we need to get rid of these special characters.

Because of these special characters, it makes the Developers job tough :
To parse the content of a file
To convert the special characters from the file

Let us explain how can we get rid of these special characters with a simple example

test.log
^[[1;31mTest 1^[[0m
^[[1;31mTest 2^[[0m
^[[1;31mTest 3^[[0m
^[[1;31mTest 4^[[0m
^[[1;31mTest 5^[[0m  


In the above test.log file, first of all, what is displayed as ^[ is not ^ and [
But it is the ASCII ESC character, produced by Esc or Ctrl[ (the ^ notation means the Ctrl key).

We can use the following regular expression :

s/\e\[[\d;]*[a-zA-Z]//g;

Note: 
\e represents escape character in the above regular expression (substituting instead of ^[ )
We can shorten from [a-zA-Z] to just [mK], based on the requirement
You can make use of the above regular expression while parsing the file as well (line by line)

In case if you want to backup file (test.log.bak) instead of changing in the original file (test.log) then use the following :
perl -pi.bak -e 's/\e\[[\d;]*[a-zA-Z]//g' test.log

The following will remove the special chars in test.log
perl -pi -e 's/\e\[[\d;]*[a-zA-Z]//g' test.log

Output after removing
Test 1
Test 2
Test 3
Test 4
Test 5
  



Jul 8, 2013

Unit Testing in Perl

We can write test scripts in Perl using Test::Simple and Test::More in easy way.

Test::More is an yet another framework for writing test scripts

Lets explain Test::More module with examples.

ok() vs is() 

1) Similar to ok(), is() and isnt() compare their two arguments with eq and ne respectively and use the result of that to determine if the test succeeded or failed. 

is($hsh{a}, 200, 'Test 200');
isnt($hsh{a}, 200, 'Test 200');

are similar to these:

ok($hsh{a} eq 200, 'Test 200');
ok($hsh{a} ne 200, 'Test 200');

2) They produce better diagnostics on failure. 
ok() cannot know what you are testing for (beyond the name)
but 
is() and isnt() know what the test was and why it failed. 

In case of test fail, ok() shows fail like below :
ok($hsh{a} == 200, 'Test 200');
# not ok 5 - Fail
#   Failed test 'Fail'
#   at main.pl line 21.

In case of test fail, is() or isnt() shows fail like below :
is($hsh{a}, 200, 'Test 200');
# not ok 5 - Fail
#   Failed test 'Fail'
#   at main.pl line 20.
#          got: '100'
#     expected: '200'

like()
like() allows to use regular exporessions in writing test cases
The following example, we are trying to check $str contains string 'freedom' or not

like($str, qr/freedom/i, 'String contains Freedom - ignore case');
ok($str =~ /freedom/i, 'String contains Freedom - ignore case');


cmp_ok()
cmp_ok() also useful in those cases where you are giving explicit conditions like >=, <=, ==, != etc.,

cmp_ok($scalar, "<=", 200, '10 is less than or equal to 200 - pass');
cmp_ok($scalar, "==", 10, '10 is equal to 10 - pass');

can_ok()

Syntax:
can_ok($module, @methods);
can_ok($object, @methods);

can_ok checks if a mthod exists in an object, here $obj is the object of "support" class
can_ok also checks if a method exists in an class, here "support" is the class
can_ok is good at taking mutiples methods to test at one shot

E.g.,
can_ok($obj, "add");
can_ok("support", "multiply");
can_ok("support", qw(test add multiply));

Lets explain the above methods with an example :
In the following example, we use support.pm for testing object, class and methods

support.pm
#!/usr/bin/perl

package support;

use strict;
use warnings;
use Data::Dumper;

sub new {
    my($class) = shift;
        
    my($self) = {};

    return(bless($self, $class));
}


sub multiply {
  my $a = shift;
  my $b = shift;
 
  return ($a*$b);
}  

sub add {
  my $a = shift;
  my $b = shift;
 
  return ($a+$b);
}  

sub test {
    print "\n test mehthod inside support package"; 
}  

1;  

main.pl
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Test::More tests=>20;
use support;

my $obj = new support;

#You usually want to test if the module you're testing loads ok, rather than just vomiting if its load fails. It's recommended that you run use_ok() inside a BEGIN block so its functions are exported at compile-time and prototypes are properly honored.
BEGIN { use_ok("CGI"); }

#print "\n Obj : " . Dumper($obj);


#Testing Packages and Objects -----------------------------------------------
#Testing an object belongs to a class or not
ok (defined($obj) && ref $obj eq 'support', 'support obj new worked');

#can_ok checks if a mthod exists in an object, here $obj is the object of "support" class
#can_ok also checks if a method exists in an class, here "support" is the class
#can_ok is good at taking mutiples methods to test at one shot
can_ok($obj, "add");
can_ok("support", "multiply");
can_ok("support", qw(test add multiply));


#isa_ok checks if an object belongs to a class or not
isa_ok($obj, 'support');


#Testing Values -----------------------------------------------
my $scalar = 10;
my %hsh    = (a=>100, b=>200, c=>300);
my $str    = 'Freedom lies in being bold';


#is() vs ok()
#is() is recommended over ok()
#ok() function doesn't provide good diagnostic output.
#ok() cannot know what you are testing for (beyond the name), but is() and isnt() know what the test was and why it failed.

ok($scalar == 10, 'Pass');
is($scalar, 10, 'Pass');

is($hsh{a}, 100, 'Pass');
ok($hsh{a} == 100, 'Pass');

#is($hsh{a}, 200, 'Fail');
# not ok 5 - Fail
#   Failed test 'Fail'
#   at main.pl line 20.
#          got: '100'
#     expected: '200'

#ok($hsh{a} == 200, 'Fail');
# not ok 5 - Fail
#   Failed test 'Fail'
#   at main.pl line 21.

isnt($scalar, 20, 'Pass');

#like() allows to use regular exporessions in writing test cases
like($str, qr/freedom/i, 'String contains Freedom - ignore case');
ok($str =~ /freedom/i, 'String contains Freedom - ignore case');

#It's also useful in those cases where you are comparing numbers and applying conditions
cmp_ok($scalar, "<=", 200, '10 is less than or equal to 200 - pass');
cmp_ok($scalar, "==", 10, '10 is equal to 10 - pass');


Output:

1..20
ok 1 - use CGI;
ok 2 - support obj new worked
ok 3 - support->can('add')
ok 4 - support->can('multiply')
ok 5 - support->can(...)
ok 6 - The object isa support
ok 7 - Pass
ok 8 - Pass
ok 9 - Pass
ok 10 - Pass
ok 11 - Pass
ok 12 - String contains Freedom - ignore case
ok 13 - String contains Freedom - ignore case
ok 14 - 10 is less than or equal to 200 - pass
ok 15 - 10 is equal to 10 - pass
# Looks like you planned 20 tests but ran 15.
  


Jul 2, 2013

Perl Debug

Perl Debug is simple tool to easily debug perl scripts.

Few people like me find it difficult using Debug, but to be frank this is quite simple and straight forward. It makes the life of programmer easy to trace the bugs in a fast mode.

Without this, programmer needs to write unnecessary Print statements inside code and check where exactly things going wrong.

If you invoke Perl with the -d switch, your script runs under the Perl source debugger.

This works like an interactive Perl environment, prompting for debugger commands that let you examine source code, set breakpoints, dynamically pass the values of variables, etc.

This is so convenient that you often fire up the debugger all by itself just to test out Perl constructs interactively.

Debug Interactively:
Andrew E. Page, written an useful CPAN module Devel::Ptkdb, using this we can debug interactively.

This makes life much easier, no need to use <debug> prompt instead one can use the interactive mode to set the break points and pass the values to arguments dynamically.


How to Debug Perl Script Interactively using above module
perl -d:ptkdb myscript.pl  (for graphical representation of debugging)

Normal Debugger
perl debugger works using -d switch
Once you execute script using -d switch, it will change to debug prompt as mentioned below :
DB<1> ....
DB<2> ....

Some commonly used commands in Debug mode:

1) "s"  (Stepping through line by line execution) 
Keep pressing "s" will execute line by line

2) "l"  (List line) 
e.g.,
l 20                   #Lists line 20
l 20-25             #Lists 20 to 25 lines of script
l <subroutine>   #Lists a sub-routine

3) "b" (Setting break point on subroutine)
    "b" <subroutine>  
     #This will allow you to set a break point (set a break point on first line of subroutine)

4) "c" <subrutine>    
#This will take you directly to that break point (set one-time bkpt at subname and continue)

5) "q"                         
#It will quit  the debugger        


Jun 29, 2013

Inheritance in Perl

Inheritance with an Example using Perl

Inheritance:
Inheritance is nothing but inheriting methods/properties from Parent class to child class.
Inheritance is one of main pillars of Object Oriented Concepts.

Inheritance in Perl :
In Perl, inheritance is accomplished by placing the names of parent classes into a special array called @ISA
The elements of @ISA are searched left to right for any missing methods.

E.g.,
our @ISA = qw(AAA BBB);
#This represents we are inheriting from Packages AAA and BBB respectively.

 Lets define with an example as mentioned below :

Inventory.pm (Parent of Pen)
Pen.pm           (Pen inherits methods from Inventory)
main.pl            (Create instance of Pen and accessing the methods of Inventory class as well)


Inventory.pm

#!/usr/bin/perl

package Inventory;

use strict;
use warnings;
use Data::Dumper;

sub new {
    my($class)  = shift;
    my(%params) = @_;
    bless {
       "NUMBER"    => $params{"NUMBER"},
       "QUANTITY" => $params{"QUANTITY"}
    }, $class;
}

1;  


Pen.pm

#!/usr/bin/perl

package Pen;

use strict;
use warnings;
use Data::Dumper;

use Inventory;

our @ISA = qw(Inventory);    #### Inheritance

sub new {
    my($class) = shift;
    my(%params) = @_;

    #my($self) = Inventory->new(@_);
    my($self) = new Inventory(@_);

    $self->{"COLOR"} = $params{"COLOR"};
    return(bless($self, $class));
}

1;
  

main.pl

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Pen;

my $pen = new Pen(
        "NUMBER"   => "12A-34",
        "QUANTITY" => 34,
        "COLOR"    => "blue"
        );

print("The part number is " . $pen->{'NUMBER'}   . "\n");
print("The quantity is "    . $pen->{'QUANTITY'} . "\n");
print("The ink color is "   . $pen->{'COLOR'}    . "\n");
  

Output:
The part number is 12A-34
The quantity is 34
The ink color is blue


Jun 28, 2013

Package or Class Example in Perl

Packages or Classes in Perl (OOPS Concept)

Definition :
In Perl, a class is corresponds to a Package.
To create a class in Perl, we first build a package.
A package is a self-contained unit of user-defined variables and subroutines, which can be re-used over and over again.
They provide a separate namespace within a Perl program that keeps subroutines and variables from conflicting with those in other packages.

Rules to be followed for a module in Perl :
The module in perl in general terms means a namespace defined in a file. Certain modules are only collections of function. In perl the modules must follow the following guidelines:
- The file name of a module must the same as the package name.
- The general naming convention of naming a package is to begin with a capital letter, but not mandatory always.
- All the file name should have the extension of "pm".
- Last line of a package is 1 i.e., returning always 1 as mentioned in the example below.
- In case no object oriented technique is used the package should be derived from the Exporter class.
- Also if no object oriented techniques are used the module should export its functions and variables to the main namespace using the @EXPORT and @EXPOR_OK arrays.
The use directive is used to load the modules.

Creating/Defining Object :
To create an instance of a class (an object) we need an object constructor.
This constructor is a method defined within the package.

What is Bless : 
You create a hash object and bless the hash object to who ever calling the constructor (new method) of a package/class.

E.g.,
Blessing an object based on the parameters passed to the constructor method
my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
 bless $self, $class;

Blessing an empty object is also possible
my $self = {};
bless $self , $class

E.g.,
Package Person;
sub new {
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    bless $self, $class;
    return $self;
}
1;

Creating Instance of Object :
In order to access package level methods, you need to create object of that class and then start accessing package level variables/methods.

my $object = new Person( "David", "Johnson", 23234345);
In the above example, we are creating object of Person from main.pl, here we are passing required parameters and the constructor (new method) of Person class will return a formatted hash object.

You can even bless an empty hash object as well.        
In case of empty object, the following returns empty object
my $object = new Person();

E.g.,
use Person;
use Data::Dumper;
my $object = new Person( "David", "Johnson", 23234345);

print "\n Person Object is : " . Dumper($object);

Information Hiding : 
This should not allow the users from modifying the object data.
You should allow the users to access the core object using setter/getter methods, there by achieving hiding object data from outside the world.

Let's see the below example for better understanding :
Support.pm has defined some getter(setFirstName) and setter(getFirstName) for accessing firstName
main.pl is accessing the firstName using getter(setFirstName) and setter(getFirstName)

support.pm

#!/usr/bin/perl

package support;

use strict;
use warnings;
use Data::Dumper;

sub new
{
    my $class = shift;
    print "\nClass Name : " . $class;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    # Print all the values just for clarification.
    print "\nFirst Name is $self->{_firstName}";
    print "\nLast Name is $self->{_lastName}";
    print "\nSSN is $self->{_ssn}";
    bless $self, $class;
    return $self;
}

sub setFirstName {
    my ( $self, $firstName ) = @_;
    $self->{_firstName} = $firstName if defined($firstName);
    return $self->{_firstName};
}

sub getFirstName {
    my( $self ) = @_;
    return $self->{_firstName};
}

1;
  

main.pl

#!/usr/bin/perl
use support;

use strict;
use warnings;
use Data::Dumper;

my $object = new support( "David", "Johnson", 23234345);

# Get first name which is set using constructor.
my $firstName = $object->getFirstName();

print "\n\nBefore Setting First Name is : $firstName";

# Now Set first name using helper function.
$object->setFirstName( "Mohd" );

# Now get first name set by helper function.
$firstName = $object->getFirstName();

print "\nAfter Setting First Name is : $firstName\n";  


Output :

Class Name : support
First Name is David
Last Name is Johnson
SSN is 23234345

Before Setting First Name is : David
After Setting First Name is : Mohd 
 


Perl How to Export Methods

We have Exporter module in Perl :

Modules are packages but which has the capabilities of exporting selective subroutines/scalars/arrays/hashes of the package to the namespace of the main package itself.

So for the interpreter these look as though the subroutines are part of the main package itself and so there is no need to use the scope resolution operator while calling them.

It may do this by providing a mechanism for exporting some of its symbols into the symbol table of any package using it

There are times we want to export/expose methods or variables to other classes.

All these can be achieved by using module called "Exporter" module.

This is usually done like:
use Exporter;
our @ISA = ('Exporter');

Also we want to export only few methods instead of all methods, this can be achieved by
# Functions and variables which are exported by default
our @EXPORT = (multiply, $var1);

We might not want to export methods by default instead want to export on demand.
# Functions and variables which can be optionally exported
our @EXPORT_OK = (add);

Lets explain the same in detail with a class/package example :
support.pm package, it is exporting few methods
main.pl is making use of the exported methods/vars from support.pm package

support.pm

#!/usr/bin/perl

package support;

use strict;
use warnings;
use Data::Dumper;

use base qw(Exporter);  #which inturn equals to use Exporter; our @ISA = qw(Exporter);

#Exporting the add and subtract routine
our @EXPORT    = qw(multiply $var1);

#Exporting the multiply and divide routine on demand basis.
our @EXPORT_OK = qw(add);

our $var1 = 'global_var';

sub multiply {
  my $a = shift;
  my $b = shift;
 
  return ($a*$b);
}  

sub add {
  my $a = shift;
  my $b = shift;
 
  return ($a+$b);
}  

1;
  

main.pl

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

use support;          #Which is for EXPORT, export default ones (multiply, $val) by default 
use support qw(add);  #Which is for EXPORT_OK, export on demand (add on demand)

my $result = multiply(109,201);

print "\n Result after multiplication : " . $result;

print "\n Var from support.pm : " . $var1;

print "\n Result after addition : " . add(10,20);
  

Output:
 Result after multiplication : 21909
 Var from support.pm : global_var
 Result after addition : 30  



Delete element from Hash in Perl

You can use 'delete' command for deleting an element from an hash.
It will delete key-value pair.

Please refer to the below script:

#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my %hash = ( a => 10, b => 20, c => 30, d => 40);

print "\n Before Deleting : " . Dumper(\%hash);

delete $hash{a};

print "\n After Deleting : " . Dumper(\%hash);  

Output:

Before Deleting : $VAR1 = {
          'c' => 30,
          'a' => 10,
          'b' => 20,
          'd' => 40
        };

 After Deleting : $VAR1 = {
          'c' => 30,
          'b' => 20,
          'd' => 40
        };
  

Delete element from Array in Perl

You can use 'delete' command for deleting an element from an array.
When you delete an element from the middle of an array, that particular elements is undef (ie., not defined)

Please refer to the below program:

#!/usr/bin/perl 

use strict;
use warnings;
use Data::Dumper;

my @array = qw/10 20 30 40 50/;

print "\n Before Deleting :" . Dumper(\@array);

delete $array[2];

print "\n After Deleting :" . Dumper(\@array);
  

Output:

Before Deleting :$VAR1 = [
          '10',
          '20',
          '30',
          '40',
          '50'
        ];

 After Deleting :$VAR1 = [
          '10',
          '20',
          undef,
          '40',
          '50'
        ];
  


Execute Perl or Python or PHP online

I come across a very good site where you can run perl online and see the results.

http://www.compileonline.com/execute_perl_online.php

Advantages:
Just write the code (in the left panel) and run on the fly.
If case of any errors, it is shown on the right panel
They also prive an input.txt (in one of the tab) and practice reading/writing files
When you click on Multiple Files, it also gives support.pm, we can practice package concepts.

You can also run online Python/PHP/Java/Shell/JavaScript/HTML etc.,

Python:
http://www.compileonline.com/execute_python3_online.php

PHP:
http://www.compileonline.com/execute_php_online.php

Java:
http://www.compileonline.com/compile_java_online.php

Shell Scripting:
http://www.compileonline.com/execute_ksh_online.php

Java Script:
http://www.compileonline.com/try_javascript_online.php

HTML:
http://www.compileonline.com/try_html5_online.php

and many more on home page

http://www.compileonline.com/

Jun 11, 2013

Shift Unshift in Perl

Shift/Unshift => It works at the start of the array

Shift     => It removes element from start of an array
Unshift => It appends element to start of an array

use strict;
use warnings;

my @names = ("Foo", "Bar", "Baz");

print "\n Array : " . Dumper(\@names);

my $first = shift @names;     #Shift - removes element at the start of the array

print "$first";               #Foo
print "\n After Shift : " . Dumper(\@names);   #Bar Baz

unshift @names, "Moo";         # UnShift - adds element at the start of the array
print "\n After UnShift : " . Dumper(\@names);   # Moo Bar Baz  

Push Pop in Perl

Push/Pop      => It works at the end of the array
Push => Appends an element to end of an array
Pop  => Deletes an element from end of an array

@myNames = ('Larry', 'Curly');
push(@myNames, 'Moe');     #It adds 'Moe' at the end of the array

O/P: ('Larry', 'Curly', 'Moe')


@myNames = ('Larry', 'Curly', 'Moe');
$oneName = pop(@myNames);  #It removes 'Moe' from the end of the array

O/P: ('Larry', 'Curly')  

Delete an entry in Array and Hash in Perl

Let's discuss about deleting an element in array and hash respectively.

Deleting an element from an array:
use strict;
use warnings;

use Data::Dumper;

my @array = qw/10 20 30 40 50/;

print "\n Before Deleting :" . Dumper(\@array);

delete $array[2];

print "\n After Deleting :" . Dumper(\@array); 

Output:
Before Deleting :$VAR1 = [
          '10',
          '20',
          '30',
          '40',
          '50'
        ];

 After Deleting :$VAR1 = [
          '10',
          '20',
          undef,
          '40',
          '50'
        ];
 


Deleting an element from Hash:
use strict;
use warnings;

use Data::Dumper;

my %hash = ( a => 10, b => 20, c => 30, d => 40);

print "\n Before Deleting : " . Dumper(\%hash);

delete $hash{a};

print "\n After Deleting : " . Dumper(\%hash);

Output:
Before Deleting : $VAR1 = {
          'c' => 30,
          'a' => 10,
          'b' => 20,
          'd' => 40
        };

 After Deleting : $VAR1 = {
          'c' => 30,
          'b' => 20,
          'd' => 40
        };
  


May 29, 2013

Perl one liners

Most popular Perl One Liners

Sort File:
perl -e 'print sort {lc($a) cmp lc($b)} <>' input.txt > output.txt

Print Reverse of File:
perl -e 'print reverse <>' input.txt

Substitute the occurrences in a file:
perl -pi -e 's/foo/bar/g' input.txt      (It won't take backup)
perl -pi.bak -e 's/foo/bar/g' input.txt  (It will take backup)

Remove Duplicates and print the contents of a File :
perl -ne '$H{$_}++ or print $_' testfile.txt

Don't confuse with -n and -p switches
-n switch
  The 'n' switch will automatically add a while loop in your one-liner.
  This will help you to process each input line one by one. Each input line will be in '$_'
-p switch
  The 'p' switch is similar to 'n' switch (while loop around program) but it has an advantage of printing the line automatically.


You can use the following perl one lines from unix command line
(Reference: http://www.alfoos.com/perl-one-liners-howto/)

-e switch 
Allows you to pass a script to STDIN
Allows you to provide the program as an argument rather than in a file.
You don't want to have to create a script file for every little Perl one-liner.

# command-line that reverses the whole file by lines
         perl -e 'print reverse <>' input.txt

Note:
<> in list context returns all the lines in the file

How to Sort a file which contains strings (without case) in Ascendig order
perl -e 'print sort {lc($a) cmp lc($b)} <>' input.txt > output.txt

How to Sort a file which contains strings (without case) in Descendig order
perl -e 'print sort {lc($b) cmp lc($a)} <>' input.txt > output.txt

How to Sort a file which contains strings (with case) in Ascending Order
perl -e 'print sort {$a cmp $b} <>' input.txt > output.txt

How to Sort a file which contains strings (with case) in Descending Order
perl -e 'print sort {$b cmp $a} <>' input.txt > output.txt

How to Sort a file which contains numbers (Ascending Order)
perl -e 'print sort {$a <=> $b} <>' input.txt > output.txt

How to Sort a file which contains numbers (Decending Order)
perl -e 'print sort {$b <=> $a} <>' input.txt > output.txt

Simple Sort:
perl -we 'print sort <>' input.txt > output.txt

Sort lines by their length
perl -e 'print sort {length $a <=> length $b} <>' input.txt

-n switch
   The 'n' switch will automatically add a while loop in your one-liner.
   This will help you to process each input line one by one. Each input line will be in '$_'
 
   perl -ne 'print' input.txt   //Correct
   or
   perl -ne 'print $_' input.txt  //Correct

   perl -e 'print $_' input.txt  //Wrong

   # Delete first 10 lines 
   perl -i.old -ne 'print unless 1 .. 10' input.txt
     
   # Just lines 15 to 25 
   perl -ne 'print if 15 .. 25' input.txt
 
   Remove all blank lines of a file (-i will write into same file)
   perl -ni -e 'print unless /^$/' input.txt
 
   Remove all blank lines of a file with backup file
   perl -ni.bak -e 'print unless /^$/' input.txt
 
 
-p switch
   The 'p' switch is similar to 'n' switch (while loop around program) but it has an advantage of printing the line automatically.
 
   perl -pi -e 's/foo/bar/' *.txt
 
   perl -pe 'print' input.txt  //The lines are printed twice, once by 'p' switch and once by 'print'.

   perl -pi -e 's/foo/bar/g' input.txt (It won't take backup)
 
   perl -pi.bak -e 's/foo/bar/g' input.txt  (It will take backup)
 
-i switch 
   The 'i' switch is to do the in-line editing of file. As you can see all the one-liners above prints to STDOUT.
   Using 'i' switch you can print it to the same file from which you are reading.
   When you use 'i' switch you will not see the output in STDOUT but the output will be printed to the reading file itself.
   Modifies your input file in-place (making a backup of the original).
   Handy to modify files without the {copy, delete-original, rename} process.

   perl -i -pe 's/foo/bar/;' input.txt  //modifies in the same file (don't print to STDOUT)

   You can also pass an extension to 'i' switch.
   This will create a backup of your original file with the given extension before editing the original file
 
   perl -i.bk -pe 's/foo/bar/;' input.txt  //This take the backup

   In Perl, how to do you remove ^M from a file?
   perl -p -i -e 's/\r\n$/\n/g' file1.txt file2.txt  

-M switch 
    Although it is possible to use a -e option to load a module, Perl gives you the -M option to make that easier.

        perl -MData::Dumper -e 'my %hash = ("a" => 10, "b" => 20, "c" => 30);  print "\n Dump Value: " . Dumper(\%hash)'
perl -MCGI -e 'print "$CGI::VERSION \n"'
perl -MData::Dumper -e 'print "$Data::Dumper::VERSION \n"'

-l switch
    The 'l' switch is for processing the line terminator. As you know the default line terminator is '\n'.
You can manage the line terminator using the 'l' switch. You need to pass an octal value along 'l'.
First it does a chomp of the input records which removes that character.
Second, when doing a print it adds that character to the end of each line (by setting the $\ output record separator).
If you do not pass any octal value then '\n' will be assumed.

perl -ne 'print $_." appended to line"' testfile.txt
This will append " appended to line" in second line, this is because we havnt added 'l' switch and the string was added after '\n'.

perl -lne 'print $_." appended to line"' testfile.txt
This will " appended to line" correctly, this is because the 'l' switch removes the '\n' first then append the string and then add '\n' before printing

-a switch 
    Awk commands
    echo "Hello World" | awk '{print $2}'
    echo "Hello guys" | perl -lane 'print $F[1]'

-w switch 
   It is the same as use warnings

-d switch
   for debugging

May 23, 2013

Decimal Sort in Perl


In the below mentioned, we have a hash with decimal values (for gpa key)
Lets assume we have a hash for students in the format mentioned below.
Let's discuss how to sort decimal values in Perl. We will sort both in ascending and descending ways.

decimal_sort.pl
use strict;
use warnings;
use Data::Dumper;

my %students = (
   'John'   => {
                    'Science'   => 'PASS',
                    'Maths'     => 'PASS',
                    'chemistry' => 'PASS',
                    'Physics'   => 'PASS',
                    'gpa'       => '4.01',
                  },

  'Diana'   => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.10',
               },

  'David'    => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.00',
                }
);

print "\n\n Before Sorting : ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Ascending Order : ";
for my $each_student (sort{ $students{$a}{gpa} <=> $students{$b}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Descending Order : ";
for my $each_student (sort{ $students{$b}{gpa} <=> $students{$a}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}
print "\n\n";
  


Output:

 Before Sorting : 
 Each Student : David   GPA : 4.00
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01

 After Sorting GPA in Ascending Order : 
 Each Student : David   GPA : 4.00
 Each Student : John    GPA : 4.01
 Each Student : Diana   GPA : 4.10

 After Sorting GPA in Descending Order : 
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01
 Each Student : David   GPA : 4.00
  


Please refer to other topics in Perl like :
Sort hash by value string in perl
Sort hash by value numerically in perl
Decimal Sort in Perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


Sort hash by value string in perl


If you want to sort numbers, use <=>
If you want to sort strings, use cmp instead of <=>

Sort hash by value(String) ascending in perl by using the following Syntax:
sort{ $hash{$a} cmp $hash{$b} }

Sort hash by value(String) descending in perl by using the following Syntax:
sort{ $hash{$b} cmp $hash{$a} }

sort_by_hash_value_strings.pl
use strict;
use warnings;
use Data::Dumper;

my %students = (
    Diana    => "Science",
    Davis    => "Science",
    John     => "Maths",
    Linus    => "Physics",
    Brown    => "Maths",
    Jane     => "Science"
);


print "\n\n Before Sorting on Value(Subject): ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}

print "\n\n After Sorting Value(Subject) in Ascending Order : ";
for my $each_student (sort{ $students{$a} cmp $students{$b} } keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}

print "\n\n After Sorting Value(Subject) in Descending Order : ";
for my $each_student (sort{ $students{$b} cmp $students{$a} } keys %students) {
   print "\n Each Student : $each_student \tSubject : " . $students{$each_student};
}
print "\n\n";  


Output:

 Before Sorting on Value(Subject): 
 Each Student : Brown   Subject : Maths
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science
 Each Student : Jane    Subject : Science
 Each Student : John    Subject : Maths
 Each Student : Linus   Subject : Physics

 After Sorting Value(Subject) in Ascending Order : 
 Each Student : John    Subject : Maths
 Each Student : Brown   Subject : Maths
 Each Student : Linus   Subject : Physics
 Each Student : Jane    Subject : Science
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science

 After Sorting Value(Subject) in Descending Order : 
 Each Student : Jane    Subject : Science
 Each Student : Davis   Subject : Science
 Each Student : Diana   Subject : Science
 Each Student : Linus   Subject : Physics
 Each Student : John    Subject : Maths
 Each Student : Brown   Subject : Maths



Please refer to other topics in Perl like :
Decimal Sort in Perl
Sort hash by value string in perl
Sort hash by value numerically in perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton


Sort hash by value numerically in perl


If you want to sort numbers, use <=>
If you want to sort strings, use cmp instead of <=>

Sort hash by value(Number) ascending in perl by using the following Syntax:
sort{ $hash{$a} <=> $hash{$b} }

Sort hash by value(Number) descending in perl by using the following Syntax:
sort{ $hash{$b} <=> $hash{$a} }


sort_by_hash_value_numbers.pl

use strict;
use warnings;
use Data::Dumper;

my %students = (
   'John'   => {
                    'Science'   => 'PASS',
                    'Maths'     => 'PASS',
                    'chemistry' => 'PASS',
                    'Physics'   => 'PASS',
                    'gpa'       => '4.01',
                  },

  'Diana'   => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.10',
               },

  'David'    => {
                     'Science'   => 'PASS',
                     'Maths'     => 'PASS',
                     'chemistry' => 'PASS',
                     'Physics'   => 'PASS',
                     'gpa'       => '4.00',
                }
);

print "\n\n Before Sorting : ";
for my $each_student (sort keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Ascending Order : ";
for my $each_student (sort{ $students{$a}{gpa} <=> $students{$b}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}

print "\n\n After Sorting GPA in Descending Order : ";
for my $each_student (sort{ $students{$b}{gpa} <=> $students{$a}{gpa} } keys %students) {
   print "\n Each Student : $each_student \tGPA : " . $students{$each_student}{gpa};
}
print "\n\n";  


Output:

 Before Sorting : 
 Each Student : David   GPA : 4.00
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01

 After Sorting GPA in Ascending Order : 
 Each Student : David   GPA : 4.00
 Each Student : John    GPA : 4.01
 Each Student : Diana   GPA : 4.10

 After Sorting GPA in Descending Order : 
 Each Student : Diana   GPA : 4.10
 Each Student : John    GPA : 4.01
 Each Student : David   GPA : 4.00



Please refer to other topics in Perl like :
Decimal Sort in Perl
Sort hash by value string in perl
Sort hash by value numerically in perl
How to read command line arguments in Perl
How to pass command line arguments to perl script
Loop through Directory and read the files in Perl
How to create excel report in Perl


Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep Examples
Unix Cut Command Examples
Search a Directory in Unix
Unix For Loop
pushd & popd in Unix
Find Size of Directory
Word Count


Please refer to other topics on AWK like :
Awk Examples
Print First Two Columns of File
Print Last Two Columns of File


Please refer to other topics on Dict like :
Dict in Python
Dict keys and values in Python


Please refer to other topics on List like :
List in Python
Append to list in Python
Delete the last name from the list in Python
Remove an element from List in Python
Check an element exists in an list in Python
Python Filter Vs Map Vs List Comprehension


Please refer to other topics on File Concepts like :
Print File Content in Python
Print File in Reverse Order in Python


Please refer to Regular Expressions Concepts :
Brief on Regular Expressions
Greedy Operators in Regular Expressions in Perl
Modifiers in Regular Expressions in Perl
Capturing concept in Regular Expressions in Perl
Capture Pre Match ,Post Match, Exact match in Regular Expressions in Perl
Non Capturing Paranthesis in Regular Expressions in Perl
Substitute nth occurance in Regular Expressions in Perl
All Topics in Regular Expressions in Perl


You might also wish to read other topics on Python like :
Python Class and Object Example
Inheritance in Python
Packages in Python
Exceptions in Python
How to remove duplicate lines from a file in Perl
How to remove duplicate lines from a file in Pyhton