Subscribe to Windows IT Pro
September 24, 2001 12:00 AM

Progressive Perl for Windows: Strolling Down Win32 Paths

Windows IT Pro
InstantDoc ID #22352
Rating: (3)
Downloads
22352.zip

Recently, I've noticed a flurry of questions about Win32 paths coursing through the Internet's Usenet and email lists. Systems administrators often take paths for granted, but paths are the cornerstone of what an OS does—namely, manage files. If you've encountered problems in your Perl scripts, understanding how Perl deals with paths in Win32 OSs might help you resolve those problems. Here's a detailed look into the differences between local paths and Universal Naming Convention (UNC) paths in Win32 OSs, how Win32 paths differ from UNIX and DOS paths, and how absolute and relative paths differ.

Local vs. UNC Paths
Win32 OSs have two basic types of paths: local paths and UNC paths. Local paths are the better known because they were introduced back in the days of DOS, before Windows came onto the scene.

Local paths, such as C:\temp, begin with a drive letter. Because only 26 valid drive letters exist (i.e., A through Z), you have a limited number of local paths with which to work. When you consider that some of those drives are already taken, you have even fewer local paths. For example, typically a computer's A drive is the 3.5" floppy disk drive, the C drive is the hard disk drive, and the D drive is the CD-ROM drive. Although the latest Win32 platforms let you change a drive's letter (e.g., change the drive for CD-ROMs from D to W), you're still using one of the 26 drives for that purpose. The only way to gain additional local paths is to map a drive letter to a remote machine's shared directory.

The UNC path points to a directory or file relative to a particular networked machine. The format of this type of path is

\\machine_name\share_name\temp

where machine_name is a computer's name and share_name is a shared directory's name. Double backslashes (\\) preface the computer's name, which can be a valid NetBIOS name, DNS name, or IP address. Any further entries at the end of the path (in this case, temp) are relative to the machine's shared directory.

UNIX vs. Win32 Paths
Paths exist in both the UNIX and Win32 worlds. Administrators new to the Win32 platform need to understand the differences between the two platforms' paths. The two main differences are the delimiters and the case of the characters.

Delimiters. The most important difference between UNIX and Win32 paths is the delimiter that separates each segment of the path. In UNIX paths, a forward slash (/) delimits directories and files (e.g., /usr/bin/ foo.pl), whereas in Win32 paths, a backslash (\) is the delimiter (e.g., \temp\foo.pl). This distinction is important because Perl lets you use different delimiters to make porting scripts between platforms easier. Therefore, in Perl code, the UNIX path

@Statistics = stat( "/temp/foo.pl" );

is equivalent to the Win32 local path

@Statistics = stat( "\\temp\\foo.pl" );

Note the double backslashes in the Win32 local path. In Perl, you need to flag, or escape, a backslash with the escape character, which happens to be a backslash.

Interestingly, Perl lets you use single quotes (') as well as double quotes (") to enclose strings, including paths. However, single-quoted strings differ from double-quoted strings in two ways:

  • Unlike double-quoted strings, single-quoted strings don't support interpretation of variables. In other words, the Perl interpreter doesn't expand variable names in single-quoted strings.
  • Although you have to escape backslashes in double-quoted strings, you don't have to escape them in single-quoted strings. The Perl interpreter doesn't recognize escape sequences in single-quoted strings, except in two instances. First, the Perl interpreter recognizes the \' sequence (e.g., '\dave\'s script.pl) so that you can insert a single quote inside a single-quoted string. Second, the Perl interpreter recognizes the \\ sequence, which is useful when the sequence immediately precedes the ending single quote (e.g., '\temp\\'). This exception lets you insert a backslash at the end of a path. In this case, neither exception applies, so you can write the previous Win32 local path as
@Statistics = stat( '\temp\foo.pl' );

When you enclose a Win32 UNC path in single quotes, you run into a slight problem. The Perl interpreter reads the double backslash at the beginning of the UNC path as an escaped single backslash. The only workaround is to escape the first backslash in the pair with another backslash. Therefore, a UNC path in single quotes might look like

@Statistics =
   stat( '\\\server\temp\foo.pl' );

You handle double-quoted UNC paths similarly to single-quoted UNC paths. The only difference is that you need to escape every backslash:

@Statistics =
   stat( "\\\\server\\temp\\foo.pl" );

Although the Perl language lets you use Win32 or UNIX paths, many Perl extensions aren't as flexible, so using a path with forward slashes instead of backslashes would likely result in an error. Thus, when you write Perl code for Win32 OSs, a good rule of thumb is to follow Win32 conventions.

Character case. Unlike UNIX paths, Win32 paths are typically case insensitive. One exception might occur if you use an alternative (non-FAT16, non-FAT32, or non-NTFS) Installable File System (IFS) driver. You can develop an IFS driver that imposes case restrictions. For example, if your company has an NFS file-system driver, you might decide to impose case sensitivity. However, the default Win32 file systems (i.e., FAT16, FAT32, and NTFS) are case insensitive, so the code that Listing 1 shows is completely legal to use with those file systems.

In Listing 1, the first line lowercases the first parameter passed into the script, then assigns that parameter to the $Path variable. The call to the open() function works regardless of the case of the $Path variable's value.

Related Content:

ARTICLE TOOLS

Comments
  • Dirk
    5 years ago
    Aug 01, 2007

    na

  • Anonymous User
    7 years ago
    May 02, 2005

    gregw123: Just escape the $, as in:

    "\\\\\\\\localhost\\\\c\\$"
    or
    '\\\\\\localhost\\c$'

    Steve

  • gregw123
    8 years ago
    Sep 16, 2004

    How about Admin shares like C$. "$" stands for scalar in perl.

  • Robert
    8 years ago
    Apr 09, 2004

    update the document! forward slashes are completely legal in paths using perl. Also for UNC paths. I'm just having a problem using UNC paths on unix systems!

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.