in reply to quoting issue with system command

In general, you need to learn more about Perl strings and interpolation.

You put $contig inside of single tick quotes, and that won't interpolate.

Meanwhile, you wrote ">".$maid_dir."\\".$maid."_aln.out" where you didn't interpolate where a Perl programmer normally would, even though you were using double-tick quotes! For comparison, ">$maid_dir\\${maid}_aln.out" (notice how the last one was handled since it was immediately followed by a character allowed in an identifier).

Meanwhile, use the 3-argument form of open. Don't concatenate the ">" with the name, but keep it as a separate argument. But, as another poster mentioned, passing the open file, stringified (whatever that does) to the blastall.exe command line doesn't make sense. You meant to pass the file name.

my $outfile= "$maid_dir\\${maid}_aln.out"; my $command= qq(c:\\blast\\bin\\blastall -p blastn -d "$hu_seq $hd_seq +" -i $contig +-o $alignment);
Note the use of qq(...) instead of double-tick quotes, so I could use " characters inside the string without escaping them. That's probably why you used single-quotes there in the first place.

I do like how you composed the command string separately to using it in the system command, so you could easily see what the command was.

I don't know why you are putting quotes around the two file names together with a space in between, instead of quoting each file name individually. Something about that blastall.exe I suppose, if that's correct. But beware of $maid_dir containing spaces.

—John

Replies are listed 'Best First'.
Re^2: quoting issue with system command
by lomSpace (Scribe) on May 13, 2009 at 15:42 UTC
    I have to put quotes around the two filenames with a space in
    between to create the database. $contig is the query. Also, yes
    $maid_dir has can have spaces in it.

    What is the best way to get around that?

    LomSpace
      Put the exe name in quotes, too:
      "D:\Program Files\foo.exe" arg1 arg2 "file_name with spaces.txt"
      The logic of system's argument processing is supposed to make sense on Unix, with underlying OS primitives that work in the same way. But it's a mess on Windows, where the documented meaning is implemented to some degree. Point is, it takes different code paths depending on the form, with different behavior. It's been a while since I looked at it. But if it calls the CMD.exe shell with the whole thing as a string, it behaves the same way as on the command line, as I showed. If it tries to call CreateProcess directly, the exe file name is a separate argument to that.

      I suppose I should refresh my knowledge before writing anything that makes use of that feature myself.

        So something like this:

        my $command = “c:\\blast\bin\\blastall.exe" -p blastn -d "$hu_seq $hd_seq" -i "$contig" -o $alignment”;
        print "$command\n";
        system($command);
        Oh, I am using it on Windows. My path is :

        $hu_seq is 'I:\13414 Ccl9 (88)\sequencing\13414_hu.fa'
        $hd_seq is 'I:\13414 Ccl9 (88)\sequencing\13414_hd.fa'

        This is what I am passing to the system command.
        my @command2 =('c:\\blast\\bin\\blastall.exe','-p', 'blastn','-d',"$hu_seq $hd_seq",'-i', $contig,'-o', $alignment);

        I am getting the following error message:

        NULL_Caption ERROR: Could not open i:\13414
        NULL_Caption ERROR: Arguments must start with '-' (the offending argument #7 was: 'Ccl9')

        I think that this is why I am unable to build the blastsearchable db.
        How do I get around those space in path to the variable?
        Thanks!
        LomSpace