<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/styles/rss-style.xsl"?>

<rss version="2.0"
 xmlns:blogChannel="http://backend.userland.com/blogChannelModule"
>

<channel>
<title>troglodyne.net</title>
<link>http://troglodyne.net//posts/12794b9f-69e9-4f42-83dc-2bec175d8249?format=xml</link>
<description>troglodyne.net : /posts/12794b9f-69e9-4f42-83dc-2bec175d8249</description>
<language>en</language>
<pubDate>2026-04-21T15:30:02</pubDate>
<lastBuildDate>2026-04-21T15:30:02</lastBuildDate>

<image>
<title>troglodyne.net</title>
<url>/favicon.ico</url>
<link>http://troglodyne.net</link>
<width>32</width>
<height>32</height>
<description>troglodyne.net favicon</description>
</image>
<item>
<title>How I learned to love postfix for in perl</title>
<link>http://troglodyne.net/posts/12794b9f-69e9-4f42-83dc-2bec175d8249</link>
<description><![CDATA[Suppose you do a common thing like a mapping into a hash but decide to filter the input first:
<pre>shit.pl<code lang="perl">
my %hash = map {
    "here" => $_
} grep {
    -d $_
} qw{a b c d .};
</code></pre>

This claims there is a syntax eror on line 6 where the grep starts.
This is a clear violation of the principle of least-astonishment as both the map and grep work by themselves when not chained.
We can fix this by assigning $_ like so:

<pre>fixed.pl<code lang="perl">
my %hash = map {
    my $subj = $_;
    "here" => $subj
} grep {
    my $subj = $_;
    -d $subj
} qw{a b c d .};
</code></pre>
<p>
Now we get what we expect, which is no syntax error.
This offends the inveterate golfer in me, but it is in many critic rules for a reason.
In particular when nested lexical scope inside of the map/grep body is a problem, which is not the case here.
</p>
<p>
But never fear, there is a superior construct to map in all cases...postfix for!
<pre>oxyclean.pl<code lang="perl">
my %hash = "here" => $_ for grep { -d $_ } qw{a b c .};
</code></pre>
No syntax errors <em>and</em> it's a oneliner.
It's also faster due to not assigning a lexical scope.]]></description>
<author>george</author>
<guid isPermaLink="true">http://troglodyne.net/posts/12794b9f-69e9-4f42-83dc-2bec175d8249</guid>
<pubDate>2024-08-06T22:07:16</pubDate>
<enclosure type="text/html" url="http://troglodyne.net/posts/12794b9f-69e9-4f42-83dc-2bec175d8249" />
</item>
</channel>
</rss>
