Monday, July 2, 2012

How could you write colorful text on console in Windows?


#!C:/Perl/bin/perl -w

use Win32::Console;

my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);

# Get current console colors
my $attr = $CONSOLE->Attr();

# Yellow text on green
$CONSOLE->Attr($FG_YELLOW | $BG_GREEN);
print "This is a test\n";

$CONSOLE->Attr($FG_GREEN | $BG_BLACK);
print "This is a test\n";

# Set console colors back to original
$CONSOLE->Attr($attr);

Sunday, June 24, 2012

Write function to get string between two specified words.

#!C:/Perl/bin/perl -w

my $string = "Hi, this is Rabindra Nayak! I work on Perl Scripting on UNIX platform.";

my $searchText = getText( $string, 'on', 'on' );

print $searchText;

sub getText
{
        return $1 if ( $_[0] =~ /$_[1] (.*?) $_[2]/ );
}

Output : Perl Scripting

Saturday, June 23, 2012

Run Crosstable queries for MS Access in Perl.


#!C:/Perl/bin/perl -w

use DBI;

my $DIR_PATH = 'C:/Perl/';
my $DBASE_NM = 'AccessDB.accdb';
my $PRI_TABLE_NM = "EMPLOYEE";
my $SEC_TABLE_NM = "AGE";

my $dsn = 'dbi:ODBC:driver=Microsoft Access Driver (*.mdb, *.accdb);dbq=' . $DIR_PATH . $DBASE_NM;
my $dbh = DBI->connect($dsn,undef,undef) || die "Can't reach database: $DBI::errstr";

my $sql= "SELECT PTN.EMPLOYEE_ID, PTN.EMPLOYEE_NM,
                        STN.EMPLOYEE_AGE, STN.EMPLOYEE_GNDR
                        FROM $PRI_TABLE_NM PTN INNER JOIN $SEC_TABLE_NM STN
                        ON PTN.EMPLOYEE_ID = STN.EMPLOYEE_ID";

$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement: $!";

my $COL_NM = join(',', @{$sth->{NAME_lc}});
print "$COL_NM\n";

while (@row=$sth->fetchrow_array)
{
   print join(",", @row), "\n";
}

$sth->finish;
$dbh->disconnect;

Friday, June 22, 2012

How do you display resultset with column names of the table?

#!C:/Perl/bin/perl -w

use DBI;

my $DIR_PATH = 'C:/Perl/';
my $DBASE_NM = 'AccessDB.accdb';
my $TABLE_NM = "EMPLOYEE";

my $dsn = 'dbi:ODBC:driver=Microsoft Access Driver (*.mdb, *.accdb);dbq=' . $DIR_PATH . $DBASE_NM;
my $dbh = DBI->connect($dsn,undef,undef) || die "Can't reach database: $DBI::errstr";

my $sql = "SELECT * FROM $TABLE_NM";
# my $sql = "SELECT EMPLOYEE_ID, EMPLOYEE_NM FROM $TABLE_NM WHERE ACTIVE_IND='N'";

$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement: DBI::errstr";

my $COL_NM = join(',', @{$sth->{NAME_uc}});
print "$COL_NM\n";

while (@row=$sth->fetchrow_array)
{
        print join(", ", @row), "\n";
}

$sth->finish();
$dbh->disconnect();

How do you connect Perl and MSAccess via ODBC?

#!C:/Perl/bin/perl -w

use DBI;

my $DIR_PATH = 'C:/Perl/';
my $DBASE_NM = 'AccessDB.accdb';
my $TABLE_NM = "EMPLOYEE";

my $dsn = 'dbi:ODBC:driver=Microsoft Access Driver (*.mdb, *.accdb);dbq=' . $DIR_PATH . $DBASE_NM;

my $dbh = DBI->connect($dsn,undef,undef) || die "Can't reach database: $DBI::errstr";

my $sql = "SELECT * FROM $TABLE_NM";
# my $sql = "SELECT * FROM $TABLE_NM WHERE ACTIVE_IND='N'";

$sth = $dbh->prepare($sql);
$sth->execute || die "Could not execute SQL statement: DBI::errstr";

while (@row=$sth->fetchrow_array())
{
   print "@row\n";
}

$sth->finish;
$dbh->disconnect;

*Note : This script can be used to execute simple database queries without any database installed on local machine.

Thursday, June 21, 2012

What are the various ways to execute external commands in Perl on UNIX?

There are many ways to run external commands from Perl as mentioned below:

system() - you want to execute a command and do not want to capture its output
exec() - you do not want to return to the calling perl script
backticks (``) - you want to capture the output of the command
open() - you want to pipe the command (as input or output) to your perl script

Tuesday, June 12, 2012

How do you change the shell within Perl Script?

To force the shell to csh/bash in Perl, try

`/bin/csh -c "COMMAND_NAME"`; 

or

$OUT = `/bin/bash c 'ONE=1; echo \$ONE'`;

Friday, May 11, 2012

Convert Comma-Delimited File to Pipe-Delimited File

#!/usr/bin/perl

use File::Basename qw(basename);

$me = basename ($0);

if ( $#ARGV < 0 )
{
 print "Usage : $me <CSV File Name>\n";
 exit;
}

my $IN_CSV = shift @ARGV;
my $OUT_PIPE = $IN_CSV;
$OUT_PIPE=~s/\.csv/\.txt/g;


open (OUTPUT, ">$OUT_PIPE") || die "Can not open $OUT_PIPE - $!" ;
open (INPUT, "<$IN_CSV") || die "Can not open $IN_CSV - $!" ;

while (<INPUT>)
{
 $_=~ s/\,/\|/g;
 print OUTPUT $_;
}

close INPUT;
close OUTPUT;

What is "Here-Document"?


The Unix shell has got a facility "here-document" which allows you to put input under a command, instead of in a separate text file and feed it into the program.
This is done by placing a "<<" and a character string after the command.  Then, every line after the command is interpreting as free-form text to be fed into the command, until a line is hit that has the character string.
For example,
cat <<!
This is an example of text taken literally except that variables are expanded where their variable names appear.
!
would behave exactly like
cat < file
where the file would contain:
This is an example of text taken literally except that variables are expanded where their variable names appear.

Thursday, May 10, 2012

What is the difference between "Print 'Hi $First_Name';" and "Print "Hi $First_Name";"?

The first statement in single quotes will be treated as a constatnt string. The value assigned to the variable would not be the outcome. Rather the output would be :

Hi $First_Name

On the other hand, the second statement would replace the variable with the actual value and the output would be :

Hi <Value Assigned to the variable>