Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Filehandle in subroutine in use VRML.pm

by smittypaddler (Acolyte)
on Jul 17, 2022 at 21:06 UTC ( [id://11145564]=note: print w/replies, xml ) Need Help??


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

None of the suggestions have worked, but I found a solution. In my VRML.pm file, in any subroutine printing output, I added a myprint argument and where the subroutine originally invoked &printout, I changed it to invoke \&myprint, where @lines is an array of lines to be printed:

sub egg { # prints a 3d egg. my %args=(myprint => \&printout, xaxis => 10, ...); # Produces lines of wrl output in @lines ... $myprint=$args{myprint}; &{$myprint}(\@lines); }

In DrillPressTable.pl, which uses VRML.pm, I added a global $Fh, and changed the invocation of mystart, and its code, and myprint's code as well:

my $Fh; # w/b set in mystart. &mystart($LumberFile,LF); sub mystart { my($file,$fh)=@_; open($fh,"> $file") || die("ERROR: Unable to open $file for output"); $Fh=$fh; my @lines=split(/\n/,<<END); #VRML V2.0 utf8 NavigationInfo { headlight TRUE } ... END &myprint(\@lines); } sub myprint { my($lines)=@_; foreach my $line (@{$lines}) { print $Fh "$line\n"; } }

So now in DrillPressTable.pl I can direct lines of output to multiple file handles, and I don't need to change any of the 70-odd perl programs that use my VRML.pm. They'll just take the default of &printout, which prints to STDOUT, just like before.

Replies are listed 'Best First'.
Re^3: Filehandle in subroutine in use VRML.pm
by haukex (Archbishop) on Jul 18, 2022 at 07:13 UTC
    None of the suggestions have worked

    All five (!) of the variations of solutions I posted in this thread work correctly when applied to the sample code you posted. This means that either you did not apply the suggestions correctly, or that your sample code was not representative of your actual code. In either case, see Short, Self-Contained, Correct Example and I know what I mean. Why don't you?

Re^3: Filehandle in subroutine in use VRML.pm
by BillKSmith (Monsignor) on Jul 20, 2022 at 19:34 UTC
    I am pleased to hear that you solved your problem yourself. Are you aware that this new solution is still not compliant with strict refs? You have already demonstrated the problem with this. Minor changes can break your code in ways that are very hard to debug. Not a good idea for a widely used module! At the very least, I would recommend that you specify use strict for the whole program. Specify no strict refs in the smallest possible scope.

    It now sounds like your real problem is how to change the default output of your module. Look at the library function select. It may do exactly what you want.

    Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found