http://qs321.pair.com?node_id=11145345


in reply to Re^2: Filehandle in subroutine in use VRML.pm
in thread Filehandle in subroutine in use VRML.pm

which brings me to my original question: How do I declare $fh in subroutine printout so it recognizes the intended filehandle?

The problem is not in sub printout, it's here:

&mystart($LumberFile,LF); &buildlumber(\%Fs,LF,$Ppi);

This gives us a little more context as to what's going on. You apparently want to use the "filehandles" LF and DP like variables and pass them as arguments to the subs to use. But because of the missing strict, the LF is being taken as a bareword, that is, even though it looks like a filehandle, it's actually the string "LF" (B::Deparse on the sample code from the root node shows this). Then AFAICT this is being used as a symbolic reference by open and print, and this resolves it relative to the current package. When everything is in the main package, it works, but sub printout is in a different package, so it fails.

Here's one possible solution that requires only very small changes to your code and even works under strict once you get around to using that. The do block basically generates a new filehandle (see also my node here for alternatives). This will actually give you a filehandle stored in a variable that you can pass around.

my $LF = \do { local *LF; *LF }; &mystart($LumberFile,$LF); &buildlumber(\%Fs,$LF,$Ppi);

I do still feel compelled to say that this is still just triage, though. The real issue is the missing strict compliance.