什么是打破子程序并继续处理脚本其余部分的最佳方法?
即
#!/usr/bin/perl
use strict;
use warnings;
&mySub;
print "we executed the sub partway through & continued w/ the rest
of the script...yipee!\n";
sub mySub{
print "entered sub\n";
#### Options
#exit; # will kill the script...we don't want to use exit
#next; # perldoc says not to use this to breakout of a sub
#last; # perldoc says not to use this to breakout of a sub
#any other options????
print "we should NOT see this\n";
}
Run Code Online (Sandbox Code Playgroud)
以陈述明显为代表的子程序返回的最佳方式为代价......
return
Run Code Online (Sandbox Code Playgroud)
除非问题中有一些隐藏的微妙内容,否则不清楚
编辑 - 也许我看到你得到了什么
如果你编写一个循环,那么使用循环的有效方法 last
use strict ;
use warnings ;
while (<>) {
last if /getout/ ;
do_something() ;
}
Run Code Online (Sandbox Code Playgroud)
如果你重构这个,你最终可能会使用last来退出子程序.
use strict ;
use warnings ;
while (<>) {
process_line() ;
do_something() ;
}
sub process_line {
last if /getout/ ;
print "continuing \n" ;
}
Run Code Online (Sandbox Code Playgroud)
这意味着你正在使用last你应该使用的地方return ,如果你有游荡,你会得到错误:
Exiting subroutine via last at ..... some file ...
Run Code Online (Sandbox Code Playgroud)