in reply to Escape special chars in a path
This is more of an observation rather than answering your question but using alternative delimiters in your substitutions can make the code much easier to read. Also I find that it is easier to use the hex value for a backslash rather than the character itself, e.g. converting to Unix-style paths ...
johngg@aleatico:~$ perl -Mstrict -Mwarnings -E 'say q{}; my( $path ) = qw{ C:\Users\fred\file.txt }; say $path; $path =~ s{\x5c}{/}g; say $path;' C:\Users\fred\file.txt C:/Users/fred/file.txt
Going the other way, I would use a lookahead, the chr function and the e modifier in a substitution to do escaping.
johngg@aleatico:~$ perl -Mstrict -Mwarnings -E 'say q{}; my( $path ) = qw{ C:\Users\fred\file.txt }; say $path; $path =~ s{(?=\x5c)}{ chr 92 }eg; say $path;' C:\Users\fred\file.txt C:\\Users\\fred\\file.txt
It seems easier to my eyes. I hope this is helpful.
Cheers,
JohnGG
|
---|
In Section
Seekers of Perl Wisdom