<?xml version="1.0" encoding="windows-1252"?>
<node id="291707" title="Re: Re: Perl Idioms Explained - @ary = $str =~ m/(stuff)/g" created="2003-09-15 22:08:27" updated="2005-08-10 06:55:57">
<type id="11">
note</type>
<author id="227499">
antirice</author>
<data>
<field name="doctext">
&lt;p&gt;I've always attributed this behavior to perl's DWIM approach to usability. The reason all three return the same thing is pretty simple: in the case where your regex doesn't capture anything, the actual instance that matches is returned instead. Also, if you have more than one capturing portion, it will push the extras onto the array as well. Try this:&lt;/p&gt;
&lt;code&gt;
$str = "Ab stuff Cd stuff Ef stuff";

# case 1
@ary1 = $str =~ m/(stuff)/g ;

@ary2 = $str =~ m/(st)u(ff)/g ;

print "\@ary1 = @ary1\n";
print "\@ary2 = @ary2\n";
__DATA__
outputs:
@ary1 = stuff stuff stuff
@ary2 = st ff st ff st ff
&lt;/code&gt;
&lt;p&gt;Nifty, eh? I rather like this behavior. Also please note that the g only means return all instances where the pattern matches. If you remove the g from the regexes above, then only the first match is returned.&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
&lt;p&gt;&lt;font size=-2&gt;&lt;i&gt;I just noticed this is my 200th post. Yay.&lt;/i&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p align="right"&gt;antirice &amp;nbsp; &amp;nbsp; &lt;br&gt;&lt;i&gt;The first rule of Perl club is - use Perl&lt;br&gt;The &lt;/i&gt;i&lt;i&gt;th rule of Perl club is - follow rule &lt;/i&gt;i&lt;i&gt; - 1 for &lt;/i&gt;i&lt;i&gt; &amp;gt;  1&lt;/i&gt;&lt;/p&gt;</field>
<field name="root_node">
291543</field>
<field name="parent_node">
291688</field>
</data>
</node>
