On 15/01/2014 03:09, Matt McAdory wrote:
Is there a method for determining the currently selected filehandle?
should I always check for undef and open my filehandle before writing to it?
use strict;
use warnings;
use autodie qw (:all);
use My:CustomMod_with_FH_write;
open (my $FH, ">", "filename.txt");
my $var = My:CustomMod_with_FH_write->new;
my @array = $var->sub1(); #writes to it's FH, but returns me an array of stuff
print $FH, "some stuff\n"; # works great.
while (@array) {
chomp;
my @array2 = $var->sub2($_); #gets some new stuff in another array,
prints again to the module FH
while (@array2) {
chomp;
my $thing = $var->sub3($_); # returns a scalar
print $FH "$thing\n";
################################################# BOOM!?!?
}
}
========
the last print always give me a ". . . . concatenate (.) to undefined
variable $FH near line . . . ." and I can't understand where I'm scoping
out and my $FH gets closed. Add the open below and it works. Granted
this is some dummy code, I'm not in the office right now to give
specifics, but sub calls to the module work fine. Write to the my
filehandle works fine . . . until I get to a certain level of nesting
and then it bombs and I don't understand why nor can I find a way to
print the active filehandle. What am I doing wrong? The module FH and my
FH are different files in different directories. I probably need to find
a different way of doing what I'm doing.
Should that BOOM always be:
if (undef $FH) {open (my $FH, ">>", "filename.txt");}
print $FH "$thing\n";
close $FH;
A friendly page in the fine manual to read. A nudge towards a llama or
camel reference would be appreciated. Several days show my googlefu to
be lacking. Where is my rookie mistake?
In this case the currently-selected file handle is irrelevant as all
your print statements specify the file handle explicitly.
Your problem isn't with a closed file handle, which would report
print() on closed filehandle
but with the contents of $thing. The statement
print $FH "$thing\n"
will report
Use of uninitialized value $thing in concatenation (.) or string
if $thing is undefined.
Rob
---
This email is free from viruses and malware because avast! Antivirus protection
is active.
http://www.avast.com
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/