Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

file handles

by venki (Acolyte)
on Oct 15, 2002 at 13:38 UTC ( [id://205372]=perlquestion: print w/replies, xml ) Need Help??

venki has asked for the wisdom of the Perl Monks concerning the following question:


I am more interested to know about file handles. Please let me know if there is any link to read about it. Also please let me know the answer for the below questions

What is a file handle?

Why and where we use file handles?

What difference it makes from normal file open?
Thanks Monks

Replies are listed 'Best First'.
Re: file handles
by DigitalKitty (Parson) on Oct 15, 2002 at 13:53 UTC
    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.
Re: file handles
by l2kashe (Deacon) on Oct 15, 2002 at 14:53 UTC
    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 */

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://205372]
Approved by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found