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 = (
[...]