in reply to File Handles

Thanks for your response.

I ran into something at work today that stumped me. I have always opened up files like this:

open FILEHANDLE, ">filename.txt" or die( );

I have then passed FILEHANDLE around to other functions like this:

MyFunc(\*FILEHANDLE);

I guess I have learned this in the past, but I was reminded today that FILEHANDLE is global. That seems crazy and not a very good programming practice to now have FILEHANDLE globally accessible to all of your code. I ran into something on the web today that showed a different way to open files:

open my($FileHandle), ">filename.txt" or die( );

This seems like a better way to go since $FileHandle stays within the scope of where it is defined and is not global.

Is this new approach a good idea?

How come I don't see it in the Camel book etc.?

Any help would be appreciated.



Thanks

============================================

Here is some sample code:

#!/usr/bin/perl use strict(); main(); sub main { my $FileHandle = CreateFile1("test1.txt"); print $FileHandle "hello"; print FILEHANDLE " world"; close $FileHandle; my $FileHandle2 = CreateFile2("test2.txt"); print $FileHandle2 "hello file 2"; close $FileHandle2; } sub CreateFile1 { my ($strFileName) = @_; #FILEHANDLE is globally accessible open FILEHANDLE, ">" . $strFileName; return \*FILEHANDLE; } sub CreateFile2 { my ($strFileName) = @_; #$FileHandle is private open my($FileHandle), ">" . $strFileName; return $FileHandle; }

Replies are listed 'Best First'.
Re^2: File Handles
by Corion (Patriarch) on Mar 08, 2007 at 07:53 UTC

    The old way of avoiding "global" filehandles was:

    local *FILEHANDLE; open FILEHANDLE, ">$strFileName" or die "Couldn't open '$strFileName': $!";

    The new, and far better way of doing filehandles is the lexical way, and there are no disadvantages that spring to mind immediately, so you should be using the new way unless your platform requirements only allow for Perl 5.005 (default on Solaris 2.6) or Perl 5.004 (default on older Solaris I believe).

Re^2: File Handles
by jwkrahn (Abbot) on Mar 08, 2007 at 08:09 UTC
Re^2: File Handles
by rodion (Chaplain) on Mar 08, 2007 at 08:21 UTC
    Using scalars for file handles came in with 5.6.1 as well, and yes, it is encouraged. Note that it requires that the variable be uninitialized. It makes it much easier to put file handes in an array or a hash.

    I don't have my 3rd Edition of the Camel book here at home, but I'm pretty sure it's in there, although it's not in the 2nd edition. (The third edition is considerably larger, to accomodate the many additions to perl.)

    The "perldoc perl561delta" command I mentioned previously gives this information as well, just ahead of the paragraph about 3-argument open(). It's also on the web at File and directory handles can be autovivified. It's nicely described with a concise example.

    Using "open delta" as the search string at Perl documentation is an alternative to the "grep" method mentioned in my previous post. The header at that link says its 5.8 documentation, but the previous deltas are included. Searching the web version gives you an interface that's a lot prettier than what you get grepping the text of the perl docs, but the web interface doesn't show you the context lines in each file, so you can't go to the document you want as fast. I find both approaches helpful, for different needs, but either interface will do the job, according to your preference.

Re^2: File Handles
by Joost (Canon) on Mar 08, 2007 at 10:14 UTC