Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Filehandle in subroutine in use VRML.pm

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


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found