If you were running with "use warnings" then Perl would have told you what the problem is. Filehandles aren't first-class variables in Perl so you can't just return a filehandle from a function and assign it to a variable like that. You're actually using the same filehandle (FILE) for both files - which is why you can't use it after you've closed the first filehandle.
You can get round this by using lexical filehandles.
sub openFile { my ($file, $accessMode) = @_; my $err; $accessMode = $MODE_READ unless defined $accessMode; if (exists($ACCESS_MODES{$accessMode})) { if (open(my $fh, "$ACCESS_MODES{$accessMode}$file")) { return $fh; } else { $err = $!; if (defined($returnOnError)) { return; } &throwGenError("Can't open $file: $err"); # throw exception } } else { &throwGenError("In openFile: Wrong access mode &accessMode"); } return; }
Some of your logic is a bit scary, so I haven't changed too much. You might need to check it carefully.
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
In reply to Re: Open file issues
by davorg
in thread Open file issues
by hotshot
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |