5.4. Perl basic syntax

发布时间 : 2025-10-25 13:33:01 UTC      

Page Views: 9 views

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

The Perl program consists of declarations and statements. The program executes from top to bottom, including loops, conditional controls, each statement with a semicolon ( ; ) end.

The Perl language does not have a strict format specification, and you can indent it according to your favorite style.

5.4.1. The first perl program #

5.4.2. Interactive programming #

You can use it on the command line -e option to enter a statement to execute the code, an example is as follows:

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

Enter the above command and enter, and the output result is as follows:

Hello World 

5.4.3. Script programming #

We put the following code into hello.pl file:

Example #

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

In the code /usr/bin/perl is the path to the perl interpreter. To make sure that the file has executable permissions before executing the script, we can change the file permissions to 0755:

$ chmod 0755 hello.pl $ ./hello.pl Hello, world # Output Results 

print can also use parentheses to output a string, and the followingtwo statements output the same result:

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

5.4.4. Script file #

Perl code can be written in a text file to .pl .PL as a suffix.

File names can contain numbers, symbols, and letters, but cannot contain spaces, and you can use underscores ( _ ) to replace spaces

A simple Perl file name:

run_oob.pl 

5.4.5. Annotation #

It is a good programming habit to use comments to make your program readable.

Perl comments are made by using the character # at the beginning of the statement, such as:

# This line is a comment in Perl 

Perl also supports multiline comments, and the most common method is to use POD (Plain Old Documentations) for multiline comments. The methods are as follows:

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 

Execute the above program, and the output is as follows:

Hello, world 

Note:

  • =pod =cut only at the beginning of the line.

  • To = start with =cut the end.

  • = is followed by a character. =cut doesn’t have to do it in the back.

5.4.6. Whitespace in Perl #

The Perl interpreter doesn’t care about how many blanks there are, and the following programs work:

Example #

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

Execute the above program, and the output is as follows:

Hello, world 

But if spaces and branches appear in the string, it will output as is:

Example #

#!/usr/bin/perl# Export branchprint"Hello world\\n"; 

Execute the above program, and the output is as follows:

Hello world 

All types of blanks such as spaces tab blank lines, etc., if it is outside the quotation marks, the interpreter will ignore it, and if it is inquotation marks, it will be output as is.

5.4.7. Single and double quotation marks #

Perl output strings can use single and double quotes, as follows:

Example #

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

The output is as follows:

Hello, world Hello, world\n 

As we can see from the results, double quotation marks \\n output line breaks, but single quotation marks do not.

The difference between Perl double quotes and single quotes: double quotes can normally parse some escaped characters and variables, while single quotes cannot be parsed and output 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 

5.4.8. Here document #

Here documents, also known as heredoc, hereis, here- strings, or here- scripts, are a way to define a string in command-line shell (such as sh, csh, ksh, bash, PowerShell, and zsh) and programming languages (such as Perl, PHP, Python, and Ruby).

Overview of usage:

  • 1.It must be followed by a semicolon, otherwise the compilation will not pass.

  • 2.END can be replaced with any other character, as long as the end identifier is consistent with the start identifier.

  • 3.The closing logo must be on a separate line (that is, it must start at the beginning of the line and cannot connect any whitespace and characters before and after).

  • 4.The initial identification can be without quotation marks or single and double quotation marks, and the effect without quotation marks is the same as that with double quotation marks, explaining embedded variables and escape symbols, while single quotation marks do not explain embedded variables and escape symbols.

  • 5.When the content requires embedded quotation marks (single or double quotation marks), there is no need to escape the single and double quotationmarks, which is equivalent to the use of Q and qq.

Example #

#/ Usr/bin/perl $a=10$ Var=<"EOF"; This is a Here document instance, using double quotes. You can input strings and variables here. For example: a=$aEOFprint "$var n"$ Var=<'EOF '; This is a Here document instance, using single quotes. For example: a=$aEOFprint "$var n"; 

The output result of executing the above program is:

This is a Here document instance, using double quotes. You can input strings and variables here. For example: a=10 This is a Here document instance, using single quotes. For example: a=$a 

5.4.9. Escape character #

If we need to output a special character, we can escape it with a backslash (), such as the output dollar sign ($):

Example #

#!/usr/bin/perl$result="Novice Tutorial\\"runoob\\"";print"$result\\n";print"\\$result\\n"; 

The output result of executing the above program is:

Image0

5.4.10. Perl identifier #

Perl identifier is the name used by the user when programming. The variable name, constant name, function name and statement block name used in the program are collectively referred to as identifiers.

  • Identifier components: English letters (a-z, A-Z)numbers (0-9), and underscores (_).

  • The identifier begins with an English letter or underscore.

  • Identifiers are case sensitive $runoob vs. $Runoob represents two different variables.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。