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;
}
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.