Friday, July 13, 2012

Explain the difference between a static library and a dynamic library.


Static libraries are loaded when the program is compiled and dynamically-linked libraries are loaded in while the program is running. Dynamic libraries reduces the overall footprint of memory as against the static library because linking to static libraries includes the actual code for the library function(s)/procedure(s) with the executable. Dynamic Libraries are kept at specific location and are usually shared among all the processes that use.

Tuesday, July 3, 2012

How do you find out available DSNs?


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

use DBI;

foreach my $dsn ( DBI->data_sources('ODBC') )
{
print "$dsn\n";
}

How do you find out DBI drivers installed?


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

use DBI;

my @drivers = DBI->available_drivers;
print join("\n", @drivers);

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'`;