
Dealing with the special chars in the file name
Quote:
> $, (, ), & and % that are changing the file names by replacing the
>$<name> and %<name> by null or the & and ( are giving "sh: syntax errors".
> Is there a elegant way to deal with this problem ?
>note: the file is not read directly, it is read through a lex filter, so the
>exact way is like
> open XXX, "my_lex_filter \"$filename\"|";
In the perlipc man page, there is an example of using the special "-|"
argument to open() to avoid calling /bin/sh. Minus the error checking,
it would be something like:
open XXX, "-|" or exec "your_lex_filter", $filename;
The open() sets up a pipe between the parent and child. The child then
calls "exec" with a list calls the program directly with the list as
the argument list. No intervening shell, so no shell metacharacters
are interpreted.
I guess you could also escape the metacharacters in the filename
before passing it to open():
# list of metacharacters from perl's do_exec function
$filename =~ s/([\$&*(){}\[\]'";\\|?<>~`\n])/\\$1/g;
open XXX, "your_lex_filter \"$filename\" |" or die;
--
Andrew Langmead