http://www.perlmonks.org?node_id=976798


in reply to Text matching repost

How about a different approach?
#!/usr/bin/perl use strict; use warnings; my ($start, $stop) = (qr '#ifdef|#if defined', qr '#endif'); open INPUT, '<', 'monks23.dat' or die "Error opening input: " . $!; while (<INPUT>) { if (/$start/ .. /$stop/) { # Print the lines themselves if (/$start/ or /$stop/) { print; } # Skip the rest next; } print; } close INPUT;
Glad I've finally found a useful non-numeric-iterating purpose for .. :)
Edit: Added comments, removed unnecessary regex escaping