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.

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.


0 Comments:

Post a Comment

<< Home