{"id":512,"date":"2011-11-04T13:06:04","date_gmt":"2011-11-04T13:06:04","guid":{"rendered":"http:\/\/psyphi.net\/blog\/?p=512"},"modified":"2011-11-04T13:06:04","modified_gmt":"2011-11-04T13:06:04","slug":"neat-perl-gotcha","status":"publish","type":"post","link":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/","title":{"rendered":"Neat Perl Gotcha"},"content":{"rendered":"<p>For a while now I&#8217;ve been using Test::Perl::Critic as an integral part of my daily coding. <a title=\"Test-Perl-Critic on CPAN\" href=\"http:\/\/search.cpan.org\/~thaljef\/Test-Perl-Critic\" target=\"_blank\">Test::Perl::Critic<\/a> wraps <a title=\"Perl-Critic on CPAN\" href=\"http:\/\/search.cpan.org\/~elliotjs\/Perl-Critic\" target=\"_blank\">Perl::Critic<\/a> into a set of tests which can be automatically run against a distribution. Perl Critic implements Damien Conway&#8217;s set of standard <a title=\"Perl Best Practices on Amazon\" href=\"http:\/\/www.amazon.co.uk\/gp\/product\/0596001738\/ref=as_li_ss_tl?ie=UTF8&amp;tag=psynet-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=0596001738\" target=\"_blank\">Perl Best Practices<\/a>.<\/p>\n<p>I&#8217;m not going to go into the arguments of whether they&#8217;re good or bad rules right now. Suffice to say I use nearly all of them and my code has improved because of it. Anyway, one of the rules states you shouldn&#8217;t use parentheses for core method calls like <code>length()<\/code>, <code>int()<\/code> or <code>rand()<\/code>, so most of the time I don&#8217;t, but today I wanted to do this:<\/p>\n<pre><code>my @array = map { int rand * 45 } (1..10);<\/code><\/pre>\n<p>The results come out as an array of zeros. Why? Well it&#8217;s not a broken PRNG, that&#8217;s for sure. It&#8217;s a simple case of misunderstood parsing, operator precedence and Perl internals. Looking closely <code>* 45<\/code> isn&#8217;t what you&#8217;d expect, <code>*45<\/code> refers to the typeglob named 45 in the main package. I actually have no idea how this is cast into a number in Perl but however it&#8217;s done, it evaluates to zero. <code>rand 0<\/code> seems to exhibit the same behaviour as <code>rand 1<\/code>, yielding a random float between zero and 1. <code>int rand 0<\/code> will always be zero.<\/p>\n<p>So? Well to stop the parser from taking <code>*45<\/code> as an argument to rand you need to upset PerlCritic and add those parentheses back in:<\/p>\n<pre><code>my @array = map { int rand() *45 } (1..10);<\/code><\/pre>\n<p>and it works now so back to work!<\/p>\n<p>[ The astute amongst you will realise you can just say <code>int rand(45)<\/code>. I know, but that doesn&#8217;t work nearly so well as a mildly interesting example. I blame PerlCritic ;) ]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For a while now I&#8217;ve been using Test::Perl::Critic as an integral part of my daily coding. Test::Perl::Critic wraps Perl::Critic into a set of tests which can be automatically run against a distribution. Perl Critic implements Damien Conway&#8217;s set of standard Perl Best Practices. I&#8217;m not going to go into the arguments of whether they&#8217;re good &hellip; <a href=\"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Neat Perl Gotcha&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[11],"tags":[21,714],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-programming","tag-perl","tag-perlcritic"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":4,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":516,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/512\/revisions\/516"}],"wp:attachment":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}