<?xml version="1.0" encoding="windows-1252"?>
<node id="592186" title="Re: Understanding Split and Join" created="2006-12-29 03:02:03" updated="2006-12-28 22:02:03">
<type id="11">
note</type>
<author id="299049">
ysth</author>
<data>
<field name="doctext">
[chromatic] has pointed out that split treats an empty pattern normally, not as a directive to reuse the last successfully matching pattern, as m// and s/// do.
&lt;p&gt;
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 &lt;c&gt;@lines = /^(.*\n?)/mg&lt;/c&gt;.  Another, perhaps more straightforward, is &lt;c&gt;@lines = split /^/m&lt;/c&gt;.  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.
&lt;p&gt;
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.</field>
<field name="root_node">
591988</field>
<field name="parent_node">
591988</field>
</data>
</node>
