SPUG: looking for $variable for multiline print

Yitzchak Scott-Thoennes sthoenna at efn.org
Tue Feb 5 18:08:13 CST 2002

In article <119140000.1012941463 at flashingchance>,
Matt Tucker <tuck at whistlingfish.net> wrote:
>if ( /(begin_pat .. end_pat)/s ) {
>    print $1;
>Adding the 's' will cause perl to match across multiple lines, but
>you've got to make sure you've read in multiple lines as well (perhaps
>by undef'ing $/ or setting it to '').

//s just causes . to match newlines also.  Without it, . matches any
character other than a newline.

Perhaps he wants something like (untested):

while (defined ($_ = do { local $/; readline })) {   # read in whole file
  ($match) = /^[^\n]*?begin_pat.*?end_pat[^\n]*\n?/msg;
  ($match) = /^.*?begin_pat(?:.|\n)*?end_pat.*\n?/mg; # equiv to above, w/o //s
   print $match;
This should work the same as:
while (<>) { print if /begin_pat/ .. /end_pat/ }
unless both match the same line with end_pat not following begin_pat
or begin_pat and end_pat match in different files.

Here $match gets one or more whole lines, starting from a line
matching /begin_pat/ and ending (possibly on the same line) with a
line matching /end_pat/.

As you can see, //s actually can make it harder to match multiple
lines (besides, msg is a suspected carcinogen).  //m makes ^ (and $)
match relative to newlines in the middle of the string, not just at
the beginning and ending of the string.  If you assume the end of the
file is properly newline-terminated, the msg version can be:
  ($match) = /^[^\n]*?begin_pat.*?end_pat(?>.*?\n)/msg;

BTW, Matt, Mulberry seems to split (with "\n ") message-ids in
References:.  I didn't think that was allowed.  The reply from the
Mutt-user truncated the split part, and I'd tend to trust Mutt to
not mess up given valid data.

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org

More information about the spug-list mailing list