Perl Learning Record

Source: Internet
Author: User

1. Sub-Program

Define subroutines: The keyword sub, the subroutine name (not included with the number), and the block of code enclosed with curly braces, which is the main body of the program;

Sub marine{

$n +=1;

Print "Hello,sailor number$n!\n";

}

The definition of a subroutine is global.

2. Calling subroutines

The subroutine name (preceded by a number) can be used in any expression to invoke it;

&marine;

3. Return value

In Perl, all subroutines have a return value-the subroutine does not have a "return value" or "no return value", but not all Perl programs contain useful return values;

For example, we define the following subroutine, and the last one is the addition expression:

Sub sum_of_fred_and_barney{

Print "Hey, you called the Sum_of_fred_and Barney subroutine!\n";

$fred + $barney; #这就是返回值

}

$fred = 3;

$barney = 4;

$wilma =&sum_of_fred_and_barney;

4. Parameters

Perl subroutines can have parameters. To pass the argument list into the subroutine, simply add the list expression after the subroutine call with the parentheses around it.

$n =&max (10,15); #包含两个参数的子程序调用

Perl automatically aliases the parameter list as a special array variable @_, which means that the first parameter of the subroutine is stored in $_[0], and the second parameter is stored in $_[1]

Sub max{

if ($_[0]>$_[1]) {

$_[0];

}

else{

$_[1];

}

}

Perl Learning Record

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.