open my $FH, '>', 'afile.pl'; print $FH <) { if (/^RPT/) { print $FH, "another line of your script" } elsif (/^FOO/) { print $FH, "something completely different" } elsif (/^Q/) { last; } } #### my $SCRIPT = <