Re: File Handles
by jwkrahn (Abbot) on Mar 08, 2007 at 02:05 UTC
|
| [reply] |
Re: File Handles
by rodion (Chaplain) on Mar 08, 2007 at 03:13 UTC
|
An easy method for looking up information like this in the future is to "cd" to Perl's Pod directory, which on my windows box is \Perl\lib\Pod. Then use
grep open perl*delta.pod |grep =head
The above gives
perl561delta.pod:=head2 open() with more than two arguments
and "perldoc perl561delta" gives you the documentation on changes. A "find" with "/open\(\)", with a few "next"s takes you right to what you want to see.
| [reply] [d/l] [select] |
Re: File Handles
by lustyx (Novice) on Mar 08, 2007 at 05:27 UTC
|
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;
}
| [reply] [d/l] |
|
|
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). | [reply] [d/l] |
|
|
| [reply] |
|
|
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.
| [reply] |
|
|
How come I don't see it in the Camel book etc.?
You probably have an old edition. The 3rd edition mentions it in (at least) the "open" section of the function reference chapter - page 748
| [reply] |
Re: File Handles
by lustyx (Novice) on Mar 08, 2007 at 14:50 UTC
|
Thanks, you guys are great.
Does anyone have any idea what the install base of Perl is like? Are most people on 5.6 and up?
Thanks,
Justin | [reply] |
|
|
I don't know, but here's some numbers to ponder.
The initial release of 5.6 was on 2000-03-22.
The last release of 5.6 was on 2001-04-08.
(Not counting a later release which only added support for more compilers.)
Perl 5.6 is 7 years old, and hasn't had a release in 6 years.
Hopefully, just about everyone is using at least 5.6 by now.
In case you are curious:
The initial release of 5.8 was on 2002-07-19.
The latest release of 5.8 was on 2006-01-31.
Information taken from Module::CoreList.
| [reply] |