#!/usr/bin/perl ########################################### # tieexample -- How to tie STDIN and STDOUT # Mike Schilli, 2002 (m@perlmeister.com) ########################################### use warnings; use strict; our $VERSION = "1.00"; our $CVSVERSION = '$Revision: 1.5 $'; package TieFH; sub TIEHANDLE { my($class) = @_; my $buf = ""; bless \$buf, $class; } sub READLINE { my($self) = @_; my $pos; return undef unless length($$self); if(defined($/)) { $pos = index($$self, "$/"); } else { $pos = length($$self) - 1; } return substr($$self, 0, $pos+1, ""); } sub PRINT { my($self, $line) = @_; $$self .= $line; } package main; close STDOUT; close STDIN; tie(*STDOUT,'TieFH'); tie(*STDIN, 'TieFH'); print STDIN "abc\n"; ##################### # Tied function ##################### while() { print "foo: $_"; } ##################### while() { print STDERR "stdout: $_"; } __END__ =head1 NAME tieexample - Example on how to tie STDIN and STDOUT =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS tieexample =head1 DESCRIPTION B is a sample program showing how to tie STDIN and STDOUT to buffers. After the tie, every C appends to an internal buffer, while every CSTDINE)> reads from the another buffer. This way we can teach functions/libraries that just know how to read from STDIN and write to STDOUT how to process strings. =head1 CREDITS Many thanks to Michael Renner for contributing a patch to cope with arbitrary values of the input record separator C<$/>. =head1 AUTHOR 2002, Mike Schilli