Tag Archives: Uncategorized

Perl DBI AutoCommit handling for transaction safe Database

AutoCommit Option

If your transactions are simple, you can save yourself the trouble of having to issue a lot of commits. When you make the connect call, you can specify an AutoCommit option which will perform an automatic commit operation after every successful query. Here’s what it looks like:

my $dbh = DBI->connect($dsn, $userid, $password,{AutoCommit => 1}) 
              or die $DBI::errstr;

Here AutoCommit can take value 1 or 0, where 1 means AutoCommit is on and 0 means AutoCommit is off.
In normal we use “AutoCommit in on mode“. But in the case of Insertion and updation for a transaction safe DB we have to make “AutoCommit in off mode” temporarily. Currently most of us try to write separate function as follows:

my $dbh1 = DBI->connect($dsn, $userid, $password,{AutoCommit => 0}) 
              or die $DBI::errstr;

 Actually we don’t need to write such a thing. we can handle it with

$dbh->begin_work

Begin Transaction

Many databases support transactions. This means that you can make a whole bunch of queries which would modify the databases, but none of the changes are actually made. Then at the end you issue the special SQL query COMMIT, and all the changes are made simultaneously. Alternatively, you can issue the query ROLLBACK, in which case all the changes are thrown away and database remains unchanged.

Perl DBI module provided begin_work API, which enables transactions (by turning AutoCommit off) until the next call to commit or rollback. After the next commit or rollback, AutoCommit will automatically be turned on again.

$rc  = $dbh->begin_work  or die $dbh->errstr;

COMMIT Operation

Commit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted to its orignal position.

Here is a simple example to call commit API.

$dbh->commit or die $dbh->errstr;

ROLLBACK Operation

If you are not satisfied with all the changes or you encounter an error in between of any operation , you can revert those changes to use rollback API.

Here is a simple example to call rollback API.

$dbh->rollback or die $dbh->errstr;

Today’s Date in Perl in MM/DD/YYYY format

It can be done easily as :

use POSIX qw(strftime);

my $date = strftime "%m/%d/%Y", localtime;
print $date;

How to create multidimensional arrays in Perl?

To make an array of arrays, or more accurately an array of arrayrefs, try something like this:

my @array = ();
foreach my $i ( 0 .. 10 ) {
  foreach my $j ( 0 .. 10 ) {
    push @{ $array[$i] }, $j;
  }
}

It pushes the value onto a dereferenced arrayref for you. You should be able to access an entry like this:

print $array[3][2];

Zooming image with jquery

Check the following links to see how it work

To download it please click the below links

Progress bars for file uploads and avoiding temp files

CGI.pm gives you low-level access to file upload management through a file upload hook. You can use this feature to completely turn off the temp file storage of file uploads, or potentially write your own file upload progress meter.

This is much like the UPLOAD_HOOK facility available in Apache::Request, with the exception that the first argument to the callback is an Apache::Upload object, here it’s the remote filename.

$q = CGI->new(\&hook [,$data [,$use_tempfile]]);
sub hook {
my ($filename, $buffer, $bytes_read, $data) = @_;
print “Read $bytes_read bytes of $filename\n”;
}

The $data field is optional; it lets you pass configuration information (e.g. a database handle) to your hook callback.

The $use_tempfile field is a flag that lets you turn on and off CGI.pm‘s use of a temporary disk-based file during file upload. If you set this to a FALSE value (default true) then $q->param(‘uploaded_file’) will no longer work, and the only way to get at the uploaded data is via the hook you provide.

If using the function-oriented interface, call the CGI::upload_hook() method before calling param() or any other CGI functions:

CGI::upload_hook(\&hook [,$data [,$use_tempfile]]);

This method is not exported by default. You will have to import it explicitly if you wish to use it without the CGI:: prefix.