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


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

If I've understood Mr Bliako's message correctly, it doesn't work. The open in DrillPressTable.pl and the call to printout are the same, and the error message begins "print() on unopened filehandle LF...", and printout is changed according to his message:

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

Replies are listed 'Best First'.
Re^3: Filehandle in subroutine in use VRML.pm
by haukex (Archbishop) on Jul 09, 2022 at 09:12 UTC

    ... 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

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

Re^3: Filehandle in subroutine in use VRML.pm
by bliako (Monsignor) on Jul 12, 2022 at 11:40 UTC
    I understand that you want to pass filehandles to functions with the added feature that if user specified ...

    Alas it seems I did not understand exactly. In my code above I assume that you will open a file using open my $fh, ... and that you pass around that filehandle and not a literal string/bareword representing also a filehandle, which gets complicated, having to specify also package names. So, in the code I provided the part open $fh, '>', 'xxx'; is essential. The following works for me:

    { package main; my $fh = undef; VRML::printout('some lines1', $fh); open $fh, '>', 'xxx'; VRML::printout('some lines2', $fh); close $fh; } { package VRML; sub printout { my ($lines, $_fh) = @_; my $fh = $_fh ? $_fh : *STDOUT; print $fh $lines; } }

    bw, bliako