Perl calls sub-processes by referencing sub-Processes

Source: Internet
Author: User
Tags perl interpreter
Assume that I have a sub-process, which is defined as follows:

Sub test_sub {
Print "test ";
}

I have another variable:

$ Test = "test_sub ";

The value of $ test is the name of the face process. Since I know the sub-process name through $ test, can I use $ test to call this sub-process? That is to say, I asked the user to input a sub-process name from the command line, and then the corresponding sub-process can be called according to the name.
Method 1: Call the sub-process directly based on the sub-process name entered by the user.
Complete exampleCodeAs follows:

Use warnings;
No strict 'refs ';

Sub test_sub {
Print "test ";
}

$ Test = "test_sub ";

$ Test-> ();

& {$ Test} (); # anyway to invoke the subroutine

Note that you must use

No strict 'refs ';

This statement. Otherwise, the script cannot run. The Perl interpreter will prompt an error similar to the following:
Global symbol "$ test" requires explicit package name at test2.pl Line 10.
Method 2: the more common method is to use a hash for dispatching and call the corresponding sub-process based on the user input request. For example, the following % dispatcher plays the role of binding the identification tag and corresponding sub-process reference. Through this unique tag (key), we can easily find the corresponding sub-process reference.

My % dispatcher = (
[...]

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.