Opening Files

 <  Day Day Up  >  

To read or write files in Perl, you need to open a filehandle . Filehandles in Perl are yet another kind of variable. They act as convenient references (handles, if you will) between your program and the operating system about a particular file. They contain information about how the file was opened and how far along you are in reading (or writing) the file; they also contain user -definable attributes about how the file is to be read or written.

From previous hours you're already familiar with one filehandle: STDIN . This filehandle is given to you automatically by Perl when your program starts, and it's usually connected to the keyboard device (you'll learn more details about STDIN later). The format for filehandle names is the the same as that for variable names outlined in Hour 2, "Perl's Building Blocks: Numbers and Strings," except that no type identifier appears in front of the name ( $ , @ ). For this reason, it's recommended that filehandle names be in uppercase so that they do not clash with Perl's current or future reserved words: foreach , else , if , and so on.

By the Way

You can also use a string scalar or anything that returns a string ”such as a function ”as a filehandle name. This type is called an indirect filehandle . Describing their use is a bit confusing for a primer in Perl. For more information on indirect filehandles, see the online documentation on the open function in the perlfunc manual page.


Any time you need to access a file on your disk, you need to create a new filehandle and prepare it by opening the filehandle. You open filehandles, not surprisingly, with the open function. The syntax of the open function is as follows :

 open(  filehandle, pathname  ) 

The open function takes a filehandle as its first argument and a pathname as the second argument. The pathname indicates which file you want to open, so if you don't specify a full pathname ”such as c:/ windows /system/ open will try to open the file in the current directory. If the open function succeeds, it returns a nonzero value. If the open function fails, it returns undef (false):

 if (open(MYFILE, "mydatafile")) {     # Run this if the open succeeds } else {     print "Cannot open mydatafile!\n";     exit 1; } 

In the preceding snippet, if open succeeds, it evaluates to a true value, and the if block is run with the open filehandle called MYFILE which is now open for input. Otherwise , the file cannot be opened, and the else portion of the code is run, indicating an error. In many Perl programs, this "open or fail" syntax is written using the die function. The die function stops execution of your Perl program and prints an error message:

 Died at  scriptname  line  xxx  

Here, scriptname is the name of the Perl program, and xxx is the line number where the die was encountered . The die and open functions are frequently seen together in this form:

 open(MYTEXT, "novel.txt")  die; 

This line is read as "open or die," which sums up how you will usually want your program to handle the situation when a file can't be opened. As described in Hour 3, "Controlling the Program's Flow," if the open does not succeed ”if it returns false ”then the logical OR ( ) needs to evaluate the right-hand argument (the die ). If the open succeeds ”if it returns true ”then the die is never evaluated. This idiom is also written with the other symbol for logical OR, or .

When you are done with a file, it is good programming practice to close the filehandle. Closing notifies the operating system that the filehandle is available for reuse and that any unwritten data for the filehandle can now be written to disk. Also, your operating system may allows you to open only a fixed number of filehandles; after that limit is exceeded, you cannot open more filehandles until you close some. To close filehandles, you use the close function as follows:

 close(  MYTEXT  ); 

If a filehandle name is reused ”that is, if another file is opened with the same filehandle name ”the original filehandle is first closed and then reopened.

Pathnames

Until now, you've opened only files with simple names like novel.txt that did not include a path . When you try to open a filename that doesn't specify a directory name, Perl assumes the file is in the current directory. To open a file that's in another directory, you must use a pathname . The pathname describes the path that Perl must take to find the file on your system.

You specify the pathname in the manner in which your operating system expects it, as shown in the following examples:

 open(MYFILE, "DISK5:[USER.PIERCE.NOVEL]")  die;    # VMS open(MYFILE, "Drive:folder:file")  die;    # Macintosh open(MYFILE, "/usr/pierce/novel")  die;    # Unix. 

Under Windows and MS-DOS systems, pathnames contain backslashes as separators ” for example, \Windows\users\pierce\novel.txt . The only catch is that when you use backslash-separated pathnames in a double-quoted string in Perl, the backslash character sequence gets translated to a special character. Consider this example:

 open(MYFILE, "\Windows\users\pierce\novel.txt")  die;   # WRONG 

This example will probably fail, because \n in a double-quoted string is a newline character ”not the letter n ”and all the other backslashes will get quietly removed by Perl. As you might guess from Hour 2, "Perl's Building Blocks: Numbers and Strings," one correct way to open the file is by escaping each backslash with another backslash, as follows:

 open(MYFILE, "C:\Windows\users\pierce\novel.txt")  die;   # Right, but messy. 

You can get rid of the double slashes by using the qq function as well. However, you can also use forward slash ( / ) in Perl, even under Windows and MS-DOS, to separate the elements of the path. Perl interprets them just fine, as you can see here:

 open(MYFILE, "C:/Windows/users/pierce/novel.txt")  die;   # Much nicer 

The pathnames you specify can be absolute pathnames ”for example, /home/foo in UNIX or c:/windows/win.ini in Windows ”or they can be relative pathnames ” ../junkfile in UNIX or ../bobdir/bobsfile.txt in Windows. The open function can also accept pathnames that are Universal Naming Convention (UNC) pathnames under Microsoft Windows. UNC pathnames are formatted like this:

 \  machinename  \  sharename  

Perl accepts UNC pathnames with either backslashes or forward slashes and opens files on remote systems if your operating system's networking and file sharing are otherwise set up correctly, as you can see here:

 open(REMOTE, "//fileserver/common/foofile")  die; 

On the Macintosh, pathnames are specified by volume, folder, and then file, separated by colons, as shown in Table 5.1.

 <  Day Day Up  >  


SAMS Teach Yourself Perl in 24 Hours
Sams Teach Yourself Perl in 24 Hours (3rd Edition)
ISBN: 0672327937
EAN: 2147483647
Year: 2005
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net