PERLS FROM WISDOMBAY

A technology Blog from the creator's of http://www.wisdombay.com . This Technoblog is meant to be a One Stop Shop for PERL Programmers. Here I am planning to keep a frequently updated journal filled with code, tutorial's and tips for PERL Lovers.

Tuesday, April 11, 2006

Some more blah blah on how to execute a Perl program

Welcome back !!.

The work is over for today and I thought a little bit of Perl class would be fine to get my tired mind back on track.

In Last class we talked about, hmm rather blogged about, how a perl program gets executed. In this class I will tell you the various methods by which a Perl program Can be executed (there are many way’s) and will talk a bit about command line options available for developers of Perl programs.

The various flavours of execution available for Perl programs

1. In windows machines, you can make the .pl extension registered with the perl interpreter, so that double clicking on a Perl source code file will make it execute. Installing Activestate’s Perl set’s up this automatically on your machine.
2. On your Unix flavored boxes, providing shebang statement (like #! /usr/bin/perl) in your code can make the Perl program to run as an executable provided that an executable permission is set for the file.
3. You can run Perl code on a windows machine by double clicking the perl interpreter executable and then typing your code in the resultant window. After typing in your Perl code, type CTRL + Z and an Enter key to execute the typed in Perl code. This can be done on your Unix/Linux machines also.
4. You can execute a Perl source code by typing perl followed by the source code name, like ‘perl mycode.pl’.
5. Execute single line code using ‘perl –e’ switch. The –e switch allows multiple commands given as a single line to be executed right from command line.

The format to be used on Windows machines for this is Perl –e “ command ”
ex : perl -e "print \"hello\";print \"\n Another line \" "

The above example prints

hello
Another line


In windows machines the command is to be enclosed in double quotes and any further double quotes that are to be inputted inside it are to be escaped using the ‘\’ (slash) character.

The format for Linux/Unix machine is perl –e ‘command’

ex : perl -e ‘print "hello";print "\n Another line" ‘

In Linux/Unix machines the command is to be enclosed in single quotes.


Command line utilization

Command line arguments or switches are little combination of characters which will tinker the way in which the Perl interpreter acts on a command or a script name. Switches are usually preceded by a '–' symbol. To see all the available command line options in your installed version of Perl, use the below command.

perl --help

You can use the switches as a combination or a as single units. For example if you have to specify two command line switches ‘V’ and ‘W’ in the same command you can use the below two ways.

perl –VW ‘file.pl’
or
perl –V -W ‘file.pl’


Surprise, Surprise – Perl does have a compiler

Even though perl lacked a compiler in it’s initial evolutionary stages, some where around the middle 90’s, the perl 5.005 version, started to incorporate a native compiler. This finally made perl programs free from the mercy of the must have presence of a perl interpreter at the target or client machine. Using this compiler, Perl scripts can be compiled into binary form and Software’s done on perl could be provided as standalone packages with out the need of an interpreter.

Read more on B – The Perl Compiler Here

So we will surely meet in our next class, and there we will be having a look at the practical side of Perl programming. We will start with the Basic’s of Perl language in the next session.

Sunday, April 09, 2006

How did it happen? How the output was derived from my Perl program

First of all today, I will tell you the classification of languages under which Perl falls. Perl is an interpretive language, as Perl uses an interpreter for processing a source code written using Perl’s syntax. Being an interpreted language have it’s own benefits and disadvantages. You can easily test out the code just as you are building it, this is one of the biggest gain of an interpreted language and Perl programmers can use this advantage to the maximum. A main drawback of interpreter driven languages are that they are just like a car or bike with out gas, when an interpreter is lacking. You can do nothing except stare and study such a code with out a proper interpreter.

Now we can have a sneak peek at what happened internally when we ran the command ‘perl first.pl’ in last session.

When this command is issued, Perl’s internal compiler scans through the whole source code in ‘first.pl’ and converts it into a ‘bytecode’ format. By the way a ByteCode is an internal data structure representing the source code. Now the Perl Bytecode engine executes this resultant bytecode and if there is a syntax error or some prob’s you are notified.


This is what happened internally, when we ran the command ‘Perl first.pl’ in the last session.

Friday, April 07, 2006

Let’s Wade in to the waters at the Bay, for Perl Hunting

Had a chocolate and some crispy vada's (an Indian version of doughnut - spicy and usually taken as a breakfast item) for an early breakfast today and thought about making a post in my Perl Guide .

Today, we can have a look at the intricate skill’s that are to be mastered for writing a very simple Perl program and successfully invoking the Perl interpreter for getting an output from the simple program we wrote.

About the basic structure of a Perl program and some last minute checks that we have to make

Before actually writing any code, I have to give some more of my blah, blah on basic Perl program structure and some musings on checking and tinkering the Perl Interpreter that we have installed on the machine in one of the last sessions.


The basic structure of a Perl program

1. Spaces or no spaces - For the Perl interpreter interspersing a lot of white spaces in your code, outside string literals (we will definitely find what a string literal is later), have no special meaning. You can put a lot of spaces between lines, expressions etc and they are not counted.
2. Every line that begins with a # symbol is taken as a comment by the interpreter (There is a case in which this is not true and we will come to that case soon)
3.All content after a # symbol is considered as a comment, if the # symbol is not inside a string literal.
4.A semi-colon terminates all Perl statements.

These are the general considerations that are to be burned into a new pilgrim headed on the path of acquiring Perl Wisdom.

Now just before going in to a coding session let’s do a final routine check of our machine to see if everything is ticking smoothly.

Launch a command window or shell prompt and type the below given command to see if Perl is indeed installed properly in your machine.

perl –v

This ‘perl’ command invokes the perl interpreter that we have installed on our pc and the ‘-v’ argument makes the interpreter to spit out the version information. If you are seeing version info related to the interpreter installed on your system then your PC is ready to toil for you, during your Perl harvesting season.

Here is the output that my old PC gives on Windows platform, when I execute a ‘perl –v’ command.

This is perl, v5.6.1 built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2001, Larry Wall

Binary build 638 provided by ActiveState Corp. http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Apr 13 2004 19:24:21

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page
.

If you are seeing an error message that means that the command was not recognized by the command prompt or shell prompt, then adhere to the following steps for troubleshooting.

1. Make sure that you have indeed installed Perl on your PC.
2. Make sure that the directory in which Perl was installed is included in the System PATH variable.

Let’s do coding

So with out any further ado, fire up your favorite text editor. The editor can be any one of the myriads of text editors available, so just choose one that is comfortable for you. All Perl source code files that we write are textual files, so avoid editors that support fancy letter formatting. For your Perl coding tasks on Linux or Unix ‘vi’ editor will be enough and on Windows platform you can easily make use of the ‘Notepad’.

Our First Perl program, or hmm… is it our second program

Type the below given code to the text editor of your choice.

#!/usr/bin/perl

# Print out a message to user

print "Name Please, Mister : " ;

# Read a line of Input from User

$uInp = ;

# Print the input back to user

print "Oh, it's you, " . $uInp . ",sorry for being rude";

After typing it in save the file as ‘first.pl’.

Even though our code is only 7 lines in length, I think a little explanation won’t hurt anybody. We will try out our output just after this small explanation.

In our code the first line is a very interesting statement (this is the special case about which I was talking when I told you about comments). This is a special comment statement, as it tells the system that this file is to be interpreted by the Perl interpreter whose path we are providing after the ‘#!’ characters. On your Linux or Unix machines you will have to change this line to reflect the path where Perl interpreter is installed locally. In Windows platform while doing Perl coding you can drop this line off.

SideTrack

 A side stepped trail from the main road
By the way the #! Character pair is called a ‘Shebang’ – No, No, Not the smash hit number ‘She Bangs’ from Ricky Martin – a ‘Shebang’ is a specific pair of characters used in a special line that begins a text file (commonly called a script) causing Unix-like operating systems to execute the commands in the text file using a specified interpreter (program) when executed.

Read more about ‘
Shebang


Read more about ‘
She Bangs’ by Ricky Martin

The second, fourth and sixth lines are actual comment lines in Perl. The interpreter ignores these lines but they act as a better grasp providing mechanism for Programmers.

The third and seventh lines in the code, prints text given in double quotes to the standard output. These lines utilize the ‘print’ function available in perl for doing this.

The fifth line reads a piece of input from the standard input and stores it in to a scalar variable in memory. Later in the last line we are printing back this value to the standard output by joining the value stored inside the variable with some text given inside double quotes. The dot (.) operator in Perl is used by us in the last line to accomplish the task of joining pieces of string literals together.

How to run the Perl program that we just wrote

In Unix / Linux Platforms

Type in this command.

chmod +x first.pl

This command gives our little program, executable status and we can now see the fruit of our code using the below command

first

In Windows Platform

You can directly call the Perl interpreter and pass the program name as it’s argument for executing the code. The syntax is,

perl first.pl

Now if everything goes right, then you will see an output on the screen like the one given below.

Name Please, Mister : Pramod
Oh, it's you, Pramod
,sorry for being rude


What went wrong ?

Not seeing an output similar to the one given above ?, don’t worry, we can take out our troubleshooting kit and patiently go through the check list to find the problem.

1. Minutely analyze the code for any Typos.
2. If an error message similar to ‘first not found’ is got in Unix/Linux PC, try this. Instead of typing ‘first’ to execute the program use ‘./first’. Adding the directory were you saved the perl program can also rectify the problem.
3. if you are getting an error similar to ‘/usr/bin/perl not found’ then there is some problem with the path you have given after the #! Statement or there is a problem with your Perl installation. Check for these errors and now you will be able to execute the program correctly.

That’s it the Perl traveler!

We are hitting the bed for the day in this long journey through Perl filled water way's. I am going to visit my parents today and I am looking forward to the heavenly dinner with my mom and dad.


Thursday, April 06, 2006

What if I have to ask some Questions Frequently about Perl ?

There exists many a excellent FAQ’s (Frequently Asked Question’s) catering to those creepy doubts and problems arising in the mind of a Perl developer. These FAQ’s are brilliant repositories of Perl information. These collections can give answer to almost any kind of question that a perl programmer can have like “How do I install Perl”, “How do I parse email header using Perl”, "How to construct some Gene sequence using Perl",“How to get even with my neighbour using Perl” etc ;-)

Note : Who know’s, in the near future may be there will be a hand held RPG with a nuclear payload which can be customized using Perl, and then that last question will be surely found on the FAQ.

So how to access the FAQ’s

The first stop for finding answers related your Perl musings will be the PerlFaq
. As of this blogging, there were 10 separate and extensive FAQ’s categorized on different Perl related topics at this collection. This is a very comprehensive bunch of Perl answers and is the fruit of the nice efforts of Tom Christiansen and Nathan Torkington.

Some other good FAQ’s related to Perl which can be of nice assistance to a developer are :

Perl, Unicode and i18N FAQ
- An FAQ for those developers who are into Perl, Unicode and Internationalization activities. James, Silicon Valley Perl Mongers, ex-Netscape and Yahoo contractor maintains this FAQ.

Perl5 Database Interface FAQ - by Alligator Descartes. It can be a starting point for Database programming using Perl.

OraPerl FAQ - An Faq on Perl database programming with Oracle. Oraperl is used to access Oracle from Perl programs. These day's the DBI database driver is used in place of this driver by most developers.

Perl/Tk FAQ - by Cameron Laird . A good place to find answers on Perl/Tk realted questions.

MacPerl FAQ - maintained by Vicki Brown, Rich Morin and others of MacPerl community. This is a resource for Perl developers doing their work on the Macintosh.


Talking about FAQ’s have made me a lot weary, so gotta go and have a cup of Cappuccino. We will meet soon the next time.

Tuesday, April 04, 2006

How to set up the Perl Farm (or rather technically setting up your PC for Perl Programming)

So, today I will guide you through the process of setting up a machine, for starting your action packed trek in pursuit of Perl Wisdom.

The first thing to do to get your lean mean machine to tackle Perl is to install the required files under the bonnet of your machine. To do this we have to acquire the desired version of Perl to suit your machine’s platform. The one place, which can supply you with all the needed Perl paraphernalia, is CPAN (Comprehensive Perl Archive Network)
. Here you can get hold of almost anything related to Perl and here you can download the needed binaries for installing Perl on your PC.

CPAN lists Perl binaries suitable for a majority of Operating Systems and it include versions for such exotic Os's like Windows CE and Symbian OS. If you don’t have the installation files for Perl with you, then this is the time to acquire them from the above given link.


I will cover installation issues related to Linux and Windows Boxes in this Post.

What to do if you have a Linux Box ?

Allmost all major Linux distro’s (Debian, Mandrake, Redhat, SuSe …) ships Perl along with the OS, so if you have it on your system then you are up and ready for the journey. For those who doesn’t have perl on their machine’s download the latest stable Perl package for Linux and follow the below steps.

I suppose that the build of Perl that we are going to install is ‘Perl-5.8.0’.

Step 1 : Prepare Perl for compilation

./configure.gnu --prefix=/usr

Step 2 : Compile the package

make

Step 3 : Install it

make install

That’s it !!!.


Now use the remaining time that you have, before going further, by downloading some IDE (Integrated Development Environment) for Perl.Some nice one’s available for Linux are PerlBuilder
and PerlEdit

What to do if you have a Windows Box ?

You can either download the latest binary for Windows as a standalone thing or you can download whole packages like ActivePerl , CamelPack etc. Double click the binary file to start the installation. These are fairly straight forward in their installations as each has it’s own wizard based installation programs. Even though you can do perl programming with a plain vanilla text editor like Notepad, you can also arm your machine with some IDE’s for windows like Komodo , Arachno Perl etc. These IDE’s have many features which can make Perl programming a whole lot more simple.

Now you have successfully equipped your machine with a Perl Teeth, and let’s try it out with the ubiquitous ‘Hello World’ Program.

SideTrack


 A side stepped trail from the main road
In programming parlances, a ‘Hello World’ program is considered as the first step taken towards the long process of mastering a programming language. This kind of thing started when Dennis Ritchie and Brian Kernighan released the book called ‘The C Programming Language’ (also called the ‘White Book’ or ‘Old Testament’). They gave a simple program which just printed the words “Hello World” to the standard output, and since then almost all computer language textbooks follows this standard for introducing a programming language.

Wanna read more on the Hello World thingie !! Check these out
http://en.wikipedia.org/wiki/Hello_World
http://www2.latech.edu/~acm/HelloWorld.shtml - The Hello World Project.

Here is our first taste of a Perl program,

print "Hello, world!\n";

This program does nothing more than printing "Hello, world!" followed by a newline character on to the standard output. So, let’s windup this session here. We can go further in to the intricacies of executing a Perl program in the next session.

So what is PERL actually ? (Now, we know it is not a fashion accessory)

Note :
Hey, Hey do we have to write PERL as PERL, i.e in all caps. Actually, No, Perl came as a name rather than an acronym, so there is no need to express it as an all caps entity.

Photo of Larry Wall


PERL took its first foot on planet earth in mid-80’s, when a guy named Larry Wall found the ‘awk’ utility (a very powerful data processing engine) not suitable for a bug reporting system he was building. He found the then available Unix tools no match for the problem he had at hand. Like a true hacker he was, he took the problem head-on by building a tool to meet his reporting tasks and Perl version zero was born. Later Larry Wall came up with an expansion for Perl and from then on this language got the expansion ‘ Practical Extraction and Report Language’.

BTW you can meet the whole family of Wall’s @ http://www.wall.org/

Let’s start the journey through the land of Perl with some historical diggings.

The first version of Perl, Perl 1.000 was made available on the net on December 18 1987

I am giving an excerpt from the ReadMe file that came up with the initial version of Perl

"Title: perl
Version: 1
Submitted: Jan 31 1988
Author: Larry Wall
lwall@jpl-devvax.jpl.nasa.gov
Submittor: Larry Wall
Description: a "replacement" for awk and sed"

On 5th June 1988, the second edition Perl 2.000, hit the markets.

On 18th October 1989, Perl 3.00 is released and this time Larry releases it under GNU Licensing.

21st March 1991 and it’s Perl 4.000 ‘s term to be developers mate.

Perl gets a complete make over in 1994 when the 5th version is launched on 18th October.

The latest version that is yet there in the development process is Perl 6. We will definitely see a lot of Action on Perl 6 in this Blog in the future.

So now go and take a shower as I am sure you people will have accumulated a lot of dirt and grit by following me on this historical trail. But knowing history is a good thing that will assist you in your steps in future. Next time we will meet with some hands-on Perl stuff.

Monday, April 03, 2006

Let's Dive for PERL's

PERL… What is it, a new kind of women’s fashion accessory?

PERL is a smart but very weird programming language (Hmm I fear if some PERL Guru’s might smack me for calling PERL weird. What I mean by weird here is of having strange, powerful capabilities, No, No, not the other weird.). Having done some hobby and commercial programming on PERL for sometime and having the nice satisfying experience of teaching 3 batches of Post Graduates in BioInformatics, about this topic, I find it is time to start blogging on this subject.

I plan to load this Blog with everything I have regarding PERL in a regular basis. So i invite you all to this small place on the cyberspace heartly.

Pss !! , one thing between us, you can visit this PERL House any time you want, even if i am sleeping or playing or eating or you know what :-).

By the way, you can also visit http://www.wisdombay.com for some nice techno thingies on a regular basis.

So that's the first post, ha ! what a feeling.