<?xml version="1.0" encoding="windows-1252"?>
<node id="1007787" title="new keyword &quot;size&quot; to avoid scalar trap ? ( scalar @array != scalar(LIST) )" created="2012-12-07 11:40:54" updated="2012-12-07 11:40:54">
<type id="120">
perlmeditation</type>
<author id="708738">
LanX</author>
<data>
<field name="doctext">
Hi &lt;P&gt;&lt;P&gt;

I'm meditating if Perl should introduce an explicit keyword &lt;c&gt;size&lt;/c&gt; (or &lt;c&gt;count&lt;/c&gt; ¹) always return the number of elements of lists and arrays... ([doc://length] is already taken and [doc://scalar] is to overloaden)&lt;P&gt;&lt;P&gt;

so
&lt;c&gt;
size @a := scalar @a;
&lt;/c&gt;&lt;P&gt;&lt;P&gt;

and&lt;P&gt;&lt;P&gt;

&lt;c&gt;
size (a..c) := scalar @{[a..c]}
&lt;/c&gt;&lt;P&gt;&lt;P&gt;

I know &lt;i&gt;"explicit is better than implicit"&lt;/i&gt; is more a Python dogma, but since even [BrowserUK|popes] still fall into the [id://1007687|scalar trap] we should maybe consider &lt;i&gt;"another way to do it"&lt;/i&gt; (best Perl tradition =).&lt;P&gt;&lt;P&gt;


&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-708738"&gt;
&lt;p&gt;Cheers Rolf
&lt;/div&gt;&lt;/div&gt;&lt;P&gt;

PS: I tried to provide an example implementation of size, something like&lt;P&gt;

&lt;c&gt;sub size([@$];@) {
...
}&lt;/c&gt; &lt;P&gt;

but was to lazy to fiddle around with prototypes.&lt;P&gt;

Anyway IMHO a reliable implementation will need more parsing magic than prototypes can bring to avoid disambiguation.&lt;P&gt;

¹) or [id://1007798|whatever]
</field>
</data>
</node>
