How can I call backticks without shell processing?

This is a bit tricky. Instead of writing

    @ok = `grep @opts '$search_string' @filenames`;
You have to do this:

    my @ok = ();
    if (open(GREP, "-|")) {
        while (<GREP>) {
	    chomp;
            push(@ok, $_);
        }
	close GREP;
    } else {
        exec 'grep', @opts, $search_string, @filenames;
    }
Just as with system(), no shell escapes happen when you exec() a list. Further examples of this can be found in perlipc/"Safe Pipe Opens".

Note that if you're stuck on Microsoft, no solution to this vexing issue is even possible. Even if Perl were to emulate fork(), you'd still be hosed, because Microsoft gives no argc/argv-style API. Their API always reparses from a single string, which is fundamentally wrong, but you're not likely to get the Gods of Redmond to acknowledge this and fix it for you.


Back to perlfaq8