[Wellington-pm] I must be stupid...

Olly Betts olly at survex.com
Tue May 5 05:55:45 PDT 2009


On Tue, May 05, 2009 at 07:03:11PM +0800, Gaurav Vaidya wrote:
> 2009/5/5 Andrew McMillan <andrew at morphoss.com>:
> > Can someone please explain to me why I get a difference between these:
> >  $text =~ s/\s+/ /s;
> > vs.
> >  $text = join( ' ', split( /\s+/s, $text) );
> 
> I think it's the 'g': s/\s+/ /s will only replace the first continuous
> sequence of whitespace with a single space. If you want to replace all
> the spaces, you want s/\s+/ /sg.

Note also that the "/s" modifier's *only* effect is to allow "." to also
match "\n", so it's useless here as there is no "." in the pattern.

"\s" can always match "\n", viz:

$ perl -e '$x = "abc \r\n\f\tdef\nghi";$x =~ s/\s+/ /g;print "[$x]\n"'
[abc def ghi]

Cheers,
    Olly


More information about the Wellington-pm mailing list