Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found