Tuesday, August 7, 2012

Setting Environment variable

# Function Name    : setEnv
# Input            : key and value
# Output        : none
# Description    : This funtion will set environment variable for current and child process
# Created By    : Rinkesh Bansal
#
sub setEnv($$)
{
    my ($key, $value) = @_;
    my $delimiter;
    my $os = $^O;
    if ($os eq "MSWin32")
    {
        $delimiter = ";";
    }
    else
    {
        $delimiter = ":";
    }

    $ENV{$key}=$ENV{$key} . $delimiter . $value;
}

Monday, August 6, 2012

Calculating md5dum of all files in a folder

# Function Name : md5sum_dir
# Input            : Path of folder
# Output        : none
# Description    : This function will recursively check md5sum of all files under passed folder
# Created By    : Rinkesh Bansal
#
sub md5sum_dir ($)
{
    my $path = shift;

    opendir (DIR, $path) or die "Unable to open $path: $!";
    my @files = map {$path . '/' . $_ }grep { !/^\.{1,2}$/ } readdir (DIR);
    closedir (DIR);

    foreach my $file (@files)
    {
        if (-d $file)
        {
            &md5sum_dir ($file);
        }
        else
        {
            my $md = &md5sum($file); # you can find this subroutine in my other blogs
            open (FO, ">>$result_file") or die ("Error :: Couldn't open file ($result_file) for writing\n");
            print FO "$file\t$md\n";
            close(FO);
        }
    }
}

Calculating md5sum of a given file

use Digest::MD5; # make sure to have this package added in your perl script

# Function Name : md5sum
# Input            : File for which md5sum is requested
# Output        : md5sum of the file
# Description    : This function will calculate the md5sum (checksum) of the file and return it.
# Created By    : Rinkesh Bansal
#
sub md5sum($)
{
    my $file = shift;
    die "Error::File ($file) does not exist!!!"    if (not -e $file);
    open (FO, $file);
    binmode(FO);
    my $md5 = Digest::MD5->new;
    while ()
    {
        $md5->add($_);
    }
    close(FO);
    my $checksum = $md5->hexdigest;
#    print "$checksum $file\n";
    return $checksum;
}

Clearing a screen

# Function Name    : clear_scr
# Input            : none
# Output        : none
# Description    : This funtion will clear the screen
# Created By    : Rinkesh Bansal
#
sub clear_scr()
{
    my $os = $^O;
    if ($os eq "MSWin32")
    {
        system 'cls';
    }
    else
    {
        system 'clear';
    }
}

copying a file

use File::Copy; # include this perl module in your program to make this function work

# Function Name : copyFile
# Input            : Source & Destination Location
# Output        : none
# Description    : This function will copy given source file to destination location.
# Created By    : Rinkesh Bansal
#
sub copyFile($$)
{
    my ($source, $target) = @_;
    if (-e $source)
    {
        my $targetdir;
        if (substr($target, -1) eq "/")
        {
            $targetdir = $target;
        }
        else
        {
            $targetdir = substr ($target, 0, rindex($target, "/"));
        }

        copy ($source, $target);
    }
}

logger functionality to create backup of files

# Function Name : backup_result_file
# Input            : Number of backups, Name of the result file
# Output        : none
# Description    : This function will create backup of result file. Max number of backups handle are passed as first parameter
# Created By    : Rinkesh Bansal
#
sub backup_result_file($$)
{
    my ($num_of_backup, $result_file) = @_;

    for (my $i = $num_of_backup - 2; $i > 0; $i--)
    {
        my $temp = $i + 1;
        my $source = "${result_file}.${i}";
        my $target = "${result_file}.${temp}";
        &copyFile($source, $target);
    }
    my $source = "${result_file}";
    my $target = "${result_file}.1";
    &copyFile($source, $target);
    unlink($source);
}