Hi Venki.
What is a file handle?
A file handle is a way to associate a file with a 'handle'. In other words, you 'interact' with the file *through* the file handle.
You could use a file handle like this:
#!/usr/bin/perl -w
use strict;
open( FH, ">myFile.txt" ) or die( "Oops! : $!\n" );
...code...
close FH;
The code above will try to open the file 'myFile.txt' for writing but if it cannot, it will print the word Oops! followed by the reason for the failure ( which is stored in the special $! variable ).
I'm not sure what you mean by a 'normal file open'. On *nix or BSD? Windows? It would most likely be platform dependent.
Using a file handle is a very powerful tool for a perl programmer. They permit us to open, close, print to, read from, etc. various files on our local machines or those across networks.
More info can be found here:
perldoc -f open
Hope this helps,
-Katie.
| [reply] [d/l] |
File handles are pointers to places on the filesystem (kinda), just like variables are pointers to places in memory.
so $blah points to say offset 1000 from where your programs memory starts, and if you opened /some/file as the filehandle IN, it points to a offset from the directory /some/. ...
Why would you use filehandles?
To access files and directories (on unix systems, all entries on the filesystem are really files themselves with some extra properties)...
How it works is
@blah = qw(foo bar baz);
open(IN,"/some/file") || die "Cant access file!\nReason: $!\n";
# now we want data out of predefined data
$data1 = $blah[0];
$line1 = <IN>;
# Directory access is slightly different, but basically the
# same.. here is a quick script which is basically the same
# as ls /some/dir
opendir(DIR,"/some/dir") || die "Cant open dir!\nReason: $!\n";
# instead of using <FILEHANDLE> on dirs we use readdir(FH)
foreach $file (readdir(DIR)) {
chomp($file);
print "$file\n";
}
closedir(DIR);
Perl finds where @blah points to in memory and grabs the offset for the first element, grabs the value and assigns it to the offset of data1.
The same kind of magic (though very different) happens for
the assignment to $line1. Find the offset on the filesystem for the entry of "file" in the directory "/some/.", find the value for the first element (read 'line') of the variable, and assign it to the offset of $line1
What is the difference between using filehandles and opening a file?
There is no difference between a "normal" open call to your filesystem, and having perl open a file. Both return a pointer to the actual contents (or offset it you prefer) of the file.
So I guess what Im trying to say is, files are just another type of storage, much like memory. File I/O is slower than memory, but the process of accessing a file is similar to accessing memory. The benefits of using file I/O as opposed to memory I/O is we leave RAM for the system to run with, as well as the added benefit that when we reboot the system, the data is still there on the harddrive, which is always good :).
/* And the Creator, against his better judgement, wrote man.c
*/ | [reply] [d/l] |