Filtering Your Own Output Problem You want to postprocess your program's output without writing a separate program to do so. Solution Use the forking form of open to attach a filter to yourself. For example, this will restrict your program to a hundred lines of output: head(100); while (<>) {    print; } sub head {    my $lines = shift || 20;    return if $pid = open(STDOUT, "|-");    die "cannot fork: $!" unless defined $pid;    while () {        print;        last unless --$lines;    }    exit; }