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 | |
|
Re^3: Filehandle in subroutine in use VRML.pm
by BillKSmith (Monsignor) on Jul 20, 2022 at 19:34 UTC |