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
*/
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.