|Think about Loose Coupling|
Re: calling a perl script within a perl scriptby jarich (Curate)
|on Nov 27, 2005 at 13:35 UTC||Need Help??|
I get the impression that you're approaching this problem from a shell scripting perspective. There's nothing wrong with this, but there may be other more elegant solutions you're overlooking.
For example, can you rewrite test_script.pl into a module which provides a subroutine you can then call for each filename? If so, this code would become a whole lot easier. Don't be scared of subroutines and modules, they're a lot faster than forking off processes which is what this code is doing.
It's hard to give you a good solution while test_script.pl remains a black box. Does it return input that you need, what does it return upon failure? Answers to these kind of questions can help us a great deal.
I think you'll find that the following code does what you have asked for, but I'm not convinced that it's actually the correct solution.
I use glob to ensure that you don't end up accidently working with files which aren't the kind you're looking for. Likewise, just in case files (or directories) are misnamed I also check that the file is a "normal file" which rules out directories, devices, streams etc.
Be aware that this code can open you to security problems. For example if evilly named files are inserted into that directory, bad things can happen. For example, consider the following possible file name:
As things in backticks *always* go past the shell this will be interpretted as if you'd typed on your command line:
Obviously this is a bad thing and my tests above will not stop this from happening.
If you can rewrite test_script.pl into a module which provides a subroutine which does the work you need, then that's almost certainly the way to go. If not, turn on Taint mode and check your filenames explicitly.
Hope this helpsjarich