Introduction to Perl (2)

Source: Internet
Author: User

The previous section describes the basic data types and usage methods in Perl. This section describes branch loop control and Io.

 

The syntax of the branch loop in Perl is similar to that in the C family language. The keywords include: If/else, for, while.

As mentioned earlier, in PerlCodeThe block is enclosed by braces {}, and the expression ends with a semicolon. These are similar to C,

But in Perl, if for while is used, braces are mandatory, which is different from C.

 

Careful people may find that when talking about the data type in the previous section, it does not involve shaping, floating point, bool or anything,

Only one scalar is mentioned. In fact, in Perl, these basic data types are not strictly differentiated and can all be attributed to scalar,

This is probably a common practice of scripting language, which weakens the basic type.

 

Other words are not mentioned first. The branch loop has a key data in many languages: bool.

To control the branch direction, you need a judgment point, such as how to do it and how to do false. However, in Perl, there is no specific true or false type,

Therefore, when making true/false judgments, follow the following principles:

If it is a data type, 0 is false,

If it is a string, the Null String is false.

If it is a set, the empty set is false.

These are easy to understand.

 

(1) If/else

The IF/else syntax andC LanguageConsistency. In addition to requirements, braces must exist.

Logical judgment operation: With (&), or (|), not (!) It is also syntactically consistent with C.

$ STR = "ABC ";

@ Arr = (2, 3, 4 );

If ($ STR & @ arr = 3)

{

}

Else

{

}

The above example is a traditional old-fashioned method. As I mentioned earlier, if/else and C have the same syntax, but it is not very accurate.

Perl also provides a set of methods that are not the same as C. The style is more like a natural language.

$ Var = 2;

Print "Hello World" if ($ var> 0); # note that this line is equivalent to: if ($ var> 0) {print "Hello World ";}

 

This method is like reverse order in natural language.

As mentioned above, another keyword also applies: Unless

Print "Hello World" unless ($ var> 0 );

 

(2) loop: For/while/foreach

FOr ($ I = 0; I I <100; $ I ++)


Print "Hello $ I \ n ";
}

While ($ I <100)

{

Print "Hello $ I \ n ";

$ I ++;

}

 

The preceding two examples demonstrate the syntax of for/while, which is similar to that of C in syntax.

In addition to for/while, Perl also provides a foreach dedicated to processing arrays.

@ Arr = (1, 2, 3, 4, 5 );

Foreach $ item (@ ARR)

{

Print "item: $ item \ n ";

}

Note that parentheses in the foreach row cannot be saved.

 

(3) I/O

I/O operations in Perl extend the IO concept in UNIX, and everything is abstracted into files.

Therefore, I/O operations are performed on a file handle, including standard input and standard output.

(1) standard input and standard output.

 

Print is used multiple times in the previous sample code. In the previous writing, this is the standard output, but its function is not limited to the standard output. In fact, its accurate prototype is:

Print <file handle> "Hello world \ n ";

If file handle is omitted, it is the standard output by default. The standard output handle <stdout>

Therefore, the preceding print statement is equivalent:

Print stdout "Hello world \ n ";

The corresponding standard input is stdin, and the two variables are predefined in Perl.

It can be seen as a keyword, and you do not need to add the $, @ symbol before these variables.

The standard output is shown in the previous example, but the standard input is not mentioned.

The standard input syntax is also concise:

$ Line = <stdin >;# read

Enclose the file handle with Angle brackets, which is equivalent to reading data from it.

 

(2) file I/O

 

You must use the open ()/close () function to obtain and close files.

$ Succ = open (FH, "~ /Myfile. log ");

If ($ succ)

{

$ Line = <FH >;# read one line.

@ All = <FH>; # Read the whole file.

Print "@ line \ n ";

Close (FH );

}

 

It is worth noting that the declaration of the file handle does not require the $ symbol. It is enough to write a name directly. Of course, if you like to add $, it is no problem.

The preceding example demonstrates the most basic read operations. Perl also provides file operation functions similar to those in C: Seek, tell,

It is used to locate the corresponding position of the file for reading and writing.

Their usage is similar to that in C. For more information, see the official documentation.

The preceding open () function demonstrates the most basic form of file opening. In fact, this function l also supports setting the access mode.

File Access Mode:

Access Mode example

Read (read) Open (FH, "<FILENAME"); read from the file

Write (write) Open (FH, "> FILENAME"); write to the file, overwrite the content in the old file

Append open (FH, "> FILENAME"); append data to the end of an existing file

Read and Write open (FH, "+ <FILENAME"); read and write existing files

WriteProgramOpen (pipeout, "| pipeout"); open the program Pipeline

Read program open (pipein, "pipein |"); get data from program or command output

If the opened file supports write operations, we can use the print function to write something into the file:

If (open (FH, "> ~ /File. log "))

{

Print FH "Hello file \ n ";

Close (FH );

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.