thor has asked for the wisdom of the Perl Monks concerning the following question:
Instead of calling tie *FH, "thor", "myfile", I'd like to be able to mask the tie with a subroutine in the same package. Here's what I have in the package:
So, then in my script, I'd like to be able to do this:package thor; use strict; use Carp; require Exporter; our($VERSION, @ISA, @EXPORT); $VERSION = "0.1"; @ISA = qw(Exporter); @EXPORT = qw(rdw_open); sub rdw_open(*$;@) #need a file handle and a name, but allow more for +the 3 arg open... { local *fh = shift; tie \*fh, __PACKAGE__, @_ ; } sub TIEHANDLE { my $class = shift; my $filename = shift; open my $self, $filename or croak "Couldn't open $filename: $!"; bless $self, $class; return $self; }
#!/usr/bin/perl use thor; rdw_open(FH,"myfile") or die "Couldn't open myfile: $!"; # replaces tie *FH, "thor","myfile"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can you tie a filehandle in your own package?
by Felonious (Chaplain) on Mar 01, 2002 at 05:57 UTC | |
by thor (Priest) on Mar 01, 2002 at 20:46 UTC | |
by Felonious (Chaplain) on Mar 02, 2002 at 00:06 UTC |