Perl Manual (II): Before coding

August 6, 2009 · Posted in Doc, Perl 

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!

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.

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:
#Revision History:
# 1.0/: original version
[END of Script Header]

[Function Header]
[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!)


Comments are closed.