![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Files in PerlThe file-cabinet analogy carries neatly into Perl. The Perl functions that allow you to manipulate files are:
Its always best to learn by doing, so rather than wading into a lengthy discussion of these file functions, why dont you write a program to handle the rudimentary requirements of the access counter? Enter the following code, which we will analyze shortly: #!/perl/bin/perl # access.pl # # First version. Creates or opens a file with a number # in it, increments the number, writes it back. $CountFile = "counter.dat"; # Name of counter file. # Open the file and read it. If it doesn't exist, its "contents" # will be read into a program variable as "0". open (COUNT, $CountFile); $Counter = <COUNT>; # Read the contents. # Close the file, then reopen it for output. close (COUNT); open (COUNT, ">$CountFile"); # Increment $Counter, then write it back out. Put up a message # with the new value. Close the file and exit. $Counter += 1; print COUNT $Counter; print "$CountFile has been written to $Counter times.\n"; close (COUNT); # End access.pl Save this sample code as access.pl and run it a few times from the command line. Figure 4.4 illustrates what you should see.
Opening, Closing, and Writing FilesWhen you ran access.pl you probably noticed that it uses only two of the new Perl functions listed above: open and close. They really are all you need to do simple file manipulation. And, as usual when using new Perl stuff, you saw some strange and unfamiliar operators. Lets dissect this program so you can see what youve done. The first program line sets a program variable: $CountFile = "counter.dat"; # Name of counter file. The files name is counter.dat. Of course, you can call it anything you want, within the file-naming rules of your operating system. Whatever you do, it is best to do it here, at the top of the program, and put it in a variable. You could have referred to counter.dat throughout the program, but what if you decided to change it? By putting the name in a variable, youve ensured that it only has to be changed once. Laziness and impatience win the day again! File HandlesThe next two code lines in access.pl open the file and read whatever is in it. open (COUNT, $CountFile); $Counter = <COUNT>; # Read the contents. The arguments to open take this form open (HANDLE, Expression); where Expression usually is a file name or some variable that contains one. But what is this HANDLE? When you open a file, you actually are instructing Perl to perform a rather complex series of steps. Information about the file is stored in the computers memory where you can deal with it if you want, and the processes that allow you to read from it and write to it are initialized. Perl then needs a handy way to refer to the file, so it sets up a handle, which you can treat as a variable name for the file. Until the file is closed and the handle is put back into circulation, you will perform all of your file operations on this handle.
The next line of the program shows the handle in action, and it also shows a useful shortcut. $Counter = <COUNT>; # Read the contents. You can read directly from a file handle by putting brackets (<>) around it, as you did with <COUNT>. Because you can be reasonably certain that counter.dat contains either one number or nothing, you can simply set the value of the $Counter variable to whatever you read from the file. But what about the first time, through, when counter.dat doesnt even exist yet? When you open a nonexistent file name for reading, Perl returns a NULL handle. So, setting a scalar variable to the handles contents would effectively set the variable to 0.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |