Perl 不是外壳程序,通常最好避免使用反引号运算符和 system() 函数,除非没有其他选择,或者您故意进行快速'n'dirty hack。
Learn to write a subroutine and do your recursion that way. You shouldn't be tackling recursion if you've not got the hang of subroutines.
However, let's discuss what you're doing anyway.
Your backtick invocation is OK:
`perl ./myperlscript.pl "$i"`
... and will return whatever that process puts to stdout. It follows that your program must print to standard out.
In Perl, adding items to the list being iterated does cause the iteration to continue.
push (@arr,10);
foreach $i (@arr) {
print "$i\n";
if($i > 0) {
push(@arr,$i-1);
}
}
... prints a countdown from 10 to 0. So there is potential for your code to work. However I think it's a confusing model, and not a good habit to get into. Modifying the data structure you're looping over is not generally considered good practice.
Your code showed no evidence of a stopping condition. When you recurse, you always need to consider stopping conditions.
Note that anything that happens to the @result array in a backtick invocation, has no effect on the @result array in the current process. They are invisible to one another.