in reply to windows perl and paths

Hello flieckster,

You only have to escape the backslashes if the string is double-quoted. So if you don’t need interpolation, the following are equivalent:

my $wilsonstiff = "Z:\/Partners\/Wilsons\/2push_test"; my $wilsonstiff = 'Z:\Partners\Wilsons\2push_test';

But as stevieb says, Windows understands forward slashes anyway, so you can just say:

my $wilsonstiff = "Z:/Partners/Wilsons/2push_test";

and it will still work, and allow variable interpolation if needed. For the various quoting options in Perl, see perlop#Quote-Like-Operators.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: windows perl and paths
by AnomalousMonk (Archbishop) on Nov 19, 2016 at 18:02 UTC
    ... the following are equivalent:

    my $wilsonstiff = "Z:\/Partners\/Wilsons\/2push_test";
    my $wilsonstiff = 'Z:\Partners\Wilsons\2push_test';

    I don't understand that statement:

    c:\@Work\Perl>perl -wMstrict -le "my $x = qq{Z:\/Partners\/Wilsons\/2push_test}; my $y = 'Z:\Partners\Wilsons\2push_test'; ;; print qq{'$x' '$y'}; die 'not equivalent!' unless $x eq $y; " 'Z:/Partners/Wilsons/2push_test' 'Z:\Partners\Wilsons\2push_test' not equivalent! at -e line 1.

    But I completely agree with you and stevieb that Perl understands forward slashes in paths under Windows perfectly well:

    c:\@Work\Perl>perl -wMstrict -le "my $path = 'c:/@Work/DMD2/learn'; ;; chdir $path; print for grep -d, glob 'w*'; " wc weather_report writef_format
    So I don't really understand the OP...


    Give a man a fish:  <%-{-{-{-<

      Could it be the nature of the test?

      Running your code seems to me to show that -- as asserted with respect to the use of forward slashes in windows -- M$ sees the two as equivalent (or "equal?") while perl does not. *1

      *1UPDATE! Sloppy language on my part. Insert "paths" before the closing double-hyphen. See, especially, eyepopslikeamosquito's at this node.

      Formatting in the code, the end block, etc. solely to make my reading of it easier.

      #!/usr/bin/perl -w use strict; #1176161 my $x = qq{Z:\/Partners\/Wilsons\/2push_test}; my $y = 'Z:\Partners\Wilsons\2push_test'; print qq{'$x' '$y'}; print "\n\t"; END { print "Not equivalent! \n" unless $x eq $y; } END { # multiple end blocks are executed in rev +erse order if ($!) { print "Error is $!, \n"; } else { print "No (perl) error found.\n\t Done with the second END bl +ock\n\n"; } } exit();
      and output, as expected:
      C:\Users\wheelerw>D:\_Perl_\PMonks\1176161.pl 'Z:/Partners/Wilsons/2push_test' 'Z:\Partners\Wilson No (perl) error found. Done with the second END block Not equivalent!

        I expressed myself rather poorly.

        What I meant to convey was my lack of understanding of the assertion in the OP of a need to escape forward slashes in the example code given in the OP, and my understanding, shared alike by all who replied in this thread, that Perl was quite happy with forward slashes in strings, paths, etc., regardless of OS, and equally happy with backslashes (if you can figure out the proper way to get them into a string) in paths under Windows.

        Rather vapidly, I went off on the tangent of wittering on about the differences between the strings that Athanasius gave as examples, ignoring, if only for the moment, their equivalence as paths. Oh, well...


        Give a man a fish:  <%-{-{-{-<

        ...with respect to the use of forward slashes in windows -- M$ sees the two as equivalent...
        To be more precise, the Windows API treats forward slashes and back slashes equivalently in file path arguments to all Windows API functions. However, Windows commands typically use forward slash for command line argument options.

        So, while using forward slashes in file paths is fine when calling Win32 API functions (as the perl C sources do when built for Windows), using them in command line arguments when running Windows commands can be problematic.

        In my Perl programs on Windows, I use forward slashes in all file paths, which works fine when calling all Perl internal functions. In the rare cases where I need to pass a file path to an external Windows command, I translate it like so: $filepath =~ tr{/}{\\};

        See also: forward vs back slash python question.

        Update: Note that all Win32 API functions have always treated forward slashes and back slashes equivalently in file path arguments. See also Re^2: What is the meaning of this line in Perl on linux? and Re^4: Changing filename extensions.