Perl Basic Syntax

Perl borrows features from C, sed, awk, shell scripts, and many other programming languages. Its syntax is somewhat similar to these languages, but it also has its own characteristics.

Perl programs consist of declarations and statements. The program executes from top to bottom, including loops and conditional control. Each statement with a semicolon;ends.

Perl has no strict formatting rules; you can indent according to your preferred style.


The first Perl program

Interactive programming

In the command line, you can use the-eoption to enter statements and execute code, as in the example below:

$ perl -e 'print "Hello World\n"'

After entering the above command and pressing Enter, the output is:

Hello World

Script programming

We will put the following code intohello.pla file:

Example

#!/usr/bin/perl # Output "Hello, World" print "Hello, world\n";

In the code,/usr/bin/perlis the path to the Perl interpreter. Before executing the script, make sure the file has executable permissions. We can first change the file permissions to 0755:

$ chmod 0755 hello.pl 
$ ./hello.pl 
Hello, world                   # 输出结果

print can also use parentheses to output strings. The following two statements output the same result:

print("Hello, world\n");
print "Hello, world\n";

Script file

Perl code can be written in a text file with .pl or .PL as the extension.

Filenames can contain numbers, symbols, and letters, but cannot contain spaces. You can use underscores (_) instead of spaces.

A simple Perl filename:

run_oob.pl

Comments

Using comments makes your program easy to read; this is a good programming habit.

The way to comment in Perl is to use the character # at the beginning of a statement, for example:

# 这一行是 perl 中的注释

Perl also supports multi-line comments. The most common method is to use POD (Plain Old Documentation) for multi-line comments. The method is as follows:

Example

#!/usr/bin/perl # This is a single-line comment print "Hello, world\n"; =podComment 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

Run the above program, the output is:

Hello, world

Note:

  • =pod and =cut can only be at the beginning of a line.
  • Starts with = and ends with =cut.
  • After =, there must be a character immediately following; after =cut, it is not required.

Whitespace in Perl

The Perl interpreter does not care how much whitespace there is; the following program will also run normally:

Example

#!/usr/bin/perl print "Hello, world\n";

Run the above program, the output is:

Hello, world

But if spaces and line breaks appear inside a string, it will output them as they are:

Example

#!/usr/bin/perl # Will output line breaks print "Hello world\n";

Run the above program, the output is:

Hello
          world

All types of whitespace, such as spaces, tabs, blank lines, etc., are ignored by the interpreter if they are outside quotes; if they are inside quotes, they are output as they are.


Single quotes and double quotes

Perl can use single quotes and double quotes to output strings, as shown below:

Example

#!/usr/bin/perl print "Hello, world\n"; # Double quotes print 'Hello, world\n'; # Single quotes

The output result is as follows:

Hello, world
Hello, world\n

From the results, we can see that double quotes \n outputs a newline, while single quotes do not.

The difference between double quotes and single quotes in Perl: double quotes can parse some escape characters and variables normally, while single quotes cannot parse them and will output them as they are.

Example

#!/usr/bin/perl $a = 10; print "a = $a\n"; print 'a = $a\n';

The output result is as follows:

a = 10
a = $a\n

Here document

Here document, also called 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:

  • 1. It must be followed by a semicolon, otherwise it will not compile.
  • 2. EOF can be replaced by any other character, just ensure the end marker is consistent with the start marker.
  • 3. The end marker must be at the beginning of a line and occupy a line by itself (i.e., it must start from the beginning of the line, with no whitespace or characters before or after it).
  • 4. The start marker can be without quotes or with single or double quotes. No quotes has the same effect as double quotes: embedded variables and escape symbols are interpreted. With single quotes, embedded variables and escape symbols are not interpreted.
  • 5. When the content needs embedded quotes (single or double quotes), there is no need to add escape characters; it escapes single and double quotes by itself. This is equivalent to the usage of q and qq.

Example

#!/usr/bin/perl $a = 10; $var = <<"EOF"; This is aHeredocument example, using double quotes. You can enter strings and variables here. For example:a = $a EOF print "$var\n"; $var = <<'EOF'; This is aHeredocument example, using single quotes. For example:a = $a EOF print "$var\n";

After executing the above program, the output is:

这是一个 Here 文档实例,使用双引号。
可以在这输入字符串和变量。
例如:a = 10

这是一个 Here 文档实例,使用单引号。
例如:a = $a

Escape characters

If we need to output a special character, we can use a backslash (\) to escape it, for example to output the dollar sign ($):

Example

#!/usr/bin/perl $result = "Example\"example\""; print "$result\n"; print "\$result\n";

After executing the above program, the output is:


Perl identifiers

Perl identifiers are names used by users in programming. The variable names, constant names, function names, statement block names, etc. used in a program are collectively called identifiers.

  • Identifier components: English letters (a~z, A~Z), digits (0~9), and underscores (_).
  • Identifiers begin with an English letter or an underscore.
  • Identifiers are case-sensitive; $example and $Example represent two different variables.
Other extensions