u 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::GetFullPath Name() function to determine not only the absolute path to your Perl interpreter's directory but also the absolute path to the running script. As Listing 3 shows, you can call the function in an array context and assign the resulting array's first element to the $ScriptDir variable. You need to pass in the long pathname of the $0 variable to guarantee that you don't accidentally get the short pathname. I prefer to use the Win32::Get- FullPathName() function rather than the Win32::GetCwd() function to determine the running script's directory. Using the Win32::GetCwd() function doesn't guarantee that you'll receive the location of the Perl script that's running because you can inadvertently start a process that specifies an alternative default directory.
The Path Is Clear
I wanted to use this month's column to cover some of the finer points of Win32 paths. I hope that I've answered some of the questions about paths that might have been lurking in the back of your mind.