Friday, August 17, 2007

PERL - Programming

PERL - User Input

PERL is capable of quickly taking input from a user then manipulating it in some fashion and spitting out some sort of result. Whether the task at hand is complex file manipulation or just a simple conversion script, your script may require some sort of user-input. With PERL this input comes from the command prompt or MS-DOS on a windows machine.

In a minute we will be taking a look at how to retrieve some user input via the command prompt but first, you may need to install a PERL compiler for your machine.

ActivePERL - PERL for Windows

PERL comes pre-installed on many operating systems, like Linux for example. For those of us running a version of Windows however, we must download and install some sort of program that allows us to access PERL scripts from DOS. There are many PERL compilations available for your machine. A simple Google search yields several million results.

ActivePERL is the simplest of installers available and it is also free to download. Download the program, follow the onscreen installation guide, and you should have an active installation of PERL ready to go.

To test the installation, make sure the program has fully completed installation and then run through the following:

  1. Click on Start, go to Run.
  2. Enter Command or cmd into the display box.
  3. At the command prompt type perl -v.

You should get some documentation about Larry Wall 1987-200X, and that means you are officially done installing PERL and ready to start creating some PERL scripts.

PERL - Testing...Testing Script

Just to be sure everything is running correctly. Open up notepad or your preferred simple text editor and let's make a beginning script. We'll have our script print to the command prompt with the print function just as we would if we were printing text to our web browser.

testingtesting.pl:

#! usr/bin/perl
print "Hello PERL!";

Copy the two lines above and save them in a directory that you are capable of accessing through DOS. Locate that directory in your DOS prompt and simply type the name of your PERL script into the command prompt to execute your script.

testingtesting.pl:

C:\>testingtesting.pl

Hello PERL!:

Screen Capture of Command Prompt

Upon execution, the script should print the words "Hello PERL!" at the bottom of your command prompt.

You should now have some basic concept of how to manage and run PERL scripts from the command prompt of your machine. Next we will be taking a look at how to manipulate user input via the command prompt.

Informix Dynamic Server Enterprise Edition

Informix® Dynamic Server Enterprise Edition builds on IDS Workgroup Edition with replication solutions and access control

Create a failsafe, multi-site global availability plan while maximizing IT investment, combining capabilities included in IDS Enterprise Edition

  • The optional Continuous Availability Feature also enables you to build a cluster of IDS instances around a single set of shared storage devices. Properly written applications can easily leverage this architecture for load-balancing or practically uninterrupted data services even in the event that one or more servers fail
  • The new Advanced Access Control Feature offers cell-, column- and row-level label-based access control (LBAC). This means that access to data can now be controlled down to an individual cell of information, providing greater security for your critical data and sensitive customer information
  • Quote: "Because reliability is our top priority, any outage to our network applications can hinder our ability to respond to our customers. The innovations of the new continuous availability feature of IDS 11 will assure Verizon's ability to provide uninterrupted service to our customers, day and night." —Geoff Poole, Lead Informix Database Administrator, Verizon
BM Informix® Dynamic Server (IDS) is an exceptional online transaction processing (OLTP) database that offers outstanding performance, reliability, scalability and manageability for enterprise and workgroup computing.

With a full-featured relational database management system (RDBMS) platform, IDS offers extensive capabilities to meet unique business requirements with the speed of native database functions. In addition, IDS delivers:

  • Solid, secure and reliable support for on demand environments

  • Enhanced performance, availability, and database administrator and programmer productivity

  • Reduced deployment and management costs. Customize the data server footprint with the Deployment Wizard and the SQL API and scheduler make it even easier to automate maintenance activities.

  • The GUI-based OpenAdmin Tool for IDS provides a global view of remote servers, with flexible analysis and drill-down to the query level.

  • High security for compliance with government regulations

  • Support for integrated development environments (IDEs) such as Eclipse, Java and .NET, and your choice of languages such as PHP and Ruby, and other traditional language choices.

  • Easy delivery of location information to web and SOA applications with the new Web Feature Service API.

Benefits
0

  • Lower total cost of ownership (TCO) - Many typical database administrator operations are self-managed by the IDS database, making it near hands free. Administration activities can also be controlled within an application via the SQL API. Typically, IDS customers report using one-third or less of the staff needed to manage competitors' products.

  • Shortened development cycles due to rapid deployment capabilities and choice of application development environments and languages.

  • Flexible choices for business continuity with replication choices and the Continuous Availability Feature for shared disk cluster solutions, so you don't have to rely on a "one-size-fits-all" solution.

  • Continuous availability.

  • Fully integrated business logic through IBM´s parallel-everything Dynamic Scalable Architecture (DSA). The logic runs where the data is stored, dramatically increasing the performance and efficiency of customer applications.

  • Uniform access to information, regardless of where it resides - via leading distributed database and virtual table capabilities.

Advantages for solving market problems
0

  • Replication capabilities enable customers to link stores to distribution centers, distribution centers to corporate headquarters, or any information requiring reliable and quick dissemination across a global organization.

  • The Continuous Availability Feature offers significant cost savings with support for cluster solutions, providing scalability to meet growing business demands and failover recovery from any server to ensure continuous business operations. It provides the ability for a secondary server to automatically take over in the case of a system failure, accessing the same data disk.

  • Through the use of IBM Informix DataBlade™ technology, the capabilities of the database can be extended to meet specific organizational requirements. All types of data can be managed including text, images, sound, video, time series, and spatial data. You can develop applications that use this technology to gain competitive advantage in ways not previously possible or practical, and save costs.

  • IDS is fully integrated with Web technology so that data can be easily accessed from the Internet or from the company intranet. This allows you to quickly build on demand applications that will provide competitive advantage, greater customer service, and reduced costs.

  • IDS supports a broad range of information management applications with excellent performance and reliability.

Thursday, August 16, 2007

PERL - Deleting Files

Use the unlink function to delete specific files from your web server. The best solution is often to set a variable name equal to the URL of the file you wish to delete.
PERL Code:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #The header
$file = "newtext.txt";
if (unlink($file) == 0) {
print "File deleted successfully.";
} else {
print "File was not deleted.";
}

deletefiles.pl:
File deleted successfully.
PERL - Removing Multiple Files at Once

Multiple files can be removed at once if we first create an array of files to be deleted and then loop through each one. There are several ways to go about this process.
PERL Code:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #The header
@files = ("newtext.txt","moretext.txt","yetmoretext.txt");
foreach $file (@files) {
unlink($file);
}

Beginner's Guide to CGI Scripting with Perl

Introduction

This page is designed to help novice programmers learn the Perl programming language. Specifically, it's designed to help them learn enough to run CGI scripts on a Unix Web server.

This page grows out of my own experience. When I started out on the Web I was new to Unix, and had no formal training as a programmer. I wanted to create dynamic pages for my Web site, though, and everyone said Perl was the way to go. They were right: It was the way to go. It sounds trite to say that "Perl changed my life," but that's basically what happened.

Still, it was an uphill battle. In particular, many of the resources for learning Perl seemed to assume that I was already an experienced programmer, or at least an experienced Unix user. I've made some assumptions in these pages, but not those assumptions.

Assumptions I do make:

  • You're reasonably intelligent.
  • You're an experienced computer user, of the Windows (or possibly Mac) persuasion.
  • You know, or are willing to learn elsewhere, the basics of HTML, including things like and tags.
  • You have, or are willing to get, an Internet account that lets you log into a Unix shell session, and install and run your own CGI scripts.

Before you get started, I'd like you to take a moment to consider the quotation at the top of this page. As a beginner, you'll find that the combination of Unix and Perl gives you more than enough rope to hang yourself. One of Perl's claims to fame is that it makes "easy things easy" - which it does, but among the things it makes easy are the accidental mangling of your files, or the opening up of gaping security holes on your Web server. So be careful. Or, as Perl's creator, Larry Wall, likes to say, "have the appropriate amount of fun."

Friday, August 10, 2007

Column-oriented data extraction

Perl is a terrific language for text processing, but several readers have written wondering about how to extract columns of data from text files with Perl.

For instance, when you have a text database that looks like this:

      simpson bart springfield
      flinstonefred bedrock
      rubble barney bedrock
what's the best way to extract the last name from each database record?

When every record is separated by a delimiter (such as a ',', ':', or '|' symbol) it's easy to use the split function. But in a case like this, where data is stored in fixed-width fields, and no field delimiter is used, how do you extract your data?

It's still not too hard

Although you can't jump right in and easily use the split function, the solution is almost as easy, though maybe not as obvious. To solve the "fixed-width field" problem, just use Perl's substr function.

Using our three line database above as an example, it appears that the fields of the database are defined like this:

    Field Number Field Description Field Width
    1 Last name 9
    2 First name 9
    3 City 11
Given these definitions, here's how I'd grab the data from each record from a file named database:
    open (IN, 'database');

    while () {

    chomp;

    $last_name = substr $_, 0, 9; # extract the last name field
    $first_name = substr $_, 9, 9; # extract the first name field
    $city = substr $_, 18, 11; # extract the city field

    # do something useful here with each value ...

    print "$first_name $last_name lives in $city\n";

    }

    close (IN);

The substr() function

As you can see from this code fragment, the substr function is an important part of the solution. The substr function lets you extract fields of information from strings. All you have to do is define three items:
    1. The name of the string
    2. The offset, or starting point
    3. The length of the substring to extract
In the example above, the name of the string we're extracting information from is $_, the default string when you're reading from a data file like this. This string holds the value of each record that we're reading from the database file.

The offset is really just the starting point of each field. In our sample database, the last_name field begins in column 1, the first_name field begins in column 10, and the city field begins in column 19. Given these values, the offset for each field is 0, 9, and 18, respectively. (Remember that Perl, like C, C++, Java, and other languages, begin counting at 0. Therefore, if a field starts in column 10, it's really going to be the 9th element of a string; columns 1-9 of the database will be stored in the string elements numbered 0 through 8.)

The length of each substring is also defined by the database. In our example, the lengths of the three fields are the same as the field widths, 9, 9, and 11, respectively.

How to concatenate strings with Perl

Many times when you're working with Perl you're working with strings. Oftentimes you need to concatenate strings. For instance, I recently had a need to create a temporary filename.

I was going to get the first part of the filename (the filename prefix) by calling a function that generated temporary filenames. After getting the first part of the filename, I wanted to prepend the directory "/tmp/" to the beginning of the name, and append the filename extension ".tmp" to the end of the name. "How can I do this easily?", I thought.

Method #1 - using Perl's dot operator

Concatenating strings in Perl is very easy. There are at least two ways to do it easily. One way to do it - the way I normally do it - is to use the "dot" operator (i.e., the decimal character).

The simple example shown below demonstrates how you can use the dot operator to merge, or concatenate, strings. For simplicity in this example, assume that the variable $name is assigned the value "checkbook". Next, you want to pre-pend the directory "/tmp" to the beginning of the filename, and the filename extension ".tmp" to the end of $name. Here's the code that creates the variable $filename:

$name = "checkbook";
$filename = "/tmp/" . $name . ".tmp";

#----------------------------------------------#
# $filename now contains "/tmp/checkbook.tmp" #
#----------------------------------------------#

Listing 1: Concatenating strings with the dot operator.


Method #2 - using Perl's join function

A second way to create the desired variable $filename is to use the join function. With the join function, you can merge as many strings together as you like, and you can specify what token you want to use to separate each string.

The code shown in Listing 2 below uses the join function to achieve the same result as the code shown in Listing 1:

$name = "checkbook";
$filename = join "", "/tmp/", $name, ".tmp";

#----------------------------------------------#
# $filename now contains "/tmp/checkbook.tmp" #
#----------------------------------------------#

Thursday, August 9, 2007