On Wed, Jan 15, 2014 at 12:09 AM, Matt McAdory <m...@mcadory.info> 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.
>

You mention that this is some dummy code, but for what it's worth, you got
an extra comma there.


> 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?
>
> Matt
>

You'll have to post some code that actually reproduces the issue. Keep in
mind that the 'my $FH' is a lexically scoped filehandle, so unless you're
actually passing it to the module that uses it, or using select($FH) to
change the default filehandle, there's no way that the module in question
will be able to use the filehandle in $FH.

Reply via email to