Working along the same lines, but in what I consider more "Perl-ish" fashion I'd suggest:
Code:
# ptp 20060125 Show the line before a line that matches a pattern
$p = <>;
while (<>) {
if (/^say: hello$/) { print $p };
$p = $_;
};
This eliminates the need to have a corporeal file (it will just as happily read a stream or work within a pipe), it only uses one variable, and makes better use of built in Perl features... It also produces the correct output against the test case presented.
-PatP