note
haukex
<p>Thank you for providing enough code to reproduce your issue!</p>
<p>The problem is that because you're not using [doc://strict|<c>strict "refs"</c>] (<small><i>Update:</i></small> and <c>strict "subs"</c>), there is some funky symbolic reference stuff going on here. It's unclear to me why you even need to pass the parameter <c>LF</c> in <c>&mystart($LumberFile,LF);</c>, but AFAICT what is going on here is that that call is actually being interpreted as <c>&mystart($LumberFile,"LF");</c>, and then inside <c>VRML.pm</c> there is no filehandle named <c>LF</c> because that was opened in the <c>main</c> package.</p>
<p>The best thing to do is to rewrite your code so that it works under [doc://strict] and [doc://warnings]. The minimum changes I would recommend is to change the definition and call of <c>mystart</c> as follows:</p>
<c>
mystart($LumberFile);
sub mystart {
my ($file) = @_;
open( my $fh, "> $file" ) or die ...
</c>
<p>If you need the <c>$fh</c> parameter to <c>mystart</c>, you need to show us more context as to how it gets used for us to make a recommendation for a useful replacement (<i>Update:</i> [id://11145345]).</p>
<p>However, I see several other older code styles in your sample code, for example using the <c>-w</c> shebang switch ([doc://warnings#What's-wrong-with-w-and-$^W|What's wrong with <c>-w</c> and <c>$^W</c>]), using the old ampersand-style <c>sub</c> calls ([doc://perlsub]), and the two-argument open ([id://11102684]). So here's your sample code updated to modern Perl, I strongly suggest you adopt this style for all your code:</p>
<readmore title="code">
<c>
#!/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";
}
}
</c>
<p><c>VRML.pm</c>:</p>
<c>
#!/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;
</c>
<spoiler>
<p><b>Warning:</b> <strike>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 <c>&mystart($LumberFile,*LF);</c> and <c>&mystart($LumberFile,'main::LF');</c>. But <b>don't do this</b>.</strike> <i>Update:</i> For a slightly saner minimal approach see my node [id://11145345|here].</p>
</spoiler>
</readmore>
11145311
11145311