in reply to Globalize Filehandles and Visibility in Packages?

You are getting bitten by the fact that the "use" statement is invoked as if it was inside a BEGIN block, so that the assignment:
my $FH_text_W = $main::FH_text_W;
Occurs before $main::FH_text_W is initialized.

A more traditional perl/OO way to do this would be to make the package "MyPkgDirectory::MyPkg" a true object/class, with a "new" method that initializes the object.

package MyPkgDirectory::MyPkg; # assumes MyPkgDirectory/MyPkg.pm use strict; use warnings; ... sub new{ my ($classname,$fh) = @_; die "File handle not specified" unless $fh; return bless {HANDLE => $fh}, $classname; } sub samplewrite { my ($self,$ver, $sheet) = @_; .... print {$self->{HANDLE}} "SHEET $sheet\n"; # Need funny braces to un +-confuse "print" }
Then , in "main:
... # No Pre-declaration. the handle is now a local open(my $FH_text_W, $wmode, $filename) or die "ERROR: cannot open $f +ilename for writing $!"; my $pkg = MyPkgDirectory::MyPkg::->new( $FH_text_W); $pkg->samplewrite(10, "foofoo");

        "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Replies are listed 'Best First'.
Re^2: Globalize Filehandles and Visibility in Packages?
by smknjoe (Beadle) on Mar 07, 2015 at 04:19 UTC
    Excellent. Thank you for taking the time to explain what I'm getting bit by. I really like the OO solution, but I'm afraid it is still a bit too far over my head. I want to get the whole package/module subject nailed down first. Then I will tackle OO.