Latest web development tutorials

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 with those languages, but also has its own characteristics.

Perl program and a declaration statements, the implementation of the program from top to bottom, including the circulation, control conditions, each statement with a semicolon (;) end.

Perl language is no strict format specification, you can according to their own favorite style indentation.


The first perl program

Interactive Programming

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

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

Enter the above command, press Enter, and output is:

Hello World

Scripted Programming

We will file the following code intohello.pl:

#!/usr/bin/perl

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

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

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

You can also use parentheses to print the output string, the following two statements output the same result:

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

Script files

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 comments to make your programs readable, it is good programming practice.

perl annotation method in the statement 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) for multi-line comments. Here's how:

#!/usr/bin/perl

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

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

The above program, the output is:

Hello, world

note:

  • = Pod, = cut only the line.
  • = Begin with to = cut ends.
  • = 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:

#!/usr/bin/perl

print       "Hello, world\n";

The above program, the output is:

Hello, world

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

#!/usr/bin/perl

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

The above program, the output is:

Hello
          world

All types of white such as: space, tab, blank lines, etc. If the interpreter will ignore it outside the quotation marks, as if in quotation marks will be output.


Single and double quotes

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

#!/usr/bin/perl

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

The output is as follows:

Hello, world
Hello, world\n

From the results we can see that the double quotes \ n a newline is output, but not single quotes.

The difference between Perl double quotes and single quotes: double quotes can normally resolve some of the escape character variable, while the single quotation marks will not be parsed as output.

#!/usr/bin/perl

$a = 10;
print "a = $a\n";
print 'a = $a\n';

The output is as follows:

a = 10
a = $a\n

Here Documents

Here the document also called heredoc, hereis, here- string or here- script is a command line shell (eg sh, csh, ksh, bash, PowerShell and zsh) and programming languages ​​(such as 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 use any other character instead of just the end to ensure consistency with the logo to identify the beginning.
  • 3. End the logo must occupy the top grid line alone (ie, must start from the beginning of the line, and before and after can not link any blank characters).
  • 4. Start identification number, or may be without the quotation marks with single or double quotation marks, without quotation marks and quotation marks consistent results, and explain the embedded variable escape character, single quotes are not explained embedded variables and escape symbols.
  • 5. When the content needs embedded quotes (single or double quotes), no increase tropes, single and double quotation marks to escape itself, here quite usage and q and qq of.
#!/usr/bin/perl

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

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

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, output a dollar sign ($):

#!/usr/bin/perl

$result = "本教程 \"w3big\"";
print "$result\n";
print "\$result\n";

The above program output is:


Perl identifiers

Perl identifier is programmed using the user's name, the name of the variable used in the program, the constant names, function names, statements, block name, etc. collectively referred to as an identifier.

  • Identifiers unit: English letter (a ~ z, A ~ Z), numbers (0 to 9) and underscore (_).
  • Identifier consists of a letter or underscore.
  • Identifiers are case-sensitive, $ w3big and $ w3big represent two different variables.