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
*/