{"id":9175,"date":"2014-07-17T15:14:01","date_gmt":"2014-07-17T19:14:01","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9175"},"modified":"2014-07-17T15:14:40","modified_gmt":"2014-07-17T19:14:40","slug":"guess-what-this-perl-does","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/07\/17\/guess-what-this-perl-does\/","title":{"rendered":"Guess What This Perl Does"},"content":{"rendered":"<p><a href=\"http:\/\/blog.plover.com\/prog\/perl\/barewords.html\">Mark Dominus<\/a> (author of an <a href=\"http:\/\/mjtsai.com\/blog\/2008\/12\/09\/higher-order-perl\/\">advanced Perl book<\/a>):<\/p>\n<blockquote cite=\"http:\/\/blog.plover.com\/prog\/perl\/barewords.html\"><p>A few weeks ago <a href=\"https:\/\/twitter.com\/mjdominus\/status\/474657612529938432\">I asked people to\npredict<\/a>, without trying it first, what this would print:<\/p>\n<pre>perl -le 'print(two + two == five ? \"true\" : \"false\")'<\/pre>\n<p>(If you haven&rsquo;t seen this yet, I recommend that you guess, and then\ntest your guess, before reading the rest of this article.)<\/p>\n\n<p>People familiar with Perl guess that it will print <code>true<\/code>; that is\nwhat I guessed.  The reasoning is as follows: Perl is willing to treat\nthe unquoted strings <code>two<\/code> and <code>five<\/code> as strings, as if they had been\nquoted, and is also happy to use the <code>+<\/code> and <code>==<\/code> operators on them,\nconverting the strings to numbers in its usual way.  If the strings\nhad looked like <code>\"2\"<\/code> and <code>\"5\"<\/code> Perl would have treated them as 2 and\n5, but as they don&rsquo;t look like decimal numerals, Perl interprets them\nas zeroes.  (Perl wants to issue a warning about this, but the warning is not enabled by default.\nSince the <code>two<\/code> and <code>five<\/code> are treated as\nzeroes, the result of the <code>==<\/code> comparison are true, and the string\n<code>\"true\"<\/code> should be selected and printed.<\/p><\/blockquote>\n<p>Of course, that&rsquo;s not what it does.<\/p>","protected":false},"excerpt":{"rendered":"<p>Mark Dominus (author of an advanced Perl book): A few weeks ago I asked people to predict, without trying it first, what this would print: perl -le 'print(two + two == five ? \"true\" : \"false\")' (If you haven&rsquo;t seen this yet, I recommend that you guess, and then test your guess, before reading the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[252,71],"class_list":["post-9175","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-perl","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=9175"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9175\/revisions"}],"predecessor-version":[{"id":9177,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9175\/revisions\/9177"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}