#!/usr/bin/perl use strict; my ( $prev, $this ) = ( undef(), undef() ); while( <> ) { $this = $_; /^>>[^>]/ or mySubst( \$prev ); print $prev; $prev = $this; } # finally, process the last line of input which got left over mySubst( \$prev ); print $prev; sub mySubst { my $sref = shift; $$sref =~ s/^>>>/PYTHON_PROMPT/; }