How can I do this?
open FILE, $somefile; foreach (<FILE>) { if (/some_regex/) { $want_the_next_line = <FILE>; $want_the_next_line_after_that = <FILE>; } }
Know what I mean? I basically want to slurp in a bunch of lines in the middle of my foreach, instead of having to remember my state and check it every time I iterate through. And I currently can’t find anything helpful on <> in Perldoc.
Oh, and by the way, I really don’t want to:
@file = <FILE>;
I’m sure you understand.
Use
whileinstead offoreach:The
whileloop will only read a single line from<FILE>and you can then do as you wish with it in the current iteration.Also this technique will help you to avoid reading the whole file at once.
Technical background:
foreach()requires an array, therefore reading in the whole file at once, while the expression in thewhile()loop is scalar context and is only checked for ‘false’ values, like the one EOF produces.