<?xml version="1.0" encoding="windows-1252"?>
<node id="130032" title="(Ovid) Re: Orcish Maneuver" created="2001-12-06 18:16:30" updated="2005-07-19 14:08:11">
<type id="11">
note</type>
<author id="17000">
Ovid</author>
<data>
<field name="doctext">
&lt;p&gt;The Orcish Maneuver is a term coined by Joseph Hall in his book "Effective Perl Programming" (co-authored by [merlyn]).  Here's the essence of it:&lt;/p&gt;

&lt;code&gt;
my @sorted = sort {
	( $times{$a} ||= -M $a ) &lt;=&gt;
	( $times{$b} ||= -M $b )
} @old_array;
&lt;/code&gt;

&lt;p&gt;The "orcish" (a bad attempt at humor: "or cache") maneuver basically caches the results of an expensive lookup using the ||= operator.  This operator says, in the above code, that $times{$a} will be evaluated or, if it is false, set it to the value of -M $a.  (-M is the file test operator for last modified time).  This allows you to cache the results of -M rather than call it every time.  This has the same effect as:&lt;/p&gt;

&lt;code&gt;
for ( @old_array ) {
    $times{ $_ } = -M $_;
}

my @sorted = sort { $times{$a} &lt;=&gt; $times{$b} } @old_array;
&lt;/code&gt;

&lt;p&gt;Cheers,&lt;br /&gt;
&lt;a href="/index.pl?node=Ovid&amp;lastnode_id=1072"&gt;Ovid&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;small&gt;Join the &lt;a href="http://setiathome.ssl.berkeley.edu/stats/team/team_86606.html"&gt;Perlmonks Setiathome Group&lt;/a&gt; or just click on the the link and check out our stats.&lt;/small&gt;&lt;/p&gt;</field>
<field name="root_node">
129788</field>
<field name="parent_node">
130021</field>
</data>
</node>
