Month: April 2014

Perl by Example, Fourth Edition – Chapter 3. Perl Scripts

1. How do you set up a script?
Perl scripts consist of a list of Perl statements and declarations. Statements are terminated with a semicolon (;). (Since only subroutines and report formats require declarations, they will be discussed when those topics are presented.) Variables can be created anywhere in the script and, if not initialized, automatically get a value of 0 or “null,” depending on their context. Notice that the variables in this program start with a $. Values such as numbers, strings of text, or the output of functions can be assigned to variables. Different types of variables are preceded by different “funny symbols,”

2. How are statements terminated?
Statements terminated with a semicolon.

3. What is whitespace?
Whitespace refers to spaces, tabs, and newlines. The newline is represented as “\n” and must be enclosed in double quotes. Whitespace is used to delimit words. Any number of blank spaces are allowed between symbols and words. Whitespace enclosed in single or double quotes is preserved; otherwise, it is ignored.

4. What is meant by free form?
Perl is a free-form language. Statements must be terminated with a semicolon but can be anywhere on the line and span multiple lines.

5. What is a built-in function?
When you call a Perl built-in function, you just type its name, or optionally you can type its name followed by a set of parentheses. All function names must be typed in lowercase. Many functions require arguments, messages that you send to the function.
If the function requires arguments, then place the arguments, separated by commas, right after the function name. The function usually returns something after it has performed its particular task.
A big part of any programming language is the set of functions built into the language or packaged in special libraries.

6. What is the #! line in UNIX?
If the first line of the script contains the #! symbols (called the shbang line) followed by the full pathname of the file where your version of the Perl executable resides, this tells the kernel what program is interpreting the script.

7. How do you make the script executable?
To execute a script at either the UNIX or MS-DOS prompt, type
$ perl scriptname

8. Why use comments?
You may write a very clever Perl script today and in two weeks have no idea what your script was trying to do. If you pass the script on to someone else, the confusion magnifies. Comments are plain text that allow you to insert documentation in your Perl script with no effect on the execution of the program. They are used to help you and other programmers maintain and debug scripts. Perl comments are preceded by a # mark. They extend across the line, but do not continue onto the next line.
Perl does not understand the C language comments /* and */ or C++ comments //.

9. How do you execute a Perl script if not using the shbang line.
The -e switch allows Perl to execute Perl statements at the command line instead of from a script. This is a good way to test simple Perl statements before putting them into a script file.

10. What comand-line option lets you check Perl syntax?
The -c switch is used to check the Perl syntax without actually executing the Perl commands. If the syntax is correct, Perl will tell you so. It is a good idea to always check scripts with the -c switch. This is especially important with CGI scripts written in Perl, because error messages that are normally sent to the terminal screen are sent to a log file instead.

11. What is the -e switch for?
The -e switch allows Perl to execute Perl statements at the command line instead of from a script. This is a good way to test simple Perl statements before putting them into a script file.