use strict; use warnings; package PushableFH; use strict; use warnings; sub TIEHANDLE { my $class = shift; my $rfh = shift; my $obj = {}; $obj->{FH} = $rfh; $obj->{PUSHED} = []; bless $obj, $class; } sub READLINE { my $self = shift; my $fh = $self->{FH}; return ( @{$self->{PUSHED}} > 0 ) ? pop @{$self->{PUSHED}} : <$fh>; } sub push_fh { my $self = shift; push @{$self->{PUSHED}}, @_; } package main; use strict; use warnings; use vars qw/*FILEHANDLE/; my $fhobj = tie *FILEHANDLE, "PushableFH", \*DATA; my $line = ; # Read the first line from the filehandle. print $line; $fhobj->push_fh("Hello world!\n"); # Push something onto the FH. $line = ; # Read what was just pushed. print $line; $line = ; # Read the next physical line from FH. print $line; __DATA__ This is the first line. This is the second line.