Dealing with the special chars in the file name 
Author Message
 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



Mon, 11 Sep 2000 03:00:00 GMT  
 Dealing with the special chars in the file name

Quote:

> 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;

    quotemeta [quotemeta EXPR]

    This function returns the value of EXPR (or $_
    if not specified) with all non-alphanumeric characters
    backslashed. This is the internal function
    implementing the \Q escape in interpolative
    contexts (including double-quoted strings,
    backticks, and patterns).

From the camel book, page 201

--
Mike Hitchcock




Mon, 11 Sep 2000 03:00:00 GMT  
 Dealing with the special chars in the file name

Quote:


>> 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;
>    quotemeta [quotemeta EXPR]
>    This function returns the value of EXPR (or $_
>    if not specified) with all non-alphanumeric characters
>    backslashed.

You're right. I've always thought of quotemeta as only quoting regular
expression metacharacters. Since the rules for escaping shell
metacharacters and RE metacharacters are similar, it will work for
this as well.

By the way, the quotes I put in the open() function mess things up. It
should be:

$filename = quotemeta $filename;
open XXX, "your_lex_filter $filename |" or die;

--
Andrew Langmead



Tue, 12 Sep 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Shell special chars in file names?

2. Replacing special chars with special chars

3. rename files with special chars

4. How to deal with unicode char in perl?

5. How to deal with the meta char ...

6. Problems with 14 char file names on HP-UX

7. pattern matching with special char

8. escape special chars before loading into MySQL

9. Special chars

10. special chars

11. about special chars when split()

12. Text::CSV with Special Chars?

 

 
Powered by phpBB® Forum Software