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


May 13, 2013

How to read command line arguments in Perl

The following script will take the command line arguments and print one by one.

By default, all the command line argumnets are captured in @ARGV

Here, we directly pass the arguments without any labels

#!/usr/bin/perl

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

foreach my $arg (@ARGV) {
   print $arg . "\n";
}
1;	 

Output:

perl read_command_line_params.pl 100 "Mother Teresa"
100
Mother Teresa	 


Please refer to other topics in Perl like :
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


How to pass command line arguments to perl script


We can pass command line arguments with the labels and we can make use of Getopt::Long module for achieving this

If an user tries to pass a wrong/invalid parameter, the following will throw an error.

The following script takes the parameters like
    -str (String value)
    -how_many_times (integer)
   
The following will print the string (from -str parameter) as many times the user the entered -how_many_times parameter

pass_command_line_params.pl
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use File::Basename;

#Extract the nae of this script. 
my $scriptname = basename($0);

my @usage =("$scriptname",
           "-str  = Enter a String",
           "-how_many_times = Enter an integer, how many times you want the String to print");

my $opt_help     = 0;
my $opt_str     = ""; 
my $opt_how_many_times  = ""; 

my $ret = &GetOptions('help',  \$opt_help,
                      'str=s',\$opt_str,
                      'how_many_times=i',\$opt_how_many_times);


# Check parameters
if (($ret != 1) || (! $opt_str) || (! $opt_how_many_times)){
    &usage();
    exit;   
}

sub usage {
    print "\n How to use this Script : ". Dumper(\@usage) . "\n";

    print "\n You didn't pass str parameter " if (!$opt_str);
    print "\n You didn't pass how_many_times parameter " if (!$opt_how_many_times);
    print "\n";

    exit;
}


for (my $i=0; $i<$opt_how_many_times; $i++) {
    print $opt_str. "\n";
}
1;  

Outout :
1) perl pass_command_line_params.pl -str=Peter -how_many_times=
    Value "dd" invalid for option how_many_times (number expected)

    How to use this Script : $VAR1 = [
              'pass_command_line_params.pl',
              '-str  = Enter a String',
              '-how_many_times = Enter an integer, how many times you want the String to print'
            ];


    You didn't pass how_many_times parameter  

2) perl pass_command_line_params.pl -str="Mother Teresa" -how_many_times=5
    Mother Teresa
    Mother Teresa
    Mother Teresa
    Mother Teresa
    Mother Teresa  



Please refer to other topics in Perl like :
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


Apr 27, 2013

Loop through Directory and read the files in Perl

Now, we discuss about how to loop through a directory and read all the files in Perl
I have a directory with name "humanists" which has different files as mentioned below :

humanists :
abraham_lincon.txt
alfred_nobel.txt
mahatma_gandhi.txt
mother_teresa.txt
winston_churchill.txt

All the files has details in the following format
Name: <>
Born: <>
Died: <>

Now we have to loop thorugh all the files in the directory and get the information from these files and create a hash object.

Lets dive into this example. Please drop me a comment if you have any doubts on the same.


read_humanists.pl
#!/usr/bin/perl

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

my $dir_path = "/home/prabhath/prabhath_test/test/humanists";

my %humanists_list;

opendir(IN_DIR, $dir_path) or die $!; 
open (LOG_FILE, "> read_humanists.log") or die $!; 

unless ( -d $dir_path) {
   print "\n Path not present: " . $dir_path;
} else {
   print "\n Path is present: " . $dir_path;
}


while (my $file = readdir(IN_DIR)) {
    next if $file =~ /^\./;
    
    unless (-f "$dir_path/$file") {
        print "\n -W- Not a File, Ignoring : " . "$dir_path/$file" . "\n";
        next;
    }   

    open(IN_FILE, "< $dir_path/$file")
                    ||  die "\n Cant open file for reading: " . "$dir_path/$file";

    my @lines = ;
    close(IN_FILE);

    my @newlines;
    foreach my $each_line (@lines) {
        if ($each_line =~ /^Name:/) {
           my ($name) = $each_line =~ /Name:\s(.*)$/;
           $humanists_list{$file}{"name"} = $name;
        }

        if ($each_line =~ /^Born:/) {
           my ($dob) = $each_line =~ /Born:\s(.*)$/;
           $humanists_list{$file}{"date_of_birth"} = $dob;
        }

        if ($each_line =~ /^Died:/) {
           my ($dod) = $each_line =~ /Died:\s(.*)$/;
           $humanists_list{$file}{"died"} = $dod;
        }
    }   
} 

Output:
          'abraham_lincon.txt' => {
                                    'died' => 'April 15, 1865',
                                    'date_of_birth' => 'February 12, 1809',
                                    'name' => 'Abraham Lincon'
                                  },
          'winston_churchill.txt' => {
                                       'died' => 'January 24, 1965',
                                       'date_of_birth' => 'November 30, 1874',
                                       'name' => 'Winston Churchill'
                                     },
          'alfred_nobel.txt' => {
                                  'died' => 'December 10, 1896',
                                  'date_of_birth' => 'October 21, 1833',
                                  'name' => 'Alfred Nobel'
                                },
          'mother_teresa.txt' => {
                                   'died' => 'September 5, 1997',
                                   'date_of_birth' => 'August 26, 1910',
                                   'name' => 'Mother Teresa'
                                 },
          'mahatma_gandhi.txt' => {
                                    'died' => 'January 30, 1948',
                                    'date_of_birth' => 'October 2, 1869',
                                    'name' => 'Mahatma Gandhi'
                                  }
  


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 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


How to create excel report in Perl


We can generate Microsoft Excel (XLS) report using Perl

Note:
Please install module Spreadsheet::WriteExcel before running the program

We can create worksheets
We can set styles like color/font/size etc for the cells


#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;
use Spreadsheet::WriteExcel;


my ($xls_label, $workbook, $worksheet, $format_header, $format_row);

$xls_label = "test.xls";

$workbook = Spreadsheet::WriteExcel->new($xls_label);

$worksheet = $workbook->add_worksheet();

$format_header = $workbook->add_format(); # Add a format
$format_header->set_bold();
$format_header->set_color('purple');
$format_header->set_align('center');
$format_header->set_size(12);

$format_row = $workbook->add_format(); # Add a format
$format_header->set_bold();
$format_row->set_align('center');

my $row = 0;
my $column = 0;

$worksheet->write($row, 0, 'S.No', $format_header);
$worksheet->write($row, 1, 'Humanists', $format_header);

my @humanists_arr = ("Mother Teresa", "Mahatma Gandhi", "Abraham Lincoln", "Winston Churchil", "Alfred Nobel");

foreach my $each_humanist (@humanists_arr) {
   $row++;
   $worksheet->write($row, 0, $row, $format_row);
   $worksheet->write($row, 1, $each_humanist, $format_row);
}

1;


Output :
S.No Humanists
1 Mother Teresa
2 Mahatma Gandhi
3 Abraham Lincoln
4 Winston Churchil
5 Alfred Nobel  


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 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


Apr 1, 2013

Unix Cut Comamnd


CUT command has lot of advantages in grep/cut the fields/chars from the files easily in no time

We can use CUT command in the following ways:
1) Print Characters by Position (chars)
2) Print Characters by Range (range a-b)
3) Print the fields using the delimiter

Now, we discuss the above mentioned briefly. Let's have fun with some simple examples.
In all the below examples, we use same.txt as input as mentioned below :

sample.txt
Andy,100,Ney york,USA
Arjun,200,New Delhi,India
Venkatesh,300,Chennai,India
Andy,400,Boston,USA
John,500,Chicago,USA


1) Using Cut Print Characters by Position

This command prints the fourth character in each line of the file
]# cut -c4 sample.txt
y
u
k
y
n

This command prints the fourth and sixth character in each line.
]# cut -c4,6 sample.txt
y1
u,
kt
y4
n5


2) Using Cut Print Characters by Range

This command prints the characters from fourth position to the eighth position in each line
]# cut -c4-8 sample.txt
y,100
un,20
kates
y,400
n,500


This command prints the first four characters in a line
]# cut -c-4 sample.txt
Andy
Arju
Venk
Andy
John


This command prints the characters from fourth position to the end
]# cut -c4- sample.txt
y,100,Ney york,USA
un,200,New Delhi,India
katesh,300,Chennai,India
y,400,Boston,USA
n,500,Chicago,USA


This command omits the start and end positions, then the cut command prints the entire line.
]# cut -c- sample.txt
Andy,100,Ney york,USA
Arjun,200,New Delhi,India
Venkatesh,300,Chennai,India
Andy,400,Boston,USA
John,500,Chicago,USA

3) Print the fields using the delimiter
-d  option in cut command can be used to specify the delimiter 
-f  option is used to specify the field position

This command prints the first field of comma separated line
]# cut -d',' -f1 sample.txt
Andy
Arjun
Venkatesh
Andy
John


This command prints the first and second field in each comma separated line
]# cut -d',' -f1,2 sample.txt
Andy,100
Arjun,200
Venkatesh,300
Andy,400
John,500

This command prints the first, second and third field (range 1-3) in each comma separated line
]# cut -d',' -f1-3 sample.txt
Andy,100,Ney york
Arjun,200,New Delhi
Venkatesh,300,Chennai
Andy,400,Boston
John,500,Chicago

This command prints the first two fields in each comma separated line
]# cut -d',' -f-2 sample.txt
Andy,100
Arjun,200
Venkatesh,300
Andy,400
John,500

This command prints from first field to last field in each comma separated line
]# cut -d',' -f1- sample.txt
Andy,100,Ney york,USA
Arjun,200,New Delhi,India
Venkatesh,300,Chennai,India
Andy,400,Boston,USA
John,500,Chicago,USA 



Please refer to other topics on Unix like :
Unix Delete Duplicated Lines in a File
Unix Unique Lines in a File
Unix Grep 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 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


Mar 30, 2013

Unix Sort, Uniq, Combine Files

We will discuss about 
1) Combining contents of two files in Unix 
2) Sort & Get the Unique Lines (delete duplicate lines)
3) How to get only the duplicate lines
4) How to get only the unique lines

Suppose we have two files a.txt and b.txt as mentioned below :

a.txt
10
20
30
40
50

b.txt
10
20
30
40
50
60
70
80

1) Combining contents of two files in Unix 
Combining contents of both a.txt & b.txt into c.txt
]# cat a.txt b.txt > c.txt 
10
20
30
40
50
10
20
30
40
50
60
70
80


2) Sort and Get the Unique Lines (delete duplicate lines)
]# cat c.txt | sort | uniq
10
20
30
40
50
60
70
80

3) How to get only the duplicate lines
]# cat c.txt | sort | uniq -u
60
70
80

4) How to get only the unique lines
10
20
30
40
50

5) How to get sort by ignore case
]# cat requirements.txt | sort --ignore-case > requirements_bak.txt 


Please refer to other topics on Unix like :
Unix Grep 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 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


Mar 24, 2013

Python List

List : 
A Python list contains an ordered collection of objects

E.g.,

abc = [1, 2, 3, 4, 5]

Python index of list starts from 0 (left most)
abc[0], abc[1], abc[2], abc[3], abc[4]

Python also supports negative indices from the right most, starts with -1
abc[-5], abc[-4], abc[-3], abc[-2], abc[-1]

abc[-1] is 5
abc[0] is 1



Please refer to other topics on List like :
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 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 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


Replace string in all files in a directory in perl


We will discuss briefly about the script (To replace string in all files in a directory in perl)

Please define your directory path in the following :
my $input_dir  = "/usr/src/perl_test/input/";
my $output_dir = "/usr/src/perl_test/output/";
my $log_dir    = "/usr/src/perl_test/log/";

Define the string to replace and string to replace with as mentioned below
my $str_to_replace = "Mother Teresa":
my $str_replace_with = "MOTHER TERESA";

If you want to replace a particular string in all the files in the same directory path, then give the same path for both $input_dir & $output_dir.
E.g.,
my $input_dir  = "/usr/src/perl_test/same_path/";
my $output_dir = "/usr/src/perl_test/same_path/";

Log Directory :
It creates the log file(log_<timestamp>.log) in the path mentioned in $log_dir

Important Note : 
Please take a backup before running the script for safer side in case if you are replacing the strings in the same folder


conversion_script.pl
#!/usr/intel/bin/perl

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

my $input_dir  = "/usr/src/perl_test/input/";
my $output_dir = "/usr/src/perl_test/output/";
my $log_dir    = "/usr/src/perl_test/log/";

my $str_to_replace = "Mother Teresa":
my $str_replace_with = "MOTHER TERESA";

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
 $sec  = sprintf("%02d",$sec);
 $min  = sprintf("%02d",$min);
 $hour = sprintf("%02d",$hour);
 $mday = sprintf("%02d", $mday);
 $mon  = sprintf("%02d", $mon+1);
 $year = sprintf("%04d", $year+1900);

my $output_tag = $mday .'_'. $mon .'_'. $year .'_'. $hour .'_'. $min;

opendir(IN_DIR, $input_dir)   or die $!;
opendir(OUT_DIR, $output_dir) or die $!;
opendir(LOG_DIR, $log_dir)    or die $!;

open (LOG_FILE, "> $log_dirlog_$output_tag.log");

print LOG_FILE "------------- START ------------ \n";

#print LOG_FILE "Started Copying Files from Source: $srcdir   to Destination: $dest \n";
#my $cmd = "cp -R $srcdir/* $dest/";
#`$cmd`; #or die "Chk the command : " . $cmd;
#print LOG_FILE "Finished Copying Files from Source: $srcdir   to Destination: $dest \n";

my (%files_changed, %files_not_changed, @invalid_list_files);

while (my $file = readdir(IN_DIR)) {

    # Ignore if it is not file 
    unless (-f "$input_dir$file") {
       print LOG_FILE "-W- Not a File, Ignoring : " . $file . "\n";
       next;
    }

    print LOG_FILE "\n\nFile Name : " . $file . "\n";

    open(IN_FILE, "<$input_dir$file") || warn "Cant open file for reading: " . "$input_dir$file";
    my @lines = ;
    close(IN_FILE);

    my @newlines;
    foreach my $each_line (@lines) {
        #chomp $each_line;
        if($each_line =~ /$str_to_replace/) {
            print LOG_FILE "Converting String From : " . $each_line;
            $each_line =~ s/$str_to_replace/$str_replace_with/ig;
            print LOG_FILE "Converting String To   : " . $each_line;
            push(@newlines, $each_line);
            $files_changed{$file} = 1;
        } else {
            push(@newlines, $each_line);
            $files_not_changed{$file} = 1;
        }   
    }

    open(OUT_FILE, ">$output_dir$file") || warn "-W- Cant open file for writing: " . "$output_dir$file";
    print OUT_FILE @newlines;
    close(OUT_FILE);
}

my @f_changed     = keys %files_changed;
my @f_not_changed = keys %files_not_changed;

print LOG_FILE "\n\nOutput Summary as below ------------------------- : ";

print LOG_FILE "\n List of all the Files changed     : " . Dumper(\@f_changed);
print LOG_FILE "\n List of all the Files NOT changed : " . Dumper(\@f_not_changed);

print LOG_FILE "\n\n Total No.of files changed   : " . scalar(@f_changed);
print LOG_FILE "\n Total No.of files NOT changed : " . scalar(@f_not_changed);

close (LOG_FILE);
closedir(IN_DIR);
closedir(OUT_DIR);
closedir(LOG_DIR);

1;  


Input Directory: /usr/src/perl_test/input/
1) mother_teresa_intro.txt
Mother Teresa born on August 26, 1910
Full name of Mother Teresa is "Agnes Gonxha Bojaxhiu"
Mother Teresa founded the Missionaries of Charity, which in 2012 consisted of over 4,500 sisters and is active in 133 countries.

2) mother_teresa_awards.txt
In 1962, Mother Teresa was awarded the Ramon Magsaysay Award
In 1979, Mother Teresa was awarded the Nobel Peace Prize, for work undertaken in the struggle to overcome poverty and distress
In 1980, Mother Teresa was awarded the Bharat Ratna Prize  


Output Directory: /usr/src/perl_test/output/
1) mother_teresa_intro.txt
MOTHER TERESA born on August 26, 1910
Full name of MOTHER TERESA is "Agnes Gonxha Bojaxhiu"
MOTHER TERESA founded the Missionaries of Charity, which in 2012 consisted of over 4,500 sisters and is active in 133 countries.


2) mother_teresa_awards.txt
In 1962, MOTHER TERESA was awarded the Ramon Magsaysay Award
In 1979, MOTHER TERESA was awarded the Nobel Peace Prize, for work undertaken in the struggle to overcome poverty and distress
In 1980, MOTHER TERESA was awarded the Bharat Ratna Prize  


Log Directory: /usr/src/perl_test/log/
log_<time_stamp>.log