S.
Sometimes, however, Win32 paths can create problems. For example, if you run an early Windows or DOS program, it won't understand a long pathname or one that contains spaces or mixed cases. Under these circumstances, the Win32 OS converts the path into a DOS-compatible version. When necessary, the Win32 OS
- shortens each component in the path (i.e., each directory name or filename) so that the component doesn't exceed the 8.3 format
- removes all spaces in the path
- uppercases all letters in the path
For example, a Win32 OS might convert C:\Program Files\My test file.txt to C:\PROGRA~1\MYTEST~1.TXT.
When the Win32 OS converts a filename or directory name, it renames the file or directory in relation to the files or directories that already exist. For example, suppose an application first creates the file C:\Program Files\My test file is cool.txt, then creates the file C:\Program Files\My test file is long.txt. When the Win32 OS converts the two filenames, it changes My test file is cool.txt to MYTEST~1.TXT and changes My test file is long.txt to MYTEST~2.TXT. If the application had created the files in reverse order, the Win32 OS would have changed My test file is long.txt to MYTEST~1.TXT and changed My test file is cool.txt to MYTEST~2.TXT. As this example shows, you can't determine a file's or directory's 8.3-compliant name (hereafter referred to as simply the short name) from its long name. Thus, you can run into problems if a backup program restores files in a different order than the original order of creation. In this situation, you shouldn't depend on the OS to determine the short names; rather, you should create 8.3-compliant filenames.
To determine the short name for a file or directory, you can use the Win32::GetShortPathName() function. This function returns the short pathname for a given long pathname. As Listing 2 shows, you simply pass in the long pathname as the function's sole parameter. Conversely, to obtain a long pathname, you can use the Win32::GetLongPathName() function. In this case, you pass the short pathname to the function. Win32 Perl (aka ActivePerl) includes both functions, so you don't need to load the Win32 extension before you use them.
Absolute vs. Relative Paths
In a Perl script, you can use absolute paths (also called full paths) or relative paths. An absolute path contains all the path components, from the drive letter or network share to the filename (e.g., C:\temp\foo.pl). A relative path doesn't contain all the path components (e.g., foo.pl or \somedir\foo.pl); its path is relative to the default directory.
All processes that run on a Win32 machine have a default directory. Typically, the default directory is the folder that contains the running program's .exe file. In Perl's case, the default directory is typically the directory in which the script resides. If you want a Perl script to open a file but you don't specify the file's absolute path, the script attempts to load the file from Perl's default directory.
You can use the Win32::GetCwd() function to determine the default directory for the current process. Cwd stands for current working directory; working directory is another name for the default directory. You can change the current default directory by calling the Win32::SetCwd() function and specifying the new directory name. You can even specify a UNC directory name. (Some programs, though, are unable to handle a UNC directory name.) Listing 2 contains sample code for the Win32::GetCwd() and Win32 ::SetCwd() functions.
Relative paths are extremely flexible. However, determining the full path to a file or directory based on a relative path can be difficult. The Win32:: GetFullPathName() function can help. When you call this function in an array context, the function returns an array of two strings. The first string specifies the pathname's drive and directory components, and the second string specifies the filename. For example, to determine your Perl interpreter's directory and filename, you can use either line of code that Listing 3 shows.
You can use the Win32::GetFul