|Just another Perl shrine|
Passing commands to subroutinesby citromatik (Curate)
|on Jul 01, 2009 at 15:39 UTC||Need Help??|
citromatik has asked for the
wisdom of the Perl Monks concerning the following question:
I have encountered a code like this in a production program I couldn't make run:
A sample invocation should be something like:
And if file.txt exists, is a regular file or links to a file and is readable, the command cat file.txt is executed inside the executeComm sub.
The problem arises when the file name or its path contains spaces:
This should be a valid invocation, but executeComm will receive the command cat file name.txt, and consequently, will fail. The same would happen if $ perl test.pl 'file name.txt' is passed, and perl test.pl 'file\ name.txt' would succeed, but the file tests on isReadableFile fail
A possible patch would be to escape every space after the file tests:
But this seems a weak patch... Is this solution portable? Do you anticipate the appearance of more problems?, how would you solve this in production code?