Perl Information Center Tutorials - Syntax
These tutorials were written to help you get a quick, but thorough, understanding of Perl -
the scope of the language as well as it's specific capabilities.
| Beginners
| Built-In Functions
| Advanced
| CGI Applications
|
|
|
|
|
|
Syntax
Like any language, Perl has rules on how the code must be written
for the interpreter to be able to read and execute the code. These
rules are called syntax.
The basics of Perl syntax are demonstrated in the following short
script. The script is simply various lines of code selected to demonstrate
syntax concepts and are numbered solely for the purpose of reference
in this tutorial. With Perl, lines are not normally numbered, although
a line can be given a label.
1 $a = "Hello"; # no type declaration, end with semicolon
2 $a = 'Hello'; # same variable assigned as a new data type
3 $a = # 1st line of a 2-line code
4 "Goodbye"; # 2nd line of a 2-line code
5 $a = 5; # scalar given an integer value
6 $a = 7.6; # scalar given a floating point value
7 @array = (1,"dog",4.2) # array define by list of mixed types
8 print (a$); # parentheses around function arguments
9 print a$; # parentheses optional - "7.6" is printed
10 print ("a$"); # interpolation - "7.6" is printed
11 if ($a+1) { # BLOCKS of code enclosed by { }
12 print 'True'; # single quotes, no parenthesis
13 }
14 a$ = &Print_Hello # calls subroutine Print_Hello
15 sub Print_Hello { # subroutine definition, no arguments
16 print "Hello";
17 return "done"; # a subroutine can return a value(s)
18 }
19 if ($a) {print "true"} # one line version of lines 14-16
Lines of Code
Perl scripts consist of lines of case-sensitive code which must
end in a semicolon, as in Line 1. However because Perl ignores
whitespace (spaces, tabs, line feeds), a line of code can be written
to cover multiple lines of text, as shown in Lines 3 & 4. Line 19
shows how code can also be compacted onto a single line.
Comments
Any text to the right of a # character is treated as comments
and is ignored during execution.
Variables
Perl variable names begin with $, @, or %. The basic variable
type supported by Perl is the scalar variable, which starts with
$, as in Line 1. A scalar may be a string, an integer, or a floating
point number (Lines 1,5,6).
Arrays variable names begin with @. Hash variables names begin with %.
Perl builtin functions can recognize the
type of data in a variable and will adjust their action accordingly.
Note that Perl is case-sensitive. The variables $dog and $DOG are two
different variables.
Variable Definition
Unlike other languages, Perl does not require that you pre-define a
variable or its type. To create a variable, simply use it in a line
of code. The Perl interpreter can understand the type of data that
is assigned to the variable and use it appropriately.
Strings & Quotes
String data may be enclosed in double or single quotes, as in Lines
1 & 2. However, double quotes offer the advantage of interpolating
the content between the quotes, which means that variables will be
replaced in the string with their value, as in Line 10.
Escape Characters
In addition to interpolation of variables inside a double quote, Perl
also interpolates "escape characters", such as \n \' \" \\ \t.
which correspond to a line feed, single quote, double quote, backslash,
and tab.
Parentheses
Parentheses ( ) are used in several ways by Perl. Parentheses may be used
to enclose EXPRESSIONS, as shown in Line 14. An expression is code
which resolves to a value. Parentheses are also use to enclose a
list, as shown in Line 7. Finally, parentheses can be used to
enclose arguments to a function but are optional as in Lines 8 & 9.
BLOCKS
Multiple lines of code may be enclosed by curly brackets { }. These
are called BLOCKS of code, as shown in Lines 14-16. and Lines 15-19.
Variables defined within a block do not exist outside that block (this
is called scope). Code is arranged in blocks to identify code that must
be executed in conjunction with functions such as "if" and "sub" statements
(lines 11-13 and lines 15-18). A semicolon is not required after the
last statement in a block (line 19), but it is considered good practice to use
the semicolon anyway (line 17).
Blocks may also be given a label, which may be used in controlling the flow
of program execution. See the flow control
tutorial for more information.
Subroutines
A subroutine is defined using "sub" followed by the subroutine name,
then the block of code to execute within the subroutine,
as shown in Lines 15-18. Subroutine definitions do not include arguments
because a program can pass any number of arguments to a subroutine. A
subroutine is called by using & in front of the subroutine name, as in Line 14.
Subroutines can be placed anywhere within the program. If the subroutine is
placed before the line where it is called, the & is not required. A subroutine
can return a value using a return statement (line 17).
If you have suggestions or corrections to this page, please let me know.
|