Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^3: Filehandle in subroutine in use

by haukex (Archbishop)
on Jul 09, 2022 at 09:12 UTC ( #11145386=note: print w/replies, xml ) Need Help??

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

... despite my various solutions and workarounds from my nodes here and here being much better than the following, if you're hell-bent on only modifying sub printout, here's enough rope to shoot yourself in the foot: (Update: Note the following only works because of the absence of strict...)

sub printout { my ($lines, $_fh) = @_; my $fh = $_fh ? caller.'::'.$_fh : *STDOUT; foreach my $line (@{$lines}) { print $fh "$line\n"; } } # End printout;

Though you may need to replace caller with the string "main" if printout gets called from another package. /shudder

Replies are listed 'Best First'.
Re^4: Filehandle in subroutine in use
by BillKSmith (Monsignor) on Jul 09, 2022 at 19:21 UTC
    I disagree in one detail. I believe that you must specify the package which contains open $fh, ..., not the package which contains the call to printout. Of course in this case, they are the same (main::).
      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" :-)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2022-12-07 20:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found