Perl Manual (II): Before coding

August 6, 2009 · Posted in Doc, Perl · Comments Off on Perl Manual (II): Before coding 

Before start to write code you must make a list of steps to do it correctly. You can just open your favourite text editor, write few code lines and run; this would be fine if your need something short, quick and sweet, but if you are writing a script to use in a future or to another person or group you must follow some basic rules and strongly recommended:

  • Design the program flow
  • Make reusable parts
  • Document, document, document!

 
Comments
A comment is a note into the code what is not interpreted, the Perl interpreter just ignore it. The comments are used to add information about the code and the program.

In Perl the comments are written using the # symbol. Any text after # and to the end of line is a comment.

# This is a comment

Perl hasn’t got a multi-line comment mark, to comment several lines you can put a # at the beginning of each.

# Example of
#
Multi-line comment.
 
 
Templates

Create templates like a base for your scripts is a good habit, that way all of them will have a similar style.

In the template must be information about the author, date, version and what the script do. If you are creating a subroutine in the script you must comment parameters, return and a function description too.

A template example:

[Script Header]
#!/usr/bin/perl -w
#——————–
#Script Name:
#Script Version:
#Date:
#Author:
#Description:
#Revision History:
# 1.0/: original version
#——————–
[END of Script Header]

[Function Header]
#——————–
#Function
#Version:
#Input:
#Output:
#Description:
#——————–
[END of Function Header]

 
First Line

The first line of the script must starts with #! and the Perl interpreter path. It’s not essential to do it but it’s very recommended because indicates where the program can find the interpreter and you can pass runtime options to it.

For example, to run the script in warning mode (and the interpreter is in /usr/bin) the first line has to be:

#!/usr/bin/perl -w

This run the script activating several useful warnings. There are a lot of options, for more information the best way is read the Perl manpages.

 
To be continued…

In the next part of this manual we’ll write our first script (at last!)

Perl Manual: Introduction

July 31, 2009 · Posted in Doc, Perl · Comments Off on Perl Manual: Introduction 

Perl is one of the most used and famed scripting language, it can be installed in almost all platforms and operating systems and it’s very versatile. It’s also free and there are a lot of websites with content Perl-related, CGI scripts for example. There are modules (or libraries) for the most varied programming tasks (mathematics functions, file compression/decompression, data encrypt, HTML and XML handling, networking, data base access, etc.).

Perl_Camel

The official web of Perl is http://www.perl.com and it has a lot of links to other useful information sources. Perl has a directory called CPAN (‘Comprehensive Perl Archive Network), with mirrors around the world, where you can find the main extension modules for this language.

Perl has the same virtues and flaws that the other scripting languages, like Python, Ruby, Tcl/Tk and, though less versatile, awk. The motto of Perl is “There’s more than one way to do it“, I believe it.
It’s a semi-interpreted/semi-compiled language, but we can consider it interpreted because don’t exist the intermediate compile and link stages, this make possible run the program just after write it.

In the next articles of this series I will try to explain the Perl basics for benefit from its efficiency and write speed.