Introduction to Perl's control statements and function definitions

Source: Internet
Author: User
Tags foreach exit expression scalar stdin

1. Conditional Control Statement

if (conditional expression)

{

#语句

}

Else

{

#语句

}

The Given...when structure form is:

Given (scalar)

When () {}

When () {}

When () {}

When () {}

The use of the given statement is:

#!/usr/bin/perl-w use  
5.010001;  
My $m =<stdin>;  
Given ($m)  
{when  
(/[0-9]/) {print ' It is a number\n ';}  
When (/[a-z]/)  {print ' It is a letter\n '}  
default  {print ' \ n ';}  
}

2. Loop Control statement

(1) while (conditional expression)

{

# loop Body Statement

}

(2) until (conditional expression)

{

# circulation Body

}

(3) Do

{

#循环体

}while (conditional expression)

(4) foreach scalar (scalar)

{

# circulation Body

}

The simple usages of foreach are:

#!/usr/bin/perl-w

foreach $m (1..10)

{

print "$m \ n";

}

(5) For Loop statement and foreach equivalence

form of

for (expression; expression; expression)

(6) Next,last and redo of cyclic control

The next statement is used to skip this loop, perform the next loop, and the last statement is used to exit the loop, and the redo statement is used to return to the beginning of the cycle. The difference between next and redo is that next will skip this cycle. The following are examples of usages of three types of statements:

#!/usr/bin/perl-w use  
5.01;  
my $n;  
For ($n =0 $n <10; $n + +)  
{  
say "$n";  
Say "input a command:next,last or redo";  
My $com =<stdin>;  
Last if $com =~/last/;  
Next if $com =~/next/;  
Redo if $com =~/redo/;  
}

In the above program, input last will jump out of the loop, the input redo will print the output of this cycle again, enter next will print the next number.

(7) The above mentioned last only can exit this layer loop, if you want to exit the multi-layer loop, you can use a tagged statement. Examples of use are:

#!/usr/bin/perl-w use  
5.01;  
my $num;  
my $j;  
Label:for ($num =0 $num <10; $num + +)  
{for  
($j =0; $j < $num; $j + +)  
{  
say "input a string";  
My $str =<stdin>;  
if ($str =~/stop/)  
{last  
LABEL;  
}  
Say "You have input: $str";  
}  

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

A label label is added to the front of the For Loop, and the last label is used in the inner loop to exit the two-tier loop. You can exit the loop by typing stop in the above program.

3. Definition and use of functions

The basic form of a function is

Sub < function name >

{

# function Body

}

such as defining a function

Sub Hello

{

print "Hello world\n";

}

You can call it by using the subroutine name plus & in the expression.

#! /usr/bin/perl–w  
Sub Hello  
{  
print "Hello world\n";  
}  
&hello;

Hello,world appears in the program

The following defines the Guess function, which uses circular statements to realize the function of guessing numbers:

#!/usr/bin/perl-w my  
$n =100;  
My $num =int (rand ($n));  
Sub guess{do  
{  
print ' input a # which is in the range of (0,100);  
$number =chmop (<STDIN>);  
if ($number = = $num) {  
print "riht\n";  
}  
elsif ($number < $num) {  
print "too low\n";  
}  
else {  
print ' too high\n ';  
}  
} while (1);  
}  
&guess;

Author: csdn Blog 0 Ding if sigh

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.