Just stash the handle in a member variable and when you call get_next_line call readline on the stashed handle.
package MyFile;
use Moo;
has _fh => { is => 'rw' };
sub BUILD {
my( $self, @args ) = @_;
$self->_fh( do {
open( my $fh, q{<}, $self->frobnicate_path() )
or die qq{Can't open frobnicated path: $!\n};
$fh;
} );
return;
}
sub frobnicate_path {
my( $self ) = shift;
return qq{WHATEVER.txt};
}
sub get_next_line {
my( $self ) = shift;
return readline( $self->_fh );
}
sub DEMOLISH {
if( $self->_fh ) {
close( $self->_fh ) or warn qq{Problem closing frobnicated path: $
+!\n};
}
}
1;
__END__
Edit: fixed readline in get_next_line and added frobnicate_path stub.
The cake is a lie.
The cake is a lie.
The cake is a lie.