in reply to modules, exporting, and indirect filehandles
Since you're using <$LOG> and not <LOG>, you could do the following:
use strict; use warnings; package MyPackage; sub open_file { my $fh; my $ok; require 5.006; # Require Perl 5.6 # Support both 2 and 3 arg version of open. if (@_ == 1) { $ok = open($fh, $_[0]); } else { $ok = open($fh, $_[0], $_[1]); } die("$!\n") unless $ok; return $fh; } 1;
use strict; use warnings; use MyPackage; our $LOG = open_file('log.txt'); # 'my' instead of 'our' also works while (<$LOG>) { print; }
Well, the logic to import open_file is missing, but that's easy. Check Exporter for details. Change open_file to MyPackage::open_file in the .pl until this is resolved.
Update: If you wanted to use <LOG>, how about:
use strict; use warnings; package MyPackage; sub open_file { local *fh; my $ok; # Support both 2 and 3 arg version of open. if (@_ == 1) { $ok = open(*fh, $_[0]); } else { require 5.006; # Require Perl 5.6 $ok = open(*fh, $_[0], $_[1]); } die("$!\n") unless $ok; return *fh; } 1;
use strict; use warnings; use MyPackage; *LOG = open_file('log.txt'); while (<LOG>) { print; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: modules, exporting, and indirect filehandles
by eff_i_g (Curate) on Aug 09, 2005 at 20:09 UTC | |
by ikegami (Patriarch) on Aug 09, 2005 at 20:23 UTC | |
by chromatic (Archbishop) on Aug 09, 2005 at 20:31 UTC | |
by ikegami (Patriarch) on Aug 09, 2005 at 20:35 UTC | |
by eff_i_g (Curate) on Aug 09, 2005 at 21:11 UTC | |
by eff_i_g (Curate) on Aug 09, 2005 at 20:19 UTC |