#!/usr/bin/perl use strict; use warnings; my $text=<) { SubOut($fh, \@array, $line) if $line =~ /xyz/; } close $fh; print @array; sub SubOut { my ($fh,$array_ref, $match_line) = @_; push @$array_ref, $match_line; while (my $s_line = <$fh>) { print "$s_line"; } } __END__ Prints: line following the matching line 2nd line after the matching line that has xyz <--- this is in @array