Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^3: Filehandle in subroutine in use

by haukex (Archbishop)
on Jul 07, 2022 at 20:10 UTC ( #11145345=note: print w/replies, xml ) Need Help??

in reply to Re^2: Filehandle in subroutine in use
in thread Filehandle in subroutine in use

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145345]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-12-07 19:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found