I have a perl script that takes 1 argument, stores the result in an array, after that goes to the element in the array and calls itself with the element of the array as argument and pushes the new result to the array. For some reason I can’t get it to work and the array doesn’t change.
I have something like:
#some code here that stores result in @result
foreach $i (@result){
push(@result, `perl ./myperlscript.pl "$i"`);
}
How do I get this to work? And is there a way to recursively call myperlscript.pl better than this?
If you are writing a script that calls itself you may face a design problem. Try putting the main logic into a subroutine and call that. This gives you more control and is safer, as there is no shell escaping.
You also should not modify an array you are iterating over. This might be better:
If you wan’t to go in indefinitively, you should use the traditional
forloop:If you can’t rebuild you script and you have to keep it calling itself, you should rather pipe the arguments, than putting them on the same line. Benefits include infinite length of data, and increased safety (no shell escaping!). Just consider what could happen with malformed data like
x" | tee "mySecretFile. And that is harmeless. But again, rather declare a subroutine instead of that.