Perl-13-subroutine-3

Source: Internet
Author: User
Tags perl interpreter scalar

I. subaccountProgramReferences:

In Perl, you can not only create references to scalar variables, array variables, and hash variables, but also create references to subprograms. References to subprograms are similar to function pointers in C/C ++; the constructor is as follows:

$ Pointer_to_sub = sub {# function body };

In this way, a reference to anonymous subprograms is established. The syntax for calling subprograms through subprograms is as follows:

& $ Pointer_to_sub (parameter list );

1. subroutine template:

Sometimes different subprograms can be created by returning different subprograms for reference, so that the operation can be templated, which is similar to the callback function in C/C ++, that is, the callback function;

2. arrays and subprograms:

Since the array @ _ is a one-dimensional array, no matter how many arrays are passed to the subroutine, the perl interpreter will convert all the arrays you pass into scalar values, then store these scalar values to the array @ _ in the order you pass. Similarly, if an array is returned in the return value, no matter how many sub-arrays are packed into a list, the perl interpreter converts these sub-arrays into scalar values and stores them in a one-dimensional array for the subroutine to return. Therefore, if you obtain the parameter list value in the form of a statement such as my (@ A, @ B) = @ _;, all array values are assigned to the array @, array @ B is an empty array, Because array @ _ is a one-dimensional array;

Therefore, when passing an array to a subroutine, pay attention to the following points:

◆ When the parameters passed to the subroutine are multiple arrays, you must use array references;

◆ Do not try to use the statement processing parameters such as @ array1, @ array2,..., @ arrayn) = @ _; In the subroutine unless you want

Set all parameters to a long array;

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.