Save that in Tie/Input/Insertable.pm and then you can do the following:package Tie::Input::Insertable; sub PRINT { my $self = shift; $self->{buffer} = join '', @_, $self->{buffer}; } sub TIEHANDLE { my ($class, $fh) = @_; bless {fh => $fh, buffer => ""}, $class; } sub READLINE { my $self = shift; return undef if $self->{eof}; while (-1 == index($self->{buffer}, $/)) { my $fh = $self->{fh}; my $data = <$fh>; if (length($data)) { $self->{buffer} .= $data; } else { $self->{eof} = 1; return delete $self->{buffer}; } } my $pos = index($self->{buffer}, $/) + length($/); return substr($self->{buffer}, 0, $pos, ""); } sub EOF { my $self = shift; $self->{eof} ||= not length($self->{buffer}) or $self->{fh}->eof(); } 1;
Note that I made the tied handle not be the one that I was tying it to. If you do that, then you are playing on the edges of infinite recursion. Just make the handles different and you avoid lots of possible confusion.use Tie::Input::Insertable; tie *FOO, 'TIE::Input::Insertable', *STDIN; while (<FOO>) { print FOO "Hello, world\n" if /foo/; print $_; }
In reply to Re: Manipulating STDIN
by tilly
in thread Manipulating STDIN
by ChwanRen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |