Perl: Control Structure

Source: Internet
Author: User

Perl Structure Control Statements:

If condition statement:

if (condition) {

Expression

} elsif {

Expression

} else {

Expression

}

Unless conditional statements:

The condition is false when executed.

Unless (condition) {

Expression

} elsif {

Expression

} else {

Expression

}

While Loop statement:

while (condition) {

Expression

}

while (1) {

...

} #死循环

Until Loop statement:

Execute only when the condition is false

Until (condition) {

Expression

}

foreach Loop statement:

foreach $var (@arry/list) {

Expression

}

$var loop variable is not a copy of the list element, but the list element itself.

If you modify the loop variable in the foreach body, you modify the elements in the list.

$var loop variable restores the original value after exiting the Foreach loop.

The loop variable can be omitted, and the default is to use $_ instead:

foreach (@arry/list) {

print "$_";

}

For Loop statement:

The For and foreach equivalents in Perl can be converted to each other according to the conditions.

for (initialization; condition; increment) {

Expression

}

for (;;) {

...

} #死循环

Bare BLOCK:

{

Expression

}

Last function : Jumps out of the loop, equivalent to the break of C.

Last label/expr

next function : Jump out of this cycle, continue the next cycle, the equivalent of C continue.

Next label/expr

redo function : Unconditionally jump to the top of this cycle, re-execute this cycle.

Redo Label/expr

Given-when Control Structure:

Given ($var) {

When (condition) {expression}

...

default {expression}

}

When matching for multiple projects:

foreach (@arr) {

When (condition) {expression; continue}

...

default {expression}

}

break: Jump out of given block

continue: continue with the next sentence

Conditional modifiers:

dosomething if condition;

Can also be written as:

DoSomething

if condition;

tags in the loop:

A jump can be achieved by adding a tag to the Loop keyword such as for/while.

The label is capitalized, and can be alphanumeric and glide, but not a number.

Eval Expr/block: Capturing errors

special variables [email protected] Store the error message if no exception is triggered [email protected] is empty.

eval {

....

}

if ([email protected]_) {

Print "An error occurred ([email protected]), continuing\n";

}

Evalbytes

Goto

Return

Exit

Do

Wantarray

Dump

Caller

"__file", "__line__", "__package__", "__sub__"

Perl: Control structure

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.