Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Filehandle in subroutine in use VRML.pm

by haukex (Archbishop)
on Jul 06, 2022 at 21:03 UTC ( [id://11145313]=note: print w/replies, xml ) Need Help??


in reply to Filehandle in subroutine in use VRML.pm

Thank you for providing enough code to reproduce your issue!

The problem is that because you're not using strict "refs" (Update: and strict "subs"), there is some funky symbolic reference stuff going on here. It's unclear to me why you even need to pass the parameter LF in &mystart($LumberFile,LF);, but AFAICT what is going on here is that that call is actually being interpreted as &mystart($LumberFile,"LF");, and then inside VRML.pm there is no filehandle named LF because that was opened in the main package.

The best thing to do is to rewrite your code so that it works under strict and warnings. The minimum changes I would recommend is to change the definition and call of mystart as follows:

mystart($LumberFile); sub mystart { my ($file) = @_; open( my $fh, "> $file" ) or die ...

If you need the $fh parameter to mystart, you need to show us more context as to how it gets used for us to make a recommendation for a useful replacement (Update: Re^3: Filehandle in subroutine in use VRML.pm).

However, I see several other older code styles in your sample code, for example using the -w shebang switch (What's wrong with -w and $^W), using the old ampersand-style sub calls (perlsub), and the two-argument open ("open" Best Practices). So here's your sample code updated to modern Perl, I strongly suggest you adopt this style for all your code:

#!/usr/bin/perl use warnings; use strict; use VRML; my $Dir = "vrmlstuff"; my $Subdir = "DrillPressTable"; my $LumberFile = "$Dir/$Subdir/lumber.wrl"; mystart($LumberFile); sub mystart { my ($file) = @_; open my $fh, '>', $file or die "ERROR: Unable to open $file for output"; my @lines = split /\n/, <<END; #VRML V2.0 utf8 NavigationInfo { headlight TRUE } DirectionalLight { # First child direction 0 0 -1 # Light illuminating the scene } END my $i = 0; if ( $i == 0 ) { for my $line (@lines) { print $fh "$line\n"; } } elsif ( $i == 1 ) { myprint( \@lines, $fh ); } elsif ( $i == 2 ) { printout( \@lines, $fh ); } } sub myprint { my ($lines, $fh) = @_; for my $line ( @{$lines} ) { print $fh "$line\n"; } }

VRML.pm:

#!/usr/bin/perl package VRML; use warnings; use strict; use Exporter 'import'; our @EXPORT = qw/printout/; sub printout { my ($lines, $fh) = @_; $fh = *STDOUT unless $fh; for my $line ( @{$lines} ) { print $fh "$line\n"; } } 1;

Warning: Do not do this if you want to keep your sanity, but the two ways to make the code you showed work with the absolute minimal changes to the code are &mystart($LumberFile,*LF); and &mystart($LumberFile,'main::LF');. But don't do this. Update: For a slightly saner minimal approach see my node here.

Replies are listed 'Best First'.
Re^2: Filehandle in subroutine in use VRML.pm
by smittypaddler (Acolyte) on Jul 06, 2022 at 22:25 UTC

    Thanks for the quick response. It will take me awhile to formulate why myprint() even exists, and what my ultimate goal is, so please give me some time. Regarding the archaic syntax in my perl programs, I'm 80 years old, and've been programming since 1965, so if we were conversing you'd probably hear some stuff like "Awesome," "Beet Feet" and "Bug out" from me. Anyway, I'll come back to this, and thanks again for the response.

      Regarding the archaic syntax in my perl programs, I'm 80 years old, and've been programming since 1965, so if we were conversing you'd probably hear some stuff like "Awesome," "Beet Feet" and "Bug out" from me.

      Sure, I understand, and a few of the edits I made are indeed purely stylistic (for is equivalent to foreach, dropping the parens on function calls, or running the code through perltidy), but at least the changes I named in my node above have become best practices for good reason, as explained in the links. And Use strict and warnings is quite important - your question is a good example of that and the potential of symbolic references to cause confusion and hard to diagnose bugs.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found