in reply to Manipulating STDIN

I would personally manage the data internally, but you could always look at perltie and write an appropriate tie class that did it for you. Here is an example class:
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;
Save that in Tie/Input/Insertable.pm and then you can do the following:
use Tie::Input::Insertable; tie *FOO, 'TIE::Input::Insertable', *STDIN; while (<FOO>) { print FOO "Hello, world\n" if /foo/; print $_; }
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.

Replies are listed 'Best First'.
Re: Re: Manipulating STDIN
by gmpassos (Priest) on Apr 11, 2004 at 07:56 UTC
    To tie directly STDIN you can hold the original STDIN in other GLOB, soo this will work:
    *ORIGINAL_STDIN = \*STDIN ; tie *STDIN, 'Tie::Input::Insertable', *ORIGINAL_STDIN; print "> " ; while (<STDIN>) { print STDIN "Hello, world\n" if /foo/; print "\n# $_\n" ; print "> " ; }
    And by the way, nice work in the TIEHANDLE. ;-P

    Graciliano M. P.
    "Creativity is the expression of the liberty".