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 | |
by clueless newbie (Curate) on Jun 20, 2017 at 21:31 UTC | |
Re: How to tie a lexical filehandle.
by haukex (Archbishop) on Sep 30, 2017 at 17:57 UTC |
Back to
Seekers of Perl Wisdom