{"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,"_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":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"For a while now I&#039;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&#039;s set of standard Perl Best Practices. I&#039;m not going to go into the arguments of whether they&#039;re good\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Roger Pettett\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"psyphi.net blog - Another collection of braingunk and technolint\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Neat Perl Gotcha - psyphi.net blog\" \/>\n\t\t<meta property=\"og:description\" content=\"For a while now I&#039;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&#039;s set of standard Perl Best Practices. I&#039;m not going to go into the arguments of whether they&#039;re good\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2011-11-04T13:06:04+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2011-11-04T13:06:04+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Neat Perl Gotcha - psyphi.net blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"For a while now I&#039;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&#039;s set of standard Perl Best Practices. I&#039;m not going to go into the arguments of whether they&#039;re good\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#blogposting\",\"name\":\"Neat Perl Gotcha - psyphi.net blog\",\"headline\":\"Neat Perl Gotcha\",\"author\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/author\\\/rmp\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/#organization\"},\"datePublished\":\"2011-11-04T13:06:04+00:00\",\"dateModified\":\"2011-11-04T13:06:04+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#webpage\"},\"articleSection\":\"programming, perl, perlcritic\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/psyphi.net\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/category\\\/programming\\\/#listItem\",\"name\":\"programming\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/category\\\/programming\\\/#listItem\",\"position\":2,\"name\":\"programming\",\"item\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/category\\\/programming\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#listItem\",\"name\":\"Neat Perl Gotcha\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#listItem\",\"position\":3,\"name\":\"Neat Perl Gotcha\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/category\\\/programming\\\/#listItem\",\"name\":\"programming\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/#organization\",\"name\":\"psyphi.net blog\",\"description\":\"Another collection of braingunk and technolint\",\"url\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/author\\\/rmp\\\/#author\",\"url\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/author\\\/rmp\\\/\",\"name\":\"Roger Pettett\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f715f8335258578c567eb8c22335f1cee22c971c2aff8be27c4d8d934c436503?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Roger Pettett\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#webpage\",\"url\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/\",\"name\":\"Neat Perl Gotcha - psyphi.net blog\",\"description\":\"For a while now I'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's set of standard Perl Best Practices. I'm not going to go into the arguments of whether they're good\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/2011\\\/11\\\/neat-perl-gotcha\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/author\\\/rmp\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/author\\\/rmp\\\/#author\"},\"datePublished\":\"2011-11-04T13:06:04+00:00\",\"dateModified\":\"2011-11-04T13:06:04+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/\",\"name\":\"psyphi.net blog\",\"description\":\"Another collection of braingunk and technolint\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/psyphi.net\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Neat Perl Gotcha - psyphi.net blog","description":"For a while now I'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's set of standard Perl Best Practices. I'm not going to go into the arguments of whether they're good","canonical_url":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#blogposting","name":"Neat Perl Gotcha - psyphi.net blog","headline":"Neat Perl Gotcha","author":{"@id":"https:\/\/psyphi.net\/blog\/author\/rmp\/#author"},"publisher":{"@id":"https:\/\/psyphi.net\/blog\/#organization"},"datePublished":"2011-11-04T13:06:04+00:00","dateModified":"2011-11-04T13:06:04+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#webpage"},"isPartOf":{"@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#webpage"},"articleSection":"programming, perl, perlcritic"},{"@type":"BreadcrumbList","@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/psyphi.net\/blog#listItem","position":1,"name":"Home","item":"https:\/\/psyphi.net\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/psyphi.net\/blog\/category\/programming\/#listItem","name":"programming"}},{"@type":"ListItem","@id":"https:\/\/psyphi.net\/blog\/category\/programming\/#listItem","position":2,"name":"programming","item":"https:\/\/psyphi.net\/blog\/category\/programming\/","nextItem":{"@type":"ListItem","@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#listItem","name":"Neat Perl Gotcha"},"previousItem":{"@type":"ListItem","@id":"https:\/\/psyphi.net\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#listItem","position":3,"name":"Neat Perl Gotcha","previousItem":{"@type":"ListItem","@id":"https:\/\/psyphi.net\/blog\/category\/programming\/#listItem","name":"programming"}}]},{"@type":"Organization","@id":"https:\/\/psyphi.net\/blog\/#organization","name":"psyphi.net blog","description":"Another collection of braingunk and technolint","url":"https:\/\/psyphi.net\/blog\/"},{"@type":"Person","@id":"https:\/\/psyphi.net\/blog\/author\/rmp\/#author","url":"https:\/\/psyphi.net\/blog\/author\/rmp\/","name":"Roger Pettett","image":{"@type":"ImageObject","@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/f715f8335258578c567eb8c22335f1cee22c971c2aff8be27c4d8d934c436503?s=96&d=mm&r=g","width":96,"height":96,"caption":"Roger Pettett"}},{"@type":"WebPage","@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#webpage","url":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/","name":"Neat Perl Gotcha - psyphi.net blog","description":"For a while now I'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's set of standard Perl Best Practices. I'm not going to go into the arguments of whether they're good","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/psyphi.net\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/#breadcrumblist"},"author":{"@id":"https:\/\/psyphi.net\/blog\/author\/rmp\/#author"},"creator":{"@id":"https:\/\/psyphi.net\/blog\/author\/rmp\/#author"},"datePublished":"2011-11-04T13:06:04+00:00","dateModified":"2011-11-04T13:06:04+00:00"},{"@type":"WebSite","@id":"https:\/\/psyphi.net\/blog\/#website","url":"https:\/\/psyphi.net\/blog\/","name":"psyphi.net blog","description":"Another collection of braingunk and technolint","inLanguage":"en-US","publisher":{"@id":"https:\/\/psyphi.net\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"psyphi.net blog - Another collection of braingunk and technolint","og:type":"article","og:title":"Neat Perl Gotcha - psyphi.net blog","og:description":"For a while now I'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's set of standard Perl Best Practices. I'm not going to go into the arguments of whether they're good","og:url":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/","article:published_time":"2011-11-04T13:06:04+00:00","article:modified_time":"2011-11-04T13:06:04+00:00","twitter:card":"summary_large_image","twitter:title":"Neat Perl Gotcha - psyphi.net blog","twitter:description":"For a while now I'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's set of standard Perl Best Practices. I'm not going to go into the arguments of whether they're good"},"aioseo_meta_data":{"post_id":"512","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2024-05-16 15:30:01","updated":"2025-08-15 17:28:43","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/psyphi.net\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/psyphi.net\/blog\/category\/programming\/\" title=\"programming\">programming<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tNeat Perl Gotcha\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/psyphi.net\/blog"},{"label":"programming","link":"https:\/\/psyphi.net\/blog\/category\/programming\/"},{"label":"Neat Perl Gotcha","link":"https:\/\/psyphi.net\/blog\/2011\/11\/neat-perl-gotcha\/"}],"_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}]}}