Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

windows perl and paths

by flieckster (Scribe)
on Nov 19, 2016 at 13:49 UTC ( #1176150=perlquestion: print w/replies, xml ) Need Help??

flieckster has asked for the wisdom of the Perl Monks concerning the following question:

i'm new to PERL on windows, always have been on a mac, so i'm having some issues. the code below works, but why do i have to escape all the slashes? is there a way around this?
#!/usr/bin/env perl -w my $wilsonstiff = "Z:\/Partners\/Wilsons\/2push_test"; chdir( $wilsonstiff ) or warn "Cant chanage to Z:/Partners/Wilsons/2pu +sh_test $!"; (@upload_wilsonstiff) = glob "*"; my $upload_count_wilsonstiff = @upload_wilsonstiff; print "$upload_count_wilsonstiff files found\n";

Replies are listed 'Best First'.
Re: windows perl and paths
by Athanasius (Archbishop) on Nov 19, 2016 at 14:20 UTC

    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,

      ... 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!
Re: windows perl and paths
by stevieb (Canon) on Nov 19, 2016 at 14:17 UTC

    Did you try it without the escaping backslashes? I find when I'm coding on Windows, I only need to escape a backslash in a path. Forward slashes seem to work ok without escape, so that's all I use. What version of Perl?

      I'm on strawberry perl, the other way and it can't find the directory at all.

        So you're saying that you get the specified warning when you try to chdir to Z:/Partners/Wilsons/2push_test?

        Can you, from the command line, cd Z:\Partners\Wilsons\2push_test successfully?

        These are all Strawberry Perl installs, that I ran this code against:

        use warnings; use strict; use feature 'say'; use Cwd; say getcwd(); chdir 'c:/repos' or die $!; say getcwd();

        Output:

        c:\>berrybrew exec perl chdir.pl Perl-5.24.0_64 ============== c:/ c:/repos Perl-5.22.2_64 ============== c:/ c:/repos Perl-5.18.4_64 ============== c:/ c:/repos Perl-5.10.1_32 ============== c:/ c:/repos

        So using forward slashes without escaping them most definitely works all the way back to at least 5.10.

Re: windows perl and paths
by Discipulus (Abbot) on Nov 19, 2016 at 16:58 UTC
    Hello,

    Me too, I strongly suggest forward slashes like in "./path/to/your/file.ext" you gain a little in portability too. If you need more portability you have to use the core module File::Spec and concatenate file and paths using his methods.

    As you are new on such platform I strongly advocate the use of Strawberry Perl that come with many goodies that help you to work as you do in other platform like installing directly from CPAN.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-05-28 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (99 votes). Check out past polls.

    Notices?