in reply to Re: help match this
in thread help match this

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:

if( /$start/ .. /$finish/ ) { $content .= $_; }
Now that is easier and more maintainable!

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:

$content =~ m/$start(.*)$finish/s;
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 $_.

See also the very nice discussion in Flipin good, or a total flop?.

Replies are listed 'Best First'.
Re^3: help match this
by Moron (Curate) on Jul 05, 2006 at 09:15 UTC
    The purpose of my post was to demonstrate that it was easy enough to split the matching into two. I am normally a fan of terse code, especially if the usage of an operator is unambiguous, but because of the danger of the unwary reader confusing it with the .. range operator, this requires significant explanation which would not be germane to the simple point I was making. I don't rule it out though and will bear it in mind for such time as I can think of a way to present it easily enough to potentially beginner OPs.

    -M

    Free your mind