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


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

I believe that you must specify the package which contains open $fh, ..., not the package which contains the call to printout.

That's not entirely accurate either, though. If the open call happens in a package different from the others, then the string containing the name of the filehandle will get resolved relative to that package, and none of the other packages will see the open filehandle.

use warnings; no strict; package main; $fh = "HANDLE"; package Other; open $::fh, '>/tmp/foo.txt' or die $!; package main; print $::fh "Doesn't work\n"; package VRML; print $::fh "Doesn't work\n"; package Other; print $::fh "Works\n";

The key thing is that it doesn't matter which package you specify, the string needs to reference the same filehandle everywhere - if in the above you set $fh = 'Quz::HANDLE' or any other specific package then the code works. That's why one of my original suggestions in the spoiler here was to use the string 'main::LF' instead of the bareword LF, because then every place that uses the symbolic reference will be referencing the same filehandle.

As I said in my node above, that suggestion only applies if the OP only wants to change sub printout for some reason; compared to the other ideas I still consider it to be the "worst" way to work around the issue. The weakness is that printout doesn't know which package the filename was opened in, and both of my suggestions above (main and caller) are just guesses - it's true I didn't describe this weakness in the node in that much detail, but that's because I'm hoping the OP doesn't choose this particular workaround...

Minor edits & clarifications.

Update 2: Upon rereading, I think I perhaps we both had the same idea, and I was just disagreeing with your wording of it, rather than the idea itself - in the spirit of "if we're going to be nitpicky, then let's be really nitpicky" :-)