<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4857673566967429385</id><updated>2012-02-29T18:50:17.815Z</updated><category term='PHP'/><category term='newbies'/><category term='skeptics'/><category term='Template::Toolkit'/><category term='musings'/><category term='rantings'/><category term='announcements'/><title type='text'>Podcats.in temporary accommodation.</title><subtitle type='html'>Herp derp I broke my site so here's a temp blog in the meantime.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-5635852779980713025</id><published>2012-01-30T23:00:00.000Z</published><updated>2012-01-30T23:52:43.463Z</updated><title type='text'>Simple gnome 3 wallpaper switcher</title><content type='html'>Gnome 3 uses dconf rather than gconf, so gconf-based wallpaper switchers won't work. Luckily, the actual change is only that one line if you do it right.&lt;br /&gt;&lt;br /&gt;I created a script that uses a cache of wallpapers. First, here's the script that searches your wallpapers directory for actual images (to filter out stupid Thumbs.db and things like that) and adds them to a file.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;#!/bin/bash&lt;br /&gt;find "$HOME/wallpapers" -type f \&lt;br /&gt;&amp;nbsp; -exec sh -c 'file "$1" | grep -q "image"' '{}' '{}' \; \&lt;br /&gt;&amp;nbsp; -a -fprint wallpapers-cache-new \&lt;br /&gt;\&lt;br /&gt;&amp;amp;&amp;amp; mv wallpapers-cache-new wallpapers-cache&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That outputs a file called &lt;code&gt;wallpapers-cache&lt;/code&gt;, and the wallpaper switcher itself uses that:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;#!/bin/bash&lt;br /&gt;&lt;br /&gt;WALLPAPERS="$HOME/wallpapers-cache"&lt;br /&gt;export DISPLAY=:0&lt;br /&gt;&lt;br /&gt;r=`rl -c1 $WALLPAPERS`&lt;br /&gt;&lt;br /&gt;gsettings set org.gnome.desktop.background picture-uri "file://$r"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;code&gt;rl&lt;/code&gt; comes from the &lt;code&gt;randomize-lines&lt;/code&gt; package in Debian-based OSes.&lt;br /&gt;&lt;br /&gt;Add the latter to crontab to change your desktop as often as you'd like. Voilà!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-5635852779980713025?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/5635852779980713025/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2012/01/simple-gnome-3-wallpaper-switcher.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/5635852779980713025'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/5635852779980713025'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2012/01/simple-gnome-3-wallpaper-switcher.html' title='Simple gnome 3 wallpaper switcher'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-6102698643119749924</id><published>2011-11-24T09:56:00.001Z</published><updated>2011-11-24T10:12:52.580Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='PHP'/><category scheme='http://www.blogger.com/atom/ns#' term='Template::Toolkit'/><category scheme='http://www.blogger.com/atom/ns#' term='musings'/><title type='text'>Testing for the end of a for(each) loop</title><content type='html'>One sign of a good templating language is that you can test for the last iteration of a loop without having to count the iterations. PHP is no exception! Assuming you're quite happy with hacky syntax (you have to be if you're using PHP) then you can do this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;foreach ($array as $item) {&lt;br /&gt;  if (each($array) === false) {&lt;br /&gt;    # last iteration&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;You iterate over the original array using &lt;code&gt;each&lt;/code&gt;, but ignore the output if it's not boolean false. &lt;code&gt;foreach&lt;/code&gt; derps around on the copy of the array, and when the original array finishes being iterated, &lt;code&gt;each&lt;/code&gt; returns false! So you basically iterate over the array while you iterate over the array.&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-G58z3nH6peU/Ts4YZzrKEII/AAAAAAAAAGs/vVOZ24yj8nc/s1600/yo+dawg+31.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="210" src="http://4.bp.blogspot.com/-G58z3nH6peU/Ts4YZzrKEII/AAAAAAAAAGs/vVOZ24yj8nc/s320/yo+dawg+31.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;In &lt;a href="http://template-toolkit.org/"&gt;Template::Toolkit&lt;/a&gt; you can also &lt;a href="http://template-toolkit.org/docs/manual/Directives.html#section_Loop_Processing"&gt;test for the last iteration of a loop&lt;/a&gt; with &lt;code&gt;loop.last&lt;/code&gt;, which is why that is also a good templating language.&lt;br /&gt;&lt;br /&gt;In conclusion, PHP is a templating language.&lt;br /&gt;&lt;br /&gt;Oh right, leave a comment with your own one for whatever bozo templating language you like :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-6102698643119749924?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/6102698643119749924/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/11/testing-for-end-of-foreach-loop.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/6102698643119749924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/6102698643119749924'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/11/testing-for-end-of-foreach-loop.html' title='Testing for the end of a for(each) loop'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-G58z3nH6peU/Ts4YZzrKEII/AAAAAAAAAGs/vVOZ24yj8nc/s72-c/yo+dawg+31.jpg' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-8735474182704639777</id><published>2011-11-10T17:15:00.000Z</published><updated>2012-02-28T16:15:41.946Z</updated><category scheme='http://www.blogger.com/atom/ns#' term='newbies'/><title type='text'>In Context</title><content type='html'>&lt;br /&gt;Natural language is contextual. A famous quotation demonstrates this in a way we all find familiar:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Time flies like an arrow; fruit flies like a banana.&lt;/blockquote&gt;&lt;br /&gt;Usually we interpret this as "the manner in which time flies (verb - to fly) is akin to an arrow" and "the creatures called fruit flies (noun pl. - fly) like (to find agreeable) a banana". However, we can also interpret the former as "Time (measure chronologically) flies (noun pl. - fly) the way an arrow would", except of course this is nonsense and we don't consider it.&lt;br /&gt;&lt;br /&gt;Perl is the same. The three data structures covered in &lt;a href="http://altreus.blogspot.com/2011/06/anatomy-of-types.html"&gt;previous&lt;/a&gt; &lt;a href="http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html"&gt;posts&lt;/a&gt; can be used in all sorts of ways, and Perl will attempt to do the right thing based on context. This is done by having rules about context, and what to do in those contexts.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Time Flies&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When we say "time flies" in English we can either mean a) imperative "time", noun "flies" or b) noun "time" verb "flies". In Perl we can differentiate between nouns and verbs because nouns have sigils and verbs don't.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;$scalar&lt;/code&gt; variables have &lt;code&gt;$&lt;/code&gt; and a singular name because they represent a single thing. &lt;code&gt;@array&lt;/code&gt; variables use the &lt;code&gt;@&lt;/code&gt; sigil and have plural names because they refer to more than one thing. &lt;code&gt;%hash&lt;/code&gt; variables refer to a set of named things; their names are often singular because either we refer to the set ("The option set &lt;code&gt;%option&lt;/code&gt;") or a thing from the set ("The debug option &lt;code&gt;$option{debug}&lt;/code&gt;").&lt;br /&gt;&lt;br /&gt;Verbs, of course, are subroutines and so have no sigil. That means in Perl we can't misinterpret that:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;time @flies;&lt;/pre&gt;&lt;br /&gt;Notwithstanding the fact that &lt;code&gt;time&lt;/code&gt; is already a Perl function for finding the time, we can understand this because flies &lt;i&gt;has&lt;/i&gt; to be a noun.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Flies Like A Banana&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Context in Perl is not a grammatical thing in the natural-language sense explained above, but it is used to determine what we do with the nouns that we pass around.&lt;br /&gt;&lt;br /&gt;A common example is to check for an array to have content:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;if (@flies) {&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;In list context, an array represents the list of scalars it contains, as we saw demonstrated in &lt;a href="http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html"&gt;this post&lt;/a&gt;, but in scalar context the array is treated as the number of items in it; i.e. its length.&lt;br /&gt;&lt;br /&gt;The test in an &lt;code&gt;if&lt;/code&gt; statement is in &lt;b&gt;boolean&lt;/b&gt; context. Booleans are truey and falsey values: they are scalar. An empty array in scalar context is zero, which is false; hence an empty array is false.&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;while&lt;/code&gt; loop also uses boolean, and hence scalar, context.  However, the &lt;code&gt;for(each)&lt;/code&gt; loop uses list context:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for my $fly (@flies) {&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This is sensible because if it used scalar context it would always loop over one thing, which is the number of things in the array. A scalar in list context doesn't really do anything special; it remains that scalar:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for my $fly ($one_fly) {&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;but it does help with concatenation of several things:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for my $fly ($bluebottle, $mayfly, @various_flies) {&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Fruit Flies Like&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Surprises crop up when dealing with context once operators and functions get involved. For example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $banana = map { $_-&gt;fruit_i_like } @flies;&lt;/pre&gt;&lt;br /&gt;You might think, maybe this will take the first fruit that a fly likes, or the last, or some data structure representing the result of the map. Well the latter is the closest: the data structure returned is in fact an integer representing the number of fruits the flies, in total, liked. The context in this statement is in the &lt;code&gt;=&lt;/code&gt; operator: it is &lt;i&gt;determined by&lt;/i&gt; the thing on the left and it is &lt;i&gt;enforced on&lt;/i&gt; the thing on the right. perldoc says that map in scalar context returns the length of the constructed list.&lt;br /&gt;&lt;br /&gt;We can take the first fruit liked by a fly by putting the &lt;code&gt;=&lt;/code&gt; operator in list context. In this case, that is done using parentheses:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my ($banana) = map { $_-&gt;fruit_i_like } @flies;&lt;/pre&gt;&lt;br /&gt;Parentheses only create list context on the left of &lt;code&gt;=&lt;/code&gt;; as we learned in &lt;a href="http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html"&gt;this post&lt;/a&gt;, parentheses on the right only serve to override the precedence of operators.&lt;br /&gt;&lt;br /&gt;Of course, assigning to an aggregate type is also list context:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @fruits = map { $_-&gt;fruit_i_like } @flies;&lt;br /&gt;my %likes = map { $_-&gt;name =&gt; $_-&gt;fruit_i_like } @flies;&lt;/pre&gt;&lt;br /&gt;(In this latter example we assume &lt;code&gt;fruit_i_like&lt;/code&gt; returns only one item; otherwise the constructed list will break the expected hash construction.) &lt;br /&gt;&lt;br /&gt;Another common confusion is when the operator or function itself enforces a context:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $random_fly = $flies[rand @flies];&lt;/pre&gt;&lt;br /&gt;The &lt;code&gt;rand&lt;/code&gt; function returns a random number; if it is provided with an integer it will return a random number between 0 and that integer, including 0 and excluding the integer. Normal behaviour for a function is to use an array as a parameter list, but &lt;code&gt;rand&lt;/code&gt; overrides this by enforcing scalar context on its arguments. &lt;br /&gt;&lt;br /&gt;That means that the array given to &lt;code&gt;rand&lt;/code&gt; is evaluated in scalar context, giving its length. The output is used in &lt;code&gt;[]&lt;/code&gt; to index the array; array indices are required to be integers so the decimal return value from &lt;code&gt;rand&lt;/code&gt; is truncated. Since &lt;code&gt;rand&lt;/code&gt; can never return the integer provided to it, the maximum return value from this is the last index of the array, and the minimum is zero.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Enough With The Rearranging Of The Quotation You Cited&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The context of any particular part of Perl can be subtle. I will describe a few common situations where context is key to the operation of the script.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;code&gt;if&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As explained, &lt;code&gt;if&lt;/code&gt; enforces scalar context.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;if ($fly) { ...} &lt;br /&gt;if (@flies){ ... }&lt;br /&gt;if (%like) { ... }&lt;br /&gt;if (grep { $_-&gt;likes($banana) } @flies) { ... }&lt;br /&gt;if (my ($fly) = grep { $_-&gt;flies } @times) { ... }&lt;/pre&gt;&lt;br /&gt;All of these are in scalar context. What about the last one though? We know that &lt;code&gt;()&lt;/code&gt; on the left of &lt;code&gt;=&lt;/code&gt; creates list context; but &lt;code&gt;if()&lt;/code&gt; is scalar context.&lt;br /&gt;&lt;br /&gt;Context happens in stages, naturally, just as expressions are evaluated sequentially. The &lt;code&gt;=&lt;/code&gt; operator is in list context as defined by its LHS. This allows &lt;code&gt;$fly&lt;/code&gt; to contain the first result from the grep.  The cunning thing is that the &lt;code&gt;=&lt;/code&gt; operator &lt;i&gt;itself&lt;/i&gt; returns the value on its LHS &lt;i&gt;after assignment&lt;/i&gt;: this is evaluated in scalar context and used as the boolean value for the &lt;code&gt;if&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Consider:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;if (my @times_that_fly = grep { $_-&gt;flies } @times) { ... }&lt;/pre&gt;&lt;br /&gt;You can expect &lt;code&gt;@times_that_fly&lt;/code&gt; to be set and non-empty in the &lt;code&gt;if&lt;/code&gt; block, and also for it to contain the result of the grep.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The operation of the &lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt; "diamond" operator is context-sensitive. That means it has different behaviour in list and scalar contexts. In scalar context, it returns one line of input. In list context, it returns all lines of input.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $line = &amp;lt;&amp;gt;;&lt;br /&gt;my @lines = &amp;lt;&amp;gt;;&lt;/pre&gt;&lt;br /&gt;As we know, &lt;code&gt;for&lt;/code&gt; is a list-context control structure; hence it will read all lines into memory before iterating them:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for my $line (&amp;lt;&amp;gt;)&lt;/pre&gt;&lt;br /&gt;The &lt;code&gt;while&lt;/code&gt; control construct, like &lt;code&gt;if&lt;/code&gt;, tests a boolean value, except it does it repeatedly and stops running when it is false. Hence the expression in the parentheses is evaluated in scalar context. So it is idiomatic to see &lt;code&gt;while&lt;/code&gt; used instead of &lt;code&gt;for&lt;/code&gt; when iterating over input:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;while (&amp;lt;&amp;gt;) { ... }&lt;br /&gt;while (my $line = &amp;lt;&amp;gt;) { ... }&lt;/pre&gt;&lt;br /&gt;But I hear you cry that the line of input "0" is false! But it should be treated fairly! Well for a start it will probably be "0\n", but apart from that, &lt;code&gt;while(&amp;lt;&amp;gt;)&lt;/code&gt; is actually magic and comes out as:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;while (defined($_ = &amp;lt;&amp;gt;))&lt;/pre&gt;&lt;br /&gt;and even if you set your own variable:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;while (my $line = &amp;lt;&amp;gt;) { ... }&lt;br /&gt;while (defined(my $line = &amp;lt;&amp;gt;)) { ... }&lt;/pre&gt;&lt;br /&gt;but this is &lt;i&gt;only&lt;/i&gt; true inside a &lt;code&gt;while()&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;code&gt;$array[rand @array]&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We've covered this one but it's still for this list. The &lt;code&gt;rand&lt;/code&gt; function enforces scalar context on its operand; hence, instead of the array providing a list of arguments to the function, it is itself the argument, but in scalar context, hence its length is returned. The array subscript &lt;code&gt;[]&lt;/code&gt; then truncates the return value of &lt;code&gt;rand&lt;/code&gt; to an integer.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;code&gt;=()=&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Called the goatse operator by those with a sense of humour that compels them to do so, this is not really an operator at all but a trick of context. You see, not all operators return a count in scalar context.&lt;br /&gt;&lt;br /&gt;For example, the match operator &lt;code&gt;m/.../&lt;/code&gt; or just &lt;code&gt;/.../&lt;/code&gt; merely returns true or false in scalar context. With the &lt;code&gt;/g&lt;/code&gt; modifier it returns the &lt;i&gt;next match&lt;/i&gt;! In what universe, then, can a mere Perl hacker get &lt;code&gt;m/../g&lt;/code&gt; to return the number of matches?&lt;br /&gt;&lt;br /&gt;The universe with the goatse operator of course.&lt;br /&gt;&lt;br /&gt;First we know that &lt;code&gt;()&lt;/code&gt; on the LHS of &lt;code&gt;=&lt;/code&gt; creates list context. That is all that is required for &lt;code&gt;m/../g&lt;/code&gt; to return a full list of all matches. So the match is run in list context and that's what we want. The output of our goatse operator is of course going to be a scalar since we want an integer; this enforces scalar context on the leftmost &lt;code&gt;=&lt;/code&gt;, which has the effect of putting the &lt;i&gt;return value&lt;/i&gt; of the rightmost &lt;code&gt;=&lt;/code&gt; (which is the list created by &lt;code&gt;()&lt;/code&gt;) in scalar context, hence counting it.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $count =()= /cats/g&lt;br /&gt;my $count = ( () = /cats/g )&lt;/pre&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;code&gt;@array[LIST]&lt;/code&gt;, &lt;code&gt;@hash{LIST}&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A subtle use of context is the one in array and hash subscripts. The context inside &lt;code&gt;{ }&lt;/code&gt; or &lt;code&gt;[ ]&lt;/code&gt; when accessing elements is determined by the sigil you used on the identifier in the first place. Thus:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;$array[SCALAR]&lt;br /&gt;@array[LIST]&lt;br /&gt;$hash{SCALAR}&lt;br /&gt;@hash{LIST}&lt;/pre&gt;&lt;br /&gt;This can have unexpected effects; a warning is generated if you use the @ form but only provide a single value, but in some cases the context can propagate, for example if you are calling a function or something.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Context and Subroutines&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A subroutine is called with the context of the place where you called it. This is often a useful tool, because a subroutine can also interrogate the calling context with the &lt;code&gt;wantarray&lt;/code&gt; operator. &lt;br /&gt;&lt;br /&gt;When a subroutine is called its &lt;core&gt;return&lt;/code&gt; operator inherits the calling context. If it doesn't have a &lt;code&gt;return&lt;/code&gt; operator, the usual rules apply about an implicit one. &lt;br /&gt;&lt;br /&gt;The main reason this happens is that any basic function call - i.e. one with no special behaviour based on context - should act as though the return value were in the code in place of the function call. Consider:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;rand @flies;&lt;br /&gt;&lt;br /&gt;sub get_flies {&lt;br /&gt;    # ... generate @flies&lt;br /&gt;    return @flies;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;rand get_flies();&lt;/pre&gt;&lt;br /&gt;It is reasonable to assume that, because the function returned an array, the behaviour of the latter &lt;code&gt;rand&lt;/code&gt; will be the same as the former, and it is: the &lt;code&gt;return&lt;/code&gt; of the subroutine is evaluated in scalar context because &lt;code&gt;rand&lt;/code&gt; enforces scalar context.&lt;br /&gt;&lt;br /&gt;Usually you want to save the return value of a function into some local variable, though; otherwise you have to run the function twice to use it twice, which is wasteful:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;(get_flies())[rand get_flies()]&lt;/pre&gt;&lt;br /&gt;It is important to be aware of the context in which you are calling your function, especially if the function has different behaviour in different contexts. For example, here we have list context:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;localtime(flies());&lt;/pre&gt;&lt;br /&gt;and here we have scalar context:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;rand(flies());&lt;/pre&gt;&lt;br /&gt;and here we have a syntax error:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;time(flies());&lt;/pre&gt;&lt;br /&gt;because &lt;code&gt;time&lt;/code&gt; doesn't take any arguments. By default, all subroutines give list context when called, so:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;sub context_sensitive {&lt;br /&gt;    return wantarray ? 'STRING' : qw(TWO STRINGS);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub print_things {&lt;br /&gt;    print $_, "\n" for @_;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;print_things context_sensitive;&lt;/pre&gt;&lt;br /&gt;Here the &lt;code&gt;context_sensitive&lt;/code&gt; sub is in list context, so it returns &lt;code&gt;qw(TWO STRINGS)&lt;/code&gt;, which is indeed two strings, as noted by the &lt;code&gt;print_things&lt;/code&gt; function, which will print each thing with a new line after it so we can see what's going on. However, this sort of thing can lead to confusion when you don't realise the function is context sensitive:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $string = context_sensitive;&lt;br /&gt;print_things $string;&lt;/pre&gt;&lt;br /&gt;The above code exhibits different behaviour from the first, because &lt;code&gt;context_sensitive&lt;/code&gt; was called in scalar context and the result of that was given to &lt;code&gt;print_things&lt;/code&gt;. This can be surprising, because it means that context-sensitive subroutines are not always drop-in replacements for the variable they were saved to.&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;scalar&lt;/code&gt; keyword can be used here to enforce scalar context on the subroutine.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;print_things scalar context_sensitive;&lt;/pre&gt;&lt;br /&gt;The &lt;code&gt;scalar&lt;/code&gt; keyword can be used in the general case when you want to either override or be explicit about the context of anything.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;scalar @array;&lt;br /&gt;scalar $scalar;&lt;br /&gt;scalar sub_call();&lt;br /&gt;scalar grep {...} @array;&lt;/pre&gt;&lt;br /&gt;There is no real list-context equivalent because in general list context is a superset of scalar context, but as with the goatse operator, there are exceptions. Hence the construct &lt;code&gt;() =&lt;/code&gt; can be used to evaluate something in list context when normally it would be in scalar context. The reason this is not common is the return value of this assignment will still be evaluated in scalar context, which in most cases is the same as simply not doing this at all.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Void Context&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Sometimes you might get a warning along the lines of "useless use of constant in void context". What does this mean?&lt;br /&gt;&lt;br /&gt;The difference between a statement and an expression is action. An expression is anything that returns a value: &lt;code&gt;5+5&lt;/code&gt;, &lt;code&gt;sub_call()&lt;/code&gt;, &lt;code&gt;@array&lt;/code&gt; ...&lt;br /&gt;&lt;br /&gt;A statement is one that performs an action: &lt;code&gt;$foo = 5+5&lt;/code&gt;, &lt;code&gt;sub_call()&lt;/code&gt;, &lt;code&gt;if(@array)&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Not all legal lines of code in Perl are statements:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $foo = 5+5;   # statement&lt;br /&gt;5+5;  # not a statement&lt;/pre&gt;&lt;br /&gt;It is this latter line of code that will cause the warning about void context. It should be at least slightly apparent why it is in void context: there is no operator or function call enforcing context on the expression. There is no context at this point! This is called void context; neither scalar nor list context is enforced on the simple line of code &lt;code&gt;5+5;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Void context is not always incorrect. For example, we know that the &lt;code&gt;=&lt;/code&gt; operator returns the value on the left hand side. That means that whenever you perform an assignment, the whole assignment itself is performed in void context. It all works because of the layered nature of expressions and sub-expressions. Consider:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;$foo = @bar = baz()&lt;/pre&gt;&lt;br /&gt;&lt;code&gt;baz()&lt;/code&gt; is performed in list context because of the assignment to &lt;code&gt;@bar&lt;/code&gt;, but that assignment &lt;i&gt;itself&lt;/i&gt; is performed in scalar context because of the assignment to &lt;code&gt;$foo&lt;/code&gt;. But the assignment to &lt;code&gt;$foo&lt;/code&gt; doesn't cause &lt;code&gt;baz()&lt;/code&gt; to be run in scalar context. &lt;code&gt;@bar = baz()&lt;/code&gt; happens first, and &lt;code&gt;baz()&lt;/code&gt; is in list context. This whole expression returns &lt;code&gt;@bar&lt;/code&gt; because that is the behaviour of &lt;code&gt;=&lt;/code&gt;. That means the next thing that happens is &lt;code&gt;$foo = @bar&lt;/code&gt;, which puts &lt;code&gt;@bar&lt;/code&gt; in scalar context and populates &lt;code&gt;$foo&lt;/code&gt;. This expression returns &lt;code&gt;$foo&lt;/code&gt;, but there is no other expression in this statement. So this assignment happens in void context, which does nothing.&lt;br /&gt;&lt;br /&gt;This is also void context:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;baz();&lt;/pre&gt;&lt;br /&gt;You don't get a warning for the simple reason that it is perfectly legitimate to have a function that doesn't return anything. However, you could generate your own warning if your function is useless in void context:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;sub baz {&lt;br /&gt;  warn "baz() called in void context" if not defined wantarray;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;The &lt;code&gt;wantarray&lt;/code&gt; operator is undefined in void context.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;In Summary and/or Conclusion&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The context in which you are working defines the way Perl's nouns are treated. You should always be aware of what context you are in at any one time, and how to recognise context.&lt;br /&gt;&lt;br /&gt;An assignment is in the context of the left-hand side.&lt;br /&gt;&lt;br /&gt;Tests (&lt;code&gt;while&lt;/code&gt;, &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;?:&lt;/code&gt;) are scalar context, and &lt;code&gt;for&lt;/code&gt; loops are list context.&lt;br /&gt;&lt;br /&gt;Array and hash subscripts are in the context of the sigil you use.&lt;br /&gt;&lt;br /&gt;Operators may enforce context, and some operators also enforce coercion, in order to make the operation possible. The &lt;code&gt;scalar&lt;/code&gt; operator is solely intended to enforce scalar context.&lt;br /&gt;&lt;br /&gt;The argument list to functions is in list context. Overriding this is possible with prototypes but not recommended in the majority of cases.&lt;br /&gt;&lt;br /&gt;With nothing enforcing context, the expression is in void context. Void context will throw a warning if the expression is a constant.&lt;br /&gt;&lt;br /&gt;Functions inherit the calling context for their return statement. The nullary operator &lt;code&gt;wantarray&lt;/code&gt; is used to inspect that context.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-8735474182704639777?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/8735474182704639777/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/11/in-context.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/8735474182704639777'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/8735474182704639777'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/11/in-context.html' title='In Context'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-7715327307288949140</id><published>2011-10-02T11:29:00.002+01:00</published><updated>2011-10-02T14:40:49.966+01:00</updated><title type='text'>What do you people do all day?</title><content type='html'>Change list for last Perl release:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://search.cpan.org/~flora/perl-5.14.2/pod/perl5140delta.pod"&gt;http://search.cpan.org/~flora/perl-5.14.2/pod/perl5140delta.pod&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Change list for last PHP release:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://docs.php.net/manual/en/migration53.new-features.php"&gt;http://docs.php.net/manual/en/migration53.new-features.php&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;What&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-7715327307288949140?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/7715327307288949140/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/10/what-do-you-people-do-all-day.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/7715327307288949140'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/7715327307288949140'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/10/what-do-you-people-do-all-day.html' title='What do you people do all day?'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-2197963004645244670</id><published>2011-10-01T23:08:00.000+01:00</published><updated>2011-10-04T09:44:04.229+01:00</updated><title type='text'>Understanding and Using map and grep</title><content type='html'>&lt;i&gt;Edits: There is no strict reason why map should not return a shorter list, so I'm not discouraging it any more.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;map&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; are useful tools in Perl. To understand them needs only the ability to follow some logic, but to use them requires an understanding of lists. You should read &lt;a href="http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html"&gt;this post on lists&lt;/a&gt; to understand how lists work in Perl. The main thing you should understand from that is how lists exist in memory, and how they are used with relation to arguments to functions.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;map&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; are actually operators, but they behave so much like functions that you'll find them in &lt;a href="http://perldoc.perl.org/perlfunc.html"&gt;perlfunc&lt;/a&gt;. What they do is they take a list of arguments, like functions do,&amp;nbsp;and convert that list into another list. &lt;code&gt;map&lt;/code&gt;&amp;nbsp;&lt;i&gt;transforms&lt;/i&gt;&amp;nbsp;the input list, and &lt;code&gt;grep&lt;/code&gt;&amp;nbsp;&lt;i&gt;filters &lt;/i&gt;the input list.&lt;br /&gt;&lt;br /&gt;Remember that lists with zero or one item in them are still lists.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: x-large;"&gt;Understanding &lt;code&gt;map&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;To understand &lt;code&gt;map&lt;/code&gt; you need to be able to understand the following:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;y = f(x)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If you don't, let's briefly explain it. It means that if you do something (&lt;code&gt;f&lt;/code&gt;) to a value (&lt;code&gt;x&lt;/code&gt;) you will get another value (&lt;code&gt;y&lt;/code&gt;). You've probably seen this before in graphs - the straight line formula is &lt;code&gt;y = mx + c&lt;/code&gt;; we can see that all of &lt;code&gt;mx + c&lt;/code&gt; is a function using&amp;nbsp;&lt;code&gt;x&lt;/code&gt; (&lt;code&gt;m&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are constants), meaning that if you feed an &lt;code&gt;x&lt;/code&gt; coordinate in, you will get a &lt;code&gt;y&lt;/code&gt; coordinate back. We say that &lt;i&gt;&lt;code&gt;y&lt;/code&gt; is a function of &lt;code&gt;x&lt;/code&gt;.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;In the case of map, &lt;code&gt;x&lt;/code&gt; will always be a scalar, because of how lists in Perl are lists of scalars. &lt;code&gt;y&lt;/code&gt; could be more than one value, this being how your output list can be longer than your input list. &lt;code&gt;f&lt;/code&gt; is provided to &lt;code&gt;map&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;map&lt;/code&gt; is therefore a &lt;i&gt;transformation&lt;/i&gt;. Its purpose is to &lt;i&gt;consistently transform&lt;/i&gt; your input list into an output list, by providing each item of your input list to your &lt;code&gt;f&lt;/code&gt;, and collecting the &lt;code&gt;y&lt;/code&gt;s that you get out of it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;map&lt;/code&gt; turns this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;map { f } x, x&lt;sub&gt;1&lt;/sub&gt;, x&lt;sub&gt;2&lt;/sub&gt;, x&lt;sub&gt;3&lt;/sub&gt;, x&lt;sub&gt;4&lt;/sub&gt;...&lt;/pre&gt;&lt;br /&gt;into this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;f(x), f(x&lt;sub&gt;1&lt;/sub&gt;), f(x&lt;sub&gt;2&lt;/sub&gt;), f(x&lt;sub&gt;3&lt;/sub&gt;), f(x&lt;sub&gt;4&lt;/sub&gt;)...&lt;/pre&gt;&lt;br /&gt;A simple example is mathematical: even numbers. The &lt;i&gt;n&lt;/i&gt;th even number is &lt;code&gt;2n&lt;/code&gt;—you learned this in school. If you didn't go to school then you'd better catch up.&lt;br /&gt;&lt;br /&gt;That means, for &lt;code&gt;y&lt;/code&gt; to be the &lt;code&gt;x&lt;/code&gt;th even number, our &lt;code&gt;f&lt;/code&gt; is &lt;code&gt;2x&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;f(x) = 2x&lt;br /&gt;f(1) = 2×1 = 2&lt;br /&gt;f(2) = 2×2 = 4&lt;br /&gt;...&lt;/pre&gt;&lt;br /&gt;That means that, given the numbers 1 to 10 we can find the first 10 even numbers that are greater than 0. If we provide our &lt;code&gt;f&lt;/code&gt; to &lt;code&gt;map&lt;/code&gt;, and the list &lt;code&gt;1 .. 10&lt;/code&gt;, we should get that.&lt;br /&gt;&lt;br /&gt;Defining &lt;code&gt;f(x)&lt;/code&gt; for &lt;code&gt;map&lt;/code&gt; is a simple case of using the special variable &lt;code&gt;$_&lt;/code&gt; in place of &lt;code&gt;x&lt;/code&gt;. For now, we will define our &lt;code&gt;f&lt;/code&gt; using &lt;code&gt;{}&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;print join ', ', map { $_ * 2 } 1 .. 10&lt;br /&gt;2, 4, 6, 8, 10, 12, 14, 16, 18, 20&lt;/pre&gt;&lt;br /&gt;This is a good example of what we were learning about lists in the previous post. The &lt;code&gt;join&lt;/code&gt; operator joins a list of values using the specified string, in this case a comma and a space. There is no requirement in Perl that this list be either an array or a hard-coded, comma-separated list: it can be anything that, in list context, returns a list&lt;sup&gt;1&lt;/sup&gt;.  That could be a function, or another operator like &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;grep&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;This example serves to explain in simple terms how some functionality can be routinely applied to a set of values to return a new set of values. There is no requirement that &lt;code&gt;y&lt;/code&gt; is different from &lt;code&gt;x&lt;/code&gt; after the execution of &lt;code&gt;f(x)&lt;/code&gt;. For example, you might wish to search a dictionary for a set of words, but the words themselves might be suffering from grammar, and hence have different letter cases. The dictionary itself would be entirely in lowercase, and hence you would want a lowercase edition of your set of words.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @lc_words = map { lc } @words&lt;/pre&gt;&lt;br /&gt;In this case, the &lt;code&gt;f()&lt;/code&gt; that we're applying to all members of the array is the operator &lt;code&gt;lc&lt;/code&gt;, which operates on &lt;code&gt;$_&lt;/code&gt; by default and returns a lowercase version of its operand. If the input word happened to be already lowercase, the output will equal the input. No matter.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;List Return Values&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you recall, lists are lists, in Perl. Arrays and hashes are things constructed from lists, and both of these are treated as lists when used where a list is expected. You can see this principle in action in these examples: our first example used the range operator &lt;code&gt;..&lt;/code&gt; to construct the list of integers between 1 and 10, and fed that to &lt;code&gt;map&lt;/code&gt;. The second example used the array &lt;code&gt;@words&lt;/code&gt;, which we assumed to exist for the example, as the input list.&lt;br /&gt;&lt;br /&gt;And then the output list was used in the first example as the argument list to &lt;code&gt;join&lt;/code&gt;, and in the second example it was used as the constructor for a new array. &lt;br /&gt;&lt;br /&gt;So far we have been returning only one value, but the &lt;code&gt;f&lt;/code&gt; we provide to &lt;code&gt;map&lt;/code&gt; can return any number values because it is evaluated in list context. By this mechanism you can turn an array into a hash, which is probably the most common use of that.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @words = qw(cat dog cow dog monkey);&lt;br /&gt;my %words = map { $_ =&amp;gt; 1 } @words;&lt;/pre&gt;&lt;br /&gt;The &lt;code&gt;map&lt;/code&gt; block here returns the input word, &lt;code&gt;$_&lt;/code&gt;, and 1. Remember that in &lt;a href="http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html"&gt;this post&lt;/a&gt; we learned that &lt;code&gt;=&amp;gt;&lt;/code&gt; is semantically equivalent to &lt;code&gt;,&lt;/code&gt; except with the quoting of the word to its left. Remember also that in the same post we learned that a hash is constructed from any even-sized list (or an odd-sized list, with a warning). So this block is returning two items for every one we put in, doubling the length of the list.&lt;br /&gt;&lt;br /&gt;The use of the fat comma in a construct such as this is idiomatic: since it has no actual effect on the output of the &lt;code&gt;map&lt;/code&gt; block it is really only there to hearken to the usual construction of a hash, which is a list that uses &lt;code&gt;=&amp;gt;&lt;/code&gt; a lot. The image conjured is of taking each element of &lt;code&gt;@words&lt;/code&gt; and turning it into &lt;code&gt;word =&amp;gt; 1&lt;/code&gt;, joining it all together until we have an even-sized list thus: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %words = (&lt;br /&gt;    cat =&amp;gt; 1,&lt;br /&gt;    dog =&amp;gt; 1,&lt;br /&gt;    cow =&amp;gt; 1,&lt;br /&gt;    dog =&amp;gt; 1,&lt;br /&gt;    monkey =&amp;gt; 1&lt;br /&gt;);&lt;/pre&gt;&lt;br /&gt;Knowing the intermediate steps to get to that may help:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %words = map { $_ =&amp;gt; 1 } qw(cat dog cow dog monkey);&lt;br /&gt;my %words = map { $_, 1 } qw(cat dog cow dog monkey);&lt;br /&gt;my %words = ('cat', 1, 'dog', 1, 'cow', 1, 'dog', 1, 'monkey', 1);&lt;/pre&gt;&lt;br /&gt;Of course, in a true application, you would not set &lt;code&gt;@words&lt;/code&gt; in this manner in the first place. &lt;code&gt;@words&lt;/code&gt;&amp;nbsp;will be computed, perhaps from a file passed in by name on the command line, which you have processed and turned into this array. &lt;br /&gt;&lt;br /&gt;A little knowledge about hash construction is relevant here to know the actual purpose of this &lt;code&gt;map&lt;/code&gt; block. If you understand that a repeated key in the constructing list is simply overwritten by the latest occurrence of that key, you will see that this has the effect of making the input list unique. Although the output list is exactly double the length of the input list, that list is immediately used to construct a hash. The hash itself then undertakes its usual behaviour, and in this example the repetition of "dog" in the original array is overwritten by its last appearance by the construction of that hash - which is fine in this instance because all the keys are associated with the value &lt;code&gt;1&lt;/code&gt;. Thus any repeated word is not counted.&lt;br /&gt;&lt;br /&gt;You can also skip over part of the list as well. Perl makes no assumptions on the size of your returned list when running &lt;code&gt;map&lt;/code&gt;. You can use this to omit elements from your output list while doubling the rest.&lt;br /&gt;&lt;br /&gt;To do that, simply test &lt;code&gt;$_&lt;/code&gt; and return an empty list if you don't want the element:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;map { test $_ ? ($_ =&amp;gt; 1) : () } @things&lt;/pre&gt;&lt;br /&gt;You might return the empty list if the test returns true, for example if you are trying to transform elements you have not already transformed and remove those you have:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %to_cache = map { in_cache $_ ? () : ($_ =&amp;gt; transform $_) } @stuff;&lt;/pre&gt;&lt;br /&gt;For some test &lt;code&gt;in_cache&lt;/code&gt;, which returns true if its argument is in the cache, and some function &lt;code&gt;transform&lt;/code&gt;, which returns a transformed version of its argument, you can thus construct a hash of transformed stuff ready for caching. Of course, you don't have to double the list; you can use this simply to filter it. But if you're not going to transform &lt;code&gt;$_&lt;/code&gt; then it is more sensible to use &lt;code&gt;grep&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;When Not To Use &lt;code&gt;map&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now that you have a new toy you might be tempted to use it. It is simple nature. Well as with every tool, it is unwise to use it outside its purpose. There are four principles that you should follow when playing with &lt;code&gt;map&lt;/code&gt;:&lt;br /&gt;&lt;br /&gt;&lt;i&gt;The &lt;/i&gt;&lt;i&gt;Principle of Transformation&lt;/i&gt;. You can use &lt;code&gt;map&lt;/code&gt; to test each item and conditionally return the empty list, thus shrinking the size of the list. But unless your &lt;code&gt;map&lt;/code&gt; block transforms &lt;code&gt;$_&lt;/code&gt; in some other way (by returning an altered version, or a longer list), what you have actually got is &lt;code&gt;grep&lt;/code&gt;. In some cases it may be easier to read if you use both &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt;, using the output of &lt;code&gt;grep&lt;/code&gt; as the input to &lt;code&gt;map&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;The Principle of &lt;/i&gt;&lt;i&gt;Immutability.&lt;/i&gt; You should &lt;b&gt;never transform the input list&lt;/b&gt;. That seems contradictory, but by this I mean never alter &lt;code&gt;$_&lt;/code&gt; directly; you should always return an altered copy. It is not, in all cases, possible to change &lt;code&gt;$_&lt;/code&gt;. Only when the input list is an array is it possible to overwrite $_ for &lt;i&gt;all&lt;/i&gt; values you get from the list. When it is a list constructed by some other means, even a hash, the input values are often &lt;i&gt;immutable&lt;/i&gt;, which means you can't change them so don't try. &lt;i&gt;Always&lt;/i&gt; return a copy of &lt;code&gt;$_&lt;/code&gt;, with transformations applied to the copy.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;The Principle of &lt;/i&gt;&lt;i&gt;Brevity&lt;/i&gt;. If your transformation is quite long-winded you should either a) put it in a sub or b) use a &lt;code&gt;for&lt;/code&gt; loop and push onto another array. &lt;b&gt;&lt;code&gt;map&lt;/code&gt; is a construct of brevity&lt;/b&gt;; this should be honoured.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;The Principle of&lt;/i&gt; &lt;i&gt;Containment.&lt;/i&gt; If you need to do anything else while iterating over your list other than transforming &lt;code&gt;x&lt;/code&gt; into &lt;code&gt;y&lt;/code&gt; then you should use a &lt;code&gt;for&lt;/code&gt; loop for that. Your &lt;code&gt;map&lt;/code&gt; block should have &lt;b&gt;no side effects&lt;/b&gt;, which means when the &lt;code&gt;map&lt;/code&gt; has finished executing, everything should be the same as it was when it started, except now there is a new list in memory.&lt;br /&gt;&lt;br /&gt;The general principle is that you are getting back from &lt;code&gt;map&lt;/code&gt; a new list, and leaving the old one alone. Don't run &lt;code&gt;map&lt;/code&gt; without using the result!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: x-large;"&gt;Understanding grep&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;grep&lt;/code&gt; is an operator that has many similarities to &lt;code&gt;map&lt;/code&gt;. It takes an input list, and returns another list. You provide some function &lt;code&gt;f&lt;/code&gt;, akin to that described for &lt;code&gt;map&lt;/code&gt; earlier, and it is run with each successive item from the list.&lt;br /&gt;&lt;br /&gt;The difference is simply that &lt;code&gt;grep&lt;/code&gt; returns a list of &lt;i&gt;equal size or shorter&lt;/i&gt; than the input list.&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;f&lt;/code&gt; that you give to &lt;code&gt;grep&lt;/code&gt; is a filter, not a transformation. It tests each &lt;code&gt;$_&lt;/code&gt; and returns trueness or falseness:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;f(x) ∈ (1,0)&lt;/pre&gt;&lt;br /&gt;We are not looking in this case to return a new value, &lt;code&gt;y&lt;/code&gt;, from our function, but rather any value that is true or not true. Reasonably one could point out that if you are outputting a truth value you are running a transformation. You are: but the result of &lt;code&gt;grep&lt;/code&gt; itself is part of the input list. The result of &lt;code&gt;map&lt;/code&gt; is the output list. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;grep&lt;/code&gt; can be written in terms of &lt;code&gt;map&lt;/code&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;@filtered = grep { test } @things;&lt;br /&gt;@filtered = map { test ? $_ : () } @things;&lt;/pre&gt;&lt;br /&gt;A common adage is "spelt grep, pronounced filter". That's a mnemonic, of course; it's pronounced grep.&lt;br /&gt;&lt;br /&gt;A traditional use of &lt;code&gt;grep&lt;/code&gt; is to find the defined items in an array:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;grep { defined } @array&lt;/pre&gt;&lt;br /&gt;It is not uncommon to have produced an array or list with undefined elements, especially if that list came from a &lt;code&gt;map&lt;/code&gt; block that could return undef in some cases. Another common idiom is to &lt;code&gt;map&lt;/code&gt; &lt;i&gt;and&lt;/i&gt; &lt;code&gt;grep&lt;/code&gt; at the same time:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @phone_numbers = map { $_-&amp;gt;{phone} } grep { $_-&amp;gt;{phone} } @people;&lt;/pre&gt;&lt;br /&gt;Here &lt;code&gt;@people&lt;/code&gt; is assumed to be an array of hashrefs representing people. The &lt;code&gt;grep&lt;/code&gt; filters out those for whom the key 'phone' returns a true value, and the &lt;code&gt;map&lt;/code&gt; then returns the actual value.&lt;br /&gt;&lt;br /&gt;We can thus draw this distinction between the two: &lt;b&gt;&lt;code&gt;map&lt;/code&gt; collects &lt;i&gt;output&lt;/i&gt; values, and &lt;code&gt;grep&lt;/code&gt; collects &lt;i&gt;input&lt;/i&gt; values&lt;/b&gt;. You can see this is the case: the &lt;code&gt;map&lt;/code&gt; block is assuming that the input list is a list of hashrefs, and extracts strings from them. We collect the list of things that &lt;code&gt;map&lt;/code&gt; outputs. For that to work, it means that the &lt;i&gt;output&lt;/i&gt; of &lt;code&gt;grep&lt;/code&gt; has to be hashrefs; and since the &lt;i&gt;input&lt;/i&gt; to &lt;code&gt;grep&lt;/code&gt; is hashrefs but the &lt;code&gt;grep&lt;/code&gt; block only returns a value from that hashref, we see that the output of &lt;code&gt;grep&lt;/code&gt; is just a subset of the input.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;When Not To Use &lt;code&gt;grep&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;grep&lt;/code&gt; doesn't have the principle of transformation because you're not supposed to transform the list.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;The Principle of Immutability&lt;/i&gt; - &lt;code&gt;grep&lt;/code&gt; should &lt;b&gt;not alter the input values&lt;/b&gt;; it should merely test them.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;The Principle of Containment&lt;/i&gt; - nothing in the &lt;code&gt;grep&lt;/code&gt; block should affect anything outside of the expression. &lt;b&gt;Everything should be the same after the &lt;code&gt;grep&lt;/code&gt; has finished running as it was beforehand&lt;/b&gt;, except now there is a new list in memory of (copies of) some or all of the input list.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;The Principle of Brevity&lt;/i&gt; - if you need to do a long-winded process to find out whether or not you want to keep a particular &lt;code&gt;$_&lt;/code&gt;, &lt;b&gt;take it somewhere else&lt;/b&gt;, because it doesn't belong here. Make a sub and put it in that.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Similar Things&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There is a simple concept here: You take a list, you apply a function to each element, and you get another list. &lt;code&gt;map&lt;/code&gt; is the simplest example of this because the list you create is exactly the list that &lt;code&gt;map&lt;/code&gt; returns. &lt;code&gt;grep&lt;/code&gt; is arguably more complex because the list you create with this process (a list of true and false values) is itself used to alter the input list, so you get back a different list from the one you build.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;code&gt;&lt;b&gt;sort&lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;sort&lt;/code&gt; is another list operator. Like &lt;code&gt;grep&lt;/code&gt;, the list it returns comprises elements of the input list. Unlike &lt;code&gt;grep&lt;/code&gt;, the list it returns is the same length as the input list. Clearly, it sorts the input list, and returns a new one with the items in order.&lt;br /&gt;&lt;br /&gt;An important thing to have already realised by now is that if you are sorting you have to do two variables at once, instead of one. Perl sorts, haha, this out for you by providing you &lt;code&gt;$a&lt;/code&gt; and &lt;code&gt;$b&lt;/code&gt; instead of &lt;code&gt;$_&lt;/code&gt;. Your task is to tell Perl whether &lt;code&gt;$a&lt;/code&gt; is greater than, less than, or equal to &lt;code&gt;$b&lt;/code&gt;, by returning -1, 0 or 1 respectively from the block.&lt;br /&gt;&lt;br /&gt;The operators &lt;code&gt;&amp;lt;=&amp;gt;&lt;/code&gt; for numbers and &lt;code&gt;cmp&lt;/code&gt; for strings do this easily for you, but this generalisation of the sorting process allows for you to run &lt;code&gt;$a&lt;/code&gt; and &lt;code&gt;$b&lt;/code&gt; through any algorithm in order to sort the list.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @sorted = sort { length $a &amp;lt;=&amp;gt; length $b } @strings;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;code&gt;&lt;b&gt;for&lt;/b&gt;&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;for&lt;/code&gt; loop is much more than it is in other languages, especially when used as a statement modifier. That's when you put it at the end instead of putting it first and using braces. &lt;code&gt;for&lt;/code&gt; is the operator you should use when you want to modify the array itself, rather than create a modified copy. Note you can only fully modify arrays and only the values of hashes.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;$_++ for @array; # increment every value&lt;br /&gt;$_++ for values %hash; # same&lt;br /&gt;$_ .= "-old" for @filename_backups; # append "-old" to each filename&lt;/pre&gt;&lt;br /&gt;&lt;code&gt;map&lt;/code&gt; is equivalent to doing a &lt;code&gt;for&lt;/code&gt; loop over the old array, and pushing things onto a new array:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @output;&lt;br /&gt;push @output, $_ * 2 for @input;&lt;/pre&gt;&lt;br /&gt;Presumably &lt;code&gt;map&lt;/code&gt; is more efficient, but part of Perl is that you have the option of being more expressive with your code, which is to say that you should write what you mean. If you mean to perform a map operation, use &lt;code&gt;map;&lt;/code&gt; otherwise when someone else reads your code (i.e. you, next month) there will be no head-scratching wondering why you used a for loop.&lt;br /&gt;&lt;br /&gt;Other functions in &lt;i&gt;List::Util&lt;/i&gt;, &lt;i&gt;List::MoreUtils&lt;/i&gt; and &lt;i&gt;List::UtilsBy&lt;/i&gt; serve to run a function across all items of a list in the same general way, and return either a list or a single value.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;List::Util&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;first&lt;/code&gt; - Find the first item in a list; usually used to find any item that matches the criterion. Often therefore used to find &lt;i&gt;whether&lt;/i&gt;&amp;nbsp;any item matches the criterion.&lt;br /&gt;&lt;code&gt;my $caps_key = first { uc $_ eq $_ } keys %hash;&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;reduce&lt;/code&gt; - Collapse a list into one value, by repeatedly applying the function. This is called reduction, as in "map-reduce". Note the use of $a and $b, like sort: we are trying to reduce a list into one value so we have to do two values at once, rather than one.&lt;br /&gt;&lt;code&gt;my $sum = reduce { $a + $b } @numbers;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;List::MoreUtils&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This module has many more than &lt;i&gt;List::Util&lt;/i&gt; so I won't list them all. &lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;any&lt;/code&gt;, &lt;code&gt;all&lt;/code&gt;, &lt;code&gt;notall&lt;/code&gt;, &lt;code&gt;none&lt;/code&gt; - Test the whole list. &lt;code&gt;any&lt;/code&gt; is similar to &lt;code&gt;first&lt;/code&gt; from &lt;i&gt;List::Util&lt;/i&gt;; the difference being that it will return a true value if your search succeeds, whereas &lt;code&gt;first&lt;/code&gt; could return a false value if you're looking for false values. These are essentially &lt;code&gt;grep&lt;/code&gt;, except they will stop looking if they find the answer - &lt;code&gt;grep&lt;/code&gt; will process the whole list in all cases.&lt;br /&gt;&lt;code&gt;if (any { defined } @things) {}&amp;nbsp; # if any is defined&lt;/code&gt;&lt;br /&gt;Cif (all { defined } @things) {} # if all are defined&amp;gt;&lt;br /&gt;&lt;code&gt;if (notall { defined } @things) {} # if not all are defined&lt;/code&gt;&lt;br /&gt;&lt;code&gt;if (none { defined } @things) {} # if no thing is defined&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;pairwise&lt;/code&gt; - This only works on arrays, but it takes one value from the first array and one value from the second, and gives them both to your function. Then it collects the outputs. It's like &lt;code&gt;map&lt;/code&gt;, except it does two things at a time.&lt;br /&gt;&lt;code&gt;my %hash = pairwise { $a =&amp;gt; $b } @keys, @values;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;my @totals = pairwise { $a * $b } @prices, @quantities&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-size: large;"&gt;List::UtilsBy&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is a convenience module for all those cases where normally you'd do the same thing to both &lt;code&gt;$a&lt;/code&gt; and &lt;code&gt;$b&lt;/code&gt;. I will stick to a couple of examples.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;sort_by&lt;/code&gt; - Applies the procedure to both &lt;code&gt;$a&lt;/code&gt; and &lt;code&gt;$b&lt;/code&gt; and compares the results as strings (&lt;code&gt;nsort_by&lt;/code&gt; for numbers). Saves typing.&lt;br /&gt;&lt;code&gt;my @sorted = nsort_by { length } @strings;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;my @sorted = nsort_by { $_-&amp;gt;mother-&amp;gt;mother-&amp;gt;mother-&amp;gt;age } @people;&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;uniq_by&lt;/code&gt; - Makes the list unique based on the function.&lt;br /&gt;&lt;code&gt;my @unique_by_colour = uniq_by { $_-&amp;gt;colour } @fruit;&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-size: x-large;"&gt;In Conclusion&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I hope this has given you, the newcomer to Perl, a good idea of what &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; do. I also hope it has given you an insight into the general concept of applying a function to a sequence of values, and doing something with the result. It is an operation that is much more common than you'd think, even in real life.&lt;br /&gt;&lt;br /&gt;It is the basis of mail merge, for example, where you take a template and a list of names and you put each name in the template and, with the resulting list of letters, print and send them. That's &lt;code&gt;map&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;It is the basis of searching, where you have a quantity of items and you're looking for all of those that match a certain criterion. You apply your criterion to each and keep those that match. That's &lt;code&gt;grep&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;It is the basis of sorting things by height, or alphabetically, or by the number of times they've won the World Cup: you take the list of things, and two at a time you find out how many times they've won the World Cup and you sort based on that. That's &lt;code&gt;sort&lt;/code&gt; (or &lt;code&gt;sort_by&lt;/code&gt; or &lt;code&gt;nsort_by&lt;/code&gt;).&lt;br /&gt;&lt;br /&gt;Examining or modifying a list is an awfully common operation. One thing I cannot teach you, however, is how to recognise when you should use one. Feel free to ask!  &lt;br /&gt;&lt;hr /&gt;&lt;span style="font-size: xx-small;"&gt;&lt;sup&gt;1&lt;/sup&gt; All right smart arse. Everything in list context is a list, even if it is only one scalar, because that's just a list with one item in it. The point is that this statement is true regardless of the length of the returned list.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-2197963004645244670?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/2197963004645244670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/10/understanding-and-using-map-and-grep.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/2197963004645244670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/2197963004645244670'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/10/understanding-and-using-map-and-grep.html' title='Understanding and Using map and grep'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-817379503910561492</id><published>2011-09-06T15:39:00.001+01:00</published><updated>2011-09-14T22:20:41.449+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='newbies'/><category scheme='http://www.blogger.com/atom/ns#' term='skeptics'/><category scheme='http://www.blogger.com/atom/ns#' term='musings'/><title type='text'>Einstein's Constraint: Booleans</title><content type='html'>&lt;blockquote&gt;Everything should be kept as simple as possible, but no simpler.&lt;br /&gt;&lt;span class="citation" style="text-align: right"&gt;Albert Einstein&lt;/span&gt;&lt;/blockquote&gt;Perl is a language that combines ideas from many other languages. It is a language designed by a linguist, and hence it uses principles from natural language. The design of Perl is therefore a combination of two things: convention set out by its muses and Larry Wall's desires.&lt;br /&gt;&lt;br /&gt;This meant that Perl was free to pick and choose from different conventions or invent new techniques that solved problems inherent in others'. Application of Einstein's Constraint is clear in Perl 5 (as well as a few instances of failure to apply it!), and today we will look at booleans.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;True and False&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The most obvious starting point when deciding how to implement booleans is to decide what will be true and what will be false. Probably the most common falseness and trueness are zero and not-zero, respectively, a convention popularised—if not invented—by C. In some dialects of Lisp, an empty list is false, and in many languages, True and False are their own types. &lt;br /&gt;&lt;br /&gt;JavaScript, PHP, Python: many languages have explicit types for &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;—global, singleton values that always represent trueness and falseness. This allows truth to be explicitly defined. But many languages these days, including PHP, JavaScript, Python and Perl, all employ a concept called coercion to switch between different types &lt;i&gt;implicitly&lt;/i&gt;, i.e. swapping without you having to ask for it. When this is available, we also have to consider what &lt;i&gt;other&lt;/i&gt; values have truth or falsehood.&lt;br /&gt;&lt;br /&gt;Is it simpler to a) have True and False as separate values and coerce into them, or b) use existing values, and a rule?&lt;br /&gt;&lt;br /&gt;Let's ask Einstein.&lt;br /&gt;&lt;br /&gt;To answer, we have to consider usage. Perl draws a lot on the do-what-I-mean, or DWIM, philosophy of programming, a philosophy which naturally leads to Perl automatically and transparently switching between data types where possible (and in fact it is always possible, thanks to various operators). Strings and numbers are interchangeable; objects can be converted to strings; arrays to scalars or lists. Anything can be used anywhere and a rule is applied, consistently, so that the programmer knows what to expect perl&lt;sup&gt;1&lt;/sup&gt; to do.&lt;br /&gt;&lt;br /&gt;Observing this principle it seems like we're already tending towards b. Perl is, after all, already designed with rules in mind: a consistent (and concise) set of ground rules is the easiest way to understand what to expect in a given situation. &lt;br /&gt;&lt;br /&gt;But if we follow the thought further we realise that once there is type coercion, the difference is moot. If we introduce a separate value for &lt;code&gt;true&lt;/code&gt; and a separate one for &lt;code&gt;false&lt;/code&gt;, this means we have to create a whole new set of rules for how to coerce &lt;i&gt;other&lt;/i&gt; values into these two whenever there is a boolean test. If we already have to make that decision, it then follows that it doesn't matter what we coerce &lt;i&gt;into&lt;/i&gt;: what matters is what values are &lt;i&gt;false&lt;/i&gt; and what values are &lt;i&gt;true&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;Einstein's Constraint, then, says that since we have to implement b) anyway, and know the rules, it is tautologous to implement a) as well. Indeed, there may or may not be boolean data types internally to the Perl interpreter, but this makes no difference to Perl as a language. So, we can decide that we will simply choose &lt;i&gt;values&lt;/i&gt;, rather than &lt;i&gt;types&lt;/i&gt;, to be true or false. This is sensible, because Perl doesn't have types in that respect. True, false, 0 and 1 are all scalar values.&lt;br /&gt;&lt;br /&gt;Deciding which values should be true and which should be false is the next logical step. Convention from C tells us that zero should be false; but Perl has list types, and Common Lisp suggests an empty list should be false. This nicely follows existing rules: since boolean values are scalar, and an array in scalar context is its length, then a zero-length array is naturally false because it is treated as zero. A list—however it is constructed—in scalar context returns its last item; an empty list will return no item. Nothingness, then, is falsehood.&lt;br /&gt;&lt;br /&gt;Type coercion says that the string &lt;code&gt;'0'&lt;/code&gt; is equal to &lt;code&gt;0&lt;/code&gt;, and hence false. We might consider that the strings &lt;code&gt;'00'&lt;/code&gt; and &lt;code&gt;'0E0'&lt;/code&gt;(0×10&lt;sup&gt;0&lt;/sup&gt;) also numify as &lt;code&gt;0&lt;/code&gt;. But note that &lt;code&gt;0&lt;/code&gt; will not &lt;i&gt;stringify&lt;/i&gt; as either of these. Only the string &lt;code&gt;'0'&lt;/code&gt; is fully equivalent to &lt;code&gt;0&lt;/code&gt;, because the conversion between these two will never produce a different value.&lt;br /&gt;&lt;br /&gt;Furthermore, we are coercing to booleans, not integers: that is to say, we want to know whether it is true or not, and we are not using any specific type to represent truthiness. You may spot inconsistency. In this respect, you might say that Perl &lt;i&gt;does&lt;/i&gt;&amp;nbsp;have a boolean type, and you'd be right in a sense. However, there is no &lt;i&gt;true&amp;nbsp;&lt;/i&gt;and &lt;i&gt;false&lt;/i&gt;; there is merely a state of trueness that a value can have. The value only has a value of truth when it is used as a truth value; in other languages, &lt;i&gt;true&lt;/i&gt;&amp;nbsp;and &lt;i&gt;false&lt;/i&gt; always represent truth values. Truth is contextual. How recondite.&lt;br /&gt;&lt;br /&gt;Perl therefore defines &lt;code&gt;'0'&lt;/code&gt; to be false for consistency with &lt;code&gt;0&lt;/code&gt;, but any other string to be true, including &lt;code&gt;'00'&lt;/code&gt; and the common zero-but-true value &lt;code&gt;'0E0'&lt;/code&gt;. The exception is the empty string &lt;code&gt;''&lt;/code&gt;, which falls into the "nothingness" category, since there's nothing in it, and is also false.&lt;br /&gt;&lt;br /&gt;Finally, the undefined value is conceptually equivalent to an empty list: it is a scalar with no value. That, too, is a false value.&lt;br /&gt;&lt;br /&gt;Removing the tautology has made this as simple as possible. There is no remaining tautology here.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Comparison Operators&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Einstein's Constraint removed the need for extra values from Perl to represent truth and falsehood. The same mantra in fact extends Perl's collection of operators for comparison.&lt;br /&gt;&lt;br /&gt;To explain why, we should take a look at languages that don't. JavaScript and PHP both use &lt;code&gt;==&lt;/code&gt; to compare all things: strings, integers and objects. They both also have &lt;code&gt;===&lt;/code&gt; to compare &lt;i&gt;without coercion&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;As mentioned, coercion is the practice of treating a variable as another type by silently converting from its current type to another. Using &lt;code&gt;==&lt;/code&gt; to compare two types in JavaScript or PHP will coerce both, one or neither operand to a different type, based on rules documented somewhere.&lt;br /&gt;&lt;br /&gt;Using &lt;code&gt;==&lt;/code&gt; to compare two types in Perl will coerce &lt;i&gt;both&lt;/i&gt; operands to numbers, and compare the results. Using &lt;code&gt;eq&lt;/code&gt; will coerce both operands to strings, and compare the results.&lt;br /&gt;&lt;br /&gt;Why?&lt;br /&gt;&lt;br /&gt;This becomes easy to explain when we consider the types of comparison available to us. The two obvious ones are numerical and string comparison. Two numbers are equal if they represent the same platonic value—020, 16 and 0x10 are all equal. Two strings are equal when they contain the same characters in the same order.&lt;br /&gt;&lt;br /&gt;Then you might suggest that two arrays are equal if they are the same length, which works for Perl. Or that they are equivalent: the same keys and the same values, which works for PHP. Or that they are the same actual array, which works for PHP and JavaScript.&lt;br /&gt;&lt;br /&gt;What about two objects? Perl's objects are necessarily references, so referential equality seems reasonable—but Perl also has operator overload, so the decision could be given to the objects themselves. PHP has true objects, so you might suggest that an operator overload would be good, but PHP doesn't think you can be trusted with that so it compares them by comparing their attributes instead. JavaScript also uses referential equality, but doesn't allow for operator overloading either.&lt;br /&gt;&lt;br /&gt;For PHP and JavaScript, &lt;code&gt;==&lt;/code&gt; is actually an &lt;i&gt;equivalence&lt;/i&gt; operator, and hence numerous rules are needed to determine what is equivalent to what. &lt;code&gt;===&lt;/code&gt; is also an equivalence operator: it is still not an equality operator. It just happens that the equivalence has fewer rules, and in many cases equality is the only satisfactory state.&lt;br /&gt;&lt;br /&gt;Also, we've been focusing on equality. What about the other operators, &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt;? Strings can be compared lexically: there are rules for what is "less than" and what is "greater than". Objects, well, who knows? &lt;a href="http://uk.php.net/manual/en/language.oop5.object-comparison.php"&gt;PHP's manual&lt;/a&gt; commits the fatal flaw of calling &lt;code&gt;==&lt;/code&gt; the "comparison operator", whereas it is in fact &lt;i&gt;a&lt;/i&gt; comparison operator called the &lt;i&gt;equality&lt;/i&gt; (or equivalence!) operator—a mistake which allows the manual to conveniently omit the rules for the other ones. JavaScript takes a better approach and simply decides that objects are not comparable and returns false when you try a magnitude test.&lt;br /&gt;&lt;br /&gt;But what do you do when the strings &lt;i&gt;could be&lt;/i&gt; integers? Do you compare lexically, or numerically? Neither is incorrect. You would be upset if you were trying to sort a list by lexical analysis only to find that your language was assuming they were numbers and treating them numerically, and likewise the reverse. Is the string "10" less than or greater than the string "011"? If we weren't type-coercing we would know instantly: it's a string, so it's greater. But we are, so we don't.&lt;br /&gt;&lt;br /&gt;Here is a generalised table over all of &lt;code&gt;==&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;gt;=&lt;/code&gt; operators, showing you what coercion you can expect from the languages we've mentioned, on various operands. The result column is the result of the operator &lt;code&gt;&amp;lt;&lt;/code&gt;, for reference. I chose that one because it performs the most erratically.&lt;br /&gt;&lt;br /&gt;&lt;table border="1" cellpadding="4" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;th colspan="2"&gt;Operands&lt;/th&gt; &lt;th colspan="3"&gt;Treated as&lt;/th&gt; &lt;th colspan="3"&gt;Result&lt;/th&gt; &lt;/tr&gt;&lt;tr&gt; &lt;th&gt;L&lt;/th&gt; &lt;th&gt;R&lt;/th&gt; &lt;th&gt;PHP&lt;/th&gt; &lt;th&gt;JS&lt;/th&gt; &lt;th&gt;Perl&lt;/th&gt; &lt;th&gt;PHP&lt;/th&gt; &lt;th&gt;JS&lt;/th&gt; &lt;th&gt;Perl&lt;/th&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;"0"&lt;/td&gt; &lt;td&gt;"1"&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;"a"&lt;/td&gt; &lt;td&gt;"b"&lt;/td&gt; &lt;td&gt;Strings&lt;/td&gt; &lt;td&gt;Strings&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;0*&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;"10"&lt;/td&gt; &lt;td&gt;11&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;"10"&lt;/td&gt; &lt;td&gt;"011"&lt;/td&gt; &lt;td&gt;???&lt;/td&gt; &lt;td&gt;Strings&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;"10b"&lt;/td&gt; &lt;td&gt;"11a"&lt;/td&gt; &lt;td&gt;Strings&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;[1, 2, 3]&lt;/td&gt; &lt;td&gt;[1, 2, 3, 4]&lt;/td&gt; &lt;td&gt;???&lt;/td&gt; &lt;td&gt;Objects**&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;[1, 2, 3]&lt;/td&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;Array is always greater&lt;/td&gt; &lt;td&gt;Objects**&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;0&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr&gt; &lt;td&gt;false&lt;/td&gt; &lt;td&gt;true&lt;/td&gt; &lt;td&gt;Booleans&lt;/td&gt; &lt;td&gt;Numbers&lt;/td&gt; &lt;td&gt;Numbers&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;-&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;* Non-numeric strings numify as zero, and a warning is cast that you numified a non-numeric string. ** &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt; are defined always to return false; otherwise, true is returned if they refer to the same thing.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is Einstein's Constraint again. Perl has made it as simple as possible, but no simpler. In PHP's case it is not defined generally over the five operators how the arguments will be treated. In JavaScript's case, each pair of operands is consistent across the five operators, but the language is inconsistent as the operands change. There are rules, but why should you have to remember them? In Perl's case &lt;i&gt;they are always treated as numbers&lt;/i&gt;. It cannot be simpler without being more complex elsewhere.&lt;br /&gt;&lt;br /&gt;Perl sidesteps the whole issue simply by stating that if you use any of &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, &lt;code&gt;==&lt;/code&gt;, &lt;code&gt;&amp;gt;=&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt; or the special Perl-only &lt;code&gt;&amp;lt;=&amp;gt;&lt;/code&gt;&lt;sup&gt;3&lt;/sup&gt; then they are treated as numbers; and if you use any of &lt;code&gt;lt&lt;/code&gt;, &lt;code&gt;lte&lt;/code&gt;, &lt;code&gt;eq&lt;/code&gt;, &lt;code&gt;gte&lt;/code&gt;, &lt;code&gt;gt&lt;/code&gt; or &lt;code&gt;cmp&lt;/code&gt;, then they are treated as strings. The mnemonic is simple: the mathematical operators are used on numbers, and the letters operators are used on letters.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Triple Equals&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;A hue and a cry! What audacity to not mention that PHP and JavaScript have the triple-equals operator, &lt;code&gt;===&lt;/code&gt; that enforces type checking as well. With this magical operator, we solve the problem a different way. We can, in all cases, avoid the problem of type coercion by simply demanding that it not take place.&lt;br /&gt;&lt;br /&gt;All cases? No. Since both languages have false as well as an undefined (null) value and zero, how do you test a string, read from standard input, for falsity? Or how do you compare a variable that exists, but is not defined or is false, and differentiate it from zero? And how many more rules and exceptions are there to this new operator, that can compare types as well? Are we forgetting the principle that we should be able to implicitly treat any type than any other type? Didn't we learn a lesson from the true/false thought experiment?&lt;br /&gt;&lt;br /&gt;Perl's use of two types of operators for two types of comparison remains simpler, and the main reason is that all things are &lt;i&gt;supposed&lt;/i&gt; to be coerced into all other things. That is a sound principle in Perl, but without these extra operators, other languages find a barrier preventing them from seamlessly implementing the philosophy.&lt;br /&gt;&lt;br /&gt;That aside, there is not a triple-equals version of &lt;code&gt;&amp;lt;=&lt;/code&gt; or &lt;code&gt;&amp;gt;=&lt;/code&gt; is there? Those are the troublemakers, after all. Those are the ones that force us to sort our number-like strings the way they want to, not the way we want to. How do we prevent this behaviour on these other operators? Oh sod it all, let's just have separate comparisons for strings and numbers.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: xx-small;"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: xx-small;"&gt; By convention, Perl is the language and perl is the interpreter.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: xx-small;"&gt;&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;The Constraint explained earlier shows why we got rid of the boolean type for Perl. While this row is correct for PHP's and JavaScript's two boolean values, all three languages will come a cropper if you try to compare a trueish value with a falsish one. Perl, again, simplifies it by not doing this, and therefore we can't say what Perl will treat true and false as because they don't exist. But it would be numbers.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: xx-small;"&gt;&lt;sup&gt;3&lt;/sup&gt; The spaceship operator returns -1 if A is less than B; zero if they are equal; and 1 if A is greater than B. The same test is three lines of code, or two chained ternary conditional operators, in other languages: &lt;code&gt;sort { $a &amp;lt;=&amp;gt; $b } ...&lt;/code&gt; is better than &lt;code&gt;sort { $a == $b ? 0 : $a &amp;lt; $b ? -1 : 1 } ...&lt;/code&gt; because a) it is legible. &lt;code&gt;cmp&lt;/code&gt; does the same, but for strings.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-817379503910561492?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/817379503910561492/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/09/einsteins-constraint-booleans.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/817379503910561492'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/817379503910561492'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/09/einsteins-constraint-booleans.html' title='Einstein&apos;s Constraint: Booleans'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-1550756727456772766</id><published>2011-08-10T08:52:00.000+01:00</published><updated>2011-09-14T22:21:16.096+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='musings'/><title type='text'>Your System is not Gödel-Proof</title><content type='html'>Gödel tells us, paraphrased, that no system can be fully expressed in terms of itself. For example, the dictionary, which attempts to express the meaning of all English words, nevertheless uses English to do so. You have to know enough words to learn what the other words mean, and you can build up from there.&lt;br /&gt;&lt;br /&gt;In other words, every system needs its axiomata. An axiom is essentially a fact about a system that is assumed known.&lt;br /&gt;&lt;br /&gt;This is analogous to the design of a system. It seems somehow more elegant to design a system that works based on things that are already working than to write a new procedure to make something work: these are your axiomata. Overengineering comes in when you relentlessly try to base your system on axiomata instead of simply creating a new entry in your dictionary. When you find yourself trying to find the "most elegant" solution to your problem you might actually be trying to find the "least work" solution.&lt;br /&gt;&lt;br /&gt;Overengineering, if you think about it, has the ultimate goal of having the entire thing &lt;i&gt;just work&lt;/i&gt;&amp;nbsp;if you prod at a particular pressure point in your towering mass of pre-existing code.&lt;br /&gt;&lt;br /&gt;Well stop it. You can't make entire system without writing a bit of code. Heck you don't even have a system if it's just a collection of axiomata.&amp;nbsp;You will have to write at least a bit of glue code. And don't try too hard to leave your system as a collection of axiomata for new systems. Make it work, first.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-1550756727456772766?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/1550756727456772766/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/08/your-system-is-not-godel-proof.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/1550756727456772766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/1550756727456772766'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/08/your-system-is-not-godel-proof.html' title='Your System is not Gödel-Proof'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-3026870014641832812</id><published>2011-08-02T14:27:00.002+01:00</published><updated>2011-09-14T22:21:37.126+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='newbies'/><title type='text'>Lists, and Things Made Of Lists</title><content type='html'>In the post &lt;a href="http://altreus.blogspot.com/2011/06/anatomy-of-types.html"&gt;The Anatomy of Types&lt;/a&gt;, we talked about how some of Perl's data types are &lt;i&gt;aggregate&lt;/i&gt; types, while others are not. We differentiated them as whether the type holds one scalar, or any number of scalars. The scalar data type is not aggregate—it holds but one thing—and arrays and hashes are aggregate.&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;This post is intended to explain how lists are used in the context of these data types.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Lists&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Perl's aggregate data types are the array and the hash. Each is constructed from a list. The actual definition of a list covers quite a lot of cases—a lot of ways in which these can be constructed. However, the basic concept of "a list" is pretty simple; it's an ordered sequence of (zero or more) scalars.&lt;br /&gt;&lt;br /&gt;When you assign a value to a scalar you usually either populate it with input data or assign it a literal value:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $input = &amp;lt;&amp;gt;;&lt;br /&gt;my $limit = 100;&lt;br /&gt;my $user = 'user';&lt;/pre&gt;&lt;br /&gt;When you assign a value to an aggregate data type you populate it with a list:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @lines = &amp;lt;&amp;gt;;&lt;br /&gt;my @days  = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');&lt;br /&gt;my %colour = (&lt;br /&gt;  red =&amp;gt; '#ff0000',&lt;br /&gt;  green =&amp;gt; '#00ff00',&lt;br /&gt;  blue =&amp;gt; '#0000ff',&lt;br /&gt;);&lt;/pre&gt;&lt;br /&gt;A list is a sequence of scalars. The most basic way of constructing a list is with the comma operator.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;The Comma Operator&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;Little did you, the new Perl developer, know, but the humble comma is also an operator like all others. It has low precedence, and its job is to concatenate two lists together. Things are lists when they are in list context.&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;A common misconception is that parentheses form list context. After all, every time you see a list, you see parentheses! Not strictly true. The parentheses are simply there to make sure the comma operator happens first; it is the context of the whole expression that determines context. Stay with me and I'll try and make it clearer.&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;To create a list we use the comma operator.&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;1, 2, 3, 4, 5, 6&lt;/pre&gt;&lt;br /&gt;This fragment of code makes no sense on its own and thus needs some context to make sense. However, it is an expression—it's called that because it returns a value.&lt;br /&gt;&lt;br /&gt;Where we use the expression determines the value it returns.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @array = (1, 2, 3, 4, 5, 6);&lt;/pre&gt;&lt;br /&gt;This is the example we're familiar with. The context of the expression is determined by the assignment operator. When we assign to an array, the expression on the right-hand-side of the assignment operator is in list context; thus the comma operator in our expression is in list context, and hence creates a list.&lt;br /&gt;&lt;br /&gt;Why the parentheses? This looks perfectly innocuous and, indeed, perfectly legible to any newcomer to Perl:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @array = 1, 2, 3, 4, 5, 6;&lt;/pre&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;But that's because the newcomer who reads it is not as educated as you are about to become, and is not aware that the assignment operator &lt;code&gt;=&lt;/code&gt; has &lt;i&gt;higher precedence&lt;/i&gt; than the comma operator. That means it's evaluated first. That means you get this&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;(my @array = 1), 2, 3, 4, 5, 6;&lt;/pre&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;Because you are an honourable and competent Perl developer you have enabled warnings. Thanks to this, you are warned not once but five times that you have "Useless use of a constant in void context".&lt;/div&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;In the latter example, no comma operator is evaluated in list context, because the assignment operator is evaluated first. It consumes the array (or hash) and the 1, and is then done. The remaining comma operators are then evaluated in &lt;i&gt;void context&lt;/i&gt;, which is the context anything is evaluated in when there is no operator or other syntax imposing a different context. Just saying &lt;code&gt;2&lt;/code&gt; is useless in void context&lt;sup&gt;1&lt;/sup&gt;, so Perl tells you you have done it.&lt;/div&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;In other list contexts, there are already parentheses:&lt;/div&gt;&lt;br /&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;for my $i (1, 2, 3, 4, 5, 6) { ... }&lt;/pre&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;And in some, there is no operator with higher precedence, so we don't need parentheses:&lt;/div&gt;&lt;br /&gt;&lt;pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;push @array, 1, 2, 3, 4, 5, 6;&lt;/pre&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;In scalar context (remember: this is determined by what you're assigning to), the comma operator will return its right-hand operand. That is to say, if you try to build a list with commas and assign it to a scalar, you will get the last item.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $scalar = (1, 2, 3, 4, 5, 6); # $scalar = 6&lt;/pre&gt;&lt;br /&gt;And of course if you forget the parentheses, the assignment happens first, and you get warnings.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $scalar = 1, 2, 3, 4, 5, 6; # $scalar = 1&lt;/pre&gt;&lt;br /&gt;Generally there is no reason to do this.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;Hashes&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;We didn't mention hashes above, to keep it simple. Hashes are also aggregate data types and are also constructed from lists. However, the most common way of seeing a hash constructed in code is like this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %colour = (&lt;br /&gt;  red =&amp;gt; '#ff0000',&lt;br /&gt;  green =&amp;gt; '#00ff00',&lt;br /&gt;  blue =&amp;gt; '#0000ff',&lt;br /&gt;  ...  # etc&lt;br /&gt;);&lt;/pre&gt;&lt;br /&gt;What is this? In some languages you will find that there is a specific syntax required to create a hash (or associative array—but that's a different rant) but in Perl the syntax is merely &lt;i&gt;convenience&lt;/i&gt;. There is nothing particularly special about the syntax above; you can construct a hash from any list (but of course you will be warned if you use an odd number of elements, since hashes are paired).&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %colour = (&lt;br /&gt;  'red',  '#ff0000',&lt;br /&gt;  'green', '#00ff00',&lt;br /&gt;  'blue', '#0000ff',&lt;br /&gt;  ...  # etc&lt;br /&gt;);&lt;/pre&gt;&lt;br /&gt;This operator &lt;code&gt;=&amp;gt;&lt;/code&gt; is known as the &lt;i&gt;fat comma&lt;/i&gt;, because it has the same effect and precedence as the comma, but it is 2 characters and, hence, fat. Other than that, you'll notice the other difference is that in the first example I didn't have to quote the string keys. The syntactic benefit of the fat comma is that it quotes the bareword to its left for you, which covers the majority of cases, and means you only have to quote keys that don't look like identifiers.&lt;br /&gt;&lt;br /&gt;But, ultimately, you have still created a list. You still have to use parentheses, and as we will learn further down, you can construct the hash by using anything that returns a list.&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;"Construct"?&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;Yes. We use this term when we give a variable a value. We might say we use this term when we create a new variable, but of course we can reconstruct an existing variable at any time.&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;When we declare a new array or hash but don't perform an assignment at the same time, we are implicitly constructing it from an empty list.&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;pre&gt;my @array;        # These two&lt;br /&gt;my @array = ();   # are equivalent&lt;br /&gt;my @array = 1 .. 5;            # These two are&lt;br /&gt;my @array = (1, 2, 3, 4, 5);   # also equivalent&lt;/pre&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;Constructing a hash does impose the requirement that the provided list be even in length, or else a warning will be generated. Otherwise, there is no special requirement to constructing a hash.&lt;/div&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;pre&gt;my %hash;        # These two&lt;br /&gt;my %hash = ();   # are equivalent&lt;br /&gt;my %hash = 1 .. 6;               # These two are&lt;br /&gt;my %hash = (1, 2, 3, 4, 5, 6);   # also equivalent&lt;br /&gt;my %hash = ( 'a', 1, 'b', 2 );   # And these two are &lt;br /&gt;my %hash = ( a =&amp;gt; 1, b =&amp;gt; 2 );   # also equivalent&lt;/pre&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;List Unpacking&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;List unpacking is the principle of doing what you just did, but with a list on the left hand side of the assignment operator as well as the right.&lt;br /&gt;&lt;br /&gt;Just to confuse you, parentheses on the left hand side of the assignment operator &lt;i&gt;do&lt;/i&gt; create list context&lt;sup&gt;3&lt;/sup&gt;.&lt;br /&gt;&lt;br /&gt;List unpacking takes sequential items from the source list, and assigns them in order into the scalar or aggregate values in the destination. This example involves just scalars:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my ($first, $second) = @days;&lt;/pre&gt;&lt;br /&gt;In this example, the rest of &lt;code&gt;@days&lt;/code&gt;&amp;nbsp;is ignored if it is more than 2 items long. &lt;code&gt;$first&lt;/code&gt; and &lt;code&gt;$second&lt;/code&gt; get &lt;code&gt;undef&lt;/code&gt; if &lt;code&gt;@days&lt;/code&gt; is not long enough to populate them. Using our example from earlier we will expect &lt;code&gt;$first&lt;/code&gt; and &lt;code&gt;$second&lt;/code&gt; to have &lt;code&gt;'Mon'&lt;/code&gt; and &lt;code&gt;'Tue'&lt;/code&gt; in them, respectively.&lt;br /&gt;&lt;br /&gt;This next example uses one scalar and one aggregate. If any of the items on the left is an array, it gobbles up all the rest of the list on the right.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my ($mon, @tue_to_sun) = @days;&lt;/pre&gt;&lt;br /&gt;That means this doesn't work:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my ($mon, @tue_to_sat, $sun) = @days;&lt;/pre&gt;&lt;br /&gt;While the Perl hackers could feasibly make this work, there are logical problems that are essentially unsolvable. Since Perl uses the concept of DWIM as much as possible, it is better to avoid trying to make this work than to make it not do what you meant.&lt;br /&gt;&lt;br /&gt;Logically, this brings us back to the copying of an array that we've seen before, simply by not using that scalar:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my (@days_copy) = @days;&lt;/pre&gt;&lt;br /&gt;Because the &lt;code&gt;@days_copy&lt;/code&gt;&amp;nbsp;puts the assignment operator in list context anyway, we can lose the parentheses, and we're back to square one.&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;&lt;br /&gt;You can also swap existing variables around using the same syntax. Here's an example that makes sure &lt;code&gt;$x&lt;/code&gt; is always greater than (or equal to) &lt;code&gt;$y&lt;/code&gt;:&lt;/div&gt;&lt;br /&gt;&lt;pre&gt;if ( $y &amp;gt; $x ) {&lt;br /&gt;  ($x, $y) = ($y, $x);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This list unpacking idea is usually used to fetch the parameters to a function out of the special array &lt;code&gt;@_&lt;/code&gt;. We'll see that later.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: x-large; white-space: normal;"&gt;Interchangeability&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;The fact that most newcomers to Perl don't immediately grasp is that whenever a list is required, either an array or hash can be used in its place. Both an array and a hash, used as a list, will yield their contents as such a list. Being unordered, the list you get out of a hash may not be in the same order as the list you put into the hash, but it'll have the same contents, and the pairs will maintain their association.&lt;br /&gt;&lt;br /&gt;In that vein, all of the following are valid, albeit of debatable usefulness.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @dirs = ('.', '..', '/', '/home');&lt;br /&gt;my %pointless_hash = @dirs;&lt;br /&gt;my @dirs_copy = @dirs;&lt;br /&gt;my @hash_pairs = %pointless_hash;&lt;br /&gt;my @useless_variable = (@dirs, @hash_pairs, @dirs);&lt;br /&gt;my $count = @dirs;   # You know about this of course&lt;br /&gt;&lt;br /&gt;push @dirs, @dirs;&lt;br /&gt;push @dirs, %pointless_hash;&lt;br /&gt;&lt;br /&gt;for my $item (@dirs) { ... }&lt;br /&gt;for my $key_or_value (%pointless_hash) { ... }&lt;br /&gt;&lt;br /&gt;for my $item ('/opt', @dirs, 1, 2, 3, &lt;br /&gt;@hash_pairs, %pointless_hash, $count) {&lt;br /&gt;  ...&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Both the aggregate data types simply become a list again when you use them as lists. Of course, a scalar becomes a list as well when you use it as a list:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $cur_dir = '.';&lt;br /&gt;my @dirs_to_scan = $cur_dir;&lt;/pre&gt;&lt;br /&gt;In the previous example you can see the comma operator being used with scalars, literals (also scalar, of course), arrays and hashes, all at once. Although a confusing and contrived example, it endeavours to show that the aggregate data types can be used in any list situation and will behave consistently; i.e., as a list of the scalars they contain.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;The Compound Data Structure Confusion&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;All this helps to explain the confusion of newcomers to Perl when it comes to trying to create complex data structures, which is when they don't use references to make hashes or arrays of hashes or arrays.&lt;br /&gt;&lt;br /&gt;With this new-found knowledge, it should be clear what is wrong with the following code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @dirs = ('.', '..', '/', '/home');&lt;br /&gt;my %options = (&lt;br /&gt;  dirs =&amp;gt; @dirs&lt;br /&gt;);&lt;/pre&gt;&lt;br /&gt;Of course the hash constructor is a list. The fat comma &lt;code&gt;=&amp;gt;&lt;/code&gt; is just a normal comma with style, and the array is just an array! It's in list context, so it behaves consistently—i.e. just as we've seen it behave so far.&lt;br /&gt;&lt;br /&gt;The above hash assignment is exactly equivalent to this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %options = (&lt;br /&gt;  'dirs', '.', '..', '/', '/home'&lt;br /&gt;);&lt;/pre&gt;&lt;br /&gt;... which is a 5-element list—which is a warning, as we already know. This problem is solved by the use of references, which would turn, in this example, &lt;code&gt;@dirs&lt;/code&gt; into a single scalar, essentially wrapping up the whole array as a single value in the list.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Other List Constructors&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The comma operator is not the only way of constructing a list. The range operator .. constructs a list of all numbers between two integers, or all alphabetically sequential strings between two strings of a particular length.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @array = 1 .. 6;&lt;br /&gt;my %hash = 1 .. 6;&lt;br /&gt;my @letters = 'a' .. 'z';&lt;/pre&gt;&lt;br /&gt;The &lt;code&gt;qw&lt;/code&gt; operator makes a list of strings by splitting on whitespace:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @animals = qw/cat mouse dog rat monkey/;&lt;br /&gt;my %genus = qw/&lt;br /&gt;  cat felis&lt;br /&gt;  dog canis&lt;br /&gt;  mouse mus&lt;br /&gt;/;&lt;br /&gt;use Module qw/this is a list as well/;&lt;/pre&gt;&lt;br /&gt;Note that none of these list constructors requires parentheses—because there isn't a comma in the syntax. You can use parentheses—&lt;code&gt;qw()&lt;/code&gt;—but that is the syntax of the &lt;code&gt;qw&lt;/code&gt; operator, and not treated as actual parentheses at all.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;keys and values&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A hash is an aggregate data structure that is paired. Half of its scalars are keys, and the other half are the values associated with those keys.&lt;br /&gt;&lt;br /&gt;You can query the hash for either list separately from the other. Both &lt;code&gt;keys&lt;/code&gt; and &lt;code&gt;values&lt;/code&gt; return a list.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %colour = (&lt;br /&gt;  red   =&amp;gt; '#ff0000',&lt;br /&gt;  green =&amp;gt; '#00ff00'&lt;br /&gt;  blue  =&amp;gt; '#0000ff'&lt;br /&gt;);&lt;br /&gt;my @colour_names = keys %colour;&lt;br /&gt;my @colour_hexes = values %colour;&lt;br /&gt;&lt;br /&gt;for my $colour_name ( keys %colour ) {&lt;br /&gt;  my $hex = $colour{$colour_name};&lt;br /&gt;  ...&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;As long as you don't change the hash, both &lt;code&gt;keys&lt;/code&gt; and &lt;code&gt;values&lt;/code&gt; will return the list in the same order—that is to say, if you were to interleave them again, the pairs would match up.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;map, grep and sort&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;These three operators act on lists and return another list. Everything you have seen up to now applies to both the list you input, and the list you get back.&lt;br /&gt;&lt;br /&gt;That is to say, wherever you use a list, you can use &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;grep&lt;/code&gt; or &lt;code&gt;sort&lt;/code&gt; on that list instead.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my $dir = '.';&lt;br /&gt;opendir my $dirh, $dir;&lt;br /&gt;my @files = readdir $dirh; #all files&lt;br /&gt;&lt;br /&gt;# loop all files&lt;br /&gt;for my $file ( @files ) {...} &lt;br /&gt;&lt;br /&gt;# loop some files&lt;br /&gt;for my $file ( grep { $_ !~ /\.\.?/ } @files ) {...} &lt;br /&gt;&lt;br /&gt;# loop files in alphabetical order&lt;br /&gt;for my $file ( sort @files ) {...} &lt;br /&gt;&lt;br /&gt;# loop files without their extensions&lt;sup&gt;4&lt;/sup&gt;&lt;br /&gt;for my $file ( map { s/\..+$//r } @files ) {...} &lt;/pre&gt;&lt;br /&gt;We can use &lt;code&gt;@files&lt;/code&gt; as a list directly; or we can perform a sort, map or grep on it to return a different list. &lt;code&gt;sort&lt;/code&gt; alters order of the elements; &lt;code&gt;map&lt;/code&gt; alters the elements themselves; and &lt;code&gt;grep&lt;/code&gt; reduces the number of elements.&lt;br /&gt;&lt;br /&gt;Since everything at this point is a list, you can chain them together.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;for my $file ( sort map { s/\..+$//r } grep { $_ !~ /\.\.?/ } @files ) { ... }&lt;/pre&gt;&lt;br /&gt;The input list for &lt;code&gt;sort&lt;/code&gt; is the output list of &lt;code&gt;map&lt;/code&gt;; the input list to &lt;code&gt;map&lt;/code&gt; is the output list from &lt;code&gt;grep&lt;/code&gt;; and the input list to &lt;code&gt;grep&lt;/code&gt; is the list you get by using an array in list context.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Functions&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now that we've seen lots of different uses of lists, arrays and hashes in list context, and we've seen a few different ways of constructing them,we can tackle the final confusion of newcomers to Perl: function arguments.&lt;br /&gt;&lt;br /&gt;When you pass arguments to a function they appear in the special array @_ inside the function. Let's look at how we call a function.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;sub add {&lt;br /&gt;  my ($x, $y) = @_;&lt;br /&gt;  return $x + $y;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;add 1, 2;  # returns 3&lt;/pre&gt;&lt;br /&gt;The parameter list to a function is in list context&lt;sup&gt;5&lt;/sup&gt;. It is a parameter &lt;i&gt;list&lt;/i&gt;, after all. The parameters to the add function above are 1 and 2. Look familiar? It's the comma operator in list context, creating a list out of the scalars 1 and 2. There are no parentheses because they are optional for function calls in Perl; there is no other operator on this line, so we don't need to override the precedence of the comma operator like we did at the start of the post when constructing aggregates.&lt;br /&gt;&lt;br /&gt;Since the parameter list is Just A List this means everything we've talked about so far also applies.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;sub add {   &lt;br /&gt;  my ($x, $y) = @_;&lt;br /&gt;  return $x + $y; &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;my @numbers = (1, 2); &lt;br /&gt;add @numbers;  # returns 3&lt;/pre&gt;&lt;br /&gt;The array &lt;code&gt;@numbers&lt;/code&gt; is used as a list because it is in list context, and hence its values are sent into the function and appear, as usual, in &lt;code&gt;@_&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;This, therefore, explains how you can do things like this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;sub cat_noise {&lt;br /&gt;  my %options = @_;&lt;br /&gt;&lt;br /&gt;  if ($options{meow}) {&lt;br /&gt;    say $options{meow};&lt;br /&gt;  }&lt;br /&gt;  else {&lt;br /&gt;    say "Meow.";&lt;br /&gt;  }&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;my %opts = qw/ meow purr /; &lt;br /&gt;cat_noise( %opts );&lt;/pre&gt;&lt;br /&gt;I put parentheses in here for clarity, but let's reduce this hideously contrived example using the rules we've already mapped out so far.&lt;br /&gt;&lt;br /&gt;First, we know that the traditional way of constructing a hash, with &lt;code&gt;=&amp;gt;&lt;/code&gt;, is just a tidy way of constructing a list. So a hash is just constructed from a list.&lt;br /&gt;&lt;br /&gt;We also learned that &lt;code&gt;qw&lt;/code&gt; is an operator that creates a list by splitting on whitespace, and can use any character to delimit its argument. This time we chose &lt;code&gt;/&lt;/code&gt;. This, therefore, is what Perl sees:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %opts = ('meow', 'purr'); &lt;/pre&gt;&lt;br /&gt;We then send &lt;code&gt;%opts&lt;/code&gt; into &lt;code&gt;cat_noise&lt;/code&gt;. Again, we've seen that if you use a hash where a list is expected, a list is what you get. So Perl unpacks the hash again and sends the resulting list to &lt;code&gt;cat_noise&lt;/code&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;cat_noise( 'meow', 'purr' );&lt;/pre&gt;&lt;br /&gt;Inside &lt;code&gt;cat_noise&lt;/code&gt;, the first thing we do is unpack the list provided by &lt;code&gt;@_&lt;/code&gt; into an aggregate data type—a hash called &lt;code&gt;%options&lt;/code&gt;. Then &lt;code&gt;%options&lt;/code&gt; is the basis for the body of the function, wherein we check for the existence of the &lt;code&gt;meow&lt;/code&gt; key, and say its value if it exists, and "Meow." if not.&lt;br /&gt;&lt;br /&gt;We can see therefore that the way we pass a hash into a function is to use it as a list, and then convert it back into a hash by using &lt;code&gt;@_&lt;/code&gt; as a list. Some people advocate passing this as a hash &lt;i&gt;ref&lt;/i&gt; so that you avoid constructing a new hash, which is theoretically slightly faster.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;More Common Examples&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;A hash from a map&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;Sometimes you may see a construct like this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %uniq = map { ($_ =&amp;gt; 1) } @array;&lt;br /&gt;my @array_uniq = keys %uniq;&lt;/pre&gt;&lt;br /&gt;What is happening here? As we know, &lt;code&gt;map&lt;/code&gt; returns a list and you construct a hash from a list. &lt;code&gt;map&lt;/code&gt; also &lt;i&gt;accepts&lt;/i&gt; a list, and you can use an array as a list too. In the block we give to &lt;code&gt;map&lt;/code&gt;, we actually also return a list—a 2-item list. That means that the list we get &lt;i&gt;out&lt;/i&gt; of &lt;code&gt;map&lt;/code&gt; will have 2 items for every 1 item we put into it. That one item is represented by the &lt;code&gt;$_&lt;/code&gt;, and the second item is simply 1.&lt;br /&gt;&lt;br /&gt;So if &lt;code&gt;@array&lt;/code&gt; were a list of colours:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @array = qw( red green blue yellow red );&lt;/pre&gt;&lt;br /&gt;Then Perl would create a 2-item list for each of these, and our output would be:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;( red =&amp;gt; 1, green =&amp;gt; 1, blue =&amp;gt; 1, yellow =&amp;gt; 1, red =&amp;gt; 1 );&lt;/pre&gt;&lt;br /&gt;And so we create the hash:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %uniq = ( red =&amp;gt; 1, green =&amp;gt; 1, blue =&amp;gt; 1, yellow =&amp;gt; 1, red =&amp;gt; 1 );&lt;/pre&gt;&lt;br /&gt;Since the key 'red' is repeated, the latter is accepted as the &lt;i&gt;de facto&lt;/i&gt; pairing—not that it matters because both values are 1—but 'red' still only appears once in the hash (because keys are unique).&lt;br /&gt;&lt;br /&gt;Now if we run &lt;code&gt;keys&lt;/code&gt; on it, we get back a list that contains the unique elements of the original &lt;code&gt;@array&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my @array_uniq = keys %uniq; # red, green, blue, yellow&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Default options&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;That leads us onto this:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;my %opts = (%defaults, %options);&lt;/pre&gt;&lt;br /&gt;This ought to now be clear. Both hashes are expanded to their representative lists; the contents of the &lt;code&gt;%options&lt;/code&gt; hash must come after the contents of the &lt;code&gt;%defaults&lt;/code&gt; hash. That means their values take precedence, and any missing values in &lt;code&gt;%options&lt;/code&gt; are still in the list because of &lt;code&gt;%defaults&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Further Considerations&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Left as an exercise to the reader are the ideas of building an array bit-by-bit and using that as a function parameter list, and of returning a list from a function and using &lt;i&gt;that&lt;/i&gt;&amp;nbsp;as another function's parameter list.&lt;br /&gt;&lt;br /&gt;Having seen what happens when you try to put an array or a hash into another array or hash—the list-flattening effect—you should now read &lt;a href="http://perldoc.perl.org/perlreftut.html"&gt;tutorials about references&lt;/a&gt;. These are the mechanism by which the entire array or hash can be stored as a single scalar, thus providing the logical boundaries between the list that is in the array, and the list that is in the sub-array. Or hash.&lt;br /&gt;&lt;br /&gt;The technically-minded may wish to now read about &lt;a href="http://perldoc.perl.org/perlsub.html#Prototypes"&gt;prototypes&lt;/a&gt;, being a way of changing the way Perl understands the parameter list you provide. The curious reader should be aware that prototypes are not a general tool, and can cause much confusion and inconsistency in the way you and others expect things to work if they are misused.&lt;br /&gt;&lt;br /&gt;&lt;hr /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; It may confuse you to see that &lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;code&gt;1;&lt;/code&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; is often used to return from functions and, indeed, from modules. Note that functions are evaluated in the context of where they are called, which means this &lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;i&gt;could&lt;/i&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt; be evaluated in a non-void context. Therefore, you do not get a warning about that&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;. This is true of modules too, which is why you can use any true value as the module's return value.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;sup&gt;2&lt;/sup&gt; In fact you don't get a warning about &lt;code&gt;1;&lt;/code&gt; because 0 and 1 are exempt from this warning (see perldiag). However, the warning does apply to all other constants, including strings.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;sup&gt;3&lt;/sup&gt; If you don't use the parentheses you get scalar context when assigning to a scalar, and the comma on the left suffers the same problems as it did before, i.e. the precedence is wrong. If the item immediately before the equals sign is a scalar, you get scalar context, which is the last element when you use the comma operator: &lt;code&gt;my $x = (1, 2, 3, 4, 5, 6); # x = 6&lt;/code&gt; &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;sup&gt;4&lt;/sup&gt; The /r in the substitution here (s///r) is introduced in Perl 5.14, and is used to return the altered string instead of altering the actual string. Prior to 5.14, you can do this by applying the regex to a copy of the string: &lt;code&gt;map { (my $x = $_) =~ s/\..+$//; $x } LIST&lt;/code&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;&lt;sup&gt;5&lt;/sup&gt; Function prototypes are out of the scope of this post.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-3026870014641832812?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/3026870014641832812/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/3026870014641832812'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/3026870014641832812'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/08/lists-and-things-made-of-lists.html' title='Lists, and Things Made Of Lists'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-8267186401982772817</id><published>2011-07-01T09:29:00.001+01:00</published><updated>2011-09-14T22:22:49.365+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='announcements'/><title type='text'>Introducing Protip</title><content type='html'>A while ago I had a long, protracted conversation with my manager trying to convince him that our company should have a github account for select open-source projects we, as a company, want to release into the wild, on the basis that it would be good PR et cetera. That conversation went like this:&lt;br /&gt;&lt;blockquote&gt;Me: I think we should put some open-source projects on github&lt;br /&gt;Him: Good idea. People can download this stuff anyway when it's on the web so we might as well put it out there on purpose.&lt;/blockquote&gt;It is honestly quite a pleasure to work for a manager savvy enough to hold this opinion, rather than the sort of manager you hear about who, in spite of all observational evidence, maintains a world view that the company's code is its own and the correct answer lies in various obfuscation and encryption techniques that &amp;nbsp;entirely defeat the point of the code being secured in the first place.&lt;br /&gt;&lt;br /&gt;So without further ado I present the project that spawned this highly modern thinking, &lt;a href="https://github.com/propcom/Protip"&gt;Protip&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;This is a jQuery plugin intended to make a tooltip that is actually useful. Having tried many other tooltips I found that most suffered from the same basic problem: The method of deciding &lt;i&gt;what&amp;nbsp;&lt;/i&gt;should be in the tooltip (and what the tooltip should look like) was highly arbitrary, or at least difficult to shoehorn into your average document, to the extent that the majority of your tooltip logic was creating the tooltips in the right place so that the plugin, which is meant to save you work, can see them. By which time you might as well have written your own tooltip anyway. So I did that.&lt;br /&gt;&lt;br /&gt;Protip can take a function as the tooltip specifier, and the function returns a jQuery object. Simple as that. There are a few (1) predefined such functions but generally you tell the plugin what and where your tooltip is.&lt;br /&gt;&lt;br /&gt;It is currently a bit hastily written and hence there is a certain quantity of Javascripty scope unsureties going on, but nothing a bit of a refactor won't solve.&lt;br /&gt;&lt;br /&gt;Here it is again. Go nuts. Feedback appreciated in the form of patches or pull requests.&amp;nbsp;&lt;a href="https://github.com/propcom/Protip"&gt;https://github.com/propcom/Protip&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-8267186401982772817?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/8267186401982772817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/07/introducing-protip.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/8267186401982772817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/8267186401982772817'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/07/introducing-protip.html' title='Introducing Protip'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-9015378539207311630</id><published>2011-06-21T16:53:00.002+01:00</published><updated>2011-09-14T22:22:21.245+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='rantings'/><category scheme='http://www.blogger.com/atom/ns#' term='musings'/><title type='text'>It's as if they thought it through.</title><content type='html'>I wonder why we have separate arrays and hashes in Perl. Other languages don't do it. After all, the principal difference between an array and a hash is that an array references its items by ordinal position and hashes use strings to name them. A hash could surely be conflated with an array simply by using integers as the string keys - especially since Perl can use strings and integers interchangeably.&lt;br /&gt;&lt;br /&gt;We would have to make changes, but all it would really need is a way of detecting when the user intended to use an ordinal array and when the user intended to use an associative array. This should be easy enough: all we need to do is check whether all the keys are sequential and start at zero, and we know it's an ordinal array. To accommodate the fact this may be a coincidence, we can create a second set of functions so that the user can specify that even though the array appears to be ordinal it is actually just that the keys happen to be numeric and happen to be in order starting from zero. We'd also have to change the way sort works, in fact creating two functions: one function that orders an ordinal array and re-creates the keys when the values are in their new positions, and one function that, having sorted the array by value, makes sure the keys still refer to the same value. Of course, sorting integers as strings returns a different order from sorting integers as integers ('10' is alphabetically between '1' and '2'), so we would need a &lt;code&gt;keys&lt;/code&gt; function that knew whether to return strings or integers so that we know, when sorting the list of keys, whether to sort them as strings or integers.&lt;br /&gt;&lt;br /&gt;Splicing would also require two functions, of course. It doesn't really make sense to splice a nominal array because there is no inherent order to it; but since a fundamental tenet of structural programming is that if you make two things the same, you must treat them the same, then we have to &lt;i&gt;make&lt;/i&gt;&amp;nbsp;it make sense. Since splicing is all about removing things by their position (it's very easy to remove a key from a nominal array: just remove it), we need to give associative arrays an internal order. Or possibly just whinge when we use a thing that doesn't look like an ordinal array in splice, thereby affirming a difference between ordinal and associative arrays that we are desperately trying to pretend doesn't exist.&lt;br /&gt;&lt;br /&gt;We'd also have to determine what to do when, for example, someone creates an entry in an array by giving it an ordinal position that doesn't exist. Do we create an array of suitable length and fill it with bits of emptiness in order to maintain the illusion that this array is ordinal? Or do we create it as an associative array with a single numerical key? What happens if someone creates key [2], then key [0], then key [1]? Do we sneakily go back and pretend we knew they meant this to be an ordinal array from the beginning, or do we treat this as an associative array and annoy the hell out of the user, who expected an ordinal array with three entries?&lt;br /&gt;&lt;br /&gt;And then finally an extra function is needed so that we can refer to elements by their ordinal position even if it's not a real ordinal position: after all, '-1' is a valid associative array key but in an ordinal array it means "the last element" like it does in common C functions like substr, so we'd have to create a way of referencing the array backwards without accidentally confusing a negative index with a string key.&lt;br /&gt;&lt;br /&gt;Oh yes. That's why.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Further reading&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;Here's a Wikipedia link:&amp;nbsp;http://en.wikipedia.org/wiki/Waterbed_theory — if anyone can find TimToady's paper on this on the interwebs, I'd like to link to that from here too, so I'd be grateful for that.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-9015378539207311630?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/9015378539207311630/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/06/its-as-if-they-thought-it-through.html#comment-form' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/9015378539207311630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/9015378539207311630'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/06/its-as-if-they-thought-it-through.html' title='It&apos;s as if they thought it through.'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4857673566967429385.post-1030793493020853678</id><published>2011-06-11T22:49:00.001+01:00</published><updated>2011-09-14T22:21:37.126+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='newbies'/><title type='text'>The Anatomy of Types</title><content type='html'>A chief confusion of people new to Perl is the apparently disconnected syntax used to refer to variables. Of particular consternation is the syntax used for accessing arrays and hashes: especially slices thereof. This seems to be because the creation of and use of arrays and hashes is taught at a simpler level than the level of understanding required to actually see how they work.&lt;br /&gt;&lt;br /&gt;Here's a table that shows some variables, as they are used, and how they divide up. It also shows the number of items each expression will return.&lt;br /&gt;&lt;br /&gt;&lt;table border="1" cellpadding="12" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt; &lt;th colspan="3"&gt;Expression&lt;/th&gt; &lt;th&gt;&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;     &lt;th&gt;Sigil&lt;/th&gt;     &lt;th&gt;Identifier&lt;/th&gt;     &lt;th&gt;Subscript&lt;/th&gt;  &lt;th&gt;Number of items&lt;/th&gt; &lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$&lt;/td&gt;         &lt;td&gt;scalar&lt;/td&gt;         &lt;td&gt;&lt;/td&gt;  &lt;td style="font-size: medium;"&gt;1&lt;/td&gt; &lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;%&lt;/td&gt;         &lt;td&gt;hash&lt;/td&gt;           &lt;td&gt;&lt;/td&gt; &lt;td style="font-size: medium;"&gt;Many pairs&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@&lt;/td&gt;         &lt;td&gt;array&lt;/td&gt;         &lt;td&gt;&lt;/td&gt; &lt;td style="font-size: medium;"&gt;Many&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$&lt;/td&gt;         &lt;td&gt;array&lt;/td&gt;         &lt;td&gt;[0]&lt;/td&gt;  &lt;td style="font-size: medium;"&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$&lt;/td&gt;         &lt;td&gt;hash&lt;/td&gt;         &lt;td&gt;{key}&lt;/td&gt;  &lt;td style="font-size: medium;"&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@&lt;/td&gt;         &lt;td&gt;array&lt;/td&gt;         &lt;td&gt;[0,1,2]&lt;/td&gt;  &lt;td style="font-size: medium;"&gt;Many&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@&lt;/td&gt;         &lt;td&gt;hash&lt;/td&gt;         &lt;td&gt;{'key1', 'key2'}&lt;/td&gt;  &lt;td style="font-size: medium;"&gt;Many&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;1. The Sigil&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;$&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;$ refers to a scalar. A scalar is a single, atomic item. Its contents cannot be divided without applying further processing to the scalar itself. Whenever an expression begins with a $, it is a scalar, and there is one item.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;@&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;@ refers to more than one scalar, in some order. Without a subscript, it refers to an array; otherwise it simply refers to a list. Saying it is "in order" means that we can identify any item within the list by its numerical position; it also means that there is a first, second, nth and last element in it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;%&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;% refers to a hash. A hash is also a collection of scalars, but there is no order to them. Rather than each scalar being in a known position in a list, instead half of the scalars are referred to by the other half. The "other half" are all &lt;i&gt;strings&lt;/i&gt;&amp;nbsp;and are called &lt;i&gt;keys&lt;/i&gt;. If the % is used you know that you are referring to a set of items that alternate between keys and values. Having no order, it is therefore meaningless to talk about the first, second, nth, or last element of the hash.&lt;br /&gt;&lt;br /&gt;Apply these rules to the table above. See that every expression whose sigil is a $ gives us 1 item; every variable whose sigil is @ gives us many (zero or more) items; and every variable whose sigil is % gives us many paired items.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;&lt;b&gt;2. The identifier&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;The identifier is the name of the variable. Without its sigil it is fairly meaningless because it could refer to anything&lt;sup&gt;&lt;span class="Apple-style-span" style="font-size: xx-small;"&gt;1&lt;/span&gt;&lt;/sup&gt;. With its sigil, suddenly we know what form of variable we are talking about - scalar, array or hash. And with a sigil and a subscript, we know yet again that we are talking about one or many scalars, and which type of variable the identifier refers to.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Here's the tricky part. Each identifier can refer to &lt;i&gt;all types&lt;/i&gt;. It is perfectly legitimate (albeit often quite a bad idea) to have &lt;i&gt;all three&amp;nbsp;&lt;/i&gt;of $var, @var and %var in the same scope at the same time.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This is allowable because it is impossible for there to be ambiguity. There is no crossover in either of the tables below, either within themselves or between them. A combination of sigil and subscript can tell us exactly which type of variable the identifier refers to, and therefore Perl simply allows for all types to be under a single name. Thus:&lt;/div&gt;&lt;br /&gt;&lt;table border="1" cellpadding="12" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;     &lt;th&gt;Expression&lt;/th&gt;     &lt;th&gt;Looks for&lt;/th&gt; &lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$var&lt;/td&gt;         &lt;td&gt;$var&lt;/td&gt; &lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@var&lt;/td&gt;         &lt;td&gt;@var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;%var&lt;/td&gt;         &lt;td&gt;%var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$var[0]&lt;/td&gt;         &lt;td&gt;@var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$var{key}&lt;/td&gt;         &lt;td&gt;%var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@var[0,1]&lt;/td&gt;         &lt;td&gt;@var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@var{'key1', 'key2'}&lt;/td&gt;         &lt;td&gt;%var&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;3. The Subscript&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;When you have an aggregate data structure (array or hash) you know that you are talking about possibly multiple scalars at once. Arrays are accessed by selecting an item by its position, and hashes are accessed by using the string key we associated with the scalar.&lt;br /&gt;&lt;br /&gt;Armed with the knowledge about what the sigil means we can consult the table above to pull apart the familiar way of accessing arrays and hashes to get an item out:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;my $first_item = $things[0];&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We know $first_item is a scalar because it has a $. We know $things[0] is a scalar because it has a $.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;my $first_name = $person{first_name};&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We know $first_name is a scalar because it has a $. We know $person{first_name} is a scalar because it has a $.&lt;br /&gt;&lt;br /&gt;Assigning a scalar to a scalar makes perfect sense. Although it appears that the sigil has changed on the array and hash, what we actually see is that the &lt;i&gt;identifier&lt;/i&gt;&amp;nbsp;of the array is 'array'; the &lt;i&gt;identifier&lt;/i&gt;&amp;nbsp;of the hash is 'hash'; and the choice of sigil is effected by &lt;i&gt;how much of the data structure we want&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Array and Hash Slices&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;Arrays and hashes are aggregate data types, which means they contain multiple scalars. It is reasonable therefore to expect we can request more than one item from them at the same time.&lt;br /&gt;&lt;br /&gt;Since one item is referred to with the $ sigil, and we used a $ to access a single item from the aggregate, then we can simply use @ to refer to multiple items from the same aggregate.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;my @both_names = @person{'first_name', 'last_name'};&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Observe that we can access two values from the hash by supplying both keys as a list in the subscript and using @ instead of $. This of course applies to any quantity of keys, and also applies to arrays&lt;br /&gt;&lt;br /&gt;&lt;code&gt;my @relevant_things = @things[0,3,5];&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This action of taking several selected elements from an aggregate is called slicing.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;A warning about hash slices&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;Remember to use the @ instead of the $ when taking a hash slice. The syntax of putting a list in the subscript to get a scalar refers to a long-deprecated feature that you never want to use intentionally.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;&lt;b&gt;Working Backwards&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;We can work backwards from a line of code to know what we are talking about. Perl has to do this, because we change the sigil depending on how many things we're talking about.&lt;br /&gt;&lt;br /&gt;To determine &lt;i&gt;where&lt;/i&gt;&amp;nbsp;a scalar comes from, we need to look at the subscript. Arrays and hashes don't tend to have names that immediately make it obvious that they are arrays or hashes. But subscripts have syntax that resolves this cleanly.&lt;br /&gt;&lt;br /&gt;An identifier followed by brackets - [ ] - refers to an array. An identifier followed by braces - { } - refers to a hash. An identifier followed by no subscript refers to the exact type the sigil refers to. The &lt;i&gt;sigil&lt;/i&gt;&amp;nbsp;refers to the type of the &lt;i&gt;returned&lt;/i&gt;&amp;nbsp;value. The &lt;i&gt;identifier&lt;/i&gt;, coupled with the &lt;i&gt;subscript&lt;/i&gt;, tells us what type of data structure the value comes from.&lt;br /&gt;&lt;br /&gt;Given the identifier '&lt;i&gt;var&lt;/i&gt;', the following table helps explain where the data comes from in various situations:&lt;br /&gt;&lt;br /&gt;&lt;table border="1" cellpadding="12" cellspacing="0"&gt;&lt;tbody&gt;&lt;tr&gt;     &lt;th&gt;Sigil&lt;/th&gt;     &lt;th&gt;Subscript&lt;/th&gt;  &lt;th&gt;Looks for&lt;/th&gt; &lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$&lt;/td&gt;         &lt;td&gt;&lt;/td&gt;  &lt;td&gt;$var&lt;/td&gt; &lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@&lt;/td&gt;         &lt;td&gt;&lt;/td&gt; &lt;td&gt;@var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;%&lt;/td&gt;         &lt;td&gt;&lt;/td&gt; &lt;td&gt;%var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$&lt;/td&gt;         &lt;td&gt;[ ]&lt;/td&gt;  &lt;td&gt;@var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;$&lt;/td&gt;         &lt;td&gt;{ }&lt;/td&gt;  &lt;td&gt;%var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@&lt;/td&gt;         &lt;td&gt;[ ]&lt;/td&gt;  &lt;td&gt;@var&lt;/td&gt;&lt;/tr&gt;&lt;tr style="font-size: 30px;"&gt;     &lt;td&gt;@&lt;/td&gt;         &lt;td&gt;{ }&lt;/td&gt;  &lt;td&gt;%var&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;This confirms our rule: that without a subscript, the sigil determines the variable we seek; otherwise, the subscript does.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This can be rationalised simply. If we use a subscript, we are requesting only a &lt;i&gt;part&lt;/i&gt; of the aggregate variable in question; i.e. a selection of one or several of the scalar values it contains. This means that, if a subscript is present, we can use it to determine where the data should come from.&amp;nbsp;If we don't use a subscript, it is therefore reasonable we actually intended to refer to the aggregate itself - and this is indeed the case. But in all cases, the sigil still determines the&amp;nbsp;&lt;i&gt;type&lt;/i&gt;&amp;nbsp;of data we get back, be it a scalar or a list or a paired list.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Scalars are not aggregate, so there is never a subscript that will translate into a scalar. That's why '$var' appears only once in the table.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;&lt;b&gt;Further reading&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;So far we have talked about lexical variables (think "braces"). There are two other types of variable: package and global. Package variables are accessed by their fully-qualified name ($Package::var) from other packages, or the same as above from within the package. Global variables - other than the built-in set - should be avoided.&lt;br /&gt;&lt;br /&gt;Read &lt;a href="http://perldoc.perl.org/perlmod.html#Symbol-Tables"&gt;Symbol Tables in perlmod&lt;/a&gt;&amp;nbsp;for information on package variables. And you could do worse than read about&amp;nbsp;&lt;a href="http://perldoc.perl.org/perldata.html#Typeglobs-and-Filehandles"&gt;typeglobs&lt;/a&gt;, a special internal data type for referring to the entire set of types available in the symbol table.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: xx-small;"&gt;&lt;sup&gt;1&lt;/sup&gt;Actually, it can't refer to anything at all. An identifier without a sigil is usually interpreted as subroutine call, but can result in ambiguity that causes strictures to complain about barewords. Nevertheless, a (named) subroutine is actually a package variable, and we are talking about lexicals here.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4857673566967429385-1030793493020853678?l=altreus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://altreus.blogspot.com/feeds/1030793493020853678/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://altreus.blogspot.com/2011/06/anatomy-of-types.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/1030793493020853678'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4857673566967429385/posts/default/1030793493020853678'/><link rel='alternate' type='text/html' href='http://altreus.blogspot.com/2011/06/anatomy-of-types.html' title='The Anatomy of Types'/><author><name>Altreus</name><uri>http://www.blogger.com/profile/03670647455111183841</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
