citromatik has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I have encountered a code like this in a production program I couldn't make run:
use strict; use warnings; my $ref_file = $ARGV[0]; if (isReadableFile ($ref_file)) { executeComm ("cat $ref_file"); } else { print STDERR "$ref_file Does not exist\n"; } ## In a different module... sub isReadableFile { my $file = shift; if (defined ($file) && # was a file name passed? ((-f $file) || (-l $file)) && # is the file a file or sym. link +? (-r $file) # is the file readable? ) { return 1; } else { return 0; } } sub executeComm { my ($comm) = @_; print "$comm\n"; system ($comm); print "$?\n"; }
A sample invocation should be something like:
$ perl test.pl file.txt
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:
$ perl test.pl file\ name.txt
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:
if (isReadableFile ($ref_file)) { $ref_file =~ s/ /\\ /g; ###### Added executeComm ("cat $ref_file"); } else { print STDERR "$ref_file Does not exist\n"; }
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?
citromatik
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Passing commands to subroutines
by JavaFan (Canon) on Jul 01, 2009 at 15:42 UTC | |
by BioLion (Curate) on Jul 01, 2009 at 16:11 UTC | |
by JavaFan (Canon) on Jul 01, 2009 at 17:11 UTC | |
by citromatik (Curate) on Jul 01, 2009 at 17:11 UTC | |
by ikegami (Patriarch) on Jul 01, 2009 at 18:45 UTC | |
by JavaFan (Canon) on Jul 01, 2009 at 17:29 UTC | |
by graff (Chancellor) on Jul 02, 2009 at 04:43 UTC | |
by Anonymous Monk on Jul 02, 2009 at 05:09 UTC | |
by repellent (Priest) on Jul 03, 2009 at 22:57 UTC | |
by Anonymous Monk on Jul 01, 2009 at 17:21 UTC | |
by citromatik (Curate) on Jul 01, 2009 at 17:44 UTC |