From a related question asked by Bi, I\'ve learnt how to print a matching line together with the line immediately below it. The code looks really simple:
#!p
You always want to store the last line that you saw in case the next line has your pattern and you need to print it. Using an array like you did in the second code snippet is probably overkill.
my $last = "";
while (my $line = ) {
if ($line =~ /Pattern/) {
print $last;
print $line;
print scalar ; # next line
}
$last = $line;
}