Perl basic grammar

Perl basic grammar

Perl borrowed a C, sed, awk, shell scripts, and many of the features of other programming languages, the syntax is somewhat similar to these languages, also has its own characteristics.
Perl program has a statement and statements, top-down program execution, including circulation, condition control, each statement with a semicolon (;) end.
Perl language is no strict format specification, you can be indented according to their own favorite style.

The first perl program

Interactive Programming

You can use the -e option on the command line to enter the statements to execute code examples are as follows:

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

Enter the above command, after the carriage return, the result is output:

Hello World

Scripted Programming

We will file the following code into hello.pl:
Examples

#!/usr/bin/perl

# 输出 "Hello, World"
print "Hello, world\n";

Code / usr / bin / perl is the path of perl interpreter. Before executing the script must first ensure that the file has executable permissions, we can first modify the file permissions to 0755:

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

parentheses may be used to print the output string, the same output results the following two statements:

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

Script file

perl code can be written in a text file to .pl, .PL as a suffix.
The file name can contain numbers, symbols and letters, but can not contain spaces, you can use an underscore ( _) instead of spaces.
A simple Perl file name:

run_oob.pl

Note

Use the program to read your comment to make, it is good programming practice.
Methods perl comments as statements beginning with the # character, such as:

这一行是 perl 中的注释

perl also supports multi-line comments, the most common method is to use a POD (Plain Old Documentations) to multi-line comments. Methods as below:

Examples

#!/usr/bin/perl

# 这是一个单行注释
print "Hello, world\n";

=pod 注释
这是一个多行注释
这是一个多行注释
这是一个多行注释
这是一个多行注释
=cut

The implementation of the above program, the output is:

Hello, world

note:

  1. = Pod, = cut only the line.
  2. Beginning with = to = the end of the cut.
  3. = To immediately behind a character, = cut back can not.

Perl blank

Perl interpreter does not care how many blank, the following programs can be run properly:

Examples

#!/usr/bin/perl

print       "Hello, world\n";

The implementation of the above program, the output is:

Hello, world

But if space and branches appear in the string, he will be output as:

Examples

#!/usr/bin/perl

# 会输出分行
print "Hello
          world\n";

The implementation of the above program, the output is:

Hello
          world

All types of blank such as: space, tab, blank lines, etc. If the interpretation ignores it outside the quotation marks, as it would be if output in quotation marks.

Single and double quotes

perl output string can be single and double quotation marks, as follows:

Examples

#!/usr/bin/perl

print "Hello, world\n";    # 双引号
print 'Hello, world\n';    # 单引号

Output:

Hello, world
Hello, world\n

We can see from the results in double quotation marks \ n newline output, but no single quotes.
Perl difference between double and single quotes: The double quotes can properly resolve some of the escape character variable, while the single quotation marks will not be resolved as it is output.

Examples

#!/usr/bin/perl

$a = 10;
print "a = $a\n";
print 'a = $a\n';
输出结果如下:
a = 10
a = $a\n

Here Documents

Here the document also called heredoc, hereis, here- string or here- script is a command line shell (such as sh, csh, ksh, bash, PowerShell and zsh) and programming language (like Perl, PHP, Python and Ruby) method defined in a string.

Overview:

  1. Must be followed by a semicolon, otherwise the compiler pass.
  2. END can be used in place of any other character, just to ensure consistent identity ends with the start of identity.
  3. End logo must be the top grid alone accounted for one line (which must start from the beginning of the line, before and after can not link any gaps and characters).
  4. May not begin with identification numbers or marks with a single or double quotation marks, without quotation marks consistent results with quotation marks, embedded variables and interpretation escape character, single quotes embedded variables and escape symbol is not explained.
  5. When the content needs embedded quotes (single or double), you do not need an escape character, single and double quotation marks itself escaped, and where a considerable usage of q and qq.

Examples

#!/usr/bin/perl

$a = 10;
$var = <<"EOF";
这是一个 Here 文档实例,使用双引号。
可以在这输如字符串和变量。
例如:a = $a
EOF
print "$var\n";

$var = <<'EOF';
这是一个 Here 文档实例,使用单引号。
例如:a = $a
EOF
print "$var\n";

The implementation of the above program output is:

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

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

Escape character

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

Examples

#!/usr/bin/perl


$result = "编程字典 \"CodingDict\"";
print "$result\n";
print "\$result\n";

The implementation of the above program output is:

编程字典"CodingDict"$sesult

Perl identifiers

Perl identifier is the name used by the user program, variable names used in the program, constant names, function names, statements, block name, etc. collectively referred to as an identifier.

  1. Identifiers unit: letters (a ~ z, A ~ Z ), numbers (0 to 9) and underscore ( _).
  2. Identifier preceded by letters or underscores.
  3. Identifiers are sensitive, $ CodingDict and $ CodingDict represent two different variables.
    This switched: http://codingdict.com/article/6797

Guess you like

Origin www.cnblogs.com/bczd/p/11976663.html
Recommended