【发布时间】:2015-08-12 15:52:01
【问题描述】:
我正在运行一个 Perl 脚本来控制另外两个脚本。主程序的用户可以通过下面这行调用两个辅助脚本。
do 'Task_A.pl';
脚本然后运行 Task_A.pl 但我想找到一种方法然后退出 Task_A.pl 并返回到主程序。如果可能的话,我想返回上次调用辅助脚本的函数。我不确定这叫什么,但我感谢任何可能的解决方案的意见。
这是整个主程序,目前不多。
my $selecion;
#Looping variables.
my $program_loop = 1;
while ($program_loop == 1)
{
print "Please choose one of the programs listed in the menu.\n";
#Program menu where the user chooses from the presented options.
print "[1] - Script A.\n";
print "[2] - Script B.\n";
print "[3] - Exit program.\n";
my $user_input = <>;
if ($user_input == 1) # <-- Scrip_A.pl
{
do 'Task_A.pl';
}
elsif ($user_input == 2) # <-- Scrip_B.pl
{
do 'Task_B.pl';
}
elsif ($user_input == 3) # <-- Exit Program
{
#The user can choose to exit from the menu.
print "The program will now exit.\n";
exit;
}
}
【问题讨论】:
-
程序员不用
die,他们只是GOSUB没有RETURN。 -
我认为您真正想要做的是使用函数调用。
do很可能是个糟糕的主意。请显示更多实际代码。也不清楚主脚本的用户(是程序员)如何调用辅助脚本。 -
我也是这么想的。使用
do可能不是最好的方法,但我不确定我的选择是什么。 -
如果没有必要的共享内存/变量,启动一个新进程。或者,将您的两个程序重构为共享相同接口并使用它们的模块。如果有很多全球遗留问题正在发生,这将很难。由于您的程序没有
use strict和use warnings,我会假设是这种情况。你为什么要这样做? -
一般来说,如果你使用
do,你就做错了。
标签: perl