In cases like this the flip-flop operator ( '..' in scalar context, see perlop) can really help to clean things up by eliminating the need to track state. For example, the entire body of your while loop can be replaced with this:
Now that is easier and more maintainable!if( /$start/ .. /$finish/ ) { $content .= $_; }
The snippet above will append $_ to $content anytime $_ matches $start and until it matches $finish. Note that $content will include the patterns within $start and $finish, however, so a simple regex at the end can be used to eliminate them:
This does, of course, simply reduce to the examples above which use the s modifier to allow '.' to match newlines, but it illustrates how the flip-flop operator could be used in this situation. No more need for $phase, no more funky loop control in nested 'if' statements, and no manual resetting of $_.$content =~ m/$start(.*)$finish/s;
See also the very nice discussion in Flipin good, or a total flop?.
In reply to Re^2: help match this
by bobf
in thread help match this
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |