Perl Basic Syntax
Perl borrows features from C, sed, awk, shell scripting, and many other programming languages. Its syntax is similar to these languages but also has its own characteristics.
A Perl program consists of declarations and statements, which are executed from top to bottom. It includes loops, conditional controls, and each statement ends with a semicolon (;).
Perl does not have strict formatting rules; you can indent according to your preferred style.
First Perl Program
Interactive Programming
You can use the -e option to execute code directly from the command line. Here is an example:
$ perl -e 'print "Hello World\n"'
After entering the command and pressing Enter, the output is:
Hello World
Script-Based Programming
We can place the following code in a hello.pl file:
Example
#!/usr/bin/perl
# Output "Hello, World"
print "Hello, world\n";
The /usr/bin/perl in the code is the path to the Perl interpreter. Before executing the script, ensure the file has executable permissions. You can change the file permissions to 0755:
$ chmod 0755 hello.pl
$ ./hello.pl
Hello, world # Output result
The print
function can also use parentheses to output strings. The following two statements produce the same result:
print("Hello, world\n");
print "Hello, world\n";
Script File
Perl code can be written in a text file with the .pl or .PL suffix.
The filename can include numbers, symbols, and letters but cannot contain spaces. You can use underscores (_) to replace spaces.
A simple Perl filename:
run_oob.pl
Comments
Using comments makes your program more readable, which is a good programming practice.
Perl comments are made by starting a line with the # character, like this:
# This line is a comment in Perl
Perl also supports multi-line comments, typically using POD (Plain Old Documentation) for this purpose. Here is how:
Example
#!/usr/bin/perl
# This is a single-line comment
print "Hello, world\n";
=pod Comment
This is a multi-line comment
This is a multi-line comment
This is a multi-line comment
This is a multi-line comment
=cut
Executing the above program will produce the output:
Hello, world
Note:
- =pod and =cut must be at the beginning of the line.
- Start with = and end with =cut.
- = must be followed by a character, but =cut does not need one.
Whitespace in Perl
The Perl interpreter does not care about the amount of whitespace. The following program will run normally:
Example
#!/usr/bin/perl
print "Hello, world\n";
Executing the above program will produce the output:
Hello, world
However, if whitespace and line breaks appear within a string, they will be output as is:
Example
#!/usr/bin/perl
# Will output line breaks
print "Hello
world\n";
Executing the above program will produce the output:
Hello
world
All types of whitespace, such as spaces, tabs, and blank lines, are ignored by the interpreter outside of quotes but are output as is within quotes.
Single and Double Quotes
Perl can output strings using single or double quotes, as shown below:
Example
#!/usr/bin/perl
print "Hello, world\n"; # Double quotes
print 'Hello, world\n'; # Single quotes
The output is as follows:
Hello, world
Hello, world\n
From the results, we can see that the newline character \n in double quotes outputs a line break, while in single quotes it does not.
The difference between double and single quotes in Perl: Double quotes can parse escape characters and variables normally, while single quotes cannot and will output them as is.
Example
#!/usr/bin/perl
$a = 10;
print "a = $a\n";
print 'a = $a\n';
The output is as follows:
a = 10
a = $a\n
Here Document
A here document, also known as heredoc, hereis, here-string, or here-script, is a method of defining a string in command line shells (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (like Perl, PHP, Python, and Ruby).
Usage overview:
- Must be followed by a semicolon, otherwise, the compilation will not pass.
- END can be replaced with any other character, as long as the ending identifier matches the starting identifier.
- The ending identifier must start at the beginning of the line and stand alone (i.e., it must start at the beginning of the line and cannot be preceded or followed by any whitespace or characters).
- The start identifier can be without quotes, with single quotes, or with double quotes. Without quotes or with double quotes, it interprets embedded variables and escape characters. With single quotes, it does not interpret embedded variables and escape characters.
- When content requires embedded quotes (single or double), no escape character is needed, as it inherently escapes single and double quotes, similar to the usage of q and qq.
Example
#!/usr/bin/perl
$a = 10;
$var = <<"EOF";
This is a Here document example using double quotes.
You can input strings and variables here.
For example: a = $a
EOF
print "$var\n";
$var = <<'EOF';
This is a Here document example using single quotes.
For example: a = $a
EOF
print "$var\n";
Executing the above program outputs:
This is a Here document example using double quotes.
You can input strings and variables here.
For example: a = 10
This is a Here document example using single quotes.
For example: a = $a
Escape Characters
If we need to output a special character, we can use a backslash () to escape it, for example, to output a dollar sign ($):
Example
#!/usr/bin/perl
$result = "tutorialpro.org \"tutorialpro\"";
print "$result\n";
print "\$result\n";
Executing the above program outputs:
Perl Identifiers
Perl identifiers are names used by the user during programming. Variable names, constant names, function names, block names, etc., used in the program are collectively called identifiers.
Identifier components: English letters (a~z, A~Z), digits (0~9), and underscores (_).
Identifiers start with an English letter or an underscore.
Identifiers are case-sensitive, $tutorialpro and $tutorialpro represent two different variables.