http://www.perlmonks.org?node_id=1193170

clueless newbie has asked for the wisdom of the Perl Monks concerning the following question:

Ave,

I can do a tie on a file as long as I use bareword filehandles. ie

package MyTie; use Carp; # So we can croak our error use Data::Dumper; use strict; use warnings; sub TIEHANDLE { #Carp::cluck "\nTIEHANDLE: ",Data::Dumper->Dump([\@_], +[qw(*_)]),' '; no warnings; my $class=shift; my $form=shift; open (my $self,$form,@_) # or Carp::cluck "can't open $form@_: $!" ; return bless $self,$class; # $self is a glob ref } sub CLOSE { #Carp::cluck "\nCLOSE: ",Data::Dumper->Dump([\@_],[qw(*_)] +),' '; my $self=shift; return close $self; } sub DESTROY { #Carp::cluck "\nDESTROY: ",Data::Dumper->Dump([\@_],[qw( +*_)]),' '; my $self=shift; $self->CLOSE; } sub EOF { #Carp::cluck "\nEOF: ",Data::Dumper->Dump([\@_],[qw(*_)]),' +'; my $self = shift; return eof $self; } sub FILENO { #Carp::cluck "\nFILENO: ",Data::Dumper->Dump([\@_],[qw(*_ +)]),' '; my $self=shift; return fileno $self; } sub OPEN { #Carp::cluck "\nOPEN: ",Data::Dumper->Dump([\@_],[qw(*_)]), +' '; my $self=shift; my $form=shift; $self->CLOSE; open($self,$form,@_) or croak "can't reopen '$form@_': $!"; return 1; } sub READLINE { #Carp::cluck "\nREADLINE: ",Data::Dumper->Dump([\@_],[q +w(*_)]),' '; my $self=shift; return undef if $self->EOF; #warn Data::Dumper->Dump([\(my $t=wantarray())],[qw(*wantarray)]), +' '; if (wantarray()) { my @a=<$self>; main::mySub(@a); return @a; } else { my $s=<$self>; main::mySub($s); return $s; }; } package main; use strict; use warnings; my $name='mydata'; tie *FOO,"MyTie"; open(FOO,'<',$name) or die "Can't open '$name' for reading. $!"; my $line=<FOO>; print "Scalar: $line"; $line=<FOO>; print "Scalar: $line"; my @array=<FOO>; print "Array: ",@array; while (<FOO>) { print "In while: $_"; } close(FOO); exit; sub mySub { warn "\nmain::mySub: ",Data::Dumper->Dump([\@_],[qw(*_)]),' '; return; }; __DATA__

works just fine to record what is read from *FOO.

But I need to tie a lexical filehandle so I can write

package main; use strict; use warnings; my $name='mydata'; my $FOO; tie $FOO,"MyTie"; open($FOO,'<',$name) or die "Can't open '$name' for reading. $!"; my $line=<$FOO>; print "Scalar: $line"; $line=<$FOO>; print "Scalar: $line"; my @array=<$FOO>; print "Array: ",@array; while (<$FOO>) { print "In while: $_"; } close($FOO); exit;
.

But this throws 'Can't locate object method "TIESCALAR" via package "MyTie" at ...' - where the tie is attempted. Any ideas on how to tie a lexical filehandle?

"This is perl 5, version 20, subversion 2 (v5.20.2) built for MSWin32-x86-multi-thread-64int"

Replies are listed 'Best First'.
Re: How to tie a lexical filehandle. (updated x2)
by haukex (Archbishop) on Jun 20, 2017 at 19:44 UTC
    use IO::Handle; my $fh = IO::Handle->new; tie *$fh, ...;

    Update: Or the following slightly more low-level way, the relevant code of IO::Handle->new doesn't do a whole lot more than this:

    use Symbol qw/gensym/; my $fh = gensym; tie *$fh, ...;

    Links: IO::Handle, Symbol.

    Update 2017-09-30: One more, from here:

    my $fh = \do { local *HANDLE };
      Thanks!
Re: How to tie a lexical filehandle.
by haukex (Archbishop) on Sep 30, 2017 at 17:57 UTC

    I know this is an older post, but I thought you might be interested that I just released version 0.06 of Tie::Handle::Base (it's part of File-Replace, but it don't require any non-core modules except for testing Update: as of 0.16, it's now in its own distribution, and it doesn't require any non-core modules). In particular, I thought the "Debugging" example might help you with your MyTie:

    #!/usr/bin/env perl use warnings; use strict; { package MyTie; use parent 'Tie::Handle::Base'; use Class::Method::Modifiers qw/before around/; use Data::Dump; for my $method (@Tie::Handle::Base::ALL_METHODS) { before $method => sub { dd $method, @_[1..$#_] }; } around 'READLINE' => sub { my $orig = shift; my @a = wantarray ? ($orig->(@_)) : (scalar $orig->(@_)); main::mySub(@a); return wantarray ? @a : $a[0]; }; } use Data::Dump; sub mySub { dd "mySub", @_; } #open my $fh = MyTie->new, '<', 'mydata' or die $!; my $fh = MyTie->new(*DATA); my $line = <$fh>; print "Scalar: $line"; $line = <$fh>; print "Scalar: $line"; my @array = <$fh>; print "Array: ", @array; close $fh; __DATA__ Foo Bar Quz Baz

    Output:

    ("TIEHANDLE", *main::DATA) "READLINE" ("mySub", "Foo\n") Scalar: Foo "READLINE" ("mySub", "Bar\n") Scalar: Bar "READLINE" ("mySub", "Quz\n", "Baz\n") Array: Quz Baz "CLOSE" "DESTROY"