Well, for starters, you're using 'open' wrongly.
open my $fp , '>', 'xyz' ;
Is the recommended syntax.
the bare 'FP' you have there is strongly recommended against, as it is not a lexical.
Secondly, you're re-opening file-pointers as new things. This is not very good practice, it shouldn't be a problem, but its just a bad idea. You should close the file pointer or let it run out of scope ( via a lexical ).
Thirdly, '*STDOUT' is a reference.
my $fh = *STDOUT;
print "$fh\n"; #prints '*main::STDOUT';
So when you do this:
open $fh, '>abc';
you're doing
open *STDOUT, '>abc';
and if you immediately afterwards do
print "$fh\n";
you will note it still prints *main::STDOUT
;
Some interesting code snippets that clear this up:
my $fh = *STDOUT;
open $fh, '<', "foo.txt";
print $fh "hello";
# Filehandle STDOUT opened only for input at (eval 288) line 6.
my $fh = *STDIN;
open $fh, '<', "foo.txt";
print <>;
# contents of foo.txt here
Here's a recommended way to use open:
sub foo {
my $fh;
open $fh , '<', 'file.txt' or Carp::croak('Cannot Open File.txt');
# do stuff with $fh;
close $fh or Carp::carp('Something annoying in close :S ');
}
Note that if you omit close, the file will be closed as soon as $fh goes out of visibility.