in reply to Can you tie a filehandle in your own package?

sub rdw_open(*$;@) # need a file handle and a name, but allow more fo +r the 3 arg open... { my $fh = shift; tie *{$fh}, __PACKAGE__, @_ ; }
rdw_open(*FH, "myfile") or die "Couldn't open myfile: $!";
-- O thievish Night, Why should'st thou, but for some felonious end, In thy dark lantern thus close up the stars? --Milton

Replies are listed 'Best First'.
Re: Re: Can you tie a filehandle in your own package?
by thor (Priest) on Mar 01, 2002 at 20:46 UTC
    This is great! (++ for you) One last question, then I'll be quiet for a while: Is there a way to change the subroutine call to use a file handle rather than a typeglob? i.e.
    rdw_open(FH, "myfile") or die "Couldn't open myfile: $!";
    I'd like to make this look like regular open as much as possible, and the level of peoples perl around these parts is not so great as to remember why they need the * in front of their filehandle.

    Thanks,
    thor
      It starts getting ugly:
      sub rdw_open(*$;@) # need a file handle and a name, but allow more for + the 3 arg open... { my $fh = shift; if (not ref($fh) eq 'GLOB') { $fh = "*" . (caller())[0] . "::" . $fh; } { no strict; tie *{$fh}, __PACKAGE__, @_ ; } }
      -- O thievish Night, Why should'st thou, but for some felonious end, In thy dark lantern thus close up the stars? --Milton