Tuesday 29 July 2014

PS_13_Perl - Database Access in Perl

Database Access in Perl

Database Access
There are two primary means of accessing databases under Perl. The first (and oldest) makes use of the DBM (Database Management) libraries available for most flavors of UNIX. The second (and more powerful) allows for a platform-independent interaction with more sophisticated database management systems (DBMS’s) such as Oracle, Sybase, Informix, and MySQL.

DBM
A DBM is a simple database management facility for most UNIX systems. It allows programs to store a collection of key-value pairs in binary form, thus providing rudimentary database support for Perl. To use DBM databases in Perl, you can associate a hash with a DBM database through a process similar to opening a file:

use DB_File;
tie(%ARRAYNAME, “DB_File”, “dbmfilename”);

Once the database is opened, anything you do to the hash is immediately written to the database. To break the association of the hash with the file, use the untie() function.

DBI/DBD
DBI is a module that provides a consistent interface for interaction with database solutions. The DBI approach relies on database-specific drivers (DBD’s) to translate the DBI calls as needed for each database. Further, actual manipulation of the contents of the database is performed by composing statements in Structured Query Language (SQL) and submitting them to the database server. DBI methods make use of two different types of handles

1. Database handles (like filehandles)
2. Statement handles (provide means of executing statements and manipulating their results)

Database handles are created by the connect() method:

$db_handle = DBI->connect(‘DBI:mysql:dbname:hostname’,
$username, $password);
and destroyed by the disconnect() method:
$result = $db_handle->disconnect();

The first argument to the connect() method is a string describing the data source, typically written in the form:
DBI:driver_name:database_name:host_name

Statement handles are created by the prepare() method
$st_handle = $db_handle->prepare($sql)
where $sql is a valid SQL statement, and “destroyed” using the finish() method.

The SQL statement is then executed using the execute() method
$result = $st_handle->execute();
and the results obtained using any of the fetch() methods:
@ary = $st_handle->fetchrow_array(); # fetch a single row of the
# query results
$hashref = $st_handle->fetchrow_hashref();
%hash = %$hashref;

Note that you do not directly access the results the SQL statement, but obtain them one row at a time via the fetch() methods.

The following script connects to a MySQL database and prints the contents of one of its tables:
use DBI:
use strict:
my($dsn) = ‘DBI:mysql:test:localhost’; # Data source name
my($username) = ‘user’; # User name
my($password) = ‘secret’; # Password
my($dbh,$sth); # Database and statement handles
my(@ary); # array for rows returned by query
# connect to database
$dbh = DBI->connect($dsn, $username, $password);
# issue query
$sth = $dbh->prepare(‘SELECT * FROM tablename’);
$sth->execute();
# read results of query, then clean up
while(@ary = $sth->fetchrow_array()) {
print join(“\t”, @ary), “\n”;
}
$sth->finish();
$dbh->disconnect();

Thursday 24 July 2014

Bash Script for bulk user creation

 Bash Script for bulk user creation

By using this script you can create users interactively by your choice, from a file and even you can do modifications for a user such as change password, home directory, group and restrict login.

This code has been moved to GITHUB and you can access the code from the below link.

GitHub Link : Bulk User Creation


Sample file for user creation using file:

 adam:Adam_123:testgroup:/home/adam  
 shrikant:Shri_123:root:/uhome/shrikant  
 bob:Bob_1234:testgroup:/home/bob  
 hemant:Hem_123:testgroup:/home/hemant  
 sharat:Shar_123:testgroup:/home/sharat  
 prakash:Prak_123:testgroup:/home/prakash  
 scary:Scary_123:root:/uhome/scary  
 omkar:Omkar_123:root:/uhome/omkar  
 india:India_123:root:/uhome/india  
 bhagat:Bhagat_123:root:/uhome/bhagat  
 linux:Linux_123:root:/uhome/linux  



Monday 14 July 2014

PS_12_Perl - Common Gateway Interfaces

Common Gateway Interfaces

Perl is the most commonly used language for CGI programming on the World Wide Web. The Common Gateway Interface (CGI) is an essential tool for creating and managing comprehensive websites. With CGI, you can write scripts that create interactive, user-driven applications.

Simple CGI Programs
CGI programs are invoked by accessing a URL (uniform resource locator) describing their coordinates:
http://www.mycompany.com/cgi-bin/program.plx

even though the actual location of the script on the hard drive of the server might be something like:
c:\webserver\bin\program.plx

The simplest CGI programs merely write HTML data to STDOUT (which is then
displayed by your browser):
print << ENDOFTEXT;
Content-type: text/html
<HTML>
<HEAD><TITLE>Hello, World!</TITLE></HEAD>
<BODY>
<H1>Hello, World!</H1>
</BODY>
</HTML>
ENDOFTEXT
CGI.pm

CGI.pm is a Perl module written to facilitate CGI programming. It contains within itself the wherewithal to generate HTML, parse arguments and environment variables, and maintain state over multiple transactions. Another feature which is not to be underestimated is the ability to reliably run CGI programs from the command line for the purposes of debugging. A simple example of the CGI.pm module in action is shown below:
use CGI;
$query = CGI::new(); # create CGI object
$bday = $query->param(“birthday”); # get parameters
print $query->header(); # generate Content-type line
print $query->p(“Your birthday is $bday”);

Passing Parameters
CGI programs really shine, however, when they take arguments and format their output depending on those arguments. To pass arguments to a CGI script, ampersand-delimited key-value pairs to the URL:
http://www.mycompany.com/cgi-bin/icecream.plx?flavor=mint&container=cone

Everything after the question mark is an argument to the script. Environment variables provide the script with data about the server, client, and the arguments to the script. The latter are available as the “QUERY_STRING” environment variable. The following example prints all of the environment variables:
# Print all of the necessary header info
print <<ENDOFTEXT;
Content-type: text/html
<HTML>
<HEAD><TITLE>Environment Variables</TITLE></HEAD>
<BODY>
<CENTER><H1>Environment Variables</H1></CENTER>
<TABLE>
<TR><TH>Variable</TH><TH>Value</TH>
ENDOFTEXT
# Now loop over and format environment variables
foreach $key (sort keys %ENV) {
print "<TR><TD>$key</TD><TD>$ENV{$key}</TD></TR>\n";
}
print "</TABLE></BODY></HTML>\n";

CGI.pm is particularly good at extracting parameters in a platform-independent way:
use CGI;
$query = CGI::new();
print $query->header();
print $query->start_html(-title=>'Form Parameters');
print $query->h1('Form Parameters');
foreach $name ( $query->param() ) {
$value = $query->param($name);
print "$name => $value\n";
print $query->br(); # Insert a line break
}
print $query->end_html();

Database Access
There are two primary means of accessing databases under Perl. The first (and oldest) makes use of the DBM (Database Management) libraries available for most flavors of UNIX. The second (and more powerful) allows for a platform-independent interaction with more sophisticated database management systems (DBMS’s) such as Oracle, Sybase, Informix, and MySQL.

DBM
A DBM is a simple database management facility for most UNIX systems. It allows programs to store a collection of key-value pairs in binary form, thus providing rudimentary database support for Perl. To use DBM databases in Perl, you can associate a hash with a DBM database through a process similar to opening a file:
use DB_File;
tie(%ARRAYNAME, “DB_File”, “dbmfilename”);
Once the database is opened, anything you do to the hash is immediately written to the database. To break the association of the hash with the file, use the untie() function.

DBI/DBD
DBI is a module that provides a consistent interface for interaction with database solutions. The DBI approach relies on database-specific drivers (DBD’s) to translate the DBI calls as needed for each database. Further, actual manipulation of the contents of the database is performed by composing statements in Structured Query Language (SQL) and submitting them to the database server.
DBI methods make use of two different types of handles
1. Database handles (like filehandles)
2. Statement handles (provide means of executing statements and manipulating their results)

Database handles are created by the connect() method:
$db_handle = DBI->connect(‘DBI:mysql:dbname:hostname’,
$username, $password);
and destroyed by the disconnect() method:
$result = $db_handle->disconnect();

The first argument to the connect() method is a string describing the data source, typically written in the form:
DBI:driver_name:database_name:host_name

Statement handles are created by the prepare() method
$st_handle = $db_handle->prepare($sql)
where $sql is a valid SQL statement, and “destroyed” using the finish() method.

The SQL statement is then executed using the execute() method
$result = $st_handle->execute();
and the results obtained using any of the fetch() methods:
@ary = $st_handle->fetchrow_array(); # fetch a single row of the
# query results
$hashref = $st_handle->fetchrow_hashref();
%hash = %$hashref;
Note that you do not directly access the results the SQL statement, but obtain them one row at a time via the fetch() methods. The following script connects to a MySQL database and prints the contents of one of its tables:
use DBI:
use strict:
my($dsn) = ‘DBI:mysql:test:localhost’; # Data source name
my($username) = ‘user’; # User name
my($password) = ‘secret’; # Password
my($dbh,$sth); # Database and statement handles
my(@ary); # array for rows returned by query
# connect to database
$dbh = DBI->connect($dsn, $username, $password);
# issue query
$sth = $dbh->prepare(‘SELECT * FROM tablename’);
$sth->execute();
# read results of query, then clean up
while(@ary = $sth->fetchrow_array()) {
print join(“\t”, @ary), “\n”;
}
$sth->finish();
$dbh->disconnect();

Thursday 10 July 2014

PS_11_Perl - Object-Oriented Perl

Object-Oriented Perl

In Perl, modules and object-oriented programming go hand in hand. Not all modules are written in an object-oriented fashion, but most are. A couple of definitions are warranted here:

· An object is simply a referenced thingy that happens to know which class it
belongs to.

· A class is simply a package that happens to provide methods to deal with objects.

· A method is simply a subroutine that expects an object reference (or a package name, for class methods) as its first argument.

To create an object (or instance of a class), use the class constructor. Usually the class constructor will be a function named “new,” but may be called “Create” for some Win32 modules. For example,
$tri = new Triangle::Right (side1=>3, side2=>4);

The constructor takes a list of arguments describing the properties of the object to be created (see the documentation of the module in question to determine what these should be) and returns a reference to the created object.

An example of a class constructor (internal to the module) is shown below:
package critter; # declare the name of the package
sub new {
my $class = shift; # Get class name
my $self = {}; # Initialize the object to nothing
bless $self, $class; # Declare object to be part of class
$self->_initialize();# Do other initializations
return $self;
}

Methods (subroutines expecting an object reference as their first argument) may be invoked in two ways:
Packagename->constructor(args)->method_name(args)

Or:
$object = Packagename->constructor(args);
$object->method_name(args);
Methods are simply declared subroutines in the package source file.

Saturday 5 July 2014

PS_10_Perl - Modules

Modules

Namespaces and Packages:
Namespaces store identifiers for a package, including variables, subroutines, filehandles, and formats, so that they are distinct from those of another package. The default namespace for the body of any Perl program is main. You can refer to the variables from another package by “qualifying” them with the package name. To do this, place the name of the package followed by two colons before the identifier’s name:
$Package::varname
If the package name is null, the main package is assumed.

Modules:
Modules are Perl’s answer to software packages. They extend the functionality of core Perl with additional compiled code and scripts. To make use of a package (if it’s installed on your system), call the use function:
use CGI;

This will pull in the module’s subroutines and variables at compile time. use can also take a list of strings naming entities to be imported from the module:
use Module qw(const1 const2 func1 func2);

Perl looks for modules by searching the directories listed in @INC. Modules can be obtained from the Comprehensive Perl Archive Network (CPAN) at

http://www.cpan.org/modules/

or from the ActiveState site:

http://www.ActiveState.com/packages/zips/

To install modules under UNIX, unarchive the file containing the package, change into its directory and type:
perl Makefile.PL
make
make install

On Windows, the ActivePerl distribution makes use of the “Perl Package Manager” to install/remove/update packages. To install a package, run ppm on the .ppd file associated with the module:
ppm install module.ppd

Tuesday 1 July 2014

PS_09_Perl - Files and I/O

Files and I/O

Filehandles:
A filehandle is the name for the connection between your Perl program and the operating system. Filehandles follow the same naming conventions as labels, and occupy their own namespace.

Every Perl program has three filehandles that are automatically opened for it: STDIN,
STDOUT, and STDERR:
STDIN Standard input (keyboard or file)
STDOUT Standard output (print and write send output here)
STDERR Standard error (channel for diagnostic output)

Filehandles are created using the open() function:
open(FILE,”filename”);
You can open files for reading, writing, or appending:
open(FILE,”> newout.dat”) Writing, creating a new file
open(FILE,”>> oldout.dat”) Appending to existing file
open(FILE,”< input.dat”) Reading from existing file

As an aside, under Windows, there are a number of ways to refer to the full path to a file:
c:\\temp\\file” Escape the backslash in double quotes
c:\temp\file’ Use proper path in single quotes
c:/temp/file” UNIX-style forward slashes

It is important to realize that calls to the open() function are not always successful. Perl will not (necessarily) complain about using a filehandle created from a failed open().

This is why we test the condition of the open statement:
open(F,”< badfile.dat”) or die “open: $!”

You may wish to test for the existence of a file or for certain properties before opening it.

Fortunately, there are a number of file test operators available:
File test Meaning
-e file File or directory exists
-T file File is a text file
-w file File is writable
-r file File is readable
-s file File exists and has nonzero length

These operators are usually used in a conditional expression:
if (-e myfile.dat) {
open(FILE,”< myfile.dat”) or die “open: $!\n”;
}
Even more information can be obtained about a given file using the stat() function.

Using filehandles
After a file has been opened for reading you can read from it using the diamond operator just as you have already done for STDIN:
$_ = <FILE>; or
while (<FILE>) {
statements;
}

To print to your open output file, use the filehandle as the first argument to the print statement (N.B. no commas between the filehandle and the string to print).
print FILE “Look Ma! No hands!\n”;

To change the default output filehandle from STDOUT to another one, use select:
select FILE;

From this point on, all calls to print or write without a filehandle argument will result in output being directed to the selected filehandle. Any special variables related to output will also then apply to this new default. To change the default back to STDOUT, select it:
select STDOUT;

When you are finished using a filehandle, close it using close():
close(FILE);

Formats:
Perl has a fairly sophisticated mechanism for generating formatted output. This involves using pictorial representations of the output, called templates, and the function write.
Using a format consists of three operations:
1. Defining the format (template)
2. Loading data to be printed into the variable portions of the format
3. Invoking the format.
Format templates have the following general form:
format FORMATNAME =
fieldline
$value_one, $value_two, …

Everything between the “=” and the “.” is considered part of the format and everything (in the fieldlines) will be printed exactly as it appears (whitespace counts). Fieldlines permit variable interpolation via fieldholders:

Hi, my name is @<<<<<, and I’m @< years old. <-- Fieldline
$name, $age <-- Valueline
Fieldholders generally begin with a @ and consist of characters indicated alignment/type.
@<<< Four character, left-justified field
@>>> Four character, right-justified field
@||| Four character, centered field
@###.## Six character numeric field, with two decimal places
@* Multi-line field (on line by itself – for blocks of text)
^<<<< Five character, “filled” field (“chews up” associated variables)

The name of the format corresponds to the name of a filehandle. If write is invoked on a filehandle, then the corresponding format is used. Naturally then, if you’re printing to standard output, then your format name should be STDOUT. If you want to use a format with a name other than that of your desired filehandle, set the $~ variable to the format name.

There are special formats which are printed at the top of the page. If the active format name is FNAME, then the “top” format name is FNAME_TOP. The special variable $% keeps a count of how many times the “top” format has been called and can be used to number pages.

Manipulating files & directories:
The action of opening a file for writing creates it. Perl also provides functions to
manipulate files without having to ask the operating system to do it.
unlink(filename)

Delete an existing file. Unlink can take a list of files, or wildcard as an argument as well: unlink(<*.bak>)
rename(oldname, newname)

This function renames a file. It is possible to move files into other directories by specifying a path as part of the new name. Directories also have some special function associated with them
mkdir(dirname, mode)

Create a new directory. The “mode” specifies the permissions (set this to 0777 to be safe).
rmdir(dirname)

Removes (empty) directories
chdir(dirname)

Change current working directory to dirname, File and directory attributes can be modified as well:
chmod(permission, list of files)

Change the permissions of files or directories:
666 = read and write
444 = read only
777 = read, write, and executable
utime(atime, mtime, list of files)
Modify timestamps on files or directories. “atime” is the time of the most recent access, and “mtime” is the time the file/directory was last modified.