Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: What is the meaning of this line in Perl on linux?

by Marshall (Canon)
on Mar 29, 2022 at 02:22 UTC ( [id://11142474]=note: print w/replies, xml ) Need Help??


in reply to Re: What is the meaning of this line in Perl on linux?
in thread What is the meaning of this line in Perl on linux?

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.

Replies are listed 'Best First'.
Re^3: What is the meaning of this line in Perl on linux?
by eyepopslikeamosquito (Archbishop) on Mar 29, 2022 at 02:50 UTC

    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{/}{\\};

      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.

Re^3: What is the meaning of this line in Perl on linux?
by eyepopslikeamosquito (Archbishop) on Mar 29, 2022 at 05:40 UTC

    > Modern Windows is fine with forward slashes

    To be pedantic, note that all Win32 API functions have always treated forward slashes and back slashes equivalently in file path arguments ... and the Win32 API was introduced with Windows NT/Windows 95 in the 1990s ... so you don't really need an especially "modern" version of Windows. :)

      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.

        I guess "pre-Modern" means Windows 3.1, April 6, 1992?

        Nope. All DOS versions back to at least 2.0 (March 1983) supported both forward and backslashes in paths. Using the backslash was more or less just a convention, partially enforced by programs using the forward slash to indicate an option (instead of a path). But the switch character could be changed from DOS 2.0 up to 3.31 to something else, and so the forward slash could have been used in paths even on the command line (see Re^6: moving from mac to PC -- win internal commands). The DOS API functions (i.e. int 0x21) always accepted both forward and backslashes.

        DOS 1.x had no subdirectories and thus did not need and did not use forward or backslashes. Each (floppy) drive had exactly one directory, the root directory, and thus it was sufficient to prefix a filename with the drive letter and a colon to get an absolute path.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        As you might expect, Windows PowerShell is fine with forward slashes in file paths. Hopefully, as PowerShell gradually supplants the truly awful old CMD.EXE there'll be even less need to use backslashes in Windows file paths.

Re^3: What is the meaning of this line in Perl on linux?
by haukex (Archbishop) on Mar 29, 2022 at 08:14 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142474]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found