in reply to Understanding Split and Join
A pattern that split treats specially but m// and s/// treat normally is /^/. Normally, ^ only matches at the beginning of a string. Given the /m flag, it also matches after newlines in the interior of the string. It's common to want to break a string up into lines without removing the newlines as splitting on /\n/ would do. One way to do this is @lines = /^(.*\n?)/mg. Another, perhaps more straightforward, is @lines = split /^/m. Without the /m, the ^ should match only at the beginning of the string, so the split should return only one element, containing the entire original string. Since this is useless, and splitting on /^/m instead is common, /^/ silently becomes /^/m.
This only applies to a pattern consisting of just ^; even the apparently equivalent /^(?#)/ or /^ /x are treated normally and don't split the string at all.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Understanding Split and Join
by ferreira (Chaplain) on Dec 30, 2006 at 19:34 UTC | |
by ysth (Canon) on Dec 31, 2006 at 01:44 UTC | |
by ferreira (Chaplain) on Jan 02, 2007 at 11:01 UTC |