Commits

Shlomi Fish  committed 756e948

Add src/tutorials/modern-perl/ .

  • Participants
  • Parent commits b662f2b

Comments (0)

Files changed (31)

File lib/MyNavData.pm

             subs =>
             [
                 {
+                    text => "Modern Perl by chromatic",
+                    url => "tutorials/modern-perl/",
+                    title => "The book “Modern Perl” by chromatic.",
+                },
+                {
                     text => qq{The "Perl for Newbies" Tutorial},
                     url => "tutorials/perl-for-newbies/",
                     title => "Perl Tutorial for Beginners",

File src/tutorials/modern-perl/epub/modern_perl.epub

Binary file added.

File src/tutorials/modern-perl/index.html.wml

+#include '../template.wml'
+
+<latemp_subject "Modern Perl Online Book" />
+
+<p>
+This is our own mirror of the book 
+<a href="http://onyxneon.com/books/modern_perl/"><i>Modern Perl</i></a> by
+<a href="http://www.wgz.org/chromatic/">chromatic</a>. It is distributed
+under the 
+<a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" 
+rel="license">Creative 
+Commons Attribution-Noncommercial-Share Alike 3.0 United States License</a>
+(CC-by-nc-sa).
+</p>
+
+<h2 id="chapters">Chapters</h2>
+
+<ul>
+<:{
+
+foreach my $idx (0 .. 12)
+{
+    printf qq{<li><b>Chapter %d</b> - <a href="xhtml/chapter_%02d.html">HTML</a> , <a href="pdf/chapter_%02d.pdf">PDF</a></li>\n}, (($idx) x 3);
+}
+
+}:>
+</ul>
+
+<h2 id="epub">EPUB</h2>
+
+<p>
+<a href="epub/modern_perl.epub">Modern Perl EPUB</a>
+</p>
+
+<h2><a href="iperl.html">Access the Online Text</a></h2>
+

File src/tutorials/modern-perl/pdf/chapter_00.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_01.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_02.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_03.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_04.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_05.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_06.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_07.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_08.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_09.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_10.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_11.pdf

Binary file added.

File src/tutorials/modern-perl/pdf/chapter_12.pdf

Binary file added.

File src/tutorials/modern-perl/xhtml/chapter_00.html

+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />
+<title></title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<link rel="stylesheet" href="../styles/style.css" type="text/css" />
+</head>
+<body>
+<h1 id="heading_id_2">Preface</h1>
+<p>Perl turns 23 years old later this year. The language has gone from a simple tool for system adminstration somewhere between shell scripting and C programming (Perl 1) to a powerful, general purpose language steeped in a rich heritage (Perl 5) and a consistent, coherent, rethinking of programming in general intended to last for another 25 years (Perl 6).</p>
+<p>Even so, most Perl 5 programs in the world take far too little advantage of the language. You <i>can</i> write Perl 5 programs as if they were Perl 4 programs (or Perl 3 or 2 or 1), but programs written to take advantage of everything amazing the worldwide Perl 5 community has invented, polished, and discovered are shorter, faster, more powerful, and easier to maintain than their alternatives.</p>
+<p><i>Modern Perl</i> is a loose description of how experienced and effective Perl 5 programers work. They use language idioms. They take advantage of the CPAN. They're recognizably Perlish, and they show good taste and craftsmanship and a full understanding of Perl.</p>
+<p>You can learn this too.</p>
+<h2 id="heading_id_3">Running Modern Perl</h2>
+<p>You don't have to install a new version of Perl to take advantage of most of this book, but the examples given assume that you're using Perl 5.10 or newer. Unless otherwise mentioned, code snippets always assume the basic skeleton of a program:</p>
+<div class="programlisting">
+<pre>
+<code>    #!/usr/bin/perl
+
+    use Modern::Perl;
+
+    # example code here
+    ...</code>
+</pre></div>
+<p>Other code snippets use testing functions such as <code>ok()</code>, <code>like()</code>, and <code>is()</code> (<a href="chapter_09.xhtml#testing">Testing</a>(testing)). That skeleton program is:</p>
+<div class="programlisting">
+<pre>
+<code>    #!/usr/bin/perl
+
+    use Modern::Perl;
+    <b>use Test::More 'no_Plan';</b>
+
+    # example code here
+    ...</code>
+</pre></div>
+<p>If you don't already have a modern version of Perl installed, you can install it yourself, using:</p>
+<div class="author">
+<pre>
+<code> * perlbrew
+ * Strawberry Perl
+ * local::lib</code>
+</pre></div>
+</body>
+</html>

File src/tutorials/modern-perl/xhtml/chapter_01.html

+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />
+<title></title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<link rel="stylesheet" href="../styles/style.css" type="text/css" />
+</head>
+<body>
+<h2 id="heading_id_2">The Perl Philosophy</h2>
+<p>Perl is a language for getting things done. It's flexible, forgiving, and malleable. In the hands of a capable programmer, it can perform almost any task, from one-liner calculations and automations to multi-programmer, multi-year projects and everything in between.</p>
+<p>Perl is powerful, and modern Perl--Perl which takes advantage of the best knowledge, deepest experience, and reusable idioms of the global Perl community--can be maintainable, fast, and easy to use. Perhaps most importantly, it can help you do what you need to do with little frustration and no ceremony.</p>
+<p>Perl is a pragmatic language. You, the programmer, are in charge. Rather than manipulating your mind and your problems to fit how the language designer thinks you should write programs, Perl allows you to solve your problems as you see fit.</p>
+<p>Perl is a language which can grow with you. You can write useful programs with the knowledge that you can learn in an hour of reading this book. Yet if you take the time to understand the philosophies behind the syntax, semantics, and design of the language, you can be far more productive.</p>
+<p>First, you need to know how to learn more.</p>
+<h4 id="heading_id_3">Perldoc</h4>
+<div id="perldoc"></div>
+<p>One of Perl's most useful and least appreciated features is the <code>perldoc</code> utility. This program is part of every complete Perl 5 installation <span class="footnote">(footnote: You may have to install an additional package on a free GNU/Linux distribution or another Unix-like system; on Debian and Ubuntu this is <code>perl-doc</code>.)</span>. It displays the documentation of every Perl module installed on the system--whether a core module or one installed from the Comprehensive Perl Archive Network (CPAN)--as well as thousands of pages of Perl's copious core documentation.</p>
+<div class="sidebar">
+<p>If you prefer an online version, <span class="url">http://perldoc.perl.org/</span> hosts recent versions of the Perl documentation. <span class="url">http://search.cpan.org/</span> displays the documentation of every module on the CPAN. Windows users, both ActivePerl and Strawberry Perl provide a link in your Start menu to the documentation.</p>
+</div>
+<p>The default behavior of <code>perldoc</code> is to display the documentation for a named module or a specific section of the core documentation:</p>
+<pre>
+<code>    $ <b>perldoc List::Util</b>
+    $ <b>perldoc perltoc</b>
+    $ <b>perldoc Moose::Manual</b></code>
+</pre>
+<p>The first example extracts documentation written for the <code>List::Util</code> module and displays it in a form appropriate for your screen. Community standards for CPAN modules (<a href="chapter_02.xhtml#cpan">The CPAN</a>(cpan)) suggest that additional libraries use the same documentation format and form as core modules, so there's no distinction between reading the documentation for a core library such as <code>Data::Dumper</code> or one installed from the CPAN. The standard documentation template includes a description of the module, demonstrates sample uses, and then contains a detailed explanation of the module and its interface. While the amount of documentation varies by author, the form of the documentation is remarkably consistent.</p>
+<p>The second example displays a pure documentation file, in this case the table of contents of the core documentation itself. This file describes each individual piece of the core documentation; browse it for a good understanding of Perl's breadth.</p>
+<p>The third example resembles the second; <code>Moose::Manual</code> is part of the Moose CPAN distribution (<a href="chapter_07.xhtml#moose">Moose</a>(moose)). It is also purely documentation; it contains no code.</p>
+<div class="sidebar">
+<p>Similarly, <code>perldoc perlfaq</code> will display the table of contents for Frequently Asked Questions about Perl 5. Skimming these questions is invaluable.</p>
+</div>
+<div id="iperldoc__i45q_0"></div>
+<p>The <code>perldoc</code> utility has many more abilities (see <code>perldoc perldoc</code>). Two of the most useful are the <code>-q</code> and the <code>-f</code> flags. The <code>-q</code> flag takes a keyword or keywords and searches only the Perl FAQ, displaying all results. Thus <code>perldoc -q sort</code> returns three questions: <i>How do I sort an array by (anything)?</i>, <i>How do I sort a hash (optionally by value instead of key)?</i>, and <i>How can I always keep my hash sorted?</i>.</p>
+<div id="iperldoc__i45f_0"></div>
+<p>The <code>-f</code> flag displays the core documentation for a builtin Perl function. <code>perldoc -f sort</code> explains the behavior of the <code>sort</code> operator. If you don't know the name of the function you want, use <code>perldoc perlfunc</code> to see a list of functions.</p>
+<div class="sidebar">
+<p><code>perldoc perlop</code> and <code>perldoc perlsyn</code> document Perl's symbolic operators and syntactic constructs; <code>perldoc perldiag</code> explains the meanings of Perl's warning messages.</p>
+</div>
+<div id="iPOD_0"></div>
+<p>Perl 5's documentation system is <i>POD</i>, or <i>Plain Old Documentation</i>. <code>perldoc perlpod</code> describes how POD works. The <code>perldoc</code> utility will display the POD in any Perl module you create and install for your project, and other POD tools such as <code>podchecker</code>, which validates the form of your POD, and <code>Pod::Webserver</code>, which displays local POD as HTML through a minimal webserver, will handle valid POD correctly.</p>
+<div id="iperldoc__i45l_0"></div>
+<div id="iperldoc__i45m_0"></div>
+<p><code>perldoc</code> has other uses. With the <code>-l</code> command-line flag, it displays the <i>path</i> to the documentation file rather than the contents of the documentation <span class="footnote">(footnote: Be aware that a module may have a separate <i>.pod</i> file in addition to its <i>.pm</i> file.)</span>. With the <code>-m</code> flag, it displays the entire <i>contents</i> of the module, code and all, without processing any POD instructions.</p>
+<h4 id="heading_id_4">Expressivity</h4>
+<div id="expressivity"></div>
+<div id="iWall44_Larry_0"></div>
+<div id="iLarry_Wall_0"></div>
+<p>Before Larry Wall created Perl, he studied linguistics and human languages. His experiences continue to influence Perl's design. There are many ways to write a Perl program depending on your project's style, the available time to create the program, the expected maintenance burden, or even your own personal sense of expression. You may write in a straightforward, top-to-bottom style. You may write many small and independent functions. You may model your problem with classes and objects. You may eschew or embrace advanced features.</p>
+<div id="iTIMTOWTDI_0"></div>
+<div id="iTim_Toady_0"></div>
+<p>Perl hackers have a slogan for this: <i>TIMTOWTDI</i>, pronounced "Tim Toady", or "There's more than one way to do it!"</p>
+<p>Where this expressivity can provide a large palette with which master craftsman can create amazing and powerful edifices, unwise conglomerations of various techniques can impede maintainability and comprehensibility. You can write good code or you can make a mess. The choice is yours <span class="footnote">(footnote: ... but be kind to other people, if you must make a mess.)</span>.</p>
+<p>Where other languages might suggest that one enforced way to perform any operation is the right solution, Perl allows you to optimize for your most important criteria. Within the realm of your own problems, you can choose from several good approaches--but be mindful of readability and future maintainability.</p>
+<p>As a novice to Perl, you may find certain constructs difficult to understand. The greater Perl community has discovered and promoted several idioms (<a href="chapter_10.xhtml#idioms">Idioms</a>(idioms)) which offer great power. Don't expect to understand them immediately. Some of Perl's features interact in subtle ways.</p>
+<div class="sidebar">
+<p>Learning Perl is like learning a second or third spoken language. You'll learn a few words, then string together some sentences, and eventually will be able to have small, simple conversations. Mastery comes with practice, both reading and writing. You don't have to understand all of the details of this chapter immediately to be productive with Perl. Keep the principles as you read the rest of this book.</p>
+</div>
+<p>Another design goal of Perl is to surprise experienced (Perl) programmers very little. For example, adding two scalars together with a numeric operator (<code>$first_num + $second_num</code>) is obviously a numeric operation; the operator must treat both scalars as numeric values to produce a numeric result. No matter what the contents of <code>$first_num</code> and <code>$second_num</code>, Perl will coerce them to numeric values (<a href="chapter_03.xhtml#numeric_coercion">Numeric Coercion</a>(numeric_coercion)) without requiring the user or programmer to perform this conversion manually. You've expressed your intent to treat them as numbers by choosing a numeric operator (<a href="chapter_04.xhtml#numeric_operators">Numeric Operators</a>(numeric_operators)), so Perl happily handles the rest.</p>
+<div id="iDWIM_0"></div>
+<div id="iprinciple_of_least_astonishment_0"></div>
+<p>In general, Perl programmers can expect Perl to do what you mean; this is the notion of <i>DWIM</i>--<i>do what I mean</i>. You may also see this mentioned as the <i>principle of least astonishment</i>. Given a cursory understanding of Perl (especially its <a href="chapter_01.xhtml#context_philosophy">Context</a>(context_philosophy)), it should be possible to read a single unfamiliar Perl expression and understand its intent.</p>
+<div id="ibaby_Perl_0"></div>
+<p>If you're new to Perl, you will develop this skill over time. The flip side of Perl's expressivity is that Perl novices can write useful programs before they learn all of Perl's powerful features. The Perl community often refers to this as <i>baby Perl</i>. Though it may sound dismissive, please don't take offense; everyone was a novice once. Take the opportunity to learn from more experienced programmers and ask for explanations of idioms and constructs you don't yet understand.</p>
+<div class="sidebar">
+<p>A Perl novice might multiply a list of numbers by three by writing:</p>
+<div class="programlisting">
+<pre>
+<code>    my @tripled;
+    my $count = @numbers;
+
+    for (my $i = 0; $i &lt; $count; $i++)
+    {
+        $tripled[$i] = $numbers[$i] * 3;
+    }</code>
+</pre></div>
+<p>A Perl adept might write:</p>
+<div class="programlisting">
+<pre>
+<code>    my @tripled;
+
+    for my $num (@numbers)
+    {
+        push @tripled, $num * 3;
+    }</code>
+</pre></div>
+<p>An experienced Perl hacker might write:</p>
+<div class="programlisting">
+<pre>
+<code>    my @tripled = map { $_ * 3 } @numbers;</code>
+</pre></div>
+<p>Experience writing Perl will help you to focus on what you want to do rather than how to do it.</p>
+</div>
+<p>Perl is a language intended to grow with your understanding of programming. It won't punish you for writing simple programs. It allows you to refine and expand programs for clarity, expressivity, reuse, and maintainability. Take advantage of this philosophy. It's more important to accomplish your task well than to write a conceptually pure and beautiful program.</p>
+<p>The rest of this book demonstrates how to use Perl to your advantage.</p>
+<h4 id="heading_id_5">Context</h4>
+<div id="context_philosophy"></div>
+<div id="icontext_0"></div>
+<p>Spoken languages have a notion of <i>context</i> where the correct usage or meaning of a word or phrase depends on its surroundings. You may understand this in a spoken language, where the inappropriate pluralization of "Please give me one hamburgers!" <span class="footnote">(footnote: The pluralization of the noun differs from the amount.)</span> sounds wrong or the incorrect gender of "la gato" <span class="footnote">(footnote: The article is feminine, but the noun is masculine.)</span> makes native speakers chuckle.</p>
+<p>Context in Perl is similar; the language understands expectations of the amount of data to provide as well as what kind of data to provide. Perl will happily attempt to provide exactly what you ask for.</p>
+<p>One type of context in Perl means that certain operators have different behavior if you want zero, one, or many results. It's possible that a specific construct in Perl will do something different if you say "Fetch me zero results; I don't care about any" than if you say "Fetch me one result" or "Fetch me many results."</p>
+<p>Likewise, certain contexts make it clear that you expect a numeric value, a string value, or a value that's either true or false.</p>
+<p>Context can be tricky if you try to write or read Perl code as a series of single expressions which stand apart from from their environments. You may find yourself slapping your forehead after a long debugging session when you discover that your assumptions about context were incorrect. However, if you're cognizant of contexts, they can make your code clearer, more concise, and more flexible.</p>
+<h5 id="heading_id_6">Void, Scalar, and List Context</h5>
+<div id="amount_context"></div>
+<div id="icontext__iamount_0"></div>
+<div id="iamount_context_0"></div>
+<p>One of the aspects of context governs <i>how many</i> items you expect. This is <i>amount context</i>. Compare this context to subject-verb number agreement in English. Even if you haven't learned the formal description of the rule, you probably understand the error in the sentence "Perl are a fun language". The rule in Perl is that the number of items you request determines how many you get.</p>
+<div id="ivoid_context_0"></div>
+<div id="icontext__ivoid_0"></div>
+<p>Suppose you have a function (<a href="chapter_05.xhtml#functions">Functions</a>(functions)) called <code>some_expensive_operation()</code> which performs an expensive calculation and can produce many, many results. If you call the function on its own and never use its return value, you've called the function in <i>void context</i>:</p>
+<div class="programlisting">
+<pre>
+<code>    some_expensive_operation();</code>
+</pre></div>
+<div id="iscalar_context_0"></div>
+<p>Assigning the function's return value to a single element evaluates the function in <i>scalar context</i>:</p>
+<div class="programlisting">
+<pre>
+<code>    my $single_result = some_expensive_operation();</code>
+</pre></div>
+<div id="ilist_context_0"></div>
+<p>Assigning the results of calling the function to an array (<a href="chapter_03.xhtml#arrays">Arrays</a>(arrays)) or a list, or using it in a list evaluates the function in <i>list context</i>:</p>
+<div class="programlisting">
+<pre>
+<code>    my @all_results        = some_expensive_operation();
+    my ($single_element)   = some_expensive_operation();
+    process_list_of_results( some_expensive_operation() );</code>
+</pre></div>
+<p>The second line of the previous example may look confusing; the parentheses there give a hint to the compiler that although there's only a single scalar, this assignment should occur in list context. It's semantically equivalent to assigning to a scalar and a temporary array, and then throwing away the array, except that no assignment to the array actually occurs:</p>
+<div class="programlisting">
+<pre>
+<code>    my ($single_element, @rest) = some_expensive_operation();</code>
+</pre></div>
+<div class="sidebar">
+<p>Why is context interesting for functions? Suppose <code>some_expensive_operation()</code> calculates all of the tasks you have to do around the house, sorted in order of their priority. If you have only time to do one task, you can call the function in scalar context to get a useful task--perhaps not necessarily the most important, but important enough without having to go through your whole task list. In list context, the function can perform all of the sorting and searching and comparison and give you an exhaustive list in the proper order. If you want all of that work, but only have time for a couple of tasks, you can use a one or two-element list.</p>
+</div>
+<p>Evaluating a function or expression--except for assignment--in list context can produce confusion. Lists propagate list context to the expressions they contain. Both calls to <code>some_expensive_operation()</code> occur in list context:</p>
+<div class="programlisting">
+<pre>
+<code>    process_list_of_results( some_expensive_operation() );
+
+    my %results =
+    (
+        cheap_operation     =&gt; $cheap_operation_results,
+        expensive_operation =&gt; some_expensive_operation(), # OOPS!
+    );</code>
+</pre></div>
+<div id="ioperators__iscalar_0"></div>
+<div id="iscalar_0"></div>
+<p>The latter example often surprises novice programmers who expect scalar context for the call. Use the <code>scalar</code> operator to enforce it:</p>
+<div class="programlisting">
+<pre>
+<code>    my %results =
+    (
+        cheap_operation     =&gt; $cheap_operation_results,
+        expensive_operation =&gt; scalar some_expensive_operation(),
+    );</code>
+</pre></div>
+<h5 id="heading_id_7">Numeric, String, and Boolean Context</h5>
+<div id="value_contexts"></div>
+<div id="ivalue_context_0"></div>
+<div id="icontext__ivalue_0"></div>
+<p>Another type of context determines how Perl understands a piece of data--not <i>how many</i> pieces of data you want, but what the data means. You've probably already noticed that Perl's flexible about figuring out if you have a number or a string and converting between the two as you want them. This <i>value context</i> helps to explain how it does so. In exchange for not having to declare (or at least track) explicitly what <i>type</i> of data a variable contains or a function produces, Perl offers specific type contexts that tell the compiler how to treat a given value during an operation.</p>
+<p>Suppose you want to compare the contents of two strings. The <code>eq</code> operator tells you if the strings contain the same information:</p>
+<div class="programlisting">
+<pre>
+<code>    say "Catastrophic crypto fail!" if $alice eq $bob;</code>
+</pre></div>
+<p>You may have had a baffling experience where you <i>know</i> that the strings are different, but they still compare the same:</p>
+<div class="programlisting">
+<pre>
+<code>    my $alice = 'alice';
+    say "Catastrophic crypto fail!" if $alice == 'Bob';  # OOPS</code>
+</pre></div>
+<div id="istring_context_0"></div>
+<div id="inumeric_context_0"></div>
+<div id="icontext__istring_0"></div>
+<div id="icontext__inumeric_0"></div>
+<p>The <code>eq</code> operator treats its operands as strings by enforcing <i>string context</i> on them. The <code>==</code> operator enforces <i>numeric context</i>. The example code fails because the value of both strings when treated as numbers is <code>0</code>.</p>
+<div id="iboolean_context_0"></div>
+<div id="icontext__iboolean_0"></div>
+<p><i>Boolean context</i> occurs when you use a value in a conditional statement. In the previous examples, the <code>if</code> statement evaluated the results of the <code>eq</code> and <code>==</code> operators in boolean context.</p>
+<p>Perl will do its best to coerce values to the proper type (<a href="chapter_03.xhtml#coercion">Coercion</a>(coercion)), depending on the operators you use. Be sure to use the proper operator for the type of context you want.</p>
+<p>In rare circumstances, you may need to force an explicit context where no appropriately typed operator exists. To force a numeric context, add zero to a variable. To force a string context, concatenate a variable with the empty string. To force a boolean context, double the negation operator:</p>
+<div class="programlisting">
+<pre>
+<code>    my $numeric_x =  0 + $x;  # forces numeric context
+    my $stringy_x = '' . $x;  # forces string  context
+    my $boolean_x =    !!$x;  # forces boolean context</code>
+</pre></div>
+<p>In general, type contexts are less difficult to understand and see than the amount contexts. Once you understand that they exist and know which operators provide which contexts (<a href="chapter_04.xhtml#operator_types">Operator Types</a>(operator_types)), you'll rarely make mistakes with them.</p>
+<h4 id="heading_id_8">Implicit Ideas</h4>
+<div id="implicit_ideas"></div>
+<p>Like many spoken languages, Perl provides linguistic shortcuts. Context is one such feature. Both the compiler and a programmer reading the code can understand the expected number of results or the type of an operation from existing information without adding additional explicit information to disambiguate. Others also exist.</p>
+<h5 id="heading_id_9">The Default Scalar Variable</h5>
+<div id="idefault_scalar_variable_0"></div>
+<div id="ivariables__i36__0"></div>
+<div id="i36__0"></div>
+<p>The default scalar variable, <code>$_</code>, is the best example of a linguistic shortcut in Perl. It's most notable in its <i>absence</i>: many of Perl's built in operations perform their work on the contents of <code>$_</code> in the absence of an explicit variable. You can still use <code>$_</code> as the variable, but it's often unnecessary.</p>
+<p>For example, the <code>chomp</code> operator removes any trailing newline sequence from the given string:</p>
+<div class="programlisting">
+<pre>
+<code>    my $uncle = "Bob\n";
+    say "'$uncle'";
+    chomp $uncle;
+    say "'$uncle'";</code>
+</pre></div>
+<p>Without an explicit variable, <code>chomp</code> removes the trailing newline sequence from <code>$_</code>. Thus, these two lines of code are equivalent:</p>
+<div class="programlisting">
+<pre>
+<code>    chomp $_;
+    chomp;</code>
+</pre></div>
+<p>Similarly, the <code>say</code> and <code>print</code> builtins operate on <code>$_</code> in the absence of other arguments:</p>
+<div class="programlisting">
+<pre>
+<code>    print;  # prints $_ to the currently selected filehandle
+    say;    # prints $_ to the currently selected filehandle
+            # with a trailing newline</code>
+</pre></div>
+<p>Perl's regular expression facilities (<a href="chapter_06.xhtml#regular_expressions">Regular Expressions</a>(regular_expressions)) can also operate on <code>$_</code>, performing matches, substitutions, and transliterations:</p>
+<div class="programlisting">
+<pre>
+<code>    $_ = 'My name is Paquito';
+    say if /My name is/;
+
+    s/Paquito/Paquita/;
+
+    tr/A-Z/a-z/;
+    say;</code>
+</pre></div>
+<div class="author">
+<p>Check how many other string functions this includes?</p>
+</div>
+<p>Many of Perl's scalar operators work on the default scalar variable if you do not provide an alternative.</p>
+<p>Perl's looping directives (<a href="chapter_03.xhtml#looping_directives">Looping Directives</a>(looping_directives)) also set <code>$_</code>, such as <code>for</code> iterating over a list:</p>
+<div class="programlisting">
+<pre>
+<code>    say "#<b>$_</b>" for 1 .. 10;
+
+    for (1 .. 10)
+    {
+        say "#<b>$_</b>";
+    }</code>
+</pre></div>
+<p>... or <code>while</code>:</p>
+<div class="programlisting">
+<pre>
+<code>    while (&lt;STDIN&gt;)
+    {
+        chomp;
+        say scalar reverse;
+    }</code>
+</pre></div>
+<p>... or <code>map</code> transforming a list:</p>
+<div class="programlisting">
+<pre>
+<code>    my @squares = map { <b>$_</b> * <b>$_</b> } 1 .. 10;
+    say for @squares;</code>
+</pre></div>
+<p>... or <code>grep</code> filtering a list:</p>
+<div class="programlisting">
+<pre>
+<code>    say 'Brunch time!' if grep { /pancake mix/ } @pantry;</code>
+</pre></div>
+<p>If you call functions within code that uses <code>$_</code> whether implicitly or explicitly, they may overwrite the value of <code>$_</code>. Similarly, if you write a function which uses <code>$_</code>, you may clobber a caller function's use of <code>$_</code>. Perl 5.10 allows you to use <code>my</code> to declare <code>$_</code> as a lexical variable, which prevents this clobbering behavior. Be wise.</p>
+<div class="programlisting">
+<pre>
+<code>    while (&lt;STDIN&gt;)
+    {
+        chomp;
+
+        # BAD EXAMPLE
+        my $munged = calculate_value( $_ );
+        say "Original: $_";
+        say "Munged  : $munged";
+    }</code>
+</pre></div>
+<p>In this example, if <code>calculate_value()</code> or any other function it happened to call changed <code>$_</code>, it would remain changed throughout the <code>while</code> loop. Adding a <code>my</code> declaration prevents that behavior:</p>
+<div class="programlisting">
+<pre>
+<code>    while (my $_ = &lt;STDIN&gt;)
+    {
+        ...
+    }</code>
+</pre></div>
+<p>Of course, using a named lexical can be just as clear:</p>
+<div class="programlisting">
+<pre>
+<code>    while (my $line = &lt;STDIN&gt;)
+    {
+        ...
+    }</code>
+</pre></div>
+<p>Use <code>$_</code> as you would the word "it" in formal writing: sparingly, in small and well-defined scopes.</p>
+<h5 id="heading_id_10">The Default Array Variables</h5>
+<div id="idefault_array_variables_0"></div>
+<div id="ivariables__i64__0"></div>
+<div id="i64__0"></div>
+<p>While Perl has a single implicit scalar variable, it has two implicit array variables. Perl passes arguments to functions in an array named <code>@_</code>. Array manipulation operations (<a href="chapter_03.xhtml#arrays">Arrays</a>(arrays)) performed inside functions affect this array by default. Thus, these two snippets of code are equivalent:</p>
+<div class="programlisting">
+<pre>
+<code>    sub foo
+    {
+        my $arg = shift;
+        ...
+    }
+
+    sub foo_explicit
+    {
+        my $arg = shift @_;
+        ...
+    }</code>
+</pre></div>
+<p><i>Unlike</i> <code>$_</code>, Perl automatically localizes <code>@_</code> for you when you call other functions. The array operators <code>shift</code> and <code>pop</code> operate on <code>@_</code> with no other operands provided.</p>
+<div id="ivariables__i64ARGV_0"></div>
+<div id="i64ARGV_0"></div>
+<p>Outside of all functions, the default array variable <code>@ARGV</code> contains the command-line arguments to the program. The same array operators which use <code>@_</code> implicitly <i>within</i> functions use <code>@ARGV</code> implicitly outside of functions. You cannot use <code>@_</code> when you mean <code>@ARGV</code>.</p>
+<div id="inull_filehandle_0"></div>
+<p><code>ARGV</code> has one special case. If you read from the null filehandle <code>&lt;&gt;</code>, Perl will treat every element in <code>@ARGV</code> as the <i>name</i> of a file to open for reading. (If <code>@ARGV</code> is empty, Perl will read from standard input.) This implicit <code>@ARGV</code> behavior is useful for writing short programs, such as this command-line filter which reverses its input:</p>
+<div class="programlisting">
+<pre>
+<code>    while (&lt;&gt;)
+    {
+        chomp;
+        say scalar reverse;
+    }</code>
+</pre></div>
+<div class="sidebar">
+<p>Why <code>scalar</code>? <code>say</code> imposes list context on its operands. <code>reverse</code> passes its context on to its operands, treating them as a list in list context and a concatenated string in scalar context. If this sounds confusing, it can be: if Perl 5 arguably should have had different operators for these different operations.</p>
+</div>
+<p>If you run it with a list of files:</p>
+<div class="screen">
+<pre>
+<code>    $ <b>perl reverse_lines.pl encrypted/*.txt</b></code>
+</pre></div>
+<p>... the result will be one long stream of output. Without any arguments, you can provide your own standard input by piping in from another program or typing directly.</p>
+</body>
+</html>

File src/tutorials/modern-perl/xhtml/chapter_02.html

+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />
+<title></title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<link rel="stylesheet" href="../styles/style.css" type="text/css" />
+</head>
+<body>
+<h2 id="heading_id_2">Perl and Its Community</h2>
+<p>One of Larry's main goals for Perl 5 was to encourage Perl development and evolution outside the core distribution. Perl 4 had several forks, because there was no easy way to connect it to a relational database, for example. Larry wanted people to create and maintain their own extensions without fragmenting Perl into thousands of incompatible pidgins.</p>
+<p>You can add technical mechanisms for extensions, but you must also consider community aspects as well. Extensions and enhancements that no one shares are extensions and enhancements that everyone has to build and test and debug and maintain themselves. Yet shared extensions and libraries are worthless if you can't find them, or you can't enhance them, or you don't have permission to use them.</p>
+<p>Fortunately, the Perl community exists. It's strong and healthy. It welcomes willing participants at all levels--and not just for people who produce and share code. Consider taking advantage of the knowledge and experience of countless other Perl programmers, and to share your abilities as well.</p>
+<div id="perl_community"></div>
+<h3 id="heading_id_3">Community Sites</h3>
+<div id="iperl46org_0"></div>
+<div id="iwebsites__iperl46org_0"></div>
+<p>Perl's homepage is <span class="url">http://www.perl.org/</span>. This site hosts documentation, source code, tutorials, mailing lists, and several important community projects. If you're new to Perl, the Perl beginners mailing list is a friendly place to ask novice questions and get accurate and helpful answers. See <span class="url">http://beginners.perl.org/</span>.</p>
+<p>An important subdomain of note is <span class="url">http://dev.perl.org/</span>, a central site for core development of Perl 5, Perl 6 <span class="footnote">(footnote: The main Perl 6 site is <span class="url">http://www.perl6.org/</span>)</span>, and even Perl 1.</p>
+<div id="iPerl_Pub_0"></div>
+<p>Perl.com publishes several articles and tutorials about Perl programming every month. Its archives reach back into the 20th century. See <span class="url">http://www.perl.com/</span>.</p>
+<div id="icpan46org_0"></div>
+<div id="iwebsites__icpan46org_0"></div>
+<p>The CPAN's (<a href="chapter_02.xhtml#cpan">The CPAN</a>(cpan)) central location is <span class="url">http://www.cpan.org/</span>, though experienced users spend more time on <span class="url">http://search.cpan.org/</span>. This central software distribution hub of reusable, free Perl code is an essential part of the Perl community.</p>
+<div id="iPerlMonks_0"></div>
+<p>PerlMonks, at <span class="url">http://perlmonks.org/</span>, is a venerable community site devoted to questions and answers and other discussions about Perl programming. It celebrated its tenth anniversary in December 2009, making it one of the longest lasting web communities dedicated to any programming language.</p>
+<div id="iuse_Perl59_0"></div>
+<div id="iblogs46perl46org_0"></div>
+<p>Several community sites offer news and commentary. The oldest is <span class="url">http://use.perl.org/</span>; it has offered custom journals for many years. Several well-known Perl hackers write there. <span class="url">http://blogs.perl.org/</span> is a newer community site dedicated to blogging about Perl.</p>
+<div id="iEnlightened_Perl_Organization_0"></div>
+<p>Other sites aggregate the Perl-related musings of Perl hackers, including <span class="url">http://perlsphere.net/</span>, <span class="url">http://planet.perl.org/</span>, and <span class="url">http://ironman.enlightenedperl.org/</span>. The latter is part of an initiative from the Enlightened Perl Organization (<span class="url">http://enlightenedperl.org/</span>) to increase the amount of and improve the quality of Perl publishing on the web.</p>
+<div id="iPerl_Buzz_0"></div>
+<p>Perl Buzz (<span class="url">http://perlbuzz.com/</span>) collects and republishes some of the most interesting and useful Perl news on a regular basis.</p>
+<h3 id="heading_id_4">Development Sites</h3>
+<div id="iRT_0"></div>
+<div id="iBest_Practical_0"></div>
+<p>Best Practical Solutions (<span class="url">http://bestpractical.com/</span>) maintains an installation of their popular request tracking system, RT, for CPAN authors as well as Perl 5 and Perl 6 development. Every CPAN distribution has its own RT queue, linked from <code>search.cpan.org</code> and available on <span class="url">http://rt.cpan.org/</span>. Perl 5 and Perl 6 have separate RT queues available on <span class="url">http://rt.perl.org/</span>.</p>
+<div id="iPerl_5_Porters_0"></div>
+<div id="ip5p_0"></div>
+<p>The Perl 5 Porters (also known as <i>p5p</i>) mailing list is the central point for Perl 5 development discussion. See <span class="url">http://lists.cpan.org/showlist.cgi?name=perl5-porters</span>.</p>
+<div id="iTPF_0"></div>
+<div id="iThe_Perl_Foundation_0"></div>
+<div id="iTPF_wiki_0"></div>
+<p>The Perl Foundation (<span class="url">http://www.perlfoundation.org/</span>) hosts a wiki for all things Perl 5. See <span class="url">http://www.perlfoundation.org/perl5</span>.</p>
+<div id="iGithub_0"></div>
+<div id="iGitpan_0"></div>
+<p>Many Perl hackers use Github (<span class="url">http://github.com/</span>) to host their projects <span class="footnote">(footnote: ... including the drafts of this book at <span class="url">http://github.com/chromatic/modern_perl_book/</span>))</span>. One particularly notable page on Github is Gitpan, which hosts Git repositories chronicling the complete history of every distribution on the CPAN. See <span class="url">http://github.com/gitpan/</span>.</p>
+<h3 id="heading_id_5">Events</h3>
+<div id="iYAPC_0"></div>
+<p>There are plenty of events in the physical world as well. The Perl community holds a lot of conferences, workshops, and seminars. In particular, the community-run YAPC--Yet Another Perl Conference--is a successful, local, low-cost conference model held on multiple continents. See <span class="url">http://yapc.org/</span>.</p>
+<p>The Perl Foundation wiki lists other events at <span class="url">http://www.perlfoundation.org/perl5/index.cgi?perl_events</span>.</p>
+<div id="iPerl_Mongers_0"></div>
+<p>There are also hundreds of local Perl Mongers groups which get together frequently for technical talks and social interaction. See <span class="url">http://www.pm.org/</span>.</p>
+<h3 id="heading_id_6">IRC</h3>
+<div id="iIRC_0"></div>
+<div id="iIRC__i35moose_0"></div>
+<div id="iIRC__i35catalyst_0"></div>
+<div id="iIRC__i35perl45help_0"></div>
+<div id="iIRC__i35perl_0"></div>
+<p>When Perl mongers aren't at local meetups or conferences or workshops, many collaborate and chat online through IRC, a textual group chat system from the early days of the Internet. Many of the most popular and useful Perl projects have their own IRC channels, such as <i>#moose</i> or <i>#catalyst</i>.</p>
+<p>The main server for Perl community is <span class="url">irc://irc.perl.org/</span>. Other notable channels include <i>#perl-help</i>, for general assistance on Perl programming, and <i>#perl-qa</i>, devoted to testing and other quality issues. Be aware that the channel <i>#perl</i> is <i>not</i> for general help--instead, it's a general purpose room for discussing whatever its participants want to discuss <span class="footnote">(footnote: ... and it's not often friendly to people who ask basic programming questions.)</span>.</p>
+<h3 id="heading_id_7">The CPAN</h3>
+<div id="cpan"></div>
+<p>Perl 5 is a pragmatic language. It'll help you get your work done. Yet the ever-pragmatic Perl community has extended that language and made their work available to the world. If you have a problem to solve, chances are someone's already uploaded code to the CPAN for it.</p>
+<p>The line between a modern language and its libraries is fuzzy. Is a language only syntax? Is it the core libraries? Is it the availability of external libraries and the ease at which you can use them within your own projects?</p>
+<div id="iCPAN_0"></div>
+<p>Regardless of how you answer those questions for any other language, modern Perl programming makes heavy use of the CPAN (<span class="url">http://www.cpan.org/</span>). The CPAN, or Comprehensive Perl Archive Network, is an uploading and mirroring system for redistributable, reusable Perl code. It's one of--if not <i>the</i>--largest archives of libraries of code in the world.</p>
+<div id="idistribution_0"></div>
+<div id="imodule_0"></div>
+<p>CPAN mirrors <i>distributions</i>, which tend to be collections of reusable Perl code. A single distribution can contain one or more <i>modules</i>, or self-contained libraries of Perl code. Each distribution lives in its own namespace on the CPAN and contains its own metadata. You can build, install, test, and update each distribution. Distributions may depend on other distributions. For this reason, installing distributions through a CPAN client is often simpler than doing so manually.</p>
+<div class="sidebar">
+<p>The CPAN <i>adds</i> hundreds of registered contributors and thousands of indexed modules in hundreds of distributions every month. Those numbers do not take into account updates. In late April 2010, search.cpan.org reported 8156 uploaders, 81436 modules, and 20065 distributions.</p>
+</div>
+<div id="isearch46cpan46org_0"></div>
+<p>The CPAN itself is merely a mirroring service. Authors upload distributions containing modules, and the CPAN sends them to mirror sites, from which users and CPAN clients download, configure, build, test, and install distributions. Yet the CPAN has succeeded because of this simplicity, and because of the contributions of thousands of volunteers who've built on this distribution system to produce something greater. In particular, community standards have evolved to identify the attributes and characteristics of well-formed CPAN distributions. These include:</p>
+<ul>
+<li>Standards for installation to work with automated CPAN installers.</li>
+<li>Standards for metadata to describe what each distribution includes and any dependencies of the distribution.</li>
+<li>Standards for documentation and licensing to describe what the distribution does and how you may use it.</li>
+</ul>
+<p>Additional CPAN services provide comprehensive automated testing and reporting of every CPAN distribution for adherence to packaging and distribution guidelines and correctness of behavior on various platforms and versions of Perl. Every CPAN distribution has its own ticket queue on <span class="url">http://rt.cpan.org/</span> for reporting bugs and working with authors. Distributions also have historical versions available on the CPAN, ratings, annotations for the documentation, and other useful information. All of this is available from <span class="url">http://search.cpan.org/</span>.</p>
+<div id="iCPAN46pm_0"></div>
+<div id="iCPANPLUS_0"></div>
+<p>Modern Perl installations include two clients to connect to, search, download, build, test, and install CPAN distributions, CPAN.pm and CPANPLUS. They behave equivalently; their use is a matter of taste. This book recommends the use of CPAN.pm solely due to its ubiquity.</p>
+<p>If you use a recent version of CPAN.pm (as of this writing, 1.9402 is the latest stable release), CPAN.pm configuration is largely decision-free. For any complete installation of Perl, you may start the client with:</p>
+<div class="programlisting">
+<pre>
+<code>    $ <b>cpan</b></code>
+</pre></div>
+<p>To install a distribution:</p>
+<div class="programlisting">
+<pre>
+<code>    $ <b>cpan Modern::Perl</b></code>
+</pre></div>
+<div id="iEric_Wilhelm_0"></div>
+<div id="iCPAN46pm__iconfiguration_0"></div>
+<div id="iCPAN46pm__itroubleshooting_0"></div>
+<p>Eric Wilhelm's excellent tutorial on configuring CPAN.pm (<span class="url">http://learnperl.scratchcomputing.com/tutorials/configuration/</span>) is worth reading twice, especially its section on troubleshooting.</p>
+<div class="sidebar">
+<p>Even though the CPAN client is a core module for the Perl 5 distribution, you may also have to install standard development tools such as a <code>make</code> utility and possibly a C compiler to install all of the distributions you want. Windows users, see Strawberry Perl (<span class="url">http://strawberryperl.com/</span>) and Strawberry Perl Professional. Mac OS X users need their developer tools installed. Unix and Unix-like users, consult your local system administrator.</p>
+</div>
+<p>For your work setting up a CPAN client and an environment to build and install distributions, you get access to libraries for everything from database access to profiling tools to protocols for almost every network device ever created to sound and graphics libraries and wrappers for shared libraries on your system.</p>
+<p>Modern Perl without the CPAN is just another language. Modern Perl with the CPAN is amazing.</p>
+<div class="sidebar">
+<p>Serious Perl developers often manage their own Perl library paths or even full installations. Several projects help to make this possible.</p>
+<p><code>App::cpanminus</code> is a new CPAN client with goals of speed, simplicity, and zero configuration. Installation is as easy as:</p>
+<div class="programlisting">
+<pre>
+<code>    $ curl -LO http://xrl.us/cpanm
+    $ chmod +x cpanm</code>
+</pre></div>
+<p><code>App::perlbrew</code> is a system to manage and to switch between your own installations of multiple versions and configurations of Perl. Installation is as easy as:</p>
+<div class="programlisting">
+<pre>
+<code>    $ curl -LO http://xrl.us/perlbrew
+    $ chmod +x perlbrew
+    $ ./perlbrew install
+    $ perldoc App::perlbrew</code>
+</pre></div>
+<p>The <code>local::lib</code> CPAN distribution allows you to install and to manage distributions in your own user directory, rather than for the system as a whole. This is an effective way to maintain CPAN distributions without affecting other users. Installation is somewhat more involved than the previous two distributions. See <span class="url">http://search.cpan.org/perldoc?local::lib</span> for more details.</p>
+<p>All three distributions projects tend to assume a Unix-like environment (such as a GNU/Linux distribution or even Mac OS X). Windows users, see the Padre all-in-one download (<span class="url">http://padre.perlide.org/download.html</span>).</p>
+</div>
+</body>
+</html>

File src/tutorials/modern-perl/xhtml/chapter_03.html

+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta name="generator" content="HTML Tidy for Linux/x86 (vers 25 March 2009), see www.w3.org" />
+<title></title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<link rel="stylesheet" href="../styles/style.css" type="text/css" />
+</head>
+<body>
+<h2 id="heading_id_2">The Perl Language</h2>
+<p>The Perl language has several smaller parts which combine to form its syntax. Unlike spoken language, where nuance and tone of voice and intuition allow people to communicate despite slight misunderstandings and fuzzy concepts, computers and source code require precision. You can write effective Perl code without knowing every detail of every language feature, but you must understand how they work together to write Perl code well.</p>
+<h3 id="heading_id_3">Names</h3>
+<div id="names"></div>
+<div id="inames_0"></div>
+<div id="iidentifiers_0"></div>
+<p><i>Names</i> (or <i>identifiers</i>) are everywhere in Perl programs: variables, functions, packages, classes, and even filehandles have names. These names all start with a letter or an underscore. They may optionally include any combination of letters, numbers, and underscores. When the <code>utf8</code> pragma (<a href="chapter_09.xhtml#pragmas">Pragmas</a>(pragmas), <a href="chapter_03.xhtml#unicode">Unicode and Strings</a>(unicode)) is in effect, you may use any valid UTF-8 characters in identifiers. These are all valid Perl identifiers:</p>
+<div class="programlisting">
+<pre>
+<code>    my $name;
+    my @_private_names;
+    my %Names_to_Addresses;
+
+    sub anAwkwardName3;
+
+    # with <code>use utf8;</code> enabled
+    package Ingy::Döt::Net;</code>
+</pre></div>
+<p>These are invalid Perl identifiers:</p>
+<div class="programlisting">
+<pre>
+<code>    my $invalid name;
+    my @3;
+    my %~flags;
+
+    package a-lisp-style-name;</code>
+</pre></div>
+<div id="isymbolic_lookups_0"></div>
+<p>These rules only apply to names which appear in literal form in source code; that is, if you've typed it directly like <code>sub fetch_pie</code> or <code>my $wafflemaker</code>.</p>
+<p>Perl's dynamic nature makes it possible to refer to entities with names generated at runtime or provided as input to a program. These are <i>symbolic lookups</i>. You get more flexibility this way at the expense of some safety. In particular, invoking functions or methods indirectly or looking up namespaced symbols lets you bypass Perl's parser, which is the only part of Perl that enforces these grammatic rules. Be aware that doing so can produce confusing code; a hash (<a href="chapter_03.xhtml#hashes">Hashes</a>(hashes)) or nested data structure (<a href="chapter_03.xhtml#nested_data_structures">Nested Data Structures</a>(nested_data_structures)) is often clearer.</p>
+<h4 id="heading_id_4">Variable Names and Sigils</h4>
+<div id="ivariables__inames_0"></div>
+<div id="iscalar_variables_0"></div>
+<div id="ivariables__iscalars_0"></div>
+<div id="iscalars_0"></div>
+<div id="iarray_variables_0"></div>
+<div id="ivariables__iarrays_0"></div>
+<div id="ihash_variables_0"></div>
+<div id="ivariables__ihashes_0"></div>
+<p><i>Variable names</i> always have a leading sigil which indicates the type of the variable's value. <i>Scalar variables</i> (<a href="chapter_03.xhtml#scalars">Scalars</a>(scalars)) have a leading dollar sign (<code>$</code>) character. <i>Array variables</i> (<a href="chapter_03.xhtml#arrays">Arrays</a>(arrays)) have a leading at sign (<code>@</code>) character. <i>Hash variables</i> (<a href="chapter_03.xhtml#hashes">Hashes</a>(hashes)) have a leading percent sign (<code>%</code>) character:</p>
+<div class="programlisting">
+<pre>
+<code>    my $scalar;
+    my @array;
+    my %hash;</code>
+</pre></div>
+<p>These sigils offer a sort of namespacing for the variables, where it's possible (though often confusing) to have variables of the same name but different types:</p>
+<div class="programlisting">
+<pre>
+<code>    my ($bad_name, @bad_name, %bad_name);</code>
+</pre></div>
+<p>Perl won't get confused, but people reading the code will.</p>
+<div id="ivariant_sigils_0"></div>
+<p>Perl 5 uses <i>variant sigils</i>, where the sigil on a variable may change depending on what you do with it. For example, to access a (scalar) element of an array or a hash, the sigil changes to the dollar sign (<code>$</code>) character:</p>
+<div class="programlisting">
+<pre>
+<code>    my $hash_element  = $hash{ $key };
+    my $array_element = $array[ $index ]
+
+    $hash{ $key }     = 'value';
+    $array[ $index ]  = 'item';</code>
+</pre></div>
+<div id="ilvalue_0"></div>
+<div id="irvalue_0"></div>
+<p>In the latter two lines, using a scalar element of an aggregate as an <i>lvalue</i> (the target of an assignment, on the left side of the <code>=</code> character) imposes scalar context (<a href="chapter_01.xhtml#context_philosophy">Context</a>(context_philosophy)) on the <i>rvalue</i> (the value assigned, on the right side of the <code>=</code> character).</p>
+<div id="islicing_0"></div>
+<p>Similarly, accessing multiple elements of a hash or an array--an operation known as <i>slicing</i>--uses the at symbol (<code>@</code>) as the leading sigil and enforces list context when used as an lvalue:</p>
+<div class="programlisting">
+<pre>
+<code>    my @hash_elements  = @hash{ @keys };
+    my @array_elements = @array[ @indexes ];</code>
+</pre></div>
+<div class="author">
+<p>Add an example that uses a hash slice as an lvalue? I.E.</p>
+<p>my %hash; @hash{ @keys } = @values;</p>
+</div>
+<p>The most reliable way to determine the type of a variable--scalar, array, or hash--is to look at the operations performed on it. Scalars support all basic operations, such as string, numeric, and boolean manipulations. Arrays support indexed access through square brackets. Hashes support keyed access through curly brackets.</p>
+<h4 id="heading_id_5">Package-Qualified Names</h4>
+<div id="ifully45qualified_name_0"></div>
+<p>Occasionally you may need to refer to functions or variables in a separate namespace. Often you will need to refer to a class by its <i>fully-qualified name</i>. These names are collections of package names joined by double colons (<code>::</code>). That is, <code>My::Fine::Package</code> refers to a logical collection of variables and functions.</p>
+<p>While the standard naming rules apply to package names, by convention user-defined packages all start with uppercase letters. The Perl core reserves lowercase package names for built-in pragmas (<a href="chapter_09.xhtml#pragmas">Pragmas</a>(pragmas)), such as <code>strict</code> and <code>warnings</code>. This is a policy enforced by community guidelines, rather than technical mechanisms.</p>
+<p>Namespaces do not nest in Perl 5. The relationship between between <code>Some::Package</code> and <code>Some::Package::Refinement</code> is only a storage mechanism, with no further implications on the relationships between parent and child or sibling packages. When Perl looks up a symbol in <code>Some::Package::Refinement</code>, it looks in the <code>main::</code> symbol table for a symbol representing the <code>Some::</code> namespace, then in there for the <code>Package::</code> namespace, and so on. It's your responsibility to make any <i>logical</i> relationships between entities obvious when you choose names and organize your code.</p>
+<h3 id="heading_id_6">Variables</h3>
+<div id="variables"></div>
+<div id="ivariable_0"></div>
+<p>A <i>variable</i> in Perl is a storage location for a value (<a href="chapter_03.xhtml#values">Values</a>(values)). You can work with values directly, but all but the most trivial code works with variables. A variable is a level of indirection; it's easier to explain the Pythagorean theorem in terms of the variables <code>a</code>, <code>b</code>, and <code>c</code> than with the side lengths of every right triangle you can imagine. This may seem basic and obvious, but to write robust, well-designed, testable, and composable programs, you must identify and exploit points of genericity wherever possible.</p>
+<h4 id="heading_id_7">Variable Naming</h4>
+<div id="ivariables__inames_1"></div>
+<p>Not all variables require names, but most of the variables you will encounter in your programs will have names. Variables in Perl 5 must conform to the standard rules of identifier naming (<a href="chapter_03.xhtml#names">Names</a>(names)). Variables also have leading sigils.</p>
+<div class="author">
+<p>the Variable Naming section seems quite redundant with the Variable Names and Sigils section from names.pod, which gets inserted directly above this section in chapter 3</p>
+</div>
+<h4 id="heading_id_8">Variable Scopes</h4>
+<div id="variable_scopes"></div>
+<div id="ivariables__iscope_0"></div>
+<div id="iscope_0"></div>
+<p>Variables also have visibility, depending on their scope (<a href="chapter_05.xhtml#scope">Scope</a>(scope)). Most of the variables you will encounter have lexical scope <a href="chapter_05.xhtml#lexical_scope">Lexical Scope</a>(lexical_scope)), as the expected visibility of these variables is local.</p>
+<p>Package variables have global visibility throughout the program, but they are only available when prefixed with the name of the containing package. For example:</p>
+<div class="programlisting">
+<pre>
+<code>    package Store::IceCream;
+
+    use vars '$num_flavors';
+    $num_flavors = 42;</code>
+</pre></div>
+<div id="ipragmas__ivars_0"></div>
+<div id="ivars_0"></div>
+<p>The <code>vars</code> pragma declares a global variable without violating the rules of the <code>strict</code> pragma. Within the <code>Store::IceCream</code> package, <code>$num_flavors</code> is available without a namespace prefix. Outside of that package, it is available as <code>$Store::IceCream::num_flavors</code>. If <code>$num_flavors</code> were a lexical variable (declared with <code>my</code>), it would not be available outside of the enclosing scope.</p>
+<div id="iour_0"></div>
+<div id="ikeywords__iour_0"></div>
+<p>You may also declare package variables with the <code>our</code> keyword. Unlike <code>vars</code> variables, these variables have a lexical scope:</p>
+<div class="programlisting">
+<pre>
+<code>    {
+        package Store::Candy;
+
+        our $jellybeans;
+    }
+
+    # $Store::Candy::jellybeans not visible as $jellybeans here</code>
+</pre></div>
+<p>... yet remember that files themselves have their own lexical scopes, where the <code>package</code> declaration on its own does not create a new scope:</p>
+<div class="programlisting">
+<pre>
+<code>    package Store::Toy;
+
+    our $discount = 0.10;
+
+    package Store::Music;
+
+    # $Store::Toy::discount still visible as $discount
+    say "Our current discount is $discount!";</code>
+</pre></div>
+<div class="sidebar">
+<div id="ilexicals__ipads_0"></div>
+<div id="ilexpads_0"></div>
+<div id="isymbol_tables_0"></div>
+<p>This difference in visibility between package variables and lexical variables is apparent in the different storage mechanisms of these variables within Perl 5 itself. Lexical variables get stored in <i>lexical pads</i> attached to scopes. Every new entry into such a lexical scope requires Perl to create a new pad to contain the values of the variables for that particular entry into the scope. (This is how a function can call itself and not clobber the values of existing variables.)</p>
+<p>Package variables have a storage mechanism called symbol tables. Each package has a single symbol table, and every package variable has an entry in this table. You can inspect and modify this symbol table from Perl; this is how importing works (<a href="chapter_05.xhtml#importing">Importing</a>(importing)).</p>
+</div>
+<h4 id="heading_id_9">Variable Sigils</h4>
+<div id="sigils"></div>
+<div id="ivariables__isigils_0"></div>
+<div id="isigils_0"></div>
+<p>In Perl 5, the sigil of the variable in a declaration determines the type of the variable, whether scalar, array, or hash. The sigil of the variable used to access the variable determines the type of access to its value. Sigils on variables vary depending on what you do to the variable. For example, you declare an array as <code>@values</code>. You access the first element--a single value--of the array with <code>$values[0]</code>. You access a list of values from the array with <code>@values[ @indices ]</code>. See the arrays (<a href="chapter_03.xhtml#arrays">Arrays</a>(arrays)) and hashes (<a href="chapter_03.xhtml#hashes">Hashes</a>(hashes)) sections for more.</p>
+<h4 id="heading_id_10">Anonymous Variables</h4>
+<div id="ianonymous_variables_0"></div>
+<div id="ivariables__ianonymous_0"></div>
+<p>Perl 5 variables do not <i>need</i> names; Perl can allocate storage space for variables without storing them in lexical pads or symbol tables. These are <i>anonymous</i> variables. The only way to access them is by reference (<a href="chapter_03.xhtml#references">References</a>(references)).</p>
+<h4 id="heading_id_11">Variables, Types, and Coercion</h4>
+<div id="ivariables__itypes_0"></div>
+<div id="itypes__icontainers_0"></div>
+<div id="itypes__ivariables_0"></div>
+<div id="icontainer_types_0"></div>
+<p>Perl 5 variables do not enforce types on their values. You may store a string in a variable in one line, append to that variable a number on the next, and reassign a reference to a function (<a href="chapter_03.xhtml#function_references">Function References</a>(function_references)) on the third. The types of the <i>values</i> is flexible (or dynamic), but the type of the <i>variable</i> is static. A scalar variable can only hold scalars. An array variable only contains lists. A hash variable must contain an even-sized list of key/value pairs.</p>
+<p>Assigning to a variable may cause coercion (<a href="chapter_03.xhtml#coercion">Coercion</a>(coercion)). The documented way to determine the number of entries in an array is to evaluate that array in scalar context (<a href="chapter_01.xhtml#context_philosophy">Context</a>(context_philosophy)). Because a scalar variable can only ever contain a scalar, assigning an array to a scalar imposes scalar context on the operation and produces the number of elements in the array:</p>
+<div class="programlisting">
+<pre>
+<code>    my $count = @items;</code>
+</pre></div>
+<p>The relationship between variable types, sigils, and context is vital to a proper understanding of Perl.</p>
+<h3 id="heading_id_12">Values</h3>
+<div id="values"></div>
+<p>Effective Perl programs depend on the accurate representation and manipulation of values.</p>
+<div id="ivariables_0"></div>
+<div id="ivariables_1"></div>
+<p>Computer programs contain <i>variables</i>: containers which hold <i>values</i>. Values are the actual data the programs manipulate. While it's easy to explain what that data might be--your aunt's name and address, the distance between your office and a golf course on the moon, or the weight of all cookies you've eaten in the past year--the rules regarding the format of that data are often strict. Writing an effective program often means understanding the best (simplest, fastest, most compact, or easiest) way of representing that data.</p>
+<p>While the structure of a program depends heavily on the means by which you model your data with appropriate variables, these variables would be meaningless if they couldn't accurately contain the data itself--the values.</p>
+<h4 id="heading_id_13">Strings</h4>
+<div id="istrings_0"></div>
+<p>A <i>string</i> is a piece of data with no particular formatting, no particular contents, and no semantic meaning beyond the fact that it's a string. It could be your name. It could be the contents of an image file read from your hard drive. It could be the Perl program itself. A string has no meaning to the program until you give it meaning. A string is a fixed amount of data delineated by quotes of some form, yet Perl strings can grow or shrink as you add to or remove from them.</p>
+<p>Most strings use either single or double quotes:</p>
+<div class="programlisting">
+<pre>
+<code>    my $name    = <b>'Donner Odinson, Bringer of Despair'</b>;
+    my $address = <b>"Room 539, Bilskirnir, Valhalla"</b>;</code>
+</pre></div>
+<div id="istrings__isingle45quoted_0"></div>
+<p>Characters in a <i>single-quoted string</i> represent themselves literally, with two exceptions. You may embed a single quote inside a single-quoted string by escaping the quote with a leading backlash:</p>
+<div class="programlisting">
+<pre>
+<code>    my $reminder = 'Don<b>\'</b>t forget to escape the single quote!';</code>
+</pre></div>
+<p>If a backslash occurs at the end of the string, escape it with another backslash, lest the Perl parser believe you are trying to escape the trailing single quote:</p>
+<div class="programlisting">
+<pre>
+<code>    my $exception = 'This string ends with a backslash, not a quote: \\';</code>
+</pre></div>
+<div class="author">
+<p>Is it worth mentioning \\ here? Like, what happens when you put \ or \\ or \\\ or \\' or \\\' in a single-quoted string?</p>
+</div>
+<div id="istrings__idouble45quoted_0"></div>
+<p>A <i>double-quoted string</i> has more complex (and often, more useful) behavior. For example, you may encode non-printable characters in the string:</p>
+<div class="programlisting">
+<pre>
+<code>    my $tab       = "<b>\t</b>";
+    my $newline   = "<b>\n</b>";
+    my $carriage  = "<b>\r</b>";
+    my $formfeed  = "<b>\f</b>";
+    my $backspace = "<b>\b</b>";</code>
+</pre></div>
+<p>A string declaration may cross logical newlines, such that these two strings are equivalent:</p>
+<div class="programlisting">
+<pre>
+<code>    my $escaped = "two\nlines";
+    my $literal = "two
+    lines";
+    is( $escaped, $literal, '\n and newline are equivalent' );</code>
+</pre></div>
+<p>You <i>can</i> enter these characters directly in the strings, but it's often difficult to see the visual distinction between one tab character and four (or two or eight) spaces.</p>
+<div id="iinterpolation_0"></div>
+<div id="istrings__iinterpolation_0"></div>
+<p>You may also <i>interpolate</i> the value of a scalar variable or the values of an array within a double-quoted string directly:</p>
+<div class="programlisting">
+<pre>
+<code>    my $factoid = "Did you know that <b>$name</b> lives at <b>$address</b>?";</code>
+</pre></div>
+<p>You may include a literal double-quote inside a double-quoted string by escaping it with a leading backslash:</p>
+<div class="programlisting">
+<pre>
+<code>    my $quote = "\"Ouch,\", he cried.  \"That <i>hurt</i>!\"";</code>
+</pre></div>
+<div id="iquoting_operators_0"></div>
+<div id="iq_0"></div>
+<div id="ioperators__iq_0"></div>
+<div id="iqq_0"></div>
+<div id="ioperators__iqq_0"></div>
+<p>If you find that hideously ugly, you may use an alternate <i>quoting operator</i>. The <code>q</code> operator performs single quoting, while the <code>qq</code> operator performs double quoting. In each case, you may choose your own delimiter for the string. The character immediately following the operator determines the beginning and end of the string. If the character is the opening character of a balanced pair--such as opening and closing braces--the closing character will be the final delimiter. Otherwise, the character itself will be both the starting and ending delimiter.</p>
+<div class="programlisting">
+<pre>
+<code>    my $quote     = <b>qq{</b>"Ouch", he said.  "That <i>hurt</i>!"<b>}</b>;
+    my $reminder  = <b>q^</b>Didn't need to escape the single quote!<b>^</b>;
+    my $complaint = <b>q{</b>It's too early to be awake.<b>}</b>;</code>
+</pre></div>
+<div id="iheredocs_0"></div>
+<div id="istrings__iheredocs_0"></div>
+<p>Even though you can declare a complex string with a series of embedded escape characters, sometimes it's easier to declare a multi-line string on multiple lines. The <i>heredoc</i> syntax lets you assign one or more lines of a string with a different syntax:</p>
+<div class="programlisting">
+<pre>
+<code>    my $blurb =&lt;&lt;'END_BLURB';
+
+    He looked up.  "Time is never on our side, my child.  Do you see the irony?
+    All they know is change.  Change is the constant on which they all can
+    agree.  Whereas we, born out of time to remain perfect and perfectly
+    self-aware, can only suffer change if we pursue it.  It is against our
+    nature.  We rebel against that change.  Shall we consider them greater
+    for it?"
+    END_BLURB</code>
+</pre></div>
+<p>The <code>&lt;&lt;'END_BLURB'</code> syntax has three parts. The double angle-brackets introduce the heredoc. The quotes determine whether the heredoc obeys single-quoted or double-quoted behavior with regard to variable and escape character interpolation. They're optional; the default behavior is double-quoted interpolation. The <code>END_BLURB</code> itself is an arbitrary identifier which the Perl 5 parser uses as the ending delimiter.</p>
+<p>Be careful; regardless of the indentation of the heredoc declaration itself, the ending delimiter <i>must</i> begin in the first column of the program:</p>
+<div class="programlisting">
+<pre>
+<code>    sub some_function {
+        my $ingredients =&lt;&lt;'END_INGREDIENTS';
+        Two eggs
+        One cup flour
+        Two ounces butter
+        One-quarter teaspoon salt
+        One cup milk
+        One drop vanilla
+        Season to taste
+    END_INGREDIENTS
+    }</code>
+</pre></div>
+<div class="sidebar">
+<p>If the identifier begins with whitespace, that same whitespace must be present exactly in the ending delimiter. Even if you do indent the identifier, Perl 5 will <i>not</i> remove equivalent whitespace from the start of each line of the heredoc.</p>
+</div>
+<p>You may use a string in other contexts, such as boolean or numeric; its contents will determine the resulting value (<a href="chapter_03.xhtml#coercion">Coercion</a>(coercion)).</p>
+<h4 id="heading_id_14">Unicode and Strings</h4>
+<div id="unicode"></div>
+<div id="iunicode_0"></div>
+<p><i>Unicode</i> is a system for representing characters in the world's written languages. While most English text uses a character set of only 127 characters (which requires seven bits of storage and fits nicely into eight-bit bytes), it's naïve to believe that you won't someday need an umlaut, for example.</p>
+<p>Perl 5 strings can represent either of two related but different data types:</p>
+<div id="icodepoint_0"></div>
+<ul>
+<li>Sequences of Unicode characters</li>
+<li style="list-style: none; display: inline">
+<p>The Unicode character set contains characters from the scripts of most languages, and various other symbols. Each character has a <i>codepoint</i>, a own unique number which identifies it in the Unicode character set.</p>
+</li>
+<li>Sequences of octets</li>
+<li style="list-style: none; display: inline">
+<p>Binary data is a sequence of 8 bit numbers, each of which can represent a number between 0 and 255.</p>
+</li>
+</ul>
+<p>Unicode strings and binary strings look very similar. They each have a <code>length()</code>, and you can perform standard string operations on them such as concatenation, splicing, and regular expression processing. Any string which is not purely binary data is textual data, and should be a sequence of Unicode characters.</p>
+<p>However, because of how your operating system represents data on disk or from users or over the network--as sequences of octets--Perl can't know if the data you read is an image file or a text document or anything else. By default, Perl treats all incoming data as sequences of octets. Any additional meaning of the string's contents are your responsibility.</p>
+<h5 id="heading_id_15">Character Encodings</h5>
+<div id="iencoding_0"></div>
+<div id="iUnicode__iencoding_0"></div>
+<div id="iUTF458_0"></div>
+<div id="iASCII_0"></div>
+<div id="iLatin451_0"></div>
+<p>A Unicode string is a sequence of octets which represent a sequence of characters. A <i>Unicode encoding</i> maps octet sequences to characters. Some encodings, such as UTF-8, can encode all of the characters in the Unicode character set. Others, represent a subset of Unicode characters. For example, ASCII encodes plain English text with no accented characters and Latin-1 can represent text in most languages which use the Latin script.</p>
+<p>If you always decode to and from the appropriate encoding at the inputs and outputs of your program, you will avoid many problems.</p>
+<h5 id="heading_id_16">Unicode in Your Filehandles</h5>
+<div id="iopen_0"></div>
+<div id="iIO_layers_0"></div>
+<p>One source of Unicode input is filehandles. If you tell Perl that a specific filehandle works with encoded text, Perl can convert the data to Unicode strings automatically. To do this, add a IO layer to the mode of the <code>open</code> builtin. An <i>IO layer</i> wraps around input or output and performs some sort of conversion of the data. In this case, the <code>:utf8</code> layer performs decoding of UTF-8 data:</p>
+<div class="programlisting">
+<pre>
+<code>    use autodie;
+
+    open my $fh, '&lt;:utf8', $textfile;
+
+    my $unicode_string = &lt;$fh&gt;;</code>
+</pre></div>
+<div id="ibinmode_0"></div>
+<div id="ibuiltins__ibinmode_0"></div>
+<p>You may also modify an existing filehandle with <code>binmode</code>:</p>
+<div class="programlisting">
+<pre>
+<code>    binmode $fh, ':utf8';</code>
+</pre></div>
+<p>This works for input as well as output:</p>
+<div class="programlisting">
+<pre>
+<code>    binmode STDOUT, ':utf8';
+    say $unicode_string;</code>
+</pre></div>
+<p>Without the <code>utf8</code> mode, printing Unicode strings to a filehandle will result in a warning (<code>Wide character in %s</code>), because files contain octets, not Unicode characters.</p>
+<h5 id="heading_id_17">Unicode in Your Data</h5>
+<div id="iEncode_0"></div>
+<div id="iEncode__idecode4041_0"></div>
+<div id="iEncode__iencode4041_0"></div>
+<p>The core module <code>Encode</code> provides a function named <code>decode()</code> to convert a scalar containing data in a known format to a Unicode string. For example, if you have UTF-8 data:</p>
+<div class="programlisting">
+<pre>
+<code>    my $string = decode('utf8', $data);</code>
+</pre></div>
+<p>The corresponding <code>encode()</code> function converts from Perl's internal encoding to the desired output encoding:</p>
+<div class="programlisting">
+<pre>
+<code>    my $latin1 = encode('iso-8859-1', $string);</code>
+</pre></div>
+<h5 id="heading_id_18">Unicode in Your Programs</h5>
+<div id="ipragmas__iutf8_0"></div>
+<div id="iutf8_0"></div>
+<div id="iencoding_1"></div>
+<p>You may include Unicode characters in your programs in three ways. The easiest is to use the <code>utf8</code> pragma (<a href="chapter_09.xhtml#pragmas">Pragmas</a>(pragmas)), which tells the Perl parser to interpret the rest of the source code file with the UTF-8 encoding This allows you to use Unicode characters in strings as well in identifiers:</p>
+<div class="programlisting">
+<pre>
+<code>    use utf8;
+
+    sub £_to_¥ { ... }
+
+    my $pounds = £_to_¥('1000£');</code>
+</pre></div>
+<p>To <i>write</i> this code, your text editor must understand UTF-8 and you must save the file with the appropriate encoding.</p>
+<div id="iescapes__i_x123125_0"></div>
+<div id="i_x123125_0"></div>
+<p>You may also use the Unicode escape sequence to represent character encodings. The syntax <code>\x{}</code> represents a single character; place the hex form of the character's Unicode number within the curly brackets:</p>
+<div class="programlisting">
+<pre>
+<code>    my $escaped_thorn = "\x{00FE}";</code>
+</pre></div>
+<div class="sidebar">
+<p>Note that these escapes interpolate only within double-quoted strings.</p>
+</div>
+<div id="icharnames_pragma_0"></div>
+<div id="ipragmas__icharnames_0"></div>
+<div id="iescapes__i_N123125_0"></div>
+<div id="i_N123125_0"></div>
+<p>Some Unicode characters have names. Though these are more verbose, they can be clearer to read than Unicode numbers. You must use the <code>charnames</code> pragma to enable them. Use the <code>\N{}</code> escape to refer to them:</p>
+<div class="programlisting">
+<pre>
+<code>    use charnames ':full';
+    use Test::More tests =&gt; 1;
+
+    my $escaped_thorn = "\x{00FE}";
+    my $named_thorn   = "\N{LATIN SMALL LETTER THORN}";
+
+    is( $escaped_thorn, $named_thorn, 'Thorn equivalence check' );</code>
+</pre></div>
+<p>You may use the <code>\x{}</code> and <code>\N{}</code> forms within regular expressions as well as anywhere else you may legitimately use a string or a character.</p>
+<h5 id="heading_id_19">Implicit Conversion</h5>
+<p>Most Unicode problems in Perl arise from the fact that a string could be either a sequence of octets or a sequence of characters. Perl allows you to combine these types through the use of implicit conversions. When these conversions are wrong, they're rarely <i>obviously</i> wrong.</p>
+<p>When Perl concatenates a sequences of octets with a sequence of Unicode characters, it implicitly decodes the octet sequence using the Latin-1 encoding. The resulting string contains Unicode characters. When you print Unicode characters, Perl encodes the string using UTF-8, because Latin-1 cannot represent the entire set of Unicode characters.</p>
+<p>This asymmetry can lead to Unicode strings encoded as UTF-8 for output and decoded as Latin-1 when input.</p>
+<p>Worse yet, when the text contains only English characters with no accents, the bug hides--because both encodings have the same representation for every such character.</p>
+<div class="programlisting">
+<pre>
+<code>    my $hello    = "Hello, ";
+    my $greeting = $hello . $name;</code>
+</pre></div>
+<p>If <code>$name</code> contains an English name such as <code>Alice</code> you will never notice any problem, because the Latin-1 representation is the same as the UTF-8 representation.</p>
+<p>If, on the other hand, <code>$name</code> contains a name like <code>José</code>, <code>$name</code> can contain several possible values:</p>
+<ul>
+<li><code>$name</code> contains four Unicode characters.</li>
+<li><code>$name</code> contains four Latin-1 octets representing four Unicode characters.</li>
+<li><code>$name</code> contains five UTF-8 octets representing four Unicode characters.</li>
+</ul>
+<p>The string literal has several possible scenarios:</p>
+<ul>
+<li>ASCII string literal</li>
+<li style="list-style: none; display: inline">
+<div class="programlisting">
+<pre>
+<code>    my $hello = "Hello, ";</code>
+</pre></div>
+<p>The string literal contains octets.</p>
+</li>
+<li>Latin-1 string literal with no explicit encoding, such as:</li>
+<li style="list-style: none; display: inline">
+<div class="programlisting">
+<pre>
+<code>    my $hello = "¡Hola, ";</code>
+</pre></div>
+<p>The string literal contains octets.</p>
+</li>
+<li>Non ASCII string literal with the <code>utf8</code> or <code>encoding</code> pragma:</li>
+<li style="list-style: none; display: inline">
+<div class="programlisting">
+<pre>
+<code>    use utf8;
+    my $hello = "שלום, ";</code>
+</pre></div>
+<p>The string literal contains Unicode characters.</p>
+</li>
+</ul>
+<p>If both <code>$hello</code> and <code>$name</code> are Unicode strings, the concatenation will produce another Unicode string.</p>
+<p>If both strings are octet streams, Perl will concatenate them into a new octet string. If both values are octets of the same encoding--both Latin-1, for example, the concatenation will work correctly. If the octets do not share an encoding, the concatenation append UTF-8 data to Latin-1 data, producing a sequence of octets which makes sense in <i>neither</i> encoding. This could happen if the user entered a name as UTF-8 data and the greeting were a Latin-1 string literal, but the program decoded neither.</p>
+<p>If only one of the values is a Unicode string, Perl will decode the other as Latin-1 data. If this is not the correct encoding, the resulting Unicode characters will be wrong. For example, if the user input were UTF-8 data and the string literal were a Unicode string, the name will be incorrectly decoded into five Unicode characters <code>José</code> instead of <code>José</code> because the UTF-8 data means something else when decoded as Latin-1 data.</p>
+<p>See <code>perldoc perluniintro</code> for far more detail Unicode, encodings, and how to manage incoming and outgoing data in a Unicode world.</p>
+<h4 id="heading_id_20">Numbers</h4>
+<div id="inumbers_0"></div>
+<div id="iintegers_0"></div>
+<div id="ifloating45point_values_0"></div>
+<p>Perl also supports numbers, both integers and floating-point values. They support scientific notation as well as binary, octal, and hexadecimal representations:</p>
+<div class="programlisting">
+<pre>
+<code>    my $integer   = 42;
+    my $float     = 0.007;
+    my $sci_float = 1.02e14;
+    my $binary    = <b>0b</b>101010;
+    my $octal     = <b>0</b>52;
+    my $hex       = <b>0x</b>20;</code>
+</pre></div>
+<p>The emboldened characters are the numeric prefixes for binary, octal, and hex notation respectively. Be aware that the leading zero always indicates octal mode; this can occasionally produce unanticipated confusion.</p>
+<div id="inumbers__iunderscore_separator_0"></div>
+<div id="iunderscore_0"></div>
+<div class="sidebar">
+<p>Even though you can represent floating-point values explicitly in Perl 5 with perfect accuracy, Perl 5 stores them internally in a binary format. Comparing floating-point values is sometimes imprecise in specific ways; consult <code>perldoc perlnumber</code> for more details.</p>
+</div>
+<p>You may not use commas to separate thousands in numeric literals because the parser will interpret the commas as comma operators. You <i>can</i> use underscores in other places within the number, however. The parser will treat them as invisible characters. Your readers may not. These are equivalent:</p>
+<div class="programlisting">
+<pre>
+<code>    my $billion = 1000000000;
+    my $billion = 1_000_000_000;
+    my $billion = 10_0_00_00_0_0_0;</code>
+</pre></div>
+<p>Consider the most readable alternative, however.</p>
+<div id="iScalar5858Util_0"></div>
+<div id="ilooks_like_number_0"></div>
+<div id="iScalar5858Util__ilooks_like_number_0"></div>
+<p>Because of coercion (<a href="chapter_03.xhtml#coercion">Coercion</a>(coercion)), Perl programmers rarely have to worry about converting text read from outside the program to numbers. Perl will treat anything which looks like a number as a number in numeric contexts. Even though it almost always does so correctly, occasionally it's useful to know if something really does look like a number. The core module <code>Scalar::Util</code> contains a function named <code>looks_like_number</code> which returns a true value if Perl will consider the given argument numeric.</p>
+<div id="iRegexp5858Common_0"></div>
+<p>The <code>Regexp::Common</code> module from the CPAN also provides several well-tested regular expressions to identify valid <i>types</i> (whole number, integer, floating-point value) of numeric values.</p>
+<h4 id="heading_id_21">Undef</h4>
+<div id="iundef_0"></div>
+<p>Perl 5 has a value which represents an unassigned, undefined, and unknown value: <code>undef</code>. Declared but undefined scalar variables contain <code>undef</code>:</p>
+<div class="programlisting">
+<pre>
+<code>    my $name = undef;   # unnecessary assignment
+    my $rank;           # also contains undef</code>
+</pre></div>
+<div id="iundef__icoercions_0"></div>
+<p><code>undef</code> evaluates to false in boolean context. Interpolating <code>undef</code> into a string--or evaluating it in a string context--produces an <code>uninitialized value</code> warning:</p>
+<div class="programlisting">
+<pre>
+<code>    my $undefined;
+    my $defined = $undefined . '... and so forth';</code>
+</pre></div>
+<p>... produces:</p>
+<div class="screen">
+<pre>
+<code>    Use of uninitialized value $undefined in concatenation (.) or string...</code>
+</pre></div>
+<h4 id="heading_id_22">The Empty List</h4>
+<div id="iempty_list_0"></div>
+<div id="i4041_0"></div>
+<p>When used on the right-hand side of an assignment, the <code>()</code> construct represents an empty list. When evaluated in scalar context, this evaluates to <code>undef</code>. In list context, it is effectively an empty list.</p>
+<p>When used on the left-hand side of an assignment, the <code>()</code> construct enforces list context. To count the number of elements returned from an expression in list context without using a temporary variable, you use the idiom (<a href="chapter_10.xhtml#idioms">Idioms</a>(idioms)):</p>
+<div class="programlisting">
+<pre>
+<code>    my $count = <b>()</b> = get_all_clown_hats();</code>
+</pre></div>
+<p>Because of the right associativity (<a href="chapter_04.xhtml#associativity">Associativity</a>(associativity)) of the assignment operator, Perl first evaluates the second assignment by calling <code>get_all_clown_hats()</code> in list context. This produces a list.</p>
+<p>Assignment to the empty list throws away all of the values of the list, but that assignment takes place in scalar context, which evaluates to the number of items on the right hand side of the assignment. As a result, <code>$count</code> contains the number of elements in the list returned from <code>get_all_clown_hats()</code>.</p>
+<p>You don't have to understand all of the implications of this code right now, but it does demonstrate how a few of Perl's fundamental design features can combine to produce interesting and useful behavior.</p>
+<h4 id="heading_id_23">Lists</h4>
+<div id="ilists_0"></div>
+<p>A list is a comma-separated group of one or more expressions.</p>
+<p>Lists may occur verbatim in source code as values:</p>
+<div class="programlisting">
+<pre>
+<code>    my @first_fibs = (1, 1, 2, 3, 5, 8, 13, 21);</code>
+</pre></div>
+<p>... as targets of assignments:</p>
+<div class="programlisting">
+<pre>
+<code>    my ($package, $filename, $line) = caller();</code>
+</pre></div>
+<p>... or as lists of expressions:</p>
+<div class="programlisting">
+<pre>
+<code>    say name(), ' =&gt; ', age();</code>
+</pre></div>
+<p>Note that you do not need parentheses to <i>create</i> lists; where present, the parentheses in these examples group expressions to change the <i>precedence</i> of those expressions (<a href="chapter_04.xhtml#precedence">Precedence</a>(precedence)).</p>
+<div id="ioperators__irange_0"></div>
+<div id="ioperators__i4646_0"></div>
+<div id="i4646_0"></div>
+<div id="irange_operator_0"></div>
+<p>You may use the range operator to create lists of literals in a compact form:</p>
+<div class="programlisting">
+<pre>
+<code>    my @chars = 'a' .. 'z';
+    my @count = 13 .. 27;</code>
+</pre></div>
+<div id="iqw4041_0"></div>
+<div id="ioperators__iqw4041_0"></div>
+<div id="iqw4041_operator_0"></div>
+<p>... and you may use the <code>qw()</code> operator to split a literal string on whitespace to produce a list of strings:</p>
+<div class="programlisting">
+<pre>
+<code>    my @stooges = qw( Larry Curly Moe Shemp Joey Kenny );</code>
+</pre></div>
+<div class="sidebar">
+<p>Perl will produce a warning if a <code>qw()</code> contains a comma or the comment character (<code>#</code>), because not only are such characters rarely included in a <code>qw()</code>, their presence usually indicates an oversight.</p>
+</div>
+<p>Lists can (and often do) occur as the results of expressions, but these lists do not appear literally in source code.</p>
+<p>Lists and arrays are not interchangeable in Perl. Lists are values and arrays are containers. You may store a list in an array and you may coerce an array to a list, but they are separate entities. For example, indexing into a list is always occurs in list context. Indexing into an array can occur in scalar context (for a single element) or list context (for a slice):</p>
+<div class="programlisting">
+<pre>
+<code>    # you do not need to understand this all
+    sub context
+    {
+        my $context = wantarray;
+        say defined $context
+             ? $context
+                 ? 'list'
+                 : 'scalar'
+             : 'void';
+        return 0;
+    }
+
+    my @list_slice  = (1, 2, 3)[context()];
+    my @array_slice = @list_slice[context()];
+    my $array_index = $array_slice[context()];</code>
+</pre></div>
+<h3 id="heading_id_24">Control Flow</h3>
+<div id="control_flow"></div>
+<div id="icontrol_flow_0"></div>
+<p>Perl's basic <i>control flow</i> is straightforward. Program execution starts at the beginning (the first line of the file executed) and continues to the end:</p>
+<div class="programlisting">
+<pre>
+<code>    say 'At start';
+    say 'In middle';
+    say 'At end';</code>
+</pre></div>
+<div id="icontrol_flow_directives_0"></div>
+<p>Most programs need more complex control flow. Perl's <i>control flow directives</i> change the order of execution--what happens next in the program--depending on the values of arbitrarily complex expressions.</p>
+<h4 id="heading_id_25">Branching Directives</h4>
+<div id="iif_0"></div>
+<div id="icontrol_flow_directives__iif_0"></div>
+<div id="ipostfix_if_0"></div>
+<div id="ipostfix_expressions__iif_0"></div>
+<p>The <code>if</code> directive evaluates a conditional expression and performs the associated action only when the conditional expression evaluates to a true value:</p>
+<div class="programlisting">
+<pre>
+<code>    say 'Hello, Bob!' if $name eq 'Bob';</code>
+</pre></div>
+<p>This postfix form is useful for simple expressions. A block form groups multiple expressions into a single unit:</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name eq 'Bob')
+    {
+        say 'Hello, Bob!';
+        found_bob();
+    }</code>
+</pre></div>
+<p>Note that the postfix form does not <i>require</i> parentheses around its condition, while the block form does. The conditional expression may also be complex:</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name eq 'Bob' &amp;&amp; not greeted_bob())
+    {
+        say 'Hello, Bob!';
+        found_bob();
+    }</code>
+</pre></div>
+<p>... though in this case, parenthesization of the expression in postfix form may add clarity <span class="footnote">(footnote: It may also argue against using the postfix form.)</span>:</p>
+<div class="programlisting">
+<pre>
+<code>    greet_bob() if ($name eq 'Bob' &amp;&amp; not greeted_bob());</code>
+</pre></div>
+<div id="iunless_0"></div>
+<div id="icontrol_flow_directives__iunless_0"></div>
+<div id="ipostfix_unless_0"></div>
+<div id="ipostfix_expressions__iunless_0"></div>
+<p>The <code>unless</code> directive is a negated form of <code>if</code>. Perl will perform the requested action when the conditional expression evaluates to <i>false</i>:</p>
+<div class="programlisting">
+<pre>
+<code>    say "You're no Bob!" unless $name eq 'Bob';</code>
+</pre></div>
+<p>Like <code>if</code>, <code>unless</code> also has a block form. Unlike <code>if</code>, the block form of <code>unless</code> is much rarer than its postfix form:</p>
+<div class="programlisting">
+<pre>
+<code>    unless (is_leap_year() and is_full_moon())
+    {
+        frolic();
+        gambol();
+    }</code>
+</pre></div>
+<p><code>unless</code> works very well for postfix conditionals, especially parameter validation in functions (<a href="chapter_10.xhtml#postfix_parameter_validation">Postfix Parameter Validation</a>(postfix_parameter_validation)):</p>
+<div class="programlisting">
+<pre>
+<code>    sub frolic
+    {
+        return unless @_;
+
+        for my $chant (@_)
+        {
+            ...
+        }
+    }</code>
+</pre></div>
+<p><code>unless</code> can be difficult to read with multiple conditions; this is one reason it appears rarely in its block form.</p>
+<div id="ielse_0"></div>
+<div id="icontrol_flow_directives__ielse_0"></div>
+<p>The block forms of <code>if</code> and <code>unless</code> both work with the <code>else</code> directive, which provides code to run when the conditional expression does not evaluate to true (for <code>if</code>) or false (for <code>unless</code>):</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name eq 'Bob')
+    {
+        say 'Hi, Bob!';
+        greet_user();
+    }
+    else
+    {
+        say "I don't know you.";
+        shun_user();
+    }</code>
+</pre></div>
+<p><code>else</code> blocks allow you to rewrite <code>if</code> and <code>unless</code> conditionals in terms of each other:</p>
+<div class="programlisting">
+<pre>
+<code>    <b>unless</b> ($name eq 'Bob')
+    {
+        say "I don't know you.";
+        shun_user();
+    }
+    else
+    {
+        say 'Hi, Bob!';
+        greet_user();
+    }</code>
+</pre></div>
+<p>If you read the previous example out loud, you may notice the awkward pseudocode phrasing: "Unless this name is Bob, do this. Otherwise, do something else." The implied double negative can be confusing. Perl provides both <code>if</code> and <code>unless</code> to allow you to phrase your conditionals in the most natural and readable way. Likewise, you can choose between positive and negative assertions with regard to the comparison operators you use:</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name <b>ne</b> 'Bob')
+    {
+        say "I don't know you.";
+        shun_user();
+    }
+    else
+    {
+        say 'Hi, Bob!';
+        greet_user();
+    }</code>
+</pre></div>
+<p>The double negative implied by the presence of the <code>else</code> block argues against this particular phrasing.</p>
+<div id="ielsif_0"></div>
+<div id="icontrol_flow_directives__ielsif_0"></div>
+<p>One or more <code>elsif</code> directives may follow an <code>if</code> block form and may precede any single <code>else</code>. You may use as many <code>elsif</code> blocks as you like, but you may not change the order in which the block types appear:</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name eq 'Bob')
+    {
+        say 'Hi, Bob!';
+        greet_user();
+    }
+    elsif ($name eq 'Jim')
+    {
+        say 'Hi, Jim!';
+        greet_user();
+    }
+    else
+    {
+        say "You're not my uncle.";
+        shun_user();
+    }</code>
+</pre></div>
+<p>You may also use the <code>elsif</code> block with an <code>unless</code> chain, but the resulting code may be unclear. There is no <code>elseunless</code>.</p>
+<p>There is no <code>else if</code> construct; this is a syntax error:</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name eq 'Rick')
+    {
+        say 'Hi, cousin!';
+    }
+
+    # warning; syntax error
+    else if ($name eq 'Kristen')
+    {
+        say 'Hi, cousin-in-law!';
+    }</code>
+</pre></div>
+<h4 id="heading_id_26">The Ternary Conditional Operator</h4>
+<div id="iternary_conditional_0"></div>
+<div id="icontrol_flow_directives__iternary_conditional_0"></div>
+<p>The <i>ternary conditional</i> operator offers an alternate approach to control flow. It evaluates a conditional expression and evaluates to one of two different results:</p>
+<div class="programlisting">
+<pre>
+<code>    my $time_suffix = after_noon($time) ? 'morning' : 'afternoon';</code>
+</pre></div>
+<p>The conditional expression precedes the question mark character (<code>?</code>) and the colon character (<code>:</code>) separates the alternatives. The alternatives are literals or (parenthesized) expressions of arbitrary complexity, including other ternary conditional expressions, though readability may suffer.</p>
+<div class="sidebar">
+<p>An interesting, though obscure, idiom is to use the ternary conditional to select between alternative <i>variables</i>, not only values:</p>
+<div class="programlisting">
+<pre>
+<code>    push @{ rand() &gt; 0.5 ? \@red_team : \@blue_team }, Player-&gt;new();</code>
+</pre></div>
+<p>Again, weigh the benefits of clarity versus the benefits of conciseness.</p>
+</div>
+<h5 id="heading_id_27">Short Circuiting</h5>
+<div id="ishort_circuiting_behavior_0"></div>
+<div id="ishort_circuiting_0"></div>
+<p>Perl performs a type of behavior known as <i>short-circuiting</i> when it encounters complex expressions--expressions composed of multiple evaluated expressions. If Perl can determine that a complex expression would succeed or fail as a whole without evaluating every subexpression, it will not evaluate subsequent subexpressions. This is most obvious with an example:</p>
+<div class="programlisting">
+<pre>
+<code>    say "Both true!" if ok(1, 'first subexpression')
+                     &amp;&amp; ok(1, 'second subexpression');</code>
+</pre></div>
+<div class="sidebar">
+<p>The return value of <code>ok()</code> (<a href="chapter_09.xhtml#testing">Testing</a>(testing)) is the boolean value obtained by evaluating the first argument.</p>
+</div>
+<p>This example prints:</p>
+<div class="screen">
+<pre>
+<code>    ok 1 - first subexpression
+    ok 2 - first subexpression
+    Both true!</code>
+</pre></div>
+<p>When the first subexpression--the first call to <code>ok</code>--evaluates to true, Perl must evaluate the second subexpression. When the first subexpression evaluates to false, the entire expression cannot succeed, and there is no need to check subsequent subexpressions:</p>
+<div class="programlisting">
+<pre>
+<code>    say "Both true!" if ok(0, 'first subexpression')
+                     &amp;&amp; ok(1, 'second subexpression');</code>
+</pre></div>
+<p>This example prints:</p>
+<div class="screen">
+<pre>
+<code>    not ok 1 - first subexpression</code>
+</pre></div>
+<p>Even though the second subexpression would obviously succeed, Perl never evaluates it. The logic is similar for a complex conditional expression where either subexpression must be true for the conditional as a whole to succeed:</p>
+<div class="programlisting">
+<pre>
+<code>    say "Either true!" if ok(1, 'first subexpression')
+                       || ok(1, 'second subexpression');</code>
+</pre></div>
+<p>This example prints:</p>
+<div class="screen">
+<pre>
+<code>    ok 1 - first subexpression
+    Either true!</code>
+</pre></div>
+<p>Again, with the success of the first subexpression, Perl can avoid evaluating the second subexpression. If the first subexpression were false, the result of evaluating the second subexpression would dictate the result of evaluating the entire expression.</p>
+<p>Besides allowing you to avoid potentially expensive computations, short circuiting can help you to avoid errors and warnings:</p>
+<div class="programlisting">
+<pre>
+<code>    if (exists $barbeque{pork} and $barbeque{pork} eq 'shoulder') { ... }</code>
+</pre></div>
+<h4 id="heading_id_28">Context for Conditional Directives</h4>
+<div id="icontext__iconditional_0"></div>
+<div id="itruth_0"></div>
+<div id="iboolean_truth_0"></div>
+<div id="itrue_value_0"></div>
+<div id="ifalse_value_0"></div>
+<p>The conditional directives--<code>if</code>, <code>unless</code>, and the ternary conditional operator--all evaluate an expression in boolean context (<a href="chapter_01.xhtml#context_philosophy">Context</a>(context_philosophy)). As comparison operators such as <code>eq</code>, <code>==</code>, <code>ne</code>, and <code>!=</code> all produce boolean results when evaluated, Perl coerces the results of other expressions--including variables and values--into boolean forms. Empty hashes and arrays evaluate to false.</p>
+<p>Perl 5 has no single true value, nor a single false value. Any number that evaluates to 0 is false. This includes <code>0</code>, <code>0.0</code>, <code>0e0</code>, <code>0x0</code>, and so on. The empty string (<code>''</code>) and <code>"0"</code> evaluate to false, but the strings <code>"0.0"</code>, <code>"0e0"</code>, and so on do not. The idiom <code>"0 but true"</code> evaluates to 0 in numeric context but evaluates to true, thanks to its string contents. Both the empty list and <code>undef</code> evaluate to false. Empty arrays and hashes return the number 0 in scalar context, so they evaluate to false in boolean context.</p>
+<p>An array which contains a single element--even <code>undef</code>--evaluates to true in boolean context. A hash which contains any elements--even a key and a value of <code>undef</code>--evaluates to true in boolean context.</p>
+<div class="sidebar">
+<p>The <code>Want</code> module available from the CPAN allows you to detect boolean context within your own functions. The core <code>overloading</code> pragma (<a href="chapter_09.xhtml#overloading">Overloading</a>(overloading), <a href="chapter_09.xhtml#pragmas">Pragmas</a>(pragmas)) allows you to specify what your own data types produce when evaluated in a boolean context.</p>
+</div>
+<h4 id="heading_id_29">Looping Directives</h4>
+<div id="looping_directives"></div>
+<p>Perl also provides several directives for looping and iteration.</p>
+<p><code>foreach</code> <code>for</code> <code>looping directives; for</code> <code>looping directives; foreach</code></p>
+<p>The <i>foreach</i> style loop evaluates an expression which produces a list and executes a statement or block until it has consumed that list:</p>
+<div class="programlisting">
+<pre>
+<code>    foreach (1 .. 10)
+    {
+        say "$_ * $_ = ", $_ * $_;
+    }</code>
+</pre></div>
+<p>This example uses the <code>range</code> operator to produce a list of integers from one to ten inclusive. The <code>foreach</code> directive loops over them, setting the topic variable (<code>$_</code>) to each in turn. Perl executes the block for each integer and prints the squares of the integers.</p>
+<div class="sidebar">
+<p>Though this is a <i>foreach</i>-style loop, Perl treats the keywords <code>foreach</code> and <code>for</code> interchangeably. As only the <i>type</i> of the loop governs its behavior, there are no drawbacks to using the shorter <code>for</code> keyword.</p>
+</div>
+<p>Like <code>if</code> and <code>unless</code>, the <code>for</code> loop has a postfix form:</p>
+<div class="programlisting">
+<pre>
+<code>    say "$_ * $_ = ", $_ * $_ for 1 .. 10;</code>
+</pre></div>
+<p>Similar suggestions apply for clarity and complexity.</p>
+<p>You may provide a variable to which to assign the values of the expression in place of the topic variable:</p>
+<div class="programlisting">
+<pre>
+<code>    for my $i (1 .. 10)
+    {
+        say "$i * $i = ", $i * $i;
+    }</code>
+</pre></div>
+<p>If you do so, Perl will not set the topic variable (<code>$_</code>) to the iterated values. Note also that the scope of the variable <code>$i</code> is only valid <i>within</i> the loop. If you have declared a lexical <code>$i</code> in an outer scope, that value will remain outside the loop:</p>
+<div class="programlisting">
+<pre>
+<code>    my $i = 'cow';
+
+    for my $i (1 .. 10)
+    {
+        say "$i * $i = ", $i * $i;
+    }
+
+    is( $i, 'cow', 'Lexical variable not overwritten in outer scope' );</code>
+</pre></div>
+<p>This localization occurs even if you do not redeclare the iteration variable as a lexical:</p>
+<div class="programlisting">
+<pre>
+<code>    my $i = 'horse';
+
+    for $i (1 .. 10)
+    {
+        say "$i * $i = ", $i * $i;
+    }
+
+    is( $i, 'horse', 'Lexical variable still not overwritten in outer scope' );</code>
+</pre></div>
+<h4 id="heading_id_30">Iteration and Aliasing</h4>
+<div id="iiteration_aliasing_0"></div>
+<div id="ialiasing_0"></div>
+<div id="ialiasing__iforeach_0"></div>
+<div id="ialiasing__iiteration_0"></div>
+<p>The <code>for</code> loop performs <i>aliasing</i> of the iterator variable to the values in the iteration such that you can modify values in place during iteration:</p>
+<div class="programlisting">
+<pre>
+<code>    my @nums = 1 .. 10;
+
+    $_ **= 2 for @nums;
+
+    is( $nums[0], 1, '1 * 1 is 1' );
+    is( $nums[1], 4, '2 * 2 is 4' );
+
+    ...
+
+    is( $nums[9], 100, '10 * 10 is 100' );</code>
+</pre></div>
+<p>This aliasing also works with the block style <code>foreach</code> loop:</p>
+<div class="programlisting">
+<pre>
+<code>    for my $num (@nums)
+    {
+        $num **= 2;
+    }</code>
+</pre></div>
+<p>... as well as iteration with the topic variable:</p>
+<div class="programlisting">
+<pre>
+<code>    for (@nums)
+    {
+        $_ **= 2;
+    }</code>
+</pre></div>
+<p>You cannot use aliasing to modify <i>constant</i> values, however:</p>
+<div class="programlisting">
+<pre>
+<code>    lc for qw( Huey Dewey Louie );</code>
+</pre></div>
+<p>... as this will throw an exception about modification of read-only values. There's little point in doing so anyhow.</p>
+<p>You may occasionally see the use of <code>for</code> with a single scalar variable to alias <code>$_</code> to the variable:</p>
+<div class="programlisting">
+<pre>
+<code>    for ($user_input)
+    {
+        s/(\w)/\\$1/g;  # escape non-word characters
+        s/^\s*|\s$/g;   # trim whitespace
+    }</code>
+</pre></div>
+<h4 id="heading_id_31">Iteration and Scoping</h4>
+<div id="iiterator_scoping_0"></div>
+<div id="iscope__iiterator_0"></div>
+<div id="imy_36__0"></div>
+<div id="i36___ilexical_0"></div>
+<p>Iterator scoping with the topic variable provides one common source of confusion. In this case, <code>some_function()</code> modifies <code>$_</code> on purpose. If <code>some_function()</code> called other code which modified <code>$_</code> without explicitly localizing <code>$_</code>, the iterated value in <code>@values</code> would change. Debugging this can be troublesome:</p>
+<div class="programlisting">
+<pre>
+<code>    for (@values)
+    {
+        some_function();
+    }
+
+    sub some_function
+    {
+        s/foo/bar/;
+    }</code>
+</pre></div>
+<p>If you <i>must</i> use <code>$_</code> rather than a named variable, lexicalize the topic variable with <code>my $_</code>:</p>
+<div class="programlisting">
+<pre>
+<code>    sub some_function_called_later
+    {
+        # was $_ = shift;
+        <b>my</b> $_ = shift;
+
+        s/foo/bar/;
+        s/baz/quux/;
+
+        return $_;
+    }</code>
+</pre></div>
+<p>Using a named iteration variable also prevents undesired aliasing behavior through <code>$_</code>.</p>
+<h4 id="heading_id_32">The C-Style For Loop</h4>
+<div id="ifor_loop_0"></div>
+<p>The C-style <i>for loop</i> allows the programmer to manage iteration manually:</p>
+<div class="programlisting">
+<pre>
+<code>    for (my $i = 0; $i &lt;= 10; $i += 2)
+    {
+        say "$i * $i = ", $i * $i;
+    }</code>
+</pre></div>
+<p>You must assign to an iteration variable manually, as there is no default assignment to the topic variable. Consequently there is no aliasing behavior either. Though the scope of any declared lexical variable is to the body of the block, a variable <i>not</i> declared explicitly in the iteration control section of this construct <i>will</i> overwrite its contents:</p>
+<div class="programlisting">
+<pre>
+<code>    my $i = 'pig';
+
+    for ($i = 0; $i &lt;= 10; $i += 2)
+    {
+        say "$i * $i = ", $i * $i;
+    }
+
+    isnt( $i, 'pig', '$i overwritten with a number' );</code>
+</pre></div>
+<p>This loop has three subexpressions in its looping construct. The first subexpression is an initialization section. It executes once, before the first execution of the loop body. The second subexpression is the conditional comparison subexpression. Perl evaluates this subexpression before each iteration of the loop body. When the subexpression evaluates to a true value, the loop iteration proceeds. When the subexpression evaluates to a false value, the loop iteration stops. The final subexpression executes after each iteration of the loop body.</p>
+<p>This may be more obvious with an example:</p>
+<div class="programlisting">
+<pre>
+<code>    # declared outside to avoid declaration in conditional
+    my $i;
+
+    for (
+        # loop initialization subexpression
+        say 'Initializing' and $i = 0;
+
+        # conditional comparison subexpression
+        say "Iteration: $i" and $i &lt; 10;
+
+        # iteration ending subexpression
+        say 'Incrementing $i' and $i++
+    )
+    {
+        say "$i * $i = ", $i * $i;
+    }</code>
+</pre></div>
+<p>Note the lack of a trailing semicolon at the iteration ending subexpression as well as the use of the low-precedence <code>and</code>; this syntax is surprisingly finicky. When possible, prefer the <code>foreach</code> style loop to the <code>for</code> loop.</p>
+<p>All three subexpressions are optional. You may write an infinite loop with:</p>
+<div class="programlisting">
+<pre>
+<code>    for (;;) { ... }</code>
+</pre></div>
+<h4 id="heading_id_33">While and Until</h4>
+<div id="iwhile_loop_0"></div>
+<div id="iloops__iwhile_0"></div>
+<p>A <i>while</i> loop continues until the loop conditional expression evaluates to a boolean false value. An infinite loop is much clearer when written:</p>
+<div class="programlisting">
+<pre>
+<code>    while (1) { ... }</code>
+</pre></div>
+<p>The means of evaluating the end of iteration condition in a <code>while</code> loop differs from a <code>foreach</code> loop in that the evaluation of the expression itself does not produce any side effects. If <code>@values</code> has one or more elements, this code is also an infinite loop:</p>
+<div class="programlisting">
+<pre>
+<code>    while (@values)
+    {
+        say $values[0];
+    }</code>
+</pre></div>
+<div id="idestructive_update_0"></div>
+<p>To prevent such an infinite <code>while</code> loop, you must perform a <i>destructive update</i> of the <code>@values</code> array by modifying the array with each loop iteration:</p>
+<div class="programlisting">
+<pre>
+<code>    while (my $value = shift @values)
+    {
+        say $value;
+    }</code>
+</pre></div>
+<div id="iuntil_loop_0"></div>
+<div id="iloops__iuntil_0"></div>
+<p>The <i>until</i> loop performs the opposite test as the <code>while</code> loop. Iteration continues while the loop conditional expression evaluates to false:</p>
+<div class="programlisting">
+<pre>
+<code>    until ($finished_running)
+    {
+        ...
+    }</code>
+</pre></div>
+<p>The canonical use of the <code>while</code> loop is to iterate over input from a filehandle:</p>
+<div class="programlisting">
+<pre>
+<code>    use autodie;
+
+    open my $fh, '&lt;', $file;
+
+    while (&lt;$fh&gt;)
+    {
+        ...
+    }</code>
+</pre></div>
+<p>Perl 5 interprets this <code>while</code> loop as if you had written:</p>
+<div class="programlisting">
+<pre>
+<code>    while (defined($_ = &lt;$fh&gt;))
+    {
+        ...
+    }</code>
+</pre></div>
+<p>Without the implicit <code>defined</code>, any line read from the filehandle which evaluated to false in a scalar context--a blank line or a line which contained only the character <code>0</code>--would end the loop. The <code>readline</code> operator returns an undefined value only when it has finished reading lines from the file.</p>
+<div class="sidebar">
+<p>One common mistake is to forget to remove the line-ending characters from each line; use the <code>chomp</code> keyword to do so.</p>
+</div>
+<p>Both <code>while</code> and <code>until</code> have postfix forms. The simplest infinite loop in Perl 5 is:</p>
+<div class="programlisting">
+<pre>
+<code>    1 while 1;</code>
+</pre></div>
+<p>Any single expression is suitable for a postfix <code>while</code> or <code>until</code>, such as the classic "Hello, world!" example from 8-bit computers of the early 1980s:</p>
+<div class="programlisting">
+<pre>
+<code>    print "Hello, world!  " while 1;</code>
+</pre></div>
+<p>Infinite loops may seem silly, but they're actually quite useful. A simple event loop for a GUI program or network server may be:</p>
+<div class="programlisting">
+<pre>
+<code>    $server-&gt;dispatch_results() until $should_shutdown;</code>
+</pre></div>
+<div id="ido__ilooping_0"></div>
+<div id="ilooping__ido_0"></div>
+<p>For more complex expressions, use a <code>do</code> block:</p>
+<div class="programlisting">
+<pre>
+<code>    do
+    {
+        say 'What is your name?';
+        my $name = &lt;&gt;;
+        chomp $name;
+        say "Hello, $name!" if $name;
+    } until (eof);</code>
+</pre></div>
+<p>For the purposes of parsing, a <code>do</code> block is itself a single expression, though it can contain several expressions. Unlike the <code>while</code> loop's block form, the <code>do</code> block with a postfix <code>while</code> or <code>until</code> will execute its body at least once. This construct is less common than the other loop forms, but no less powerful.</p>
+<h4 id="heading_id_34">Loops within Loops</h4>
+<div id="inested_loops_0"></div>
+<div id="iloops__inested_0"></div>
+<p>You may nest loops within other loops:</p>
+<div class="programlisting">
+<pre>
+<code>    for my $suit (@suits)
+    {
+        for my $values (@card_values)
+        {
+            ...
+        }
+    }</code>
+</pre></div>
+<p>In this case, explicitly declaring named variables is essential to maintainability. The potential for confusion as to the scoping of iterator variables is too great when using the topic variable.</p>
+<p>A common mistake with nesting <code>foreach</code> and <code>while</code> loops is that it is easy to exhaust a filehandle with a <code>while</code> loop:</p>
+<div class="programlisting">
+<pre>
+<code>    use autodie;
+
+    open my $fh, '&lt;', $some_file;
+
+    for my $prefix (@prefixes)
+    {
+        # DO NOT USE; likely buggy code
+        while (&lt;$fh&gt;)
+        {
+            say $prefix, $_;
+        }
+    }</code>
+</pre></div>
+<p>Opening the filehandle outside of the <code>for</code> loop leaves the file position unchanged between each iteration of the <code>for</code> loop. On its second iteration, the <code>while</code> loop will have nothing to read and will not execute. To solve this problem, you may re-open the file inside the <code>for</code> loop (simple to understand, but not always a good use of system resources), slurp the entire file into memory (which may not work if the file is large), or <code>seek</code> the filehandle back to the beginning of the file for each iteration (an often overlooked option):</p>
+<div class="programlisting">
+<pre>
+<code>    use autodie;
+
+    open my $fh, '&lt;', $some_file;
+
+    for my $prefix (@prefixes)