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