[comp.lang.perl] Multi-line strings and $*

gustav@tharr.UUCP (Paul Moore) (11/06/90)

I'm having a bit of trouble understanding how to use $* to handle
multiline strings.

I have a string, $article, which contains a large number of lines of
text (OK, so it's a news article...) The article may end with any number
of blank lines (ie, \n\n\n), which I want to remove.

My first thought is $article =~ s/\n+$//, with $* = 0. But the manual says
"Pattern matches on strings containing multiple newlines can produce
confusing results when $* is 0". Um.

However, if $* = 1, there seems to be no way of anchoring a pattern to
the end (or the beginning) of a string.

At this point, I started getting frustrated...

Any clues, anybody?

Thanks in advance,
Gustav.
-- 
/----------------------------------------------------------------\
| Paul Moore              E-Mail:    ...!ukc!tharr!gustav        |
| 10, Mulberry Rise       or         ...!ukc!cix!pmoore          |
| Northwich, Cheshire     or (BEST)  pmoore@cix.compulink.co.uk  |
<-- tharr *free* public access to Usenet in the UK 0234 261804 -->

piet@cs.ruu.nl (Piet van Oostrum) (11/06/90)

>>>>> In message <1391@tharr.UUCP>, gustav@tharr.UUCP (Paul Moore) (PM) writes:


PM> My first thought is $article =~ s/\n+$//, with $* = 0. But the manual says
PM> "Pattern matches on strings containing multiple newlines can produce
PM> confusing results when $* is 0". Um.

Larry will probably also comment on this, but in patch 19 it is stated that
from now on `$' will only match at string end (or at the newline just before
the end) when $*==0. So s/\n+$// works. Actually the only confusion that
remains is whether the LAST newline will be substituted. Maybe Larry can
give the rules.

Unfortunately, the manual ``page'' has not been updated with the patch.
-- 
Piet* van Oostrum, Dept of Computer Science, Utrecht University,
Padualaan 14, P.O. Box 80.089, 3508 TB Utrecht, The Netherlands.
Telephone: +31 30 531806   Uucp:   uunet!mcsun!ruuinf!piet
Telefax:   +31 30 513791   Internet:  piet@cs.ruu.nl   (*`Pete')

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/07/90)

In article <1391@tharr.UUCP> gustav@tharr.UUCP (Paul Moore) writes:
: I'm having a bit of trouble understanding how to use $* to handle
: multiline strings.
: 
: I have a string, $article, which contains a large number of lines of
: text (OK, so it's a news article...) The article may end with any number
: of blank lines (ie, \n\n\n), which I want to remove.
: 
: My first thought is $article =~ s/\n+$//, with $* = 0. But the manual says
: "Pattern matches on strings containing multiple newlines can produce
: confusing results when $* is 0". Um.
: 
: However, if $* = 1, there seems to be no way of anchoring a pattern to
: the end (or the beginning) of a string.
: 
: At this point, I started getting frustrated...

Actually, $* only applies to ^ and $, not \n.  So just leave it 0, and
treat \n like a normal character, and it should work fine.

If it DOESN'T work fine, it's a bug.

The manual should definitely be clearer...

Larry