Commits

Shlomi Fish  committed 5df5fb7

Move away the Modern Perl mirror.

This is a precursor to adding Project Wonderful ads.

  • Participants
  • Parent commits b0a58e7

Comments (0)

Files changed (59)

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

Binary file added.

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

+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta name="generator" content="HTML Tidy for Linux (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><em>Modern Perl</em> is one way to describe the way the world's most effective Perl 5 programmers work. They use language idioms. They take advantage of the CPAN. They show good taste and craft to write powerful, maintainable, scalable, concise, and effective code. You can learn these skills too!</p>
+<p>Perl first appeared in 1987 as a simple tool for system administration. Though it began by declaring and occupying a comfortable niche between shell scripting and C programming, it has become a powerful, general-purpose language family. Perl 5 has a solid history of pragmatism and a bright future of polish and enhancement <span class="footnote">(footnote: Perl 6 is a reinvention of programming based on the solid principles of Perl, but it's a subject of another book.)</span>.</p>
+<p>Over Perl's long history--especially the 17 years of Perl 5--our understanding of what makes great Perl programs has changed. While you can write productive programs which never take advantage of all the language has to offer, the global Perl community has invented, borrowed, enhanced, and polished ideas and made them available to anyone willing to learn them.</p>
+<h2 id="heading_id_3">Running Modern Perl</h2>
+<p>The <code>Modern::Perl</code> module from the CPAN (<a href="chapter_02.html#cpan">The CPAN</a>(cpan)) asks Perl to warn of dubious constructs and typos and will enable new features introduced in modern releases of Perl 5. Unless otherwise mentioned, code snippets always assume the basic skeleton of a program:</p>
+<div class="programlisting">
+<pre>
+<code>    #!/usr/bin/env perl
+
+    use Modern::Perl 2011;
+    use autodie;</code>
+</pre></div>
+<p>... which is equivalent to:</p>
+<div class="programlisting">
+<pre>
+<code>    #!/usr/bin/env perl
+
+    use 5.012;      # implies "use strict;"
+    use warnings;
+    use autodie;</code>
+</pre></div>
+<p>Some examples use testing functions such as <code>ok()</code>, <code>like()</code>, and <code>is()</code> (<a href="chapter_09.html#testing">Testing</a>(testing)). These programs follow the pattern:</p>
+<div class="programlisting">
+<pre>
+<code>    #!/usr/bin/env perl
+
+    use Modern::Perl;
+    <strong>use Test::More;</strong>
+
+    # example code here
+
+    <strong>done_testing();</strong></code>
+</pre></div>
+<p>At the time of writing, the current stable Perl 5 release family is Perl 5.14. The examples in this book work best with Perl 5.12.0 or newer. Many examples will work on older versions of Perl 5 with modest changes, but you will have more difficulty with anything older than 5.10.0.</p>
+<div id="iStrawberry_Perl_0"></div>
+<div id="iActivePerl_0"></div>
+<div id="iperlbrew_0"></div>
+<div id="iCPAN__iApp5858perlbrew_0"></div>
+<p>If you have no Perl 5 installed (or if you have an old version installed), you can install a newer release yourself. Windows users, download Strawberry Perl from <span class="url">http://www.strawberryperl.com/</span> or ActivePerl from <span class="url">http://www.activestate.com/activeperl</span>. Users of other operating systems with Perl 5 already installed (and a C compiler and the other development tools), start by installing the CPAN module <code>App::perlbrew</code> <span class="footnote">(footnote: See <span class="url">http://search.cpan.org/perldoc?App::perlbrew</span> for installation instructions.)</span>.</p>
+<p><code>perlbrew</code> allows you to install and manage multiple versions of Perl 5. This allows you to switch between versions of Perl 5 as well as to install Perl 5 and CPAN modules in your home directory without affecting the system's version. If you've ever had to beg your system administrator for permission to install software, you know how much easier your life can be now.</p>
+<h2 id="heading_id_4">Credits</h2>
+<p>This book would not have been possible without questions, comments, suggestions, advice, wisdom, and encouragement from many, many people. In particular, the author and editor thank:</p>
+<div id="credits"></div>
+John SJ Anderson, Peter Aronoff, Lee Aylward, Alex Balhatchet, Ævar Arnfjörð Bjarmason, Matthias Bloch, John Bokma, Vasily Chekalkin, Dmitry Chestnykh, E. Choroba, Tom Christiansen, Anneli Cuss, Paulo Custodio, Steve Dickinson, Kurt Edmiston, Felipe, Shlomi Fish, Jeremiah Foster, Mark Fowler, John Gabriele, Andrew Grangaard, Bruce Gray, Ask Bjørn Hansen, Tim Heaney, Graeme Hewson, Robert Hicks, Michael Hind, Mark Hindess, Yary Hluchan, Daniel Holz, Mike Huffman, Gary H. Jones II, Curtis Jewell, Mohammed Arafat Kamaal, James E Keenan, Kirk Kimmel, Yuval Kogman, Jan Krynicky, Michael Lang, Jeff Lavallee, Moritz Lenz, Andy Lester, Jean-Baptiste Mazon, Josh McAdams, Gareth McCaughan, John McNamara, Shawn M Moore, Alex Muntada, Carl Mäsak, Chris Niswander, Nelo Onyiah, Chas. Owens, ww from PerlMonks, Jess Robinson, Dave Rolsky, Gabrielle Roth, Jean-Pierre Rupp, Eduardo Santiago, Andrew Savige, Lorne Schachter, Steve Schulze, Dan Scott, Alexander Scott-Johns, Phillip Smith, Christopher E. Stith, Mark A. Stratman, Bryan Summersett, Audrey Tang, Scott Thomson, Ben Tilly, Ruud H. G. van Tol, Sam Vilain, Larry Wall, Lewis Wall, Colin Wetherbee, Frank Wiegand, Doug Wilson, Sawyer X, David Yingling, Marko Zagozen, harleypig, hbm, and sunnavy.
+<p>Any remaining errors are the fault of the stubborn author.</p>
+</body>
+</html>

File lib/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 (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">The Perl Philosophy</h1>
+<p>Perl gets things done--it's flexible, forgiving, and malleable. Capable programmers use it every day for everything from one-liners and one-off automations to multi-year, multi-programmer projects.</p>
+<p>Perl is pragmatic. You're in charge. You decide how to solve your problems and Perl will mold itself to do what you mean, with little frustration and no ceremony.</p>
+<p>Perl will grow with you. In the next hour, you'll learn enough to write real, useful programs--and you'll understand <em>how</em> the language works and <em>why</em> it works as it does. Modern Perl takes advantage of this knowledge and the combined experience of the global Perl community to help you write working, maintainable code.</p>
+<p>First, you need to know how to learn more.</p>
+<h2 id="heading_id_3">Perldoc</h2>
+<div id="perldoc"></div>
+<div id="iperldoc_0"></div>
+<p>Perl has a culture of useful documentation. The <code>perldoc</code> utility is part of every complete Perl 5 installation <span class="footnote">(footnote: However your Unix-like system may require you to install an additional package such as <code>perl-doc</code> on Debian or Ubuntu GNU/Linux.)</span>. <code>perldoc</code> 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">
+<div id="iCPAN_0"></div>
+<div id="imetacpan_0"></div>
+<p><span class="url">http://perldoc.perl.org/</span> hosts recent versions of the Perl documentation. CPAN indexes at <span class="url">http://search.cpan.org/</span> and <span class="url">http://metacpan.org/</span> provide documentation for all CPAN modules. Other Perl 5 distributions such as ActivePerl and Strawberry Perl provide local documentation in HTML formats.</p>
+</div>
+<p>Use <code>perldoc</code> to read the documentation for a module or part of the core documentation:</p>
+<div class="screen">
+<pre>
+<code>    $ <strong>perldoc List::Util</strong>
+    $ <strong>perldoc perltoc</strong>
+    $ <strong>perldoc Moose::Manual</strong></code>
+</pre></div>
+<p>The first example displays the documentation embedded within the <code>List::Util</code> module. The second example displays a pure documentation file, in this case the table of contents of the core documentation. The third example displays a pure documentation file included as part of a CPAN distribution (<a href="chapter_07.html#moose">Moose</a>(moose)). <code>perldoc</code> hides these details; there's no distinction between reading the documentation for a core library such as <code>Data::Dumper</code> or one installed from the CPAN.</p>
+<p>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>
+<div class="tip">
+<p><code>perldoc perltoc</code> displays the table of contents of the core documentation, and <code>perldoc perlfaq</code> displays the table of contents for Frequently Asked Questions about Perl 5. <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. <code>perldoc perlvar</code> lists all of Perl's symbolic variables. Skimming these files will give you a great overview of Perl 5.</p>
+</div>
+<div id="iperldoc__i45q_40search_perlfaq41_0"></div>
+<p>The <code>perldoc</code> utility has many more abilities (see <code>perldoc perldoc</code>). The <code>-q</code> option searches only the Perl FAQ for any provided keywords. Thus <code>perldoc -q sort</code> returns three questions: <em>How do I sort an array by (anything)?</em>, <em>How do I sort a hash (optionally by value instead of key)?</em>, and <em>How can I always keep my hash sorted?</em>.</p>
+<div id="iperldoc__i45f_40search_perlfunc41_0"></div>
+<p>The <code>-f</code> option displays the 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, browse the list of available builtins in <code>perldoc perlfunc</code>.</p>
+<div id="iperldoc__i45v_40search_perlvar41_0"></div>
+<p>The <code>-v</code> option looks up a builtin variable. For example, <code>perldoc -v $PID</code> displays the documentation for the variable which contains the current program's process id. Depending on your shell, you may have to quote the variable appropriately.</p>
+<p>The <code>-l</code> option causes <code>perldoc</code> to display the <em>path</em> to the documentation file rather than the contents of the documentation <span class="footnote">(footnote: Be aware that a module may have a separate <em>.pod</em> file in addition to its <em>.pm</em> file.)</span>.</p>
+<p>The <code>-m</code> option displays the entire <em>contents</em> of the module, code and all, without performing any special formatting.</p>
+<div id="iPOD_0"></div>
+<div id="iPOD__iperldoc_0"></div>
+<div id="iPOD__ipodchecker_0"></div>
+<div id="iPOD__iPod5858Webserver_0"></div>
+<div id="iCPAN__iPod5858Webserver_0"></div>
+<p>Perl 5's documentation system is <em>POD</em>, or <em>Plain Old Documentation</em>. <code>perldoc perlpod</code> describes how POD works. Other POD tools include <code>podchecker</code>, which validates the form of your POD, and <code>Pod::Webserver</code>, which displays local POD as HTML through a minimal web server.</p>
+<h2 id="heading_id_4">Expressivity</h2>
+<div id="expressivity"></div>
+<div id="iWall44_Larry_0"></div>
+<div id="iLarry_Wall_0"></div>
+<p>Larry Wall's studies of linguistics and human languages influenced the design of Perl. The language allows you tremendous freedom to solve your problems, depending on your group style, the available time, the expected lifespan of the program, or even how creative you feel. You may write simple, straightforward code or integrate into larger, well-defined programs. You may select from multiple design paradigms, and you may eschew or embrace advanced features.</p>
+<p>Where other languages enforce one best way to write any code, Perl allows <em>you</em> to decide what's most readable or useful or fun.</p>
+<div id="iTIMTOWTDI_0"></div>
+<div id="iTim_Toady_0"></div>
+<p>Perl hackers have a slogan for this: <em>TIMTOWTDI</em>, pronounced "Tim Toady", or "There's more than one way to do it!"</p>
+<p>Though this expressivity allows master craftworkers to create amazing programs, it allows the unwise or uncautious to make messes. Experience and good taste will guide you to write great code. The choice is yours--but be mindful of readability and maintainability, especially for those who come after you.</p>
+<p>Perl novices often may find certain constructs opaque. Many of these idioms (<a href="chapter_10.html#idioms">Idioms</a>(idioms)) offer great (if subtle) power. It's okay to avoid them until you're comfortable with them.</p>
+<p>Learning Perl is like learning a new spoken language. You'll learn a few words, string together sentences, and soon will enjoy simple conversations. Mastery comes with practice of reading and writing. You don't have to understand every detail of Perl to be productive, but the principles in this chapter are vital to your growth as a programmer.</p>
+<p>As another design goal, Perl tries to avoid surprising experienced (Perl) programmers. For example, adding two variables (<code>$first_num + $second_num</code>) is obviously a numeric operation (<a href="chapter_04.html#numeric_operators">Numeric Operators</a>(numeric_operators)); the addition operator must treat both as numeric values to produce a numeric result. No matter the contents of <code>$first_num</code> and <code>$second_num</code>, Perl will coerce them to numeric values (<a href="chapter_03.html#numeric_coercion">Numeric Coercion</a>(numeric_coercion)). You've expressed your intent to treat them as numbers by using a numeric operator. Perl happily does so.</p>
+<div id="iDWIM_0"></div>
+<div id="iprinciple_of_least_astonishment_0"></div>
+<p>Perl adepts often call this principle <em>DWIM</em>, or <em>do what I mean</em>. Another phrasing is that Perl follows the <em>principle of least astonishment</em>. Given a cursory understanding of Perl (especially context; <a href="chapter_01.html#context_philosophy">Context</a>(context_philosophy)), it should be possible to understand the intent of an unfamiliar Perl expression. You will develop this skill.</p>
+<div id="ibaby_Perl_0"></div>
+<p>Perl's expressivity also allows novices to write useful programs without having to understand everything. The resulting code is often called <em>baby Perl</em>, in the sense that most everyone wants to help babies learn to communicate well. Everyone begins as a novice. Through practice and learning from more experienced programmers, you will understand and adopt more powerful idioms and techniques.</p>
+<p>For example, an experienced Perl hacker might triple a list of numbers with:</p>
+<div class="programlisting">
+<pre>
+<code>    my @tripled = map { $_ * 3 } @numbers;</code>
+</pre></div>
+<p>... and 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>... while a novice might try:</p>
+<div class="programlisting">
+<pre>
+<code>    my @tripled;
+
+    for (my $i = 0; $i &lt; scalar @numbers; $i++)
+    {
+        $tripled[$i] = $numbers[$i] * 3;
+    }</code>
+</pre></div>
+<p>All three approaches accomplish the same thing, but each uses Perl in a different way.</p>
+<p>Experience writing Perl will help you to focus on <em>what</em> you want to do rather than <em>how</em> to do it. Even so, Perl will happily run simple programs. You can design and refine your programs for clarity, expressivity, reuse, and maintainability, in part or in whole. Take advantage of this flexibility and pragmatism: it's far better to accomplish your task effectively now than to write a conceptually pure and beautiful program next year.</p>
+<h2 id="heading_id_5">Context</h2>
+<div id="context_philosophy"></div>
+<div id="icontext_0"></div>
+<p>In spoken languages, the meaning of a word or phrase may depend on how you use it; the local <em>context</em> helps clarify the intent. For example, 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, just as the incorrect gender of "la gato" <span class="footnote">(footnote: The article is feminine, but the noun is masculine.)</span> makes native speakers chuckle. Consider also the pronoun "you" or the noun "sheep" which can be singular or plural depending on context.</p>
+<p>Context in Perl is similar. It governs the amount as well as the kind of data to use. Perl will happily attempt to provide exactly what you ask for--provided you do so by choosing the appropriate context.</p>
+<p>Certain Perl operations produce different behaviors when you want zero, one, or many results. A specific construct in Perl may do something different if you write "Do this, but I don't care about any results" compared to "Do this, and I expect multiple results." Other operations allow you to specify whether you expect to work with numeric data, textual data, or true or false data.</p>
+<p>Context can be tricky if you try to write or read Perl code as a series of single expressions extracted 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. If instead you're cognizant of context, your code will be more correct--and cleaner, flexible, and more concise.</p>
+<h3 id="heading_id_6">Void, Scalar, and List Context</h3>
+<div id="amount_context"></div>
+<div id="icontext__iamount_0"></div>
+<div id="iamount_context_0"></div>
+<p><em>Amount context</em> context governs <em>how many</em> items you expect from an operation. The English language's subject-verb number agreement is a close parallel. Even without knowing the formal description of this linguistic principle, you probably understand the error in the sentence "Perl are a fun language". In Perl, 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.html#functions">Declaring Functions</a>(functions)) called <code>find_chores()</code> which sorts your household todo list in order of task priority. The means by which you call this function determines what it will produce. You may have no time to do chores, in which case calling the function is an attempt to look industrious. You may have enough time to do one task, or you could have a burst of energy on a free weekend and desire to accomplish as much as possible.</p>
+<p>If you call the function on its own and never use its return value, you've called the function in <em>void context</em>:</p>
+<div class="programlisting">
+<pre>
+<code>    find_chores();</code>
+</pre></div>
+<div id="icontext__iscalar_0"></div>
+<div id="iscalar_context_0"></div>
+<p>Assigning the function's return value to a single item (<a href="chapter_03.html#scalars">Scalars</a>(scalars)) evaluates the function in <em>scalar context</em>:</p>
+<div class="programlisting">
+<pre>
+<code>    my $single_result = find_chores();</code>
+</pre></div>
+<div id="ilist_context_0"></div>
+<div id="icontext__ilist_0"></div>
+<p>Assigning the results of calling the function to an array (<a href="chapter_03.html#arrays">Arrays</a>(arrays)) or a list, or using it in a list, evaluates the function in <em>list context</em>:</p>
+<div class="programlisting">
+<pre>
+<code>    my @all_results             = find_chores();
+    my ($single_element, @rest) = find_chores();
+    process_list_of_results( find_chores() );</code>
+</pre></div>
+<p>The parentheses in the second line of the previous example group the two variable declarations (<a href="chapter_05.html#lexical_scope">Lexical Scope</a>(lexical_scope)) so that assignment will behave as you expect. If <code>@rest</code> were to go unused, you could also correctly write:</p>
+<div class="programlisting">
+<pre>
+<code>    my <strong>(</strong>$single_element<strong>)</strong>   = find_chores();</code>
+</pre></div>
+<p>.... in which case the parentheses give a hint to the Perl 5 parser that you intend list context for the assignment even though you assign only one element of a list. This is subtle, but now that you know about it, the difference of amount context between these two statements should be obvious:</p>
+<div class="programlisting">
+<pre>
+<code>    my $scalar_context = find_chores();
+    my <strong>(</strong>$list_context<strong>)</strong> = find_chores();</code>
+</pre></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 of these calls to <code>find_chores()</code> occur in list context:</p>
+<div class="programlisting">
+<pre>
+<code>    process_list_of_results( find_chores() );
+
+    my %results =
+    (
+        cheap_operation     =&gt; $cheap_results,
+        expensive_operation =&gt; find_chores(), # OOPS!
+    );</code>
+</pre></div>
+<div id="ibuiltins__iscalar_0"></div>
+<p>The latter example often surprises novice programmers, as initializing a hash (<a href="chapter_03.html#hashes">Hashes</a>(hashes)) with a list of values imposes list context on <code>find_chores</code>. Use the <code>scalar</code> operator to impose scalar context:</p>
+<div class="programlisting">
+<pre>
+<code>    my %results =
+    (
+        cheap_operation     =&gt; $cheap_results,
+        expensive_operation =&gt; <strong>scalar</strong> find_chores(),
+    );</code>
+</pre></div>
+<p>Why does context matter? A context-aware function can examine its calling context and decide how much work it must do. In void context, <code>find_chores()</code> may legitimately do nothing. In scalar context, it can find only the most important task. In list context, it must sort and return the entire list.</p>
+<h3 id="heading_id_7">Numeric, String, and Boolean Context</h3>
+<div id="value_contexts"></div>
+<div id="ivalue_context_0"></div>
+<div id="icontext__ivalue_0"></div>
+<p>Perl's other context--<em>value context</em>--governs how Perl interprets a piece of data. 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. In exchange for not having to declare (or at least track) explicitly what <em>type</em> of data a variable contains or a function produces, Perl's type contexts provide hints that tell the compiler how to treat data.</p>
+<div id="ibuiltins__ieq_0"></div>
+<p>Perl will coerce values to specific proper types (<a href="chapter_03.html#coercion">Coercion</a>(coercion)), depending on the operators you use. For example, the <code>eq</code> operator tests that strings contain the same information <em>as strings</em>:</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 <em>know</em> 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';</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>
+<div id="ibuiltins__i6161_0"></div>
+<p>The <code>eq</code> operator treats its operands as strings by enforcing <em>string context</em> on them. The <code>==</code> operator imposes <em>numeric context</em>. In numeric context, both strings evaluate to <code>0</code> (<a href="chapter_03.html#numeric_coercion">Numeric Coercion</a>(numeric_coercion)). Be sure to use the proper operator for the type of context you want.</p>
+<div id="iboolean_context_0"></div>
+<div id="icontext__iboolean_0"></div>
+<p><em>Boolean context</em> occurs when you use a value in a conditional statement. In the previous examples, <code>if</code> evaluated the results of the <code>eq</code> and <code>==</code> operators in boolean context.</p>
+<div id="icontext__iexplicit_0"></div>
+<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>Type contexts are easier to identify than amount contexts. Once you know which operators provide which contexts (<a href="chapter_04.html#operator_types">Operator Types</a>(operator_types)), you'll rarely make mistakes.</p>
+<h2 id="heading_id_8">Implicit Ideas</h2>
+<div id="implicit_ideas"></div>
+<p>Context is only one linguistic shortcut in Perl. Programmers who understand these shortcuts can glance at code and instantly understand its most important characteristics. Another important linguistic feature is the Perl equivalent of pronouns.</p>
+<h3 id="heading_id_9">The Default Scalar Variable</h3>
+<div id="default_scalar_variable"></div>
+<div id="idefault_variables__iscalar_0"></div>
+<div id="idefault_variables__i36__0"></div>
+<div id="ivariables__i36__0"></div>
+<div id="i36___idefault_scalar_variable_0"></div>
+<p>The <em>default scalar variable</em> (also called the <em>topic variable</em>), <code>$_</code>, is most notable in its <em>absence</em>: many of Perl's builtin operations 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>
+<div id="ibuiltins__ichomp_0"></div>
+<div id="ibuiltins__ichr_0"></div>
+<div id="ibuiltins__iord_0"></div>
+<div id="ibuiltins__ilc_0"></div>
+<div id="ibuiltins__ilength_0"></div>
+<div id="ibuiltins__ireverse_0"></div>
+<div id="ibuiltins__iuc_0"></div>
+<p>Many of Perl's scalar operators (including <code>chr</code>, <code>ord</code>, <code>lc</code>, <code>length</code>, <code>reverse</code>, and <code>uc</code>) work on the default scalar variable if you do not provide an alternative. For example, the <code>chomp</code> builtin removes any trailing newline sequence from its operand <span class="footnote">(footnote: See <code>perldoc -f chomp</code> and <code>$/</code> for more precise details of its behavior.)</span>:</p>
+<div class="programlisting">
+<pre>
+<code>    my $uncle = "Bob\n";
+    chomp $uncle;
+    say "'$uncle'";</code>
+</pre></div>
+<p><code>$_</code> has the same function in Perl as the pronoun <em>it</em> in English. Without an explicit variable, <code>chomp</code> removes the trailing newline sequence from <code>$_</code>. Perl understands what you mean when you say "<code>chomp</code>"; Perl will always chomp <em>it</em>, so these two lines of code are equivalent:</p>
+<div class="programlisting">
+<pre>
+<code>    chomp $_;
+    chomp;</code>
+</pre></div>
+<div id="ibuiltins__isay_0"></div>
+<div id="ibuiltins__iprint_0"></div>
+<p>Similarly, <code>say</code> and <code>print</code> operate on <code>$_</code> in the absence of other arguments:</p>
+<div class="programlisting">
+<pre>
+<code>    print;  # prints $_ to the current filehandle
+    say;    # prints "$_\n" to the current filehandle</code>
+</pre></div>
+<div id="is474747__isubstitution_operator_0"></div>
+<div id="im4747__imatch_operator_0"></div>
+<div id="itr4747__itransliteration_operator_0"></div>
+<p>Perl's regular expression facilities (<a href="chapter_06.html#regex">Regular Expressions and Matching</a>(regex)) default to <code>$_</code> to match, substitute, and transliterate:</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 id="ibuiltins__ifor_0"></div>
+<p>Perl's looping directives (<a href="chapter_03.html#looping_directives">Looping Directives</a>(looping_directives)) default to using <code>$_</code> as the iteration variable. Consider <code>for</code> iterating over a list:</p>
+<div class="programlisting">
+<pre>
+<code>    say "#<strong>$_</strong>" for 1 .. 10;
+
+    for (1 .. 10)
+    {
+        say "#<strong>$_</strong>";
+    }</code>
+</pre></div>
+<div id="ibuiltins__iwhile_0"></div>
+<p>... or <code>while</code>:</p>
+<div class="programlisting">
+<pre>
+<code>    while (&lt;STDIN&gt;)
+    {
+        chomp;
+        say scalar reverse;
+    }</code>
+</pre></div>
+<div id="ibuiltins__imap_0"></div>
+<p>... or <code>map</code> transforming a list:</p>
+<div class="programlisting">
+<pre>
+<code>    my @squares = map { <strong>$_</strong> * <strong>$_</strong> } 1 .. 10;
+    say for @squares;</code>
+</pre></div>
+<div id="ibuiltins__igrep_0"></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>As English gets confusing when you have too many pronouns and antecedents, you must take care mixing uses of <code>$_</code> implicitly or explicitly. Uncautious simultaneous use of <code>$_</code> may lead to one piece of code silently overwriting the value written by another. If you write a function which uses <code>$_</code>, you may clobber a caller function's use of <code>$_</code>.</p>
+<p>As of Perl 5.10, you may declare <code>$_</code> as a lexical variable (<a href="chapter_05.html#lexical_scope">Lexical Scope</a>(lexical_scope)) to prevent this clobbering behavior:</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>If <code>calculate_value()</code> or any other function changed <code>$_</code>, that change would persist through that iteration of the loop. Adding a <code>my</code> declaration prevents clobbering an existing instance of <code>$_</code>:</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>
+<div class="tip">
+<div id="ibuiltins__i464646_0"></div>
+<p>Perl 5.12 introduced the triple-dot (<code>...</code>) operator as a placeholder for code you intend to fill in later. Perl will parse it as a complete statement, but will throw an exception that you're trying to run unimplemented code if you try to run it. See <code>perldoc perlop</code> for more details.</p>
+</div>
+<h3 id="heading_id_10">The Default Array Variables</h3>
+<div id="default_array_variables"></div>
+<div id="idefault_variables__iarray_0"></div>
+<div id="idefault_variables__i64__0"></div>
+<div id="ivariables__i64__0"></div>
+<div id="i64__0"></div>
+<p>Perl also provides two implicit array variables. Perl passes arguments to functions (<a href="chapter_05.html#functions">Declaring Functions</a>(functions)) in an array named <code>@_</code>. Array manipulation operations (<a href="chapter_03.html#arrays">Arrays</a>(arrays)) inside functions affect this array by default, so these two snippets of code are equivalent:</p>
+<div class="programlisting">
+<pre>
+<code>    sub foo
+    {
+        my $arg = shift;
+        ...
+    }
+
+    sub foo_explicit_args
+    {
+        my $arg = shift @_;
+        ...
+    }</code>
+</pre></div>
+<div id="ibuiltins__ishift_0"></div>
+<div id="ibuiltins__ipop_0"></div>
+<p>Just as <code>$_</code> corresponds to the pronoun <em>it</em>, <code>@_</code> corresponds to the pronouns <em>they</em> and <em>them</em>. <em>Unlike</em> <code>$_</code>, Perl automatically localizes <code>@_</code> for you when you call other functions. The builtins <code>shift</code> and <code>pop</code> operate on <code>@_</code> with no other operands provided.</p>
+<div id="idefault_variables__i64ARGV_0"></div>
+<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. Perl's array operations (including <code>shift</code> and <code>pop</code>) operate on <code>@ARGV</code> implicitly outside of functions. You cannot use <code>@_</code> when you mean <code>@ARGV</code>.</p>
+<div class="tip">
+<div id="ibuiltins__ireadline_0"></div>
+<div id="ibuiltins__iglob_0"></div>
+<p>Perl's <code>&lt;$fh&gt;</code> operator is the same as the <code>readline</code> builtin. <code>readline $fh</code> does the same thing as <code>&lt;$fh&gt;</code>. As of Perl 5.10, a bare <code>readline</code> behaves just like <code>&lt;&gt;</code>, so you can now use <code>readline</code> everywhere. For historic reasons, <code>&lt;&gt;</code> is still more common, but consider using <code>readline</code> as a more readable alternative. You probably prefer <code>glob '*.html'</code> to <code>&lt;*.html&gt;</code>, right? It's the same idea.</p>
+</div>
+<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 <em>name</em> 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>
+<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 the behavior of <code>reverse</code> sounds confusing, your instincts are correct. Perl 5 arguably should have separated "reverse a string" from "reverse a list".</p>
+<p>If you run it with a list of files:</p>
+<div class="screen">
+<pre>
+<code>    $ <strong>perl reverse_lines.pl encrypted/*.txt</strong></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. Yet Perl is good for far more than small command-line programs....</p>
+</body>
+</html>

File lib/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 (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">Perl and Its Community</h1>
+<p>Perl 5's greatest accomplishment is the huge amount of reusable libraries developed for it. Where Perl 4 had forks to connect to databases such as Oracle and Sybase, for example, Perl 5 had a real extension mechanism. Larry wanted people to create and maintain their own extensions without fragmenting Perl into thousands of incompatible pidgins--and it worked.</p>
+<p>That technical accomplishment was almost as important as the growth of a community around Perl 5. <em>People</em> write libraries. <em>People</em> build on the work of other people. <em>People</em> make a community worth joining and preserving and expanding.</p>
+<p>The Perl community is strong and healthy. It welcomes willing participants at all levels, from novices to core developers. Take advantage of the knowledge and experience of countless other Perl programmers, and you'll become a better programmer.</p>
+<h2 id="heading_id_3">The CPAN</h2>
+<div id="cpan"></div>
+<p>Perl 5 is a pragmatic language on its own, 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 written--and shared--Perl code for it.</p>
+<div id="iCPAN_1"></div>
+<p>Modern Perl programming makes heavy use of the CPAN (<span class="url">http://www.cpan.org/</span>). The Comprehensive Perl Archive Network is an uploading and mirroring system for redistributable, reusable Perl code. It's one of--if not <em>the</em>--largest archives of libraries of code in the world. The CPAN offers 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 id="idistribution_0"></div>
+<div id="imodules_0"></div>
+<p>CPAN mirrors <em>distributions</em>, or collections of reusable Perl code. A single distribution can contain one or more <em>modules</em>, or self-contained libraries of Perl code. Each distribution occupies its own CPAN namespace and provides unique metadata.</p>
+<div class="tip">
+<p>The CPAN <em>adds</em> 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 November 2011, search.cpan.org reported 9359 uploaders, 101656 modules, and 23808 distributions (representing growth rates of 11.5%, 19.4%, and 14.3% since the previous edition of this book, respectively).</p>
+</div>
+<div id="isearch46cpan46org_0"></div>
+<p>The CPAN itself is merely a mirroring service. Authors upload distributions and the CPAN sends them to mirror sites, from which users and CPAN clients download, configure, build, test, and install them. The system succeeds because of this simplicity as well as the contributions of thousands of volunteers who've built on this distribution system. In particular, community standards have evolved to identify the attributes and characteristics of well-formed CPAN distributions. These include:</p>
+<ul>
+<li>Standards for automated CPAN installers.</li>
+<li>Standards for metadata to describe what each distribution provides and expects.</li>
+<li>Standards for documentation and licensing.</li>
+</ul>
+<p>Additional CPAN services provide comprehensive automated testing and reporting to improve the quality of packaging and correctness across platforms and Perl versions. Every CPAN distribution has its own ticket queue on <span class="url">http://rt.cpan.org/</span> for reporting bugs and working with authors. CPAN sites also link to previous distribution versions, module ratings, documentation annotations, and more. All of this is available from <span class="url">http://search.cpan.org/</span>.</p>
+<div id="iCPAN__iCPAN46pm_0"></div>
+<div id="iCPAN__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. For the most part, each of these clients is equivalent for basic installation. This book recommends the use of CPAN.pm solely due to its ubiquity. With a recent version (as of this writing, 1.9800 is the latest stable release), module installation is reasonably easy. Start the client with:</p>
+<div class="screen">
+<pre>
+<code>    $ <strong>cpan</strong></code>
+</pre></div>
+<p>To install a distribution within the client:</p>
+<div class="screen">
+<pre>
+<code>    $ <strong>cpan</strong>
+    cpan[1]&gt; <strong>install Modern::Perl</strong></code>
+</pre></div>
+<p>... or to install directly from the command line:</p>
+<div class="screen">
+<pre>
+<code>    $ <strong>cpan Modern::Perl</strong></code>
+</pre></div>
+<div id="iCPAN__iCPAN46pm_1"></div>
+<p>Eric Wilhelm's tutorial on configuring CPAN.pm <span class="footnote">(footnote: <span class="url">http://learnperl.scratchcomputing.com/tutorials/configuration/</span>)</span> includes a great troubleshooting section.</p>
+<div class="sidebar">
+<p>Even though the CPAN client is a core module for the Perl 5 distribution, you will likely need to install standard development tools such as a <code>make</code> utility and possibly a C compiler. Windows users, see Strawberry Perl (<span class="url">http://strawberryperl.com/</span>) and Strawberry Perl Professional. Mac OS X users must install XCode. Unix and Unix-like users often have these tools available (though Debian and Ubuntu users should install <code>build-essential</code>).</p>
+</div>
+<h3 id="heading_id_4">CPAN Management Tools</h3>
+<p>If your operating system provides its own installation of Perl 5, that version may be out of date or it may have its own dependencies on specific versions of CPAN distributions. Serious Perl developers often construct virtual walls between the system Perl and their development Perl installations. Several projects help to make this possible.</p>
+<p><code>App::cpanminus</code> is a relatively new CPAN client with goals of speed, simplicity, and zero configuration. Install it with <code>cpan App::cpanminus</code>, or:</p>
+<div class="screen">
+<pre>
+<code>    $ <strong>curl -LO http://xrl.us/cpanm</strong>
+    $ <strong>chmod +x cpanm</strong></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="screen">
+<pre>
+<code>    $ <strong>curl -LO http://xrl.us/perlbrew</strong>
+    $ <strong>chmod +x perlbrew</strong>
+    $ <strong>./perlbrew install</strong>
+    $ <strong>perldoc App::perlbrew</strong></code>
+</pre></div>
+<div id="iCPAN__ilocal5858lib_0"></div>
+<div id="iCPAN__iApp5858local5858lib5858helper_0"></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, though <code>App::local::lib::helper</code> can simplify the process. See <span class="url">http://search.cpan.org/perldoc?local::lib</span> and <span class="url">http://search.cpan.org/perldoc?App::local::lib::helper</span> for more details.</p>
+<p>All three 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 id="perl_community"></div>
+<h2 id="heading_id_5">Community Sites</h2>
+<div id="iperl46org_0"></div>
+<div id="iwebsites__iperl46org_0"></div>
+<p>Perl's homepage at <span class="url">http://www.perl.org/</span> links to Perl 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://learn.perl.org/faq/beginners.html</span>.</p>
+<p>The home of Perl development is <span class="url">http://dev.perl.org/</span>, which links to relevant resources for core development of Perl 5 and Perl 6 <span class="footnote">(footnote: Though see also <span class="url">http://www.perl6.org/</span>)</span>.</p>
+<div id="iperl46com_0"></div>
+<div id="iwebsites__iperl46com_0"></div>
+<p>Perl.com publishes articles and tutorials about Perl and its culture. 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>
+<div id="iMetaCPAN_0"></div>
+<div id="iwebsites__iMetaCPAN_0"></div>
+<p>The CPAN's (<a href="chapter_02.html#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. MetaCPAN (<span class="url">https://metacpan.org/</span>) is a recent alternative front end to the CPAN.</p>
+<div id="iPerlMonks_0"></div>
+<div id="iwebsites__iPerlMonks_0"></div>
+<p>PerlMonks, at <span class="url">http://perlmonks.org/</span>, is a community site devoted to discussions about Perl programming. Its eleven year history makes it one of the most venerable question and answer sites for any programming language.</p>
+<div id="iblogs46perl46org_0"></div>
+<div id="iwebsites__iblogs46perl46org_0"></div>
+<p>Several community sites offer news and commentary. <span class="url">http://blogs.perl.org/</span> is a free blog platform open to any Perl community member.</p>
+<div id="iEnlightened_Perl_Organization_0"></div>
+<div id="iPlanet_Perl_0"></div>
+<div id="iPlanet_Perl_Iron_Man_0"></div>
+<div id="iwebsites__iPlanet_Perl_0"></div>
+<div id="iwebsites__iPlanet_Perl_Iron_Man_0"></div>
+<p>Other sites aggregate the 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 and improve the quality of Perl publishing on the web.</p>
+<div id="iPerl_Buzz_0"></div>
+<div id="iwebsites__iPerl_Buzz_0"></div>
+<div id="iPerl_Weekly_0"></div>
+<div id="iwebsites__iPerl_Weekly_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. Perl Weekly (<span class="url">http://perlweekly.com/</span>) offers a weekly take on news from the Perl world.</p>
+<h2 id="heading_id_6">Development Sites</h2>
+<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 (or <em>p5p</em>) mailing list is the focal point of the development of Perl 5 itself. 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__iwiki_0"></div>
+<div id="iwebsites__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>
+<div id="iwebsites__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 sources of this book at <span class="url">http://github.com/chromatic/modern_perl_book/</span>)</span>. See especially Gitpan (<span class="url">http://github.com/gitpan/</span>), which hosts Git repositories chronicling the complete history of every distribution on the CPAN.</p>
+<div class="tip">
+<div id="iCPAN__iGit5858CPAN5858Patch_0"></div>
+<div id="iChampoux44_Yanick_0"></div>
+<p>GitPAN receives infrequent updates. As an alternative, consider using Yanick Champoux's wonderful <code>Git::CPAN::Patch</code> module.</p>
+</div>
+<h2 id="heading_id_7">Events</h2>
+<div id="iYAPC_0"></div>
+<p>The Perl community holds countless conferences, workshops, seminars, and meetings. 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>Hundreds of local Perl Mongers groups get together frequently for technical talks and social interaction. See <span class="url">http://www.pm.org/</span>.</p>
+<h2 id="heading_id_8">IRC</h2>
+<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 can't meet in person, many collaborate and chat online through the textual chat system known as IRC. Many of the most popular and useful Perl projects have their own IRC channels, such as <em>#moose</em> and <em>#catalyst</em>.</p>
+<p>The main server for Perl community is <span class="url">irc://irc.perl.org/</span>. Notable channels include <em>#perl-help</em>, for general assistance on Perl programming, and <em>#perl-qa</em>, devoted to testing and other quality issues. Be aware that the channel <em>#perl</em> is a general purpose channel for discussing whatever its participants want to discuss <span class="footnote">(footnote: ... and, as such, it's not primarily a helpdesk.)</span>.</p>
+</body>
+</html>

File lib/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 (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">The Perl Language</h1>
+<p>Like a spoken language, the whole of Perl is a combination of several smaller but interrelated parts. 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>
+<h2 id="heading_id_3">Names</h2>
+<div id="names"></div>
+<div id="inames_0"></div>
+<div id="iidentifiers_0"></div>
+<p><em>Names</em> (or <em>identifiers</em>) are everywhere in Perl programs: variables, functions, packages, classes, and even filehandles. These names all begin with a letter or an underscore and may optionally include any combination of letters, numbers, and underscores. When the <code>utf8</code> pragma (<a href="chapter_03.html#unicode">Unicode and Strings</a>(unicode)) is in effect, you may use any UTF-8 word 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 use utf8; 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><em>Names exist primarily for the benefit of the programmer</em>. These rules apply only to literal names which appear as-is in your source code, such as <code>sub fetch_pie</code> or <code>my $waffleiron</code>. Only Perl's parser enforces the rules about identifier names.</p>
+<p>Perl's dynamic nature allows you to refer to entities with names generated at runtime or provided as input to a program. These <em>symbolic lookups</em> provide flexibility at the expense of some safety. In particular, invoking functions or methods indirectly or looking up symbols in a namespace lets you bypass Perl's parser.</p>
+<p>Doing so can produce confusing code. As Mark Jason Dominus recommends so effectively <span class="footnote">(footnote: <span class="url">http://perl.plover.com/varvarname.html</span>)</span>, use a hash (<a href="chapter_03.html#hashes">Hashes</a>(hashes)) or nested data structure (<a href="chapter_03.html#nested_data_structures">Nested Data Structures</a>(nested_data_structures)).</p>
+<h3 id="heading_id_4">Variable Names and Sigils</h3>
+<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="iarrays_0"></div>
+<div id="ivariables__iarrays_0"></div>
+<div id="ihashes_0"></div>
+<div id="ivariables__ihashes_0"></div>
+<div id="isigil_0"></div>
+<p><em>Variable names</em> always have a leading <em>sigil</em> (or symbol) which indicates the type of the variable's value. <em>Scalar variables</em> (<a href="chapter_03.html#scalars">Scalars</a>(scalars)) use the dollar sign (<code>$</code>). <em>Array variables</em> (<a href="chapter_03.html#arrays">Arrays</a>(arrays)) use the at sign (<code>@</code>). <em>Hash variables</em> (<a href="chapter_03.html#hashes">Hashes</a>(hashes)) use the percent sign (<code>%</code>):</p>
+<div class="programlisting">
+<pre>
+<code>    my $scalar;
+    my @array;
+    my %hash;</code>
+</pre></div>
+<p>These sigils provide a visual namespacing for variable names. It's possible--though confusing--to declare multiple variables of the same name with different types:</p>
+<div class="programlisting">
+<pre>
+<code>    my ($bad_name, @bad_name, %bad_name);</code>
+</pre></div>
+<p>Though Perl won't get confused, people reading this code will.</p>
+<div id="ivariant_sigils_0"></div>
+<p>Perl 5's sigils are <em>variant sigils</em>. As context determines how many items you expect from an operation or what type of data you expect to get, so the sigil governs how you manipulate the data of a variable. For example, to access a single element of an array or a hash, you must use the scalar sigil (<code>$</code>):</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>The parallel with amount context is important. Using a scalar element of an aggregate as an <em>lvalue</em> (the target of an assignment, on the left side of the <code>=</code> character) imposes scalar context (<a href="chapter_01.html#context_philosophy">Context</a>(context_philosophy)) on the <em>rvalue</em> (the value assigned, on the right side of the <code>=</code> character).</p>
+<div id="islices_0"></div>
+<p>Similarly, accessing multiple elements of a hash or an array--an operation known as <em>slicing</em>--uses the at symbol (<code>@</code>) and imposes list context <span class="footnote">(footnote: ... even if the list itself has zero or one elements)</span>:</p>
+<div class="programlisting">
+<pre>
+<code>    my @hash_elements  = @hash{ @keys };
+    my @array_elements = @array[ @indexes ];
+
+    my %hash;
+    @hash{ @keys }     = @values;</code>
+</pre></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>
+<h3 id="heading_id_5">Namespaces</h3>
+<div id="ifully45qualified_name_0"></div>
+<div id="inamespaces_0"></div>
+<p>Perl provides a mechanism to group similar functions and variables into their own unique named spaces--<em>namespaces</em> (<a href="chapter_03.html#packages">Packages</a>(packages)). A namespace is a named collection of symbols. Perl allows multi-level namespaces, with names joined by double colons (<code>::</code>), where <code>DessertShop::IceCream</code> refers to a logical collection of related variables and functions, such as <code>scoop()</code> and <code>pour_hot_fudge()</code>.</p>
+<p>Within a namespace, you may use the short name of its members. Outside of the namespace, refer to a member using its <em>fully-qualified name</em>. That is, within <code>DessertShop::IceCream</code>, <code>add_sprinkles()</code> refers to the same function as does <code>DessertShop::IceCream::add_sprinkles()</code> outside of the namespace.</p>
+<p>While 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 core pragmas (<a href="chapter_08.html#pragmas">Pragmas</a>(pragmas)), such as <code>strict</code> and <code>warnings</code>. This is a policy enforced primarily by community guidelines.</p>
+<p>All namespaces in Perl 5 are globally visible. When Perl looks up a symbol in <code>DessertShop::IceCream::Freezer</code>, it looks in the <code>main::</code> symbol table for a symbol representing the <code>DessertShop::</code> namespace, then in there for the <code>IceCream::</code> namespace, and so on. The <code>Freezer::</code> is visible from outside of the <code>IceCream::</code> namespace. The nesting of the former within the latter is only a storage mechanism, and implies nothing further about relationships between parent and child or sibling packages. Only a programmer can make <em>logical</em> relationships between entities obvious--by choosing good names and organizing them well.</p>
+<h2 id="heading_id_6">Variables</h2>
+<div id="variables"></div>
+<div id="ivariable_0"></div>
+<p>A <em>variable</em> in Perl is a storage location for a value (<a href="chapter_03.html#values">Values</a>(values)). While a trivial program can manipulate values directly, most programs work with variables to simplify the logic of the code. A variable represents values; it's easier to explain the Pythagorean theorem in terms of the variables <code>a</code>, <code>b</code>, and <code>c</code> than by intuiting its principle by producing a long list of valid values. This concept may seem basic, but effective programming requires you to manage the art of balancing the generic and reusable with the specific.</p>
+<h3 id="heading_id_7">Variable Scopes</h3>
+<div id="variable_scopes"></div>
+<div id="ivariables__iscope_0"></div>
+<div id="iscope_0"></div>
+<div id="ibuiltins__ipackage_0"></div>
+<p>Variables are visible to portions of your program depending on their scope (<a href="chapter_05.html#scope">Scope</a>(scope)). Most of the variables you will encounter have lexical scope (<a href="chapter_05.html#lexical_scope">Lexical Scope</a>(lexical_scope)). <em>Files</em> themselves provide their own lexical scopes, such that the <code>package</code> declaration on its own does not create a new scope:</p>
+<div class="programlisting">
+<pre>
+<code>    package Store::Toy;
+
+    my $discount = 0.10;
+
+    package Store::Music;
+
+    # $discount still visible
+    say "Our current discount is $discount!";</code>
+</pre></div>
+<div id="ibuiltins__ipackage_1"></div>
+<p>As of Perl 5.14, you may provide a block to the <code>package</code> declaration. This syntax <em>does</em> provide a lexical scope:</p>
+<div class="programlisting">
+<pre>
+<code>    package Store::Toy
+    {
+        my $discount = 0.10;
+    }
+
+    package Store::Music
+    {
+        # $discount not available
+    }
+
+    package Store::BoardGame;
+
+    # $discount still not available</code>
+</pre></div>
+<h3 id="heading_id_8">Variable Sigils</h3>
+<div id="sigils"></div>
+<div id="ivariables__isigils_0"></div>
+<div id="isigils_0"></div>
+<p>The sigil of the variable in a declaration determines the type of the variable: scalar, array, or hash. The sigil used when accessing a variable varies depending on what you do to the variable. For example, you declare an array as <code>@values</code>. Access the first element--a single value--of the array with <code>$values[0]</code>. Access a list of values from the array with <code>@values[ @indices ]</code>.</p>
+<h3 id="heading_id_9">Anonymous Variables</h3>
+<div id="ianonymous_variables_0"></div>
+<div id="ivariables__ianonymous_0"></div>
+<p>Perl variables do not <em>require</em> names. Names exist to help you, the programmer, keep track of an <code>$apple</code>, <code>@barrels</code>, or <code>%cheap_meals</code>. Variables created <em>without</em> literal names in your source code are <em>anonymous</em> variables. The only way to access anonymous variables is by reference (<a href="chapter_03.html#references">References</a>(references)).</p>
+<h3 id="heading_id_10">Variables, Types, and Coercion</h3>
+<div id="ivariables__itypes_0"></div>
+<div id="ivariables__icontainer_type_0"></div>
+<div id="ivariables__ivalue_type_0"></div>
+<p>A variable in Perl 5 represents both a value (a dollar cost, available pizza toppings, guitar shops with phone numbers) and the container which stores that value. Perl's type system deals with <em>value types</em> and <em>container types</em>. While a variable's <em>container type</em>--scalar, array, or hash--cannot change, Perl is flexible about a variable's value type. 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.html#function_references">Function References</a>(function_references)) on the third.</p>
+<p>Performing an operation on a variable which imposes a specific value type may cause coercion (<a href="chapter_03.html#coercion">Coercion</a>(coercion)) from the variable's existing value type.</p>
+<p>For example, the documented way to determine the number of entries in an array is to evaluate that array in scalar context (<a href="chapter_01.html#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 an array evaluated in scalar context returns the number of elements in the array:</p>
+<div class="programlisting">
+<pre>
+<code>    my $count = @items;</code>
+</pre></div>
+<p>This relationship between variable types, sigils, and context is essential.</p>
+<h2 id="heading_id_11">Values</h2>
+<div id="values"></div>
+<div id="ivariables_0"></div>
+<div id="ivalues_0"></div>
+<p>The structure of a program depends heavily on the means by which you model your data with appropriate variables.</p>
+<p>Where variables allow the abstract manipulation of data, the values they hold make programs concrete and useful. The more accurate your values, the better your programs. These values are data--your aunt's name and address, the distance between your office and a golf course on the moon, or the weight of all of the cookies you've eaten in the past year. Within your program, the rules regarding the format of that data are often strict. Effective programs need effective (simple, fast, most compact, most efficient) ways of representing their data.</p>
+<h3 id="heading_id_12">Strings</h3>
+<div id="istrings_0"></div>
+<p>A <em>string</em> is a piece of textual or binary data with no particular formatting or contents. It could be your name, the contents of an image file, or your program itself. A string has meaning in the program only when you give it meaning.</p>
+<div id="istrings__idelimiters_0"></div>
+<p>To represent a literal string in your program, surround it with a pair of quoting characters. The most common <em>string delimiters</em> are single and double quotes:</p>
+<div class="programlisting">
+<pre>
+<code>    my $name    = <strong>'Donner Odinson, Bringer of Despair'</strong>;
+    my $address = <strong>"Room 539, Bilskirnir, Valhalla"</strong>;</code>
+</pre></div>
+<div id="istrings__isingle45quoted_0"></div>
+<p>Characters in a <em>single-quoted string</em> represent themselves literally, with two exceptions. Embed a single quote inside a single-quoted string by escaping the quote with a leading backslash:</p>
+<div class="programlisting">
+<pre>
+<code>    my $reminder = 'Don<strong>\'</strong>t forget to escape '
+                 . 'the single quote!';</code>
+</pre></div>
+<p>You must also escape any backslash at the end of the string to avoid escaping the closing delimiter and producing a syntax error:</p>
+<div class="programlisting">
+<pre>
+<code>    my $exception = 'This string ends with a '
+                  . 'backslash, not a quote: <strong>\\</strong>';</code>
+</pre></div>
+<p>Any other backslash will be part of the string as it appears, unless two backslashes are adjacent, in which case the first will escape the second:</p>
+<div class="programlisting">
+<pre>
+<code>    is('Modern <strong>\</strong> Perl', 'Modern <strong>\\</strong> Perl',
+        'single quotes backslash escaping');</code>
+</pre></div>
+<div id="istrings__idouble45quoted_0"></div>
+<p>A <em>double-quoted string</em> has several more special characters available. For example, you may encode otherwise invisible whitespace characters in the string:</p>
+<div class="programlisting">
+<pre>
+<code>    my $tab       = "<strong>\t</strong>";
+    my $newline   = "<strong>\n</strong>";
+    my $carriage  = "<strong>\r</strong>";
+    my $formfeed  = "<strong>\f</strong>";
+    my $backspace = "<strong>\b</strong>";</code>
+</pre></div>
+<p>This demonstrates a useful principle: the syntax used to declare a string may vary. You can represent a tab within a string with the <code>\t</code> escape or by typing a tab directly. Within Perl's purview, both strings behave the same way, even though the specific representation of the string may differ in the source code.</p>
+<p>A string declaration may cross logical newlines; these two declarations are equivalent:</p>
+<div class="programlisting">
+<pre>
+<code>    my $escaped = "two\nlines";
+    my $literal = "two
+    lines";
+    is $escaped, $literal, 'equivalent \n and newline';</code>
+</pre></div>
+<p>These sequences are often easier to read than their whitespace equivalents.</p>
+<div id="iconcatenation_0"></div>
+<div id="istrings__iconcatenation_0"></div>
+<div id="ioperators__i46_0"></div>
+<p>Perl strings have variable lengths. As you manipulate and modify strings, Perl will change their sizes as appropriate. For example, you can combine multiple strings into a larger string with the <em>concatenation</em> operator <code>.</code>:</p>
+<div class="programlisting">
+<pre>
+<code>    my $kitten = 'Choco' . ' ' . 'Spidermonkey';</code>
+</pre></div>
+<p>This is effectively the same as if you'd initialized the string all at once.</p>
+<div id="iinterpolation_0"></div>
+<div id="istrings__iinterpolation_0"></div>
+<p>You may also <em>interpolate</em> the value of a scalar variable or the values of an array within a double-quoted string, such that the <em>current</em> contents of the variable become part of the string as if you'd concatenated them:</p>
+<div class="programlisting">
+<pre>
+<code>    my $factoid = "<strong>$name</strong> lives at <strong>$address</strong>!";
+
+    # equivalent to
+    my $factoid = $name . ' lives at ' . $address . '!';</code>
+</pre></div>
+<div id="iescaping_0"></div>
+<p>Include a literal double-quote inside a double-quoted string by <em>escaping</em> it (that is, preceding it with a leading backslash):</p>
+<div class="programlisting">
+<pre>
+<code>    my $quote = "\"Ouch,\", he cried.  \"That <em>hurt</em>!\"";</code>
+</pre></div>
+<div id="ioperators__iquoting_0"></div>
+<div id="iq__isingle_quoting_operator_0"></div>
+<div id="ioperators__iq_0"></div>
+<div id="iqq__idouble_quoting_operator_0"></div>
+<div id="ioperators__iqq_0"></div>
+<p>When repeated backslashing becomes unwieldy, use an alternate <em>quoting operator</em> by which you can choose an alternate string delimiter. The <code>q</code> operator indicates single quoting, while the <code>qq</code> operator provides double quoting behavior. The character immediately following the operator determines the characters used to delimit the strings. 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     = <strong>qq{</strong>"Ouch", he said.  "That <em>hurt</em>!"<strong>}</strong>;
+    my $reminder  =  <strong>q^</strong>Don't escape the single quote!<strong>^</strong>;
+    my $complaint =  <strong>q{</strong>It's too early to be awake.<strong>}</strong>;</code>
+</pre></div>
+<div id="iheredocs_0"></div>
+<div id="istrings__iheredocs_0"></div>
+<p>When declaring a complex string with a series of embedded escapes is tedious, use the <em>heredoc</em> syntax to assign one or more lines of a string:</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.
+    We instead, born out of time, remain perfect and
+    perfectly self-aware. We only suffer change as 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- or double-quoted behavior. The default behavior is double-quoted interpolation. <code>END_BLURB</code> 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 <em>must</em> start at the beginning of the line:</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 before the ending delimiter. Yet if you indent the identifier, Perl 5 will <em>not</em> remove equivalent whitespace from the start of each line of the heredoc.</p>
+</div>
+<p>Using a string in a non-string context will induce coercion (<a href="chapter_03.html#coercion">Coercion</a>(coercion)).</p>
+<h3 id="heading_id_13">Unicode and Strings</h3>
+<div id="unicode"></div>
+<div id="iUnicode_0"></div>
+<p><em>Unicode</em> is a system for representing the characters of 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.</p>
+<p>Perl 5 strings can represent either of two separate but related data types:</p>
+<div id="icodepoint_0"></div>
+<ul>
+<li>Sequences of Unicode characters</li>
+<li style="list-style: none; display: inline">
+<p>Each character has a <em>codepoint</em>, a unique number which identifies it in the Unicode character set.</p>
+</li>
+<li>Sequences of octets</li>
+<li style="list-style: none; display: inline">
+<div id="ioctet_0"></div>
+<p>Binary data is a sequence of <em>octets</em>--8 bit numbers, each of which can represent a number between 0 and 255.</p>
+</li>
+</ul>
+<div class="tip">
+<p>Why <em>octet</em> and not <em>byte</em>? Assuming that one character fits in one byte will cause you no end of Unicode grief. Separate the idea of memory storage from character representation.</p>
+</div>
+<p>Unicode strings and binary strings look similar. Each has a <code>length()</code>. Each supports standard string operations 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. You must add a specific meaning to that data.</p>
+<h4 id="heading_id_14">Character Encodings</h4>
+<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 represents a sequence of characters. A <em>Unicode encoding</em> maps octet sequences to characters. Some encodings, such as UTF-8, can encode all of the characters in the Unicode character set. Other encodings represent a subset of Unicode characters. For example, ASCII encodes plain English text with no accented characters, while Latin-1 can represent text in most languages which use the Latin alphabet.</p>
+<p>To avoid most Unicode problems, always decode to and from the appropriate encoding at the inputs and outputs of your program.</p>
+<div class="tip">
+<p>Perl 5.12 supports the Unicode 5.2 standard, while Perl 5.14 supports Unicode 6.0. If you need to care about the differences between Unicode versions, you probably already know to see <span class="url">http://unicode.org/versions/</span>.</p>
+</div>
+<h4 id="heading_id_15">Unicode in Your Filehandles</h4>
+<div id="iopen_0"></div>
+<div id="ibuiltins__iopen_0"></div>
+<div id="iIO_layers_0"></div>
+<p>When you tell Perl that a specific filehandle (<a href="chapter_09.html#files">Files</a>(files)) works with encoded text, Perl will convert the incoming octets to Unicode strings automatically. To do this, add an IO layer to the mode of the <code>open</code> builtin. An <em>IO layer</em> wraps around input or output and converts the data. In this case, the <code>:utf8</code> layer decodes 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>, whether for input or output:</p>
+<div class="programlisting">
+<pre>
+<code>    binmode $fh, ':utf8';
+    my $unicode_string = &lt;$fh&gt;;
+
+    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>
+<h4 id="heading_id_16">Unicode in Your Data</h4>
+<div id="iEncode_0"></div>
+<div id="idecode4041_0"></div>
+<div id="iencode4041_0"></div>
+<p>The core module <code>Encode</code> provides a function named <code>decode()</code> to convert a scalar containing data to a Unicode string. The corresponding <code>encode()</code> function converts from Perl's internal encoding to the desired output encoding:</p>
+<div class="programlisting">
+<pre>
+<code>    my $from_utf8 = decode('utf8', $data);
+    my $to_latin1 = encode('iso-8859-1', $string);</code>
+</pre></div>
+<h4 id="heading_id_17">Unicode in Your Programs</h4>
+<div id="ipragmas__iutf8_0"></div>
+<div id="iutf8_pragma_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_08.html#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 and identifiers:</p>
+<div class="programlisting">
+<pre>
+<code>    use utf8;
+
+    sub £_to_¥ { ... }
+
+    my $yen = £_to_¥('1000£');</code>
+</pre></div>
+<p>To <em>write</em> this code, your text editor must understand UTF-8 and you must save the file with the appropriate encoding.</p>
+<div id="istrings__i_x123125_0"></div>
+<div id="i_x123125__iescape_sequence_for_character_encodings_0"></div>
+<p>Within double-quoted strings, you may 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 id="icharnames_pragma_0"></div>
+<div id="ipragmas__icharnames_0"></div>
+<div id="istrings__i_N123125_0"></div>
+<div id="i_N123125__iescape_sequence_for_named_character_encodings_0"></div>
+<p>Some Unicode characters have names, and these names are often clearer to read than Unicode numbers. Use the <code>charnames</code> pragma to enable them and 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>
+<h4 id="heading_id_18">Implicit Conversion</h4>
+<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 <em>obviously</em> 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 will contain Unicode characters. When you print Unicode characters, Perl will encode the string using UTF-8, because Latin-1 cannot represent the entire set of Unicode characters--Latin-1 is a subset of UTF-8.</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 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 <em>Alice</em> you will never notice any problem, because the Latin-1 representation is the same as the UTF-8 representation. If <code>$name</code> contains a name such as <em>José</em>, <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>It is an ASCII string literal and contains octets.</li>
+<li style="list-style: none; display: inline">
+<div class="programlisting">
+<pre>
+<code>    my $hello = "Hello, ";</code>
+</pre></div>
+</li>
+<li>It is a Latin-1 string literal with no explicit encoding and contains octets.</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>It is a non-ASCII string literal with the <code>utf8</code> or <code>encoding</code> pragma in effect and contains Unicode characters.</li>
+<li style="list-style: none; display: inline">
+<div class="programlisting">
+<pre>
+<code>    use utf8;
+    my $hello = "Kuirabá, ";</code>
+</pre></div>
+</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, for example a concatenation appending UTF-8 data to Latin-1 data, then the resulting sequence of octets makes sense in <em>neither</em> 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 would be incorrectly decoded into five Unicode characters to form <em>José</em> (<em>sic</em>) instead of <em>José</em> because the UTF-8 data means something else when decoded as Latin-1 data.</p>
+<p>See <code>perldoc perluniintro</code> for a far more detailed explanation of Unicode, encodings, and how to manage incoming and outgoing data in a Unicode world <span class="footnote">(footnote: For <em>far</em> more detail about managing Unicode effectively throughout your programs, see Tom Christiansen's answer to "Why does Modern Perl avoid UTF-8 by default?" <span class="url">http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/6163129#6163129</span>)</span>.</p>
+<div class="sidebar">
+<div id="iunicode_strings_0"></div>
+<div id="ipragmas__ifeature_0"></div>
+<p>Perl 5.12 added a feature, <code>unicode_strings</code>, which enables Unicode semantics for all string operations within its scope. Perl 5.14 improved this feature; if you work with Unicode in Perl, it's worth upgrading to at least Perl 5.14.</p>
+</div>
+<h3 id="heading_id_19">Numbers</h3>
+<div id="inumbers_0"></div>
+<div id="iintegers_0"></div>
+<div id="ifloating45point_values_0"></div>
+<p>Perl supports numbers as both integers and floating-point values. You may represent them with scientific notation as well as in binary, octal, and hexadecimal forms:</p>
+<div class="programlisting">
+<pre>
+<code>    my $integer   = 42;
+    my $float     = 0.007;
+    my $sci_float = 1.02e14;
+    my $binary    = <strong>0b</strong>101010;
+    my $octal     = <strong>0</strong>52;
+    my $hex       = <strong>0x</strong>20;</code>
+</pre></div>
+<div id="inumbers__irepresentation_prefixes_0"></div>
+<div id="i0b_0"></div>
+<div id="i0_0"></div>
+<div id="i0x_0"></div>
+<p>The emboldened characters are the numeric prefixes for binary, octal, and hex notation respectively. Be aware that a leading zero on an integer <em>always</em> indicates octal mode.</p>
+<div id="inumbers__iunderscore_separator_0"></div>
+<div id="iunderscore_0"></div>
+<div class="tip">
+<p>Even though you can write floating-point values explicitly in Perl 5 with perfect accuracy, Perl 5 stores them internally in a binary format. This representation 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, lest the parser interpret the commas as comma operators. Instead, use underscores within the number. 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.</p>
+<div id="iScalar5858Util_0"></div>
+<div id="ilooks_like_number4041_0"></div>
+<p>Because of coercion (<a href="chapter_03.html#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 <em>as</em> a number in numeric contexts. In the rare circumstances where you need to know if something looks like a number to Perl, use the <code>looks_like_number</code> function from the core module <code>Scalar::Util</code>. This function 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 provides several well-tested regular expressions to identify more specific valid <em>types</em> (whole number, integer, floating-point value) of numeric values.</p>
+<h3 id="heading_id_20">Undef</h3>
+<div id="iundef_0"></div>
+<p>Perl 5's <code>undef</code> value represents an unassigned, undefined, and unknown value. 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. Evaluating <code>undef</code> in a string context--such as interpolating it into a string--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>
+<div id="ibuiltins__idefined_0"></div>
+<p>The <code>defined</code> builtin returns a true value if its operand evaluates to a defined value (anything other than <code>undef</code>):</p>
+<div class="programlisting">
+<pre>
+<code>    my $status = 'suffering from a cold';
+
+    say <strong>defined</strong> $status;  # 1, which is a true value
+    say <strong>defined</strong> undef;    # empty string; a false value</code>
+</pre></div>
+<h3 id="heading_id_21">The Empty List</h3>
+<div id="iempty_list_0"></div>
+<div id="i4041__iempty_list_0"></div>
+<p>When used on the right-hand side of an assignment, the <code>()</code> construct represents an empty list. In scalar context, this evaluates to <code>undef</code>. In list context, it is an empty list. When used on the left-hand side of an assignment, the <code>()</code> construct imposes list context. To count the number of elements returned from an expression in list context without using a temporary variable, use the idiom (<a href="chapter_10.html#idioms">Idioms</a>(idioms)):</p>
+<div class="programlisting">
+<pre>
+<code>    my $count = <strong>()</strong> = get_all_clown_hats();</code>
+</pre></div>
+<p>Because of the right associativity (<a href="chapter_04.html#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>If you find that concept confusing right now, fear not. As you understand how Perl's fundamental design features fit together in practice, it will make more sense.</p>
+<h3 id="heading_id_22">Lists</h3>
+<div id="ilists_0"></div>
+<p>A list is a comma-separated group of one or more expressions. 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>Parentheses do not <em>create</em> lists. The comma operator creates lists. Where present, the parentheses in these examples group expressions to change their <em>precedence</em> (<a href="chapter_04.html#precedence">Precedence</a>(precedence)).</p>
+<div id="ioperators__irange_0"></div>
+<div id="ioperators__i4646_0"></div>
+<div id="i4646__irange_operator_0"></div>
+<p>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="ioperators__iqw4041_0"></div>
+<div id="iqw4041__iquote_words_operator_0"></div>
+<p>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="tip">
+<p>Perl will emit a warning if a <code>qw()</code> contains a comma or the comment character (<code>#</code>), because not only are such characters rare 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. 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 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>    # don't worry about the details right now
+    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()];
+
+    say context(); # list context
+    context();     # void context</code>
+</pre></div>
+<h2 id="heading_id_23">Control Flow</h2>
+<div id="control_flow"></div>
+<div id="icontrol_flow_0"></div>
+<p>Perl's basic <em>control flow</em> 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>Perl's <em>control flow directives</em> change the order of execution--what happens next in the program--depending on the values of their expressions.</p>
+<h3 id="heading_id_24">Branching Directives</h3>
+<div id="icontrol_flow_directives__iif_0"></div>
+<p>The <code>if</code> directive performs the associated action only when its conditional expression evaluates to a <em>true</em> 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>While the block form requires parentheses around its condition, the postfix form does not.</p>
+<p>The conditional expression may consist of multiple subexpressions, as long as it evaluates to a single top-level expression:</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name eq 'Bob' &amp;&amp; not greeted_bob())
+    {
+        say 'Hello, Bob!';
+        found_bob();
+    }</code>
+</pre></div>
+<p>In the postfix form, adding parentheses can clarify the intent of the code at the expense of visual cleanliness:</p>
+<div class="programlisting">
+<pre>
+<code>    greet_bob() if ($name eq 'Bob' &amp;&amp; not greeted_bob());</code>
+</pre></div>
+<div id="icontrol_flow_directives__iunless_0"></div>
+<p>The <code>unless</code> directive is a negated form of <code>if</code>. Perl will perform the action when the conditional expression evaluates to <em>false</em>:</p>
+<div class="programlisting">
+<pre>
+<code>    say "You're not Bob!" unless $name eq 'Bob';</code>
+</pre></div>
+<p>Like <code>if</code>, <code>unless</code> also has a block form, though many programmers avoid it, as it rapidly becomes difficult to read with complex conditionals:</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.html#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>
+<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>    unless ($name eq 'Bob')
+    {
+        say "I don't know you.";
+        shun_user();
+    }
+    else
+    {
+        say 'Hi, Bob!';
+        greet_user();
+    }</code>
+</pre></div>
+<p>However, the implied double negative of using <code>unless</code> with an <code>else</code> block can be confusing. This example may be the only place you ever see it.</p>
+<p>Just as Perl provides both <code>if</code> and <code>unless</code> to allow you to phrase your conditionals in the most readable way, you can choose between positive and negative conditional operators:</p>
+<div class="programlisting">
+<pre>
+<code>    if ($name <strong>ne</strong> 'Bob')
+    {
+        say "I don't know you.";
+        shun_user();
+    }
+    else
+    {
+        say 'Hi, Bob!';
+        greet_user();
+    }</code>
+</pre></div>
+<p>... though the double negative implied by the presence of the <code>else</code> block suggests inverting the conditional.</p>
+<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>:</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>An <code>unless</code> chain may also use an <code>elsif</code> block <span class="footnote">(footnote: Good luck deciphering that!)</span>. There is no <code>elseunless</code>.</p>
+<p>Writing <code>else if</code> is a syntax error <span class="footnote">(footnote: Larry prefers <code>elsif</code> for aesthetic reasons, as well the prior art of the Ada programming language.)</span>:</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>
+<h3 id="heading_id_25">The Ternary Conditional Operator</h3>
+<div id="iternary_conditional_0"></div>
+<div id="icontrol_flow_directives__iternary_conditional_0"></div>
+<p>The <em>ternary conditional</em> operator evaluates a conditional expression and produces one of two alternatives:</p>
+<div class="programlisting">
+<pre>
+<code>    my $time_suffix = after_noon($time)
+                    ? 'afternoon'
+                    : 'morning';</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 expressions of arbitrary complexity--including other ternary conditional expressions.</p>
+<div class="sidebar">
+<p>An interesting, though obscure, idiom is to use the ternary conditional to select between alternative <em>variables</em>, 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>
+<h4 id="heading_id_26">Short Circuiting</h4>
+<div id="short_circuiting"></div>
+<div id="ishort45circuiting_0"></div>
+<p>Perl exhibits <em>short-circuiting</em> behavior when it encounters complex conditional expressions. When 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, 'subexpression one' )
+                     &amp;&amp; ok( 1, 'subexpression two' );
+
+    done_testing();</code>
+</pre></div>
+<p>The return value of <code>ok()</code> (<a href="chapter_09.html#testing">Testing</a>(testing)) is the boolean value obtained by evaluating the first argument, so this code prints:</p>
+<div class="screen">
+<pre>
+<code>    ok 1 - subexpression one
+    ok 2 - subexpression two
+    Both true!</code>
+</pre></div>
+<p>When the first subexpression--the first call to <code>ok</code>--evaluates to a true value, Perl must evaluate the second subexpression. If the first subexpression had evaluated to a false value, there would be no need to check subsequent subexpressions, as the entire expression could not succeed:</p>
+<div class="programlisting">
+<pre>
+<code>    say "Both true!" if ok( 0, 'subexpression one' )
+                     &amp;&amp; ok( 1, 'subexpression two' );</code>
+</pre></div>
+<p>This example prints:</p>
+<div class="screen">
+<pre>
+<code>    not ok 1 - subexpression one</code>
+</pre></div>
+<p>Even though the second subexpression would obviously succeed, Perl never evaluates it. The same short-circuiting behavior is evident for logical-or operations:</p>
+<div class="programlisting">
+<pre>
+<code>    say "Either true!" if ok( 1, 'subexpression one' )
+                       || ok( 1, 'subexpression two' );</code>
+</pre></div>
+<p>This example prints:</p>
+<div class="screen">
+<pre>
+<code>    ok 1 - subexpression one
+    Either true!</code>
+</pre></div>
+<p>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, as in the case where using an undefined value might raise a warning:</p>
+<div class="programlisting">
+<pre>
+<code>    my $bbq;
+    if (defined $bbq and $bbq eq 'brisket') { ... }</code>
+</pre></div>
+<h3 id="heading_id_27">Context for Conditional Directives</h3>
+<div id="icontext__iconditional_0"></div>
+<div id="iboolean__itrue_0"></div>
+<div id="itrue_0"></div>
+<div id="ifalse_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.html#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.</p>
+<p>Perl 5 has no single true value, nor a single false value. Any number which 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 a false value, 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 true in boolean context, thanks to its string contents.</p>
+<p>Both the empty list and <code>undef</code> evaluate to a false value. Empty arrays and hashes return the number 0 in scalar context, so they evaluate to a false value in boolean context. 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 a true value in boolean context.</p>
+<div class="tip">
+<div id="iCPAN__iWant_0"></div>
+<div id="ipragmas__ioverloading_0"></div>
+<p>The <code>Want</code> module from the CPAN allows you to detect boolean context within your own functions. The core <code>overloading</code> pragma (<a href="chapter_09.html#overloading">Overloading</a>(overloading)) allows you to specify what your own data types produce when evaluated in various contexts.</p>
+</div>
+<h3 id="heading_id_28">Looping Directives</h3>
+<div id="looping_directives"></div>
+<div id="ilooping_directives__ifor_0"></div>
+<div id="ilooping_directives__iforeach_0"></div>
+<p>Perl provides several directives for looping and iteration. The <em>foreach</em>-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 range 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> (<a href="chapter_01.html#default_scalar_variable">The Default Scalar Variable</a>(default_scalar_variable)) to each in turn. Perl executes the block for each integer and prints the squares of the integers.</p>
+<div class="tip">
+<div id="ibuiltins__ifor_1"></div>
+<div id="ibuiltins__iforeach_0"></div>
+<p>Many Perl programmers refer to iteration as <code>foreach</code> loops, but Perl treats the names <code>foreach</code> and <code>for</code> interchangeably. The subsequent code determines the type and behavior of the loop.</p>
+</div>
+<p>Like <code>if</code> and <code>unless</code>, this loop has a postfix form:</p>
+<div class="programlisting">
+<pre>
+<code>    say "$_ * $_ = ", $_ * $_ for 1 .. 10;</code>
+</pre></div>
+<p>A <code>for</code> loop may use a named variable instead of the topic:</p>
+<div class="programlisting">
+<pre>
+<code>    for my $i (1 .. 10)
+    {
+        say "$i * $i = ", $i * $i;
+    }</code>
+</pre></div>
+<p>When a <code>for</code> loop uses an iterator variable, the variable scope is <em>within</em> the loop. Perl will set this lexical to the value of each item in the iteration. Perl will not modify the topic variable (<code>$_</code>). If you have declared a lexical <code>$i</code> in an outer scope, its value will persist 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', 'Value preserved in outer scope' );</code>
+</pre></div>
+<p>This localization occurs even if you do not redeclare the iteration variable as a lexical <span class="footnote">(footnote: ... but <em>do</em> declare your iteration variables as lexicals to reduce their scope.)</span>:</p>
+<div class="programlisting">
+<pre>
+<code>    my $i = 'horse';
+
+    for $i (1 .. 10)
+    {
+        say "$i * $i = ", $i * $i;
+    }
+
+    is( $i, 'horse', 'Value preserved in outer scope' );</code>
+</pre></div>
+<h3 id="heading_id_29">Iteration and Aliasing</h3>
+<div id="iiteration__ialiasing_0"></div>
+<div id="ialiasing_0"></div>
+<div id="ialiasing__iiteration_0"></div>
+<p>The <code>for</code> loop <em>aliases</em> the iterator variable to the values in the iteration such that any modifications to the value of the iterator modifies the iterated value in place:</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>for</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 <em>constant</em> values, however:</p>
+<div class="programlisting">
+<pre>
+<code>    for (qw( Huex Dewex Louid ))
+    {
+        $_++;
+        say;
+    }</code>
+</pre></div>
+<p>Instead Perl will produce an exception about modification of read-only values.</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/\A\s+/;       # trim leading whitespace
+        s/\s+\z/;       # trim trailing whitespace
+
+        $_ = quotemeta; # escape non-word characters
+    }</code>
+</pre></div>
+<h3 id="heading_id_30">Iteration and Scoping</h3>
+<div id="iiteration__iscoping_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. Consider a function <code>topic_mangler()</code> which modifies <code>$_</code> on purpose. If code iterating over a list called <code>topic_mangler()</code> without protecting <code>$_</code>, debugging fun would ensue:</p>
+<div class="programlisting">
+<pre>
+<code>    for (@values)
+    {
+        topic_mangler();
+    }
+
+    sub topic_mangler
+    {
+        s/foo/bar/;
+    }</code>
+</pre></div>
+<p>If you <em>must</em> use <code>$_</code> rather than a named variable, make the topic variable lexical with <code>my $_</code>:</p>
+<div class="programlisting">
+<pre>
+<code>    sub topic_mangler
+    {
+        # was $_ = shift;
+        <strong>my</strong> $_ = 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>
+<h3 id="heading_id_31">The C-Style For Loop</h3>
+<div id="iloops__ifor_0"></div>
+<p>The C-style <em>for loop</em> requires you to manage the conditions of iteration:</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 explicitly assign to an iteration variable in the looping construct, as this loop performs neither aliasing nor assignment to the topic variable. While any variable declared in the loop construct is scoped to the lexical block of the loop, there is no lexicalization of a variable declared outside of the loop construct:</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>The looping construct may have three subexpressions. The first subexpression--the initialization section--executes only once, before the loop body executes. Perl evaluates the second subexpression--the conditional comparison--before each iteration of the loop body. When this evaluates to a true value, iteration proceeds. When it evaluates to a false value, iteration stops. The final subexpression executes after each iteration of the loop body.</p>
+<div class="programlisting">
+<pre>
+<code>    for (
+        # loop initialization subexpression
+        say 'Initializing', my $i = 0;
+
+        # conditional comparison subexpression
+        say "Iteration: $i" and $i &lt; 10;
+
+        # iteration ending subexpression
+        say 'Incrementing ' . $i++
+    )
+    {
+        say "$i * $i = ", $i * $i;
+    }</code>
+</pre></div>
+<p>Note the lack of a semicolon after the final subexpression as well as the use of the comma operator and 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. An infinite <code>for</code> loop might be:</p>
+<div class="programlisting">
+<pre>
+<code>    for (;;) { ... }</code>
+</pre></div>
+<h3 id="heading_id_32">While and Until</h3>
+<div id="iloops__iwhile_0"></div>
+<p>A <em>while</em> loop continues until the loop conditional expression evaluates to a boolean false value. An idiomatic infinite loop is:</p>
+<div class="programlisting">
+<pre>
+<code>    while (1) { ... }</code>
+</pre></div>
+<p>Unlike the iteration <code>foreach</code>-style loop, the <code>while</code> loop's condition has no side effects by itself. That is, 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, use a <em>destructive update</em> of the <code>@values</code> array by modifying the array with each loop iteration:</p>
+<div class="programlisting">
+<pre>
+<code>    while (@values)
+    {
+        my $value = shift @values;
+        say $value;
+    }</code>
+</pre></div>
+<p>Modifying <code>@values</code> inside of the <code>while</code> condition check also works, but it has some subtleties related to the truthiness of each value.</p>
+<div class="programlisting">
+<pre>
+<code>    while (my $value = shift @values)
+    {
+        say $value;
+    }</code>
+</pre></div>
+<p>This loop will exit as soon as it reaches an element that evaluates to a false value, not necessarily when it has exhausted the array. That may be the desired behavior, but is often surprising to novices.</p>
+<div id="iloops__iuntil_0"></div>
+<p>The <em>until</em> loop reverses the sense of the test of the <code>while</code> loop. Iteration continues while the loop conditional expression evaluates to a false value:</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>