Perl language learning notes Day 2

Source: Internet
Author: User

Labels: styles use different files for the Learning Program

1. in Perl, the subprogram name and variable name are different namespaces. You can use the same name to name subprograms and variables respectively. However, we do not recommend that you use this method.

2. The definition of subprograms is global. The definition of the front edge will be overwritten after the subprograms with the same name.

3. Define a subroutine

Sub subprogram {

# Body

}

4. Call a subroutine

& Subprogram

5. All subprograms return values. The result of the last row is returned by default.

6. When the subroutine does not define a parameter, the passed parameter is passed to "@ _" by default, and can be accessed through $ _ [0], $ _ [1;

7. Create a private variable in the subroutine

My ($ M, $ N );

My will not change the context of the assignment;

My variable is only used in the innermost statement block where it is located (such as for and foreach). If it is exceeded, it is invalid;

If it is not in any statement block, it is valid for the entire program file and used as the private variable of the entire program file;

My is enclosed by parentheses, and all variables in the brackets are declared. Without parentheses, it is only useful for the variable following it. My ($ M, $ N), my $ m, $ N;

8. Use strict: use more strict syntax rules, which are useful to the current statement block and the remaining parts;

9. If Perl syntactically identifies a subroutine call, it can omit "&". However, when the subroutine name is the same as the Perl built-in function, "&" cannot be omitted;

10. Persistent private variables

Example:

Sub run {

State $ n = 0;# Initialization once, re-execution will be ignored

$ N + = 1;

}

Cannot be used for Array and hash type variables;


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.