<?xml version="1.0" encoding="windows-1252"?>
<node id="527973" title="Perl Idioms Explained - my $count = () = /.../g" created="2006-02-04 14:14:17" updated="2006-02-04 09:14:17">
<type id="120">
perlmeditation</type>
<author id="1936">
japhy</author>
<data>
<field name="doctext">
&lt;b&gt;Perl Idioms Explained - &lt;c&gt;my $count = () = /.../g&lt;/c&gt;&lt;/b&gt;

&lt;c&gt;
my $str = "here are some words";
my $count = () = $str =~ /\S+/g;
&lt;/c&gt;

The "empty parentheses" idiom is one that is used to force list context on an expression, and yet return a scalar:  the count of items in that list.  We are often told that there is no way to get the size of a list -- it must either be iterated over, or stored in an array, or some other means -- because when you &lt;i&gt;try&lt;/i&gt; treating a list as a scalar, you're no longer dealing with a list:  you're enforcing scalar context on some expression.  Here are some examples:

&lt;c&gt;
my $last_operand = ("a", "b", "c");  # "c"
my $true_or_false = /\w+/g;          # 1 or ""
my $dunno = some_function();         # who knows?!
&lt;/c&gt;

The last example is particularly nasty:  the return value of the function will be evaluated in scalar context.  If that value is &lt;c&gt;@array&lt;/c&gt;, then you'll get the size of the array; if it's &lt;c&gt;@array[0,1]&lt;/c&gt;, you'll get &lt;c&gt;$array[1]&lt;/c&gt;.

&lt;p&gt;

So how does the empty parentheses idiom work?  It uses a little-exercised rule:  &lt;b&gt;a list assignment in scalar context returns the number of elements on the right-hand side of the list assignment&lt;/b&gt;.  That's probably also rarely-spoken, since it's a bit dense.  What it means is that an expression like &lt;c&gt;(LIST1) = (LIST2)&lt;/c&gt;, in scalar context, returns the number of elements in &lt;c&gt;LIST2&lt;/c&gt; (not &lt;c&gt;LIST1&lt;/c&gt;).  This means &lt;c&gt;(LIST1)&lt;/c&gt; could be replaced with &lt;c&gt;()&lt;/c&gt; and the scalar value returned would still be the size of &lt;c&gt;LIST2&lt;/c&gt;.

&lt;p&gt;

This trick is most frequently used in conjunction with a regex with the "global" switch on it.  In scalar context, such a regex would only match once, but in list context, it matches exhaustively.  To get all such matches, one would do &lt;c&gt;@matches = /.../g&lt;/c&gt;.  But if we aren't interested in keeping the matches around, and instead just want to know how many there are, we could replace &lt;c&gt;@matches&lt;/c&gt; with &lt;c&gt;()&lt;/c&gt;, and evaluate it all in scalar context: &lt;c&gt;my $count = (() = /.../g);&lt;/c&gt;.  Of course, those outer parentheses are unnecessary, so the idiom becomes &lt;c&gt;my $count = () = /.../g;&lt;/c&gt;.

&lt;p&gt;

&lt;b&gt;Summary&lt;/b&gt;

&lt;p&gt;

It forces an expression to be evaluated in list context, and returns the number of elements returned by that expression.

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-1936"&gt;
&lt;hr/&gt;
Jeff &lt;tt&gt;&lt;font color="#0000ff"&gt;[japhy]&lt;/font&gt;&lt;/tt&gt; Pinyan,
[id://371157|P.L., P.M., P.O.D, X.S.]:

&lt;a href="http://japhy.perlmonk.org/modules/"&gt;Perl&lt;/a&gt;,

&lt;a href="http://japhy.perlmonk.org/modules/Regexp-Parser/"&gt;regex&lt;/a&gt;,

and &lt;a href="http://lists.perl.org/showlist.cgi?name=perl5-porters"&gt;&lt;tt&gt;perl&lt;/tt&gt;&lt;/a&gt;

&lt;a href="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=hacker"&gt;hacker&lt;/a&gt;

&lt;br&gt;
&lt;i&gt;How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ &lt;b&gt;Meister Eckhart&lt;/b&gt;&lt;/i&gt;
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
