Using Perl on Windows, how can I guarantee that I get the path in the right case after chdir?

Consider the following code:

print cwd . "\n"; $str= "../source"; # note the lower case 's' chdir($str); print cwd . "\n"; 

If my current directory is c:\parentdir\Source (note the capital "S"), the output of this will be:

  c: / parentdir / Source
 c: / parentdir / source

This causes problems in my routine, which takes care of the correct case of folder names. $ str is passed to my subroutine, so I cannot know in advance whether it has the correct case. How to determine the correct path name that matches $str ?

More details here:

  • I understand that ../source is a pathological example, but it serves to illustrate the problem. This happens even if $str asks for a folder other than the current one.
  • I tried many options, including rel2abs , glob search on $str and others, but all of them seem to return " source " instead of " source ".
  • I could look for $str/.. for all directories, convert them all to absolute paths and compare them with the version of the absolute path $str , but this seems like a hack. I was hoping for something more elegant.
+4
source share
1 answer
 #!/usr/bin/perl use warnings; use strict; use Cwd; use File::Spec::Functions qw( canonpath ); use Win32; print canonpath( cwd ), "\n"; chdir '../source'; print canonpath( cwd ), "\n"; print canonpath( Win32::GetLongPathName( cwd ) ), "\n"; 
  C: \ DOCUME ~ 1 \ ... \ LOCALS ~ 1 \ Temp \ t \ Source> t
 C: \ DOCUME ~ 1 \ ... \ LOCALS ~ 1 \ Temp \ t \ Source
 C: \ DOCUME ~ 1 \ ... \ LOCALS ~ 1 \ Temp \ t \ source
 C: \ Documents and Settings \ ... \ Local Settings \ Temp \ t \ Source 
+10
source

Source: https://habr.com/ru/post/1379471/


All Articles