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" :-)


In reply to Re^5: Filehandle in subroutine in use VRML.pm (updated) by haukex
in thread Filehandle in subroutine in use VRML.pm by smittypaddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.