Re^2: What is the meaning of this line in Perl on linux?
by LanX (Saint) on Mar 28, 2022 at 09:31 UTC
|
> to avoid LTS
I learned the term slasheritis in (emacs) lisp, which doesn't know raw strings. So since all regexes are strings you have to type \\\\ to escape a single backslash from two levels of interpretation. That's even worse than in Perl.
The discussion of other programming languages in this WP page looks incomplete to me, because it doesn't always handle the question how to escape the delimiter of a raw string.
Like this Ruby code
> 'C:\Foo\Bar.txt'
won't have any possibility to escape the ' , because \ isn't meta anymore.
While Go discussed this limitation
> there is no escape code for a backtick in a raw string.
| [reply] [d/l] [select] |
Re^2: What is the meaning of this line in Perl on linux?
by Marshall (Canon) on Mar 29, 2022 at 02:22 UTC
|
Wow! lots of LTS discussion!
I would prefer: s|/|\\|g; to s{/}{\\}g; but they are equivalent.
I also like your idea of using a module for the translation.
However, I am perplexed as to why the OP wants to do that in the first place? I write a lot of Perl on Windows and Windows just isn't DOS anymore. Modern Windows is fine with forward slashes. Better than good code is: "no code". I don't see the need to convert forward slashes to back slashes.
Having said that, I do have a vague recollection of some command from the Windows command line where I had to use backslash as part of the path to get the command to work. This may have been a legacy issue with ancient MS code. I have never seen such a requirement when using Perl. It could exist, but I haven't come across it yet.
Anyway, my advice is "don't run a format conversion where it is not necessary".
Update: Clarification: On the command line, Windows will display back slash, but that does not mean that you have to use them:
C:\Users\xxx\Documents\PerlProjects> cd C:/Users
C:\Users>
A Perl program is not getting what Windows displays for its command line. | [reply] [d/l] [select] |
|
Modern Windows is fine with forward slashes
Not quite, as described in more detail at: Re^4: windows perl and paths
BTW, I'm surprised nobody suggested using the tr transliteration operator: $localdir =~ tr{/}{\\};
| [reply] [d/l] |
|
Ok. Excellent point!
You point out what was probably the cause of my vaguely remembered problem: "However, Windows commands typically use forward slash for command line argument options". If forward slash means "option" then the command will "barf" with a forward slashed file path because it thinks that part of the file path is an option! That explains my error case!
I think we are in 100% agreement about file paths within Perl. Perl itself will always get forward slashed file names. Always use forward slash for file paths within Perl programs.
There is an exception if you want to launch a Windows system command. That may barf due to conflicts between an option switch vs a file path.
If you need to do a translation for that reason, I would put a comment in the code as to why the "/" is being translated to "\".
I stand by my recommendation not to translate formats unless you need to.
Your suggestion about tr is also to be commended. For a one 2 one translation, nothing beats tr for performance.
| [reply] |
|
| [reply] |
|
What a hoot!
I guess "pre-Modern" means Windows 3.1, April 6, 1992?
Most folks who just look at the command line, see the back slashes and don't really think about or reference the API spec.
I think that the case for using forward slash on Windows as well as Unix has been made. There are many folks who don't know that forward slash works on Windows (with exception that you pointed out). The fact that the Windows command line displays back slashes throws many folks off.
| [reply] |
|
|
|
|
|
I don't see the need to convert forward slashes to back slashes.
With all the talk of APIs in this subthread I'd just like to remind everyone of the probably hundreds if not thousands of nodes on this site of people doing things like split /\\/, /\\([^\\]+)$/, and many more variations of hardcoded separators. Extrapolate that out to all the other programming languages and I think there is tons of non-portable code out there, therefore I personally always use the respective OSs' native format.
| [reply] [d/l] [select] |
Re^2: What is the meaning of this line in Perl on linux?
by Anonymous Monk on Mar 28, 2022 at 08:08 UTC
|
There is changing the Two forward slashes to two backward slashes
$localdir =~ s/\\\///g;
The above line can i use for changing the backward slashes to forward slashes
| [reply] [d/l] |
|
# delimiters
# | | |
# v v v
$localdir =~ s/\//\\/g;
# ^^ ^^
# | |
# search part replacement
But for regexes, I would recommend using what I suggested:
$localdir =~ s{\\}{/}g;
But as I said, using a module for this is even better:
use warnings;
use strict;
use Path::Class qw/foreign_file foreign_dir/;
my $file = foreign_file('Win32', "..\\Hello\\World.txt");
print $file->as_foreign('Unix'), "\n"; # prints "../Hello/World.txt"
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
No. The above line is a syntax error and Perl tells you so.
Maybe you want to learn about Perl quoting rules for strings? The backslash \ is special in Perl strings and is used to mark the next character as exempt from the quoting rules. See Quote-and-Quote-like-Operators, especially the section about Escape Sequences. There it explains why you need to write \\ when you want to match a single \ in a regular expression.
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
| [reply] |
|
|
|