<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE dtbook SYSTEM "dtbook-2005-3.dtd">
<?xml-stylesheet type="text/css" href="daisy.css" media="screen"?>
<?xml-stylesheet type="text/xsl" href="daisyTransform.xsl" media="screen"?>
<dtbook xmlns="http://www.daisy.org/z3986/2005/dtbook/" version="2005-3">
  <head>
    <meta name="dtb:uid" content="ebookpdfteachyourselfperlin21days"/>
    <meta name="dc:Title" content="(ebook Pdf) Teach Yourself Perl In 21 Days"/>
  </head>
  <book>
    <frontmatter id="frontmatter_00001">
      <doctitle id="doctitle_00002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#doctitle_00002">(ebook Pdf) Teach Yourself Perl In 21 Days</doctitle>
      <docauthor id="docauthor_00003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#docauthor_00003">Unknown</docauthor>
      <level1 id="level1_00004">
        <h1 id="h1_00005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#h1_00005">Producer's Note</h1>
        <level2 id="level2_00006">
          <h2 id="h2_00007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#h2_00007">About Internet Archive Daisy Books</h2>
          <p id="p_00008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00008">This book was produced in DAISY format by the Internet Archive.  The
    book pages were scanned and converted to DAISY format
    automatically.  This process relies on optical character
    recognition, and is somewhat susceptible to errors.  These errors
    may include weird characters, non-words, and incorrect guesses at
    structure.  Page numbers and headers or footers may remain from
    the scanned page.  The Internet Archive is working to improve the
    scanning process and resulting books, but in the meantime, we hope
    that this book will be useful to you.
    </p>
        </level2>
        <level2 id="level2_00009">
          <h2 id="h2_00010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#h2_00010">About this DAISY book</h2>
          <p id="p_00011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00011">This book as paragraph navigation, but is otherwise unstructured.</p>
        </level2>
        <level2 id="level2_00012">
          <h2 id="h2_00013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#h2_00013">About the Internet Archive</h2>
          <p id="p_00014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00014">The Internet Archive was founded in 1996
    to build an Internet library
and to promote universal access to all knowledge.  The Archive's purposes
include offering permanent access for researchers, historians,
scholars, people with disabilities, and the general public to
historical collections that exist in digital format.  The Internet Archive
includes texts, audio, moving images, and software as well as archived
web pages, and provides specialized services for information access
for the blind and other persons with disabilities.
    </p>
        </level2>
      </level1>
    </frontmatter>
    <bodymatter id="bodymatter_00015">
      <level1 id="level1_00016">
        <h1 id="h1_00017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#h1_00017">Book</h1>
        <p id="p_00018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00018"> •  Workshop</p>
        <p id="p_00019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00019"> o  Quiz o  Exercises</p>
        <p id="p_00020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00020"> Day  2  Basic Operators and Control Flow</p>
        <p id="p_00021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00021"> •   Storing in Scalar Variables Assignment</p>
        <p id="p_00022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00022"> o  The Definition of a Scalar Variable</p>
        <p id="p_00023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00023"> o  Scalar Variable Syntax o  Assigning a Value to a Scalar Variable</p>
        <p id="p_00024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00024"> •  Performing Arithmetic</p>
        <p id="p_00025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00025"> o  Example of Miles-to-Kilometers Conversion</p>
        <p id="p_00026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00026"> o  The chop Library Function</p>
        <p id="p_00027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00027"> •  Expressions</p>
        <p id="p_00028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00028"> o  Assignments and Expressions</p>
        <p id="p_00029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00029"> •  Other Perl Operators</p>
        <p id="p_00030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00030"> •  Introduction to Conditional Statements</p>
        <p id="p_00031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00031"> •  The if Statement</p>
        <p id="p_00032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00032"> o  The Conditional Expression</p>
        <p id="p_00033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00033"> o  The Statement Block o  Testing for Equality Using ==</p>
        <p id="p_00034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00034"> o  Other Comparison Operators</p>
        <p id="p_00035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00035"> •  Two-Way Branching Using if and  else</p>
        <p id="p_00036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00036"> •  Multi-Way Branching Using  elsif</p>
        <p id="p_00037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00037"> •  Writing Loops Using the while Statement</p>
        <p id="p_00038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00038"> •  Nesting Conditional Statements</p>
        <p id="p_00039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00039"> •  Looping Using the until Statement</p>
        <p id="p_00040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00040"> •  Summary</p>
        <p id="p_00041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00041"> •  O&amp;A</p>
        <p id="p_00042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00042"> •  Workshop</p>
        <p id="p_00043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00043"> o  Quiz o  Exercises</p>
        <p id="p_00044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00044"> Day  3  Understanding Scalar Values</p>
        <p id="p_00045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00045"> •   What Is a Scalar Value?</p>
        <p id="p_00046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00046"> •  Integer Scalar Values</p>
        <p id="p_00047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00047"> o  Integer Scalar Value Limitations</p>
        <p id="p_00048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00048"> •  Floating-Point Scalar Values</p>
        <p id="p_00049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00049"> o  Floating-Point Arithmetic and Round-Off Error</p>
        <p id="p_00050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00050"> •  Using Octal and Hexadecimal Notation</p>
        <p id="p_00051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00051"> o  Decimal Notation o  Octal Notation</p>
        <p id="p_00052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00052"> o  Hexadecimal Notation o  Why Bother?</p>
        <p id="p_00053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00053"> •  Character Strings</p>
        <p id="p_00054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00054"> o  Using Double-Quoted Strings</p>
        <p id="p_00055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00055"> o  Escape Sequences o  Single-Quoted Strings</p>
        <p id="p_00056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00056"> •  Interchangeability of Strings and Numeric Values</p>
        <p id="p_00057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00057"> o  Initial Values of Scalar Variables</p>
        <p id="p_00058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00058"> •  Summary</p>
        <p id="p_00059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00059"> •  Q&amp;A</p>
        <p id="p_00060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00060"> •  Workshop</p>
        <p id="p_00061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00061"> o  Quiz o  Exercises</p>
        <p id="p_00062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00062"> Day 4  More Operators</p>
        <p id="p_00063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00063"> •   Using the Arithmetic Operators</p>
        <p id="p_00064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00064"> o  Exponentiation o  The Remainder Operator</p>
        <p id="p_00065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00065"> o  Unary Negation</p>
        <p id="p_00066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00066"> •  Using Comparison Operators</p>
        <p id="p_00067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00067"> o  Integer-Comparison Operators</p>
        <p id="p_00068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00068"> o  String-Comparison Operators o  String Comparison Versus Integer Comparison</p>
        <p id="p_00069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00069"> o  Comparison and Floating-Point Numbers</p>
        <p id="p_00070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00070"> •  Using Logical Operators</p>
        <p id="p_00071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00071"> o  Evaluation Within Logical Operators</p>
        <p id="p_00072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00072"> o  Logical Operators as Subexpressions</p>
        <p id="p_00073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00073"> •  Using Bit-Manipulation Operators</p>
        <p id="p_00074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00074"> o  What Bits Are and How They Are Used</p>
        <p id="p_00075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00075"> o  The Bit-Manipulation Operators</p>
        <p id="p_00076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00076"> •  Using the Assignment Operators</p>
        <p id="p_00077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00077"> o  Assignment Operators as Subexpressions</p>
        <p id="p_00078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00078"> •  Using Autoincrement and Autodecrement</p>
        <p id="p_00079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00079"> o  The Autoincrement Operator Pre-Increment</p>
        <p id="p_00080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00080"> o  The Autoincrement Operator Post-Increment</p>
        <p id="p_00081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00081"> o  The Autodecrement Operator o  Using Autoincrement With Strings</p>
        <p id="p_00082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00082"> •  The String Concatenation and Repetition Operators</p>
        <p id="p_00083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00083"> o  The String-Concatenation Operator</p>
        <p id="p_00084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00084"> o  The String-Repetition Operator</p>
        <p id="p_00085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00085"> o  Concatenation and Assignment</p>
        <p id="p_00086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00086"> •  Other Perl Operators</p>
        <p id="p_00087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00087"> o  The Comma Operator o  The Conditional Operator The Order of Operations</p>
        <p id="p_00088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00088"> o  Precedence o  Associativity</p>
        <p id="p_00089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00089"> o  Forcing Precedence Using Parentheses Summary</p>
        <p id="p_00090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00090"> Q&amp;A Workshop o  Quiz o  Exercises</p>
        <p id="p_00091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00091"> 5  Lists and Array Variables</p>
        <p id="p_00092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00092"> Introducing Lists Scalar Variables and Lists</p>
        <p id="p_00093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00093"> o  Lists and String Substitution Storing Lists in Array Variables Accessing an Element of an Array Variable</p>
        <p id="p_00094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00094"> o  More Details on Array Element Names Using Lists and Arrays in Perl Programs</p>
        <p id="p_00095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00095"> o  Using Brackets and Substituting for Variables Using List Ranges</p>
        <p id="p_00096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00096"> o  Expressions and List Ranges More on Assignment and Array Variables</p>
        <p id="p_00097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00097"> o  Copying from One Array Variable to Another</p>
        <p id="p_00098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00098"> o  Using Array Variables in Lists o  Substituting for Array Variables in Strings o  Assigning to Scalar Variables from Array Variables Retrieving the Length of a List</p>
        <p id="p_00099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00099"> Using Array Slices</p>
        <p id="p_00100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00100"> o  Using List Ranges in Array-Slice Subscripts</p>
        <p id="p_00101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00101"> o  Using Variables in Array-Slice Subscripts</p>
        <p id="p_00102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00102"> o  Assigning to Array Slices</p>
        <p id="p_00103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00103"> o  Overlapping Array Slices o  Using the Array-Slice Notation as a Shorthand Reading an Array from the Standard Input Lile</p>
        <p id="p_00104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00104"> Array Library Lunctions</p>
        <p id="p_00105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00105"> o  Sorting a List or Array Variable o  Reversing a List or Array Variable</p>
        <p id="p_00106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00106"> o  Using chop on Array Variables o  Creating a Single String from a List</p>
        <p id="p_00107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00107"> Splitting a String into a List</p>
        <p id="p_00108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00108"> o</p>
        <p id="p_00109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00109"> o  Other List-Manipulation Functions Summary</p>
        <p id="p_00110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00110"> Q&amp;A Workshop o  Quiz o  Exercises</p>
        <p id="p_00111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00111"> 6  Reading from and Writing to Files</p>
        <p id="p_00112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00112"> Opening a File</p>
        <p id="p_00113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00113"> o  The File Variable o  The Filename o  The File Mode</p>
        <p id="p_00114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00114"> o  Checking Whether the Open Succeeded Reading from a File</p>
        <p id="p_00115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00115"> o  File Variables and the Standard Input File</p>
        <p id="p_00116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00116"> o  Terminating a Program Using  die</p>
        <p id="p_00117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00117"> o  Reading into Array Variables Writing to a File</p>
        <p id="p_00118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00118"> o  The Standard Output File Variable</p>
        <p id="p_00119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00119"> o  Merging Two Files into One Redirecting Standard Input and Standard Output</p>
        <p id="p_00120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00120"> The Standard Error File</p>
        <p id="p_00121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00121"> Closing a File</p>
        <p id="p_00122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00122"> Determining the Status of a File</p>
        <p id="p_00123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00123"> o  File-Test Operator Syntax o  Available File-Test Operators</p>
        <p id="p_00124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00124"> o  More on the  -e  Operator o  Testing for Read Permission-the  -r  Operator</p>
        <p id="p_00125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00125"> o  Checking for Other Permissions</p>
        <p id="p_00126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00126"> o  Checking for Empty Files o  Using File-Test Operators with File Variables Reading from a Sequence of Files o  Reading into an Array Variable Using Command-Fine Arguments as Values</p>
        <p id="p_00127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00127"> o  ARGV  and the &lt;&gt; Operator Opening Pipes</p>
        <p id="p_00128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00128"> Summary</p>
        <p id="p_00129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00129"> Q&amp;A</p>
        <p id="p_00130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00130"> Workshop</p>
        <p id="p_00131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00131"> o  Quiz o  Exercises</p>
        <p id="p_00132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00132"> Day 1  Pattern Matching</p>
        <p id="p_00133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00133"> •  Introduction</p>
        <p id="p_00134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00134"> •  The Match Operators</p>
        <p id="p_00135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00135"> o  Match-Operator Precedence</p>
        <p id="p_00136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00136"> •  Special Characters in Patterns</p>
        <p id="p_00137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00137"> o  The + Character o  The f 1 Special Characters o  The * and ? Special Characters o  Escape Sequences for Special Characters</p>
        <p id="p_00138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00138"> o  Matching Any Letter or Number</p>
        <p id="p_00139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00139"> o  Anchoring Patterns o  Variable Substitution in Patterns</p>
        <p id="p_00140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00140"> o  Excluding Alternatives o  Character-Range Escape Sequences</p>
        <p id="p_00141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00141"> o  Matching Any Character o  Matching a Specified Number of Occurrences</p>
        <p id="p_00142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00142"> o  Specifying Choices o  Reusing Portions of Patterns o  Pattern-Sequence Scalar Variables</p>
        <p id="p_00143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00143"> o  Special-Character Precedence o  Specifying a Different Pattern Delimiter</p>
        <p id="p_00144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00144"> •  Pattern-Matching Options</p>
        <p id="p_00145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00145"> o  Matching All Possible Patterns</p>
        <p id="p_00146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00146"> o  Ignoring Case</p>
        <p id="p_00147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00147"> o  Treating the String as Multiple Lines</p>
        <p id="p_00148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00148"> o  Evaluating a Pattern Only Once o  Treating the String as a Single Line</p>
        <p id="p_00149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00149"> o  Using White Space in Patterns</p>
        <p id="p_00150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00150"> •  The Substitution Operator</p>
        <p id="p_00151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00151"> o  Using Pattern-Sequence Variables in Substitutions</p>
        <p id="p_00152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00152"> o  Options for the Substitution Operator</p>
        <p id="p_00153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00153"> o  Evaluating a Pattern Only Once o  Treating the String as Single or Multiple Lines</p>
        <p id="p_00154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00154"> o  Using White Space in Patterns o  Specifying a Different Delimiter</p>
        <p id="p_00155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00155"> •  The Translation Operator</p>
        <p id="p_00156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00156"> o  Options for the Translation Operator</p>
        <p id="p_00157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00157"> •  Extended Pattern-Matching</p>
        <p id="p_00158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00158"> o  Parenthesizing Without Saving in Memory</p>
        <p id="p_00159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00159"> o  Embedding Pattern Options o  Positive and Negative Look-Ahead</p>
        <p id="p_00160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00160"> o  Pattern Comments</p>
        <p id="p_00161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00161"> •  Summary</p>
        <p id="p_00162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00162"> •  O&amp;A</p>
        <p id="p_00163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00163"> •  Workshop</p>
        <p id="p_00164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00164"> o  Quiz o  Exercises</p>
        <p id="p_00165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00165"> Week 1  Week 1 in Review Week  2  Week 2 at a Glance</p>
        <p id="p_00166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00166"> •   Where You're Going</p>
        <p id="p_00167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00167"> Day  8  More Control Structures</p>
        <p id="p_00168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00168"> •   Using Single-Line Conditional Statements</p>
        <p id="p_00169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00169"> o  Problems with Single-Line Conditional Statements</p>
        <p id="p_00170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00170"> •  Looping Using the  for  Statement</p>
        <p id="p_00171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00171"> o  Using the Comma Operator in a  for  Statement</p>
        <p id="p_00172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00172"> •  Looping Through a List: The  f preach  Statement</p>
        <p id="p_00173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00173"> o  The  f preach  Local Variable o  Changing the Value of the Local Variable o  Using Returned Lists in the  f preach  Statement</p>
        <p id="p_00174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00174"> •  The  do  Statement</p>
        <p id="p_00175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00175"> •  Exiting a Loop Using the  last  Statement</p>
        <p id="p_00176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00176"> •  Using  next  to Start the Next Iteration of a Loop</p>
        <p id="p_00177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00177"> •  The  redo  Statement</p>
        <p id="p_00178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00178"> •  Using Labeled Blocks for Multilevel Jumps</p>
        <p id="p_00179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00179"> o  Using  next  and  redo  with Labels</p>
        <p id="p_00180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00180"> •  The  continue  Block</p>
        <p id="p_00181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00181"> •  The  goto  Statement</p>
        <p id="p_00182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00182"> •  Summary</p>
        <p id="p_00183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00183"> •  O&amp;A</p>
        <p id="p_00184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00184"> •  Workshop</p>
        <p id="p_00185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00185"> o  Quiz o  Exercises</p>
        <p id="p_00186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00186"> Day  9  Using Subroutines</p>
        <p id="p_00187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00187"> •   What Is a Subroutine?</p>
        <p id="p_00188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00188"> •  Defining and Invoking a Subroutine</p>
        <p id="p_00189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00189"> o  Forward References to Subroutines</p>
        <p id="p_00190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00190"> •  Returning a Value from a Subroutine</p>
        <p id="p_00191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00191"> o  Return Values and Conditional Expressions</p>
        <p id="p_00192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00192"> •  The  return  Statement</p>
        <p id="p_00193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00193"> •  Using Local Variables in Subroutines</p>
        <p id="p_00194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00194"> o  Initializing Local Variables</p>
        <p id="p_00195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00195"> •  Passing Values to a Subroutine</p>
        <p id="p_00196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00196"> o  Passing a List to a Subroutine</p>
        <p id="p_00197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00197"> •  Calling Subroutines from Other Subroutines</p>
        <p id="p_00198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00198"> •  Recursive Subroutines</p>
        <p id="p_00199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00199"> •  Passing Arrays by Name Using Aliases</p>
        <p id="p_00200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00200"> •  Using the  do  Statement with Subroutines</p>
        <p id="p_00201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00201"> •  Specifying the Sort Order</p>
        <p id="p_00202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00202"> •  Predefined Subroutines</p>
        <p id="p_00203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00203"> o  Creating Startup Code Using  BEGIN</p>
        <p id="p_00204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00204"> o  Creating Termination Code Using  END o  Handling Non-Existent Subroutines Using  AUTOLOAD</p>
        <p id="p_00205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00205"> •  Summary</p>
        <p id="p_00206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00206"> •  O&amp;A</p>
        <p id="p_00207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00207"> •  Workshop</p>
        <p id="p_00208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00208"> o  Quiz o  Exercises</p>
        <p id="p_00209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00209"> Day  10  Associative Arrays</p>
        <p id="p_00210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00210"> •   Limitations of Array Variables</p>
        <p id="p_00211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00211"> •  Definition</p>
        <p id="p_00212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00212"> •  Referring to Associative Array Elements</p>
        <p id="p_00213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00213"> •  Adding Elements to an Associative Array</p>
        <p id="p_00214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00214"> •  Creating Associative Arrays</p>
        <p id="p_00215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00215"> •  Copying Associative Arrays from Array Variables</p>
        <p id="p_00216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00216"> •  Adding and Deleting Array Elements</p>
        <p id="p_00217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00217"> •  Listing Array Indexes and Values</p>
        <p id="p_00218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00218"> •  Looping Using an Associative Array</p>
        <p id="p_00219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00219"> •  Creating Data Structures Using Associative Arrays</p>
        <p id="p_00220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00220"> o  Linked Lists o  Structures o  Trees o  Databases</p>
        <p id="p_00221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00221"> o  Example: A Calculator Program</p>
        <p id="p_00222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00222"> •  Summary</p>
        <p id="p_00223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00223"> •  Q&amp;A</p>
        <p id="p_00224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00224"> •  Workshop</p>
        <p id="p_00225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00225"> o  Quiz o  Exercises</p>
        <p id="p_00226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00226"> Day  11  Formatting Your Output</p>
        <p id="p_00227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00227"> •  Defining a Print Format</p>
        <p id="p_00228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00228"> •  Displaying a Print Format</p>
        <p id="p_00229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00229"> •  Displaying Values in a Print Format</p>
        <p id="p_00230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00230"> o  Creating a General-Purpose Print Format</p>
        <p id="p_00231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00231"> o  Choosing a Value-Field Format</p>
        <p id="p_00232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00232"> o  Printing Value-Field Characters o  Using the Multiline Field Format</p>
        <p id="p_00233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00233"> •  Writing to Other Output Files</p>
        <p id="p_00234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00234"> o  Saving the Default File Variable</p>
        <p id="p_00235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00235"> •  Specifying a Page Header</p>
        <p id="p_00236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00236"> o  Changing the Header Print Format</p>
        <p id="p_00237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00237"> •  Setting the Page Length</p>
        <p id="p_00238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00238"> o  Using  print  with Pagination</p>
        <p id="p_00239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00239"> •  Formatting Long Character Strings</p>
        <p id="p_00240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00240"> o  Eliminating Blank Lines When Formatting</p>
        <p id="p_00241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00241"> o  Supplying an Indefinite Number of Lines</p>
        <p id="p_00242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00242"> •  Lormatting Output Using  print f</p>
        <p id="p_00243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00243"> •  Summary</p>
        <p id="p_00244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00244"> •  O&amp;A</p>
        <p id="p_00245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00245"> •  Workshop</p>
        <p id="p_00246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00246"> o  Quiz o  Exercises</p>
        <p id="p_00247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00247"> Day  12  Working with the File System</p>
        <p id="p_00248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00248"> •   Lile Input and Output Lunctions</p>
        <p id="p_00249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00249"> o  Basic Input and Output Lunctions</p>
        <p id="p_00250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00250"> o  Skipping and Rereading Data o  System Read and Write Lunctions</p>
        <p id="p_00251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00251"> o  Reading Characters Using  qetc o  Reading a Binary Lile Using  binmode</p>
        <p id="p_00252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00252"> •  Directory-Manipulation Lunctions</p>
        <p id="p_00253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00253"> o  The  mkdir  Lunction o  The  chdir  Lunction o  The  opendir  Lunction o  The  closedir  Lunction</p>
        <p id="p_00254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00254"> o  The  readdir  Lunction o  The  telldir  and  seekdir  Lunctions</p>
        <p id="p_00255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00255"> o  The  rewinddir  Lunction</p>
        <p id="p_00256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00256"> o  The  rmdir  Lunction</p>
        <p id="p_00257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00257"> •  Lile-Attribute Lunctions</p>
        <p id="p_00258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00258"> o  Lile-Relocation Lunctions</p>
        <p id="p_00259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00259"> o  Link and Symbolic Link Lunctions</p>
        <p id="p_00260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00260"> o  File-Permission Functions o  Miscellaneous Attribute Functions</p>
        <p id="p_00261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00261"> •  Using DBM Files</p>
        <p id="p_00262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00262"> o  The dbmopen Function o  The dbmclose Function</p>
        <p id="p_00263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00263"> •  Summary</p>
        <p id="p_00264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00264"> •  Q&amp;A</p>
        <p id="p_00265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00265"> •  Workshop</p>
        <p id="p_00266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00266"> o  Quiz o  Exercises</p>
        <p id="p_00267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00267"> Day  13  Process ,  String ,  and Mathematical Functions</p>
        <p id="p_00268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00268"> •   Process- and Program-Manipulation Functions</p>
        <p id="p_00269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00269"> o  Starting a Process o  Terminating a Program or Process</p>
        <p id="p_00270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00270"> o  Execution Control Functions o  Miscellaneous Control Functions</p>
        <p id="p_00271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00271"> •  Mathematical Functions</p>
        <p id="p_00272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00272"> o  The_  sin and cos Functions</p>
        <p id="p_00273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00273"> o  The at an 2 Function o  The sqrt Function o  The exp Function o  The log Function o  The abs Function o  The rand and srand Functions</p>
        <p id="p_00274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00274"> •  String-Manipulation Functions</p>
        <p id="p_00275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00275"> o  The index Function o  The r index Function o  The length Function o  Retrieving String Length Using tr</p>
        <p id="p_00276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00276"> o  The pos Function o  The s ub s t r Function o  The study Function o  Case Conversion Functions</p>
        <p id="p_00277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00277"> o  The quotemeta Function</p>
        <p id="p_00278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00278"> O  The.  join Function o  The sprintf Function</p>
        <p id="p_00279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00279"> •  Summary</p>
        <p id="p_00280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00280"> •  Q&amp;A</p>
        <p id="p_00281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00281"> •  Workshop</p>
        <p id="p_00282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00282"> o  Quiz o  Exercises</p>
        <p id="p_00283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00283"> Day  14  Scalar-Conversion and List-Manipulation Functions</p>
        <p id="p_00284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00284"> •   The chop Function</p>
        <p id="p_00285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00285"> •  The chomp Function</p>
        <p id="p_00286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00286"> •  The crypt Function</p>
        <p id="p_00287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00287"> •  The hex Function</p>
        <p id="p_00288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00288"> •  The.  int Function</p>
        <p id="p_00289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00289"> •  The  oct Function</p>
        <p id="p_00290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00290"> o  The  oct Function and Hexadecimal Integers</p>
        <p id="p_00291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00291"> •  The ord and chr Functions</p>
        <p id="p_00292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00292"> •  The scalar Function</p>
        <p id="p_00293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00293"> •  The pack Function</p>
        <p id="p_00294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00294"> o  The pack Function and C Data Types</p>
        <p id="p_00295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00295"> •  The unpack Function</p>
        <p id="p_00296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00296"> o  Unpacking Strings o  Skipping Characters When Unpacking</p>
        <p id="p_00297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00297"> o  The unpack Function and uuencode</p>
        <p id="p_00298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00298"> •  The  vec Function</p>
        <p id="p_00299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00299"> •  The defined Function</p>
        <p id="p_00300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00300"> •  The undef Function</p>
        <p id="p_00301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00301"> •  Array and List Functions</p>
        <p id="p_00302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00302"> o  The qrep Function o  The splice Function o  The shift Function o  The un shift Function</p>
        <p id="p_00303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00303"> o  The push Function o  The  pop Function o  Creating Stacks and Queues</p>
        <p id="p_00304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00304"> o  The split Function o  The s ort and reverse Functions</p>
        <p id="p_00305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00305"> O  The  map Function o  The wantarray Function</p>
        <p id="p_00306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00306"> •  Associative Array Functions</p>
        <p id="p_00307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00307"> o  The keys Function o  The values Function o  The each Function o  The delete Function o  The  exists  Function</p>
        <p id="p_00308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00308"> •  Summary</p>
        <p id="p_00309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00309"> •  O&amp;A</p>
        <p id="p_00310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00310"> •  Workshop</p>
        <p id="p_00311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00311"> o  Quiz o  Exercises</p>
        <p id="p_00312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00312"> Week 2  Week 2 in Review</p>
        <p id="p_00313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00313"> Week  3  Week 3 at a Glance</p>
        <p id="p_00314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00314"> •   Where You're Going</p>
        <p id="p_00315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00315"> Day  15  System Functions</p>
        <p id="p_00316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00316"> •  System Library Emulation Functions</p>
        <p id="p_00317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00317"> o  The  cretqrent  Function o  The  setqrent  and  endqrent  Functions</p>
        <p id="p_00318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00318"> o  The  cretqrnam  Function</p>
        <p id="p_00319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00319"> o  The  get grid  Function o  The  cretnetent  Function o  The  getnetbyaddr  Function</p>
        <p id="p_00320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00320"> o  The  get net byname  Function O  The  setnetent  and  endnetent  Functions</p>
        <p id="p_00321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00321"> o  The  crethostbyaddr  Function</p>
        <p id="p_00322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00322"> o  The  gethostbyname  Function</p>
        <p id="p_00323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00323"> o  The  crethostent, sethostent,  and  endhostent  Functions</p>
        <p id="p_00324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00324"> o  The  get login  Function o  The  qetpqrp  and  setpqrp  Functions</p>
        <p id="p_00325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00325"> o  The  cretppid  Function o  The  cretpwnam  Function</p>
        <p id="p_00326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00326"> o  The  cretpwuid  Function</p>
        <p id="p_00327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00327"> o  The  cretpwent  Function</p>
        <p id="p_00328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00328"> o  The  setpwent  and  endpwent  Functions</p>
        <p id="p_00329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00329"> o  The  cretpriority  and  setpriority  Functions</p>
        <p id="p_00330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00330"> o  The  cretprotoent  Function</p>
        <p id="p_00331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00331"> o  The  cretprotobyname  and  getprotobynumber  Functions</p>
        <p id="p_00332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00332"> o  The  setprotoent  and  endprotoent  Functions</p>
        <p id="p_00333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00333"> o  The  cretservent  Function</p>
        <p id="p_00334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00334"> o  The  getservbyname  and  get servbyport  Functions</p>
        <p id="p_00335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00335"> O  The.  setservent  and  endservent  Functions</p>
        <p id="p_00336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00336"> o  The  chroot  Function O  The  i octl  Function o  The  alarm  Function o  Calling the System  select  Function</p>
        <p id="p_00337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00337"> o  The  dump  Function</p>
        <p id="p_00338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00338"> •  Socket-Manipulation Functions</p>
        <p id="p_00339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00339"> o  The  socket  Function o  The  bind  Function</p>
        <p id="p_00340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00340"> o  The 1  isten  Function</p>
        <p id="p_00341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00341"> o  The accept Function o  The  connect Function o  The shutdown Function o  The socketpair Function o  The getsockopt and setsockopt Functions</p>
        <p id="p_00342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00342"> o  The getsockname and getpeername Functions</p>
        <p id="p_00343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00343"> •  The UNIX System V IPC Functions</p>
        <p id="p_00344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00344"> o  IPC Functions and the require Statement</p>
        <p id="p_00345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00345"> o  The msqqet Function o  The msqsnd Function o  The  msqrcv Function o  The ms get 1 Function o  The shmqet Function o  The shmwrite Function</p>
        <p id="p_00346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00346"> o  The shmread Function</p>
        <p id="p_00347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00347"> o  The shmctl Function o  The semget Function o  The semop Function o  The  semctl Function</p>
        <p id="p_00348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00348"> •  Summary</p>
        <p id="p_00349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00349"> •  Q&amp;A</p>
        <p id="p_00350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00350"> •  Workshop</p>
        <p id="p_00351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00351"> o  Quiz o  Exercises</p>
        <p id="p_00352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00352"> Day  16  Command-Line Options</p>
        <p id="p_00353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00353"> •  Specifying Options</p>
        <p id="p_00354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00354"> o  Specifying Options on the Command Line</p>
        <p id="p_00355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00355"> o  Specifying an Option in the Program</p>
        <p id="p_00356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00356"> •  The  -v Option: Printing the Perl Version Number</p>
        <p id="p_00357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00357"> •  The -c Option: Checking Your Syntax</p>
        <p id="p_00358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00358"> •  The  -w Option: Printing Warnings</p>
        <p id="p_00359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00359"> o  Checking for Possible Typos o  Checking for Redefined Subroutines o  Checking for Incorrect Comparison Operators</p>
        <p id="p_00360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00360"> •  The -e Option: Executing a Single-Line Program</p>
        <p id="p_00361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00361"> •  The -s Option: Supplying Your Own Command-Line Options</p>
        <p id="p_00362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00362"> o  The - s Option and Other Command-Line Arguments</p>
        <p id="p_00363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00363"> •  The  -P  Option: Using the C Preprocessor</p>
        <p id="p_00364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00364"> o  The C Preprocessor: A Quick Overview</p>
        <p id="p_00365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00365"> •  The - I Option: Searching for C Include Piles</p>
        <p id="p_00366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00366"> •  The  -n  Option: Operating on Multiple Liles</p>
        <p id="p_00367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00367"> •  The  -p  Option: Operating on Files and Printing</p>
        <p id="p_00368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00368"> •  The  -i  Option: Editing Files</p>
        <p id="p_00369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00369"> o  Backing Up Input Files Using the  -i  Option</p>
        <p id="p_00370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00370"> •  The  -a  Option: Splitting Lines</p>
        <p id="p_00371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00371"> •  The  -F  Option: Specifying the Split Pattern</p>
        <p id="p_00372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00372"> •  The -0 Option: Specifying Input End-of-Line</p>
        <p id="p_00373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00373"> •  The -1 Option: Specifying Output End-of-Line</p>
        <p id="p_00374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00374"> •  The  -x Option: Extracting a Program from a Message</p>
        <p id="p_00375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00375"> •  Miscellaneous Options</p>
        <p id="p_00376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00376"> o  The  -u Option o  The  -U  Option o  The  -S  Option o  The  -D  Option</p>
        <p id="p_00377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00377"> o  The  -T  Option: Writing Secure Programs</p>
        <p id="p_00378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00378"> •  The  -d  Option: Using the Perl Debugger</p>
        <p id="p_00379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00379"> •  Summary</p>
        <p id="p_00380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00380"> •  O&amp;A</p>
        <p id="p_00381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00381"> •  Workshop</p>
        <p id="p_00382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00382"> o  Quiz o  Exercises</p>
        <p id="p_00383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00383"> Day  17  System Variables</p>
        <p id="p_00384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00384"> •  Global Scalar Variables</p>
        <p id="p_00385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00385"> o  The Default Scalar Variable: $</p>
        <p id="p_00386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00386"> o  The Program Name: $  0 o  The User ID: $&lt; and $&gt; o  The Group ID: $ ( and $)</p>
        <p id="p_00387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00387"> o  The Version Number: $ 1 o  The Input Line Separator: $ /</p>
        <p id="p_00388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00388"> o  The Output Line Separator: $</p>
        <p id="p_00389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00389"> o  The Output Lield Separator: $, o  The Array Element Separator: $ "</p>
        <p id="p_00390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00390"> o  The Number Output Lormat: $ #</p>
        <p id="p_00391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00391"> o  The  eval  Error Message: $@</p>
        <p id="p_00392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00392"> o  The System Error Code: $ ? o  The System Error Message: $ !</p>
        <p id="p_00393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00393"> o  The Current Line Number: $ .</p>
        <p id="p_00394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00394"> o  Multiline Matching: $* o  The First Array Subscript: $ [</p>
        <p id="p_00395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00395"> o  Multidimensional Associative Arrays and the $; Variable</p>
        <p id="p_00396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00396"> o  The Word-Break Specifier: $ :</p>
        <p id="p_00397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00397"> The Perl Process ID: $ $</p>
        <p id="p_00398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00398"> o</p>
        <p id="p_00399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00399"> o  The Current Filename:  $ARGV</p>
        <p id="p_00400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00400"> o  The Write Accumulator:  $ A A o  The Internal Debugging Value: $  A D</p>
        <p id="p_00401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00401"> o  The System File Flag: $  A F o  Controlling File Editing Using $  A I o  The Format Form-Feed Character:  $ A L</p>
        <p id="p_00402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00402"> o  Controlling Debugging:  $ A P o  The Program Start Time: $  A T o  Suppressing Warning Messages:  $ A W</p>
        <p id="p_00403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00403"> o  The $  A X  Variable</p>
        <p id="p_00404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00404"> •  Pattern System Variables</p>
        <p id="p_00405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00405"> o  Retrieving Matched Subpatterns</p>
        <p id="p_00406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00406"> o  Retrieving the Entire Pattern: $ &amp;</p>
        <p id="p_00407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00407"> o  Retrieving the Unmatched Text: the $ v  and $ ' Variables</p>
        <p id="p_00408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00408"> o  The $+ Variable</p>
        <p id="p_00409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00409"> •  File System Variables</p>
        <p id="p_00410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00410"> o  The Default Print Format: $ ~</p>
        <p id="p_00411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00411"> o  Specifying Page Length: $ = o  Lines Remaining on the Page: $-</p>
        <p id="p_00412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00412"> o  The Page Header Print Format: $  A</p>
        <p id="p_00413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00413"> o  Buffering Output: $ | o  The Current Page Number: $%</p>
        <p id="p_00414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00414"> •  Array System Variables</p>
        <p id="p_00415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00415"> o  The @ Variable o  The  @ARGV  Variable o  The  @F  Variable o  The  @INC  Variable o  The %  INC  Variable o  The  %ENV  Variable o  The %  SIG  Variable</p>
        <p id="p_00416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00416"> •  Built-In File Variables</p>
        <p id="p_00417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00417"> o  STDIN, STDOUT, and STDERR</p>
        <p id="p_00418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00418"> o  ARGV o  DATA</p>
        <p id="p_00419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00419"> o  The Underscore File Variable</p>
        <p id="p_00420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00420"> •  Specifying System Variable Names as Words</p>
        <p id="p_00421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00421"> •  Summary</p>
        <p id="p_00422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00422"> •  Q&amp;A</p>
        <p id="p_00423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00423"> •  Workshop</p>
        <p id="p_00424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00424"> o  Quiz o  Exercises</p>
        <p id="p_00425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00425"> Day  18  References in Perl 5</p>
        <p id="p_00426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00426"> Introduction to References</p>
        <p id="p_00427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00427"> Using References</p>
        <p id="p_00428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00428"> •  Using the Backslash Operator</p>
        <p id="p_00429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00429"> •  References and Arrays</p>
        <p id="p_00430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00430"> •  Multidimensional Arrays</p>
        <p id="p_00431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00431"> •  References to Subroutines</p>
        <p id="p_00432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00432"> o  Using Subroutine Templates</p>
        <p id="p_00433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00433"> •  Using Subroutines to Work with Multiple Arrays</p>
        <p id="p_00434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00434"> o  Pass By Value or By Reference?</p>
        <p id="p_00435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00435"> •  References to File Handles</p>
        <p id="p_00436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00436"> o  What Does the * variable Operator Do?</p>
        <p id="p_00437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00437"> •  Using Symbolic References... Again</p>
        <p id="p_00438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00438"> o  Declaring Variables with Curly Braces</p>
        <p id="p_00439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00439"> •  More on Hard Versus Symbolic References</p>
        <p id="p_00440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00440"> •  For More Information</p>
        <p id="p_00441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00441"> •  Summary</p>
        <p id="p_00442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00442"> •  Q&amp;A</p>
        <p id="p_00443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00443"> •  Workshop</p>
        <p id="p_00444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00444"> o  Quiz</p>
        <p id="p_00445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00445"> •  Exercises</p>
        <p id="p_00446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00446"> 19  Object-Oriented Programming in Perl</p>
        <p id="p_00447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00447"> •   An Introduction to Modules</p>
        <p id="p_00448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00448"> o  The Three Important Rules</p>
        <p id="p_00449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00449"> •  Classes in Perl</p>
        <p id="p_00450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00450"> •  Creating a Class</p>
        <p id="p_00451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00451"> •  Blessing a Constructor</p>
        <p id="p_00452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00452"> o  Instance Variables</p>
        <p id="p_00453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00453"> •  Methods</p>
        <p id="p_00454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00454"> •  Exporting Methods</p>
        <p id="p_00455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00455"> •  Invoking Methods</p>
        <p id="p_00456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00456"> •  Overrides</p>
        <p id="p_00457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00457"> •  Destructors</p>
        <p id="p_00458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00458"> •  Inheritance</p>
        <p id="p_00459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00459"> •  Overriding Methods</p>
        <p id="p_00460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00460"> •  A Few Comments About Classes and Objects in Perl</p>
        <p id="p_00461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00461"> •  Summary</p>
        <p id="p_00462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00462"> •  Q&amp;A</p>
        <p id="p_00463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00463"> •  Workshop</p>
        <p id="p_00464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00464"> o  Quiz o  Exercises</p>
        <p id="p_00465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00465"> Day  20  Miscellaneous Features of Perl</p>
        <p id="p_00466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00466"> •   The require Function</p>
        <p id="p_00467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00467"> o  The require Function and Subroutine Libraries</p>
        <p id="p_00468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00468"> o  Using require to Specify a Perl Version</p>
        <p id="p_00469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00469"> •  The  $#array  Variables</p>
        <p id="p_00470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00470"> o  Controlling Array Length Using  $#array</p>
        <p id="p_00471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00471"> •  Alternative String Delimiters</p>
        <p id="p_00472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00472"> o  Defining Strings Using &lt;&lt;</p>
        <p id="p_00473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00473"> •  Special Internal Values</p>
        <p id="p_00474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00474"> •  Using Back Quotes to Invoke System Commands</p>
        <p id="p_00475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00475"> •  Pattern Matching Using ?? and the  reset  Function</p>
        <p id="p_00476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00476"> o  Using  reset  with Variables</p>
        <p id="p_00477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00477"> •  Other Features of the &lt;&gt; Operator</p>
        <p id="p_00478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00478"> o  Scalar Variable Substitution and &lt;&gt;</p>
        <p id="p_00479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00479"> o  Creating a List of Filenames</p>
        <p id="p_00480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00480"> •  Global Indirect References and Aliases</p>
        <p id="p_00481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00481"> •  Packages</p>
        <p id="p_00482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00482"> o  Defining a Package o  Switching Between Packages</p>
        <p id="p_00483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00483"> o  The  main Package o  Referring to One Package from Another</p>
        <p id="p_00484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00484"> o  Specifying No Current Package</p>
        <p id="p_00485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00485"> o  Packages and Subroutines o  Defining Private Data Using Packages</p>
        <p id="p_00486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00486"> o  Packages and System Variables</p>
        <p id="p_00487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00487"> o  Accessing Symbol Tables</p>
        <p id="p_00488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00488"> •  Modules</p>
        <p id="p_00489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00489"> o  Creating a Module o  Importing Modules Into Your Program</p>
        <p id="p_00490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00490"> o  Using Predefined Modules</p>
        <p id="p_00491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00491"> •  Using Perl in C Programs</p>
        <p id="p_00492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00492"> •  Perl and CGI Scripts</p>
        <p id="p_00493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00493"> •  Translators and Other Supplied Code</p>
        <p id="p_00494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00494"> •  Summary</p>
        <p id="p_00495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00495"> •  O&amp;A</p>
        <p id="p_00496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00496"> •  Workshop</p>
        <p id="p_00497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00497"> o  Quiz o  Exercises</p>
        <p id="p_00498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00498"> Day  21  The Perl Debugger</p>
        <p id="p_00499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00499"> •   Entering and Exiting the Perl Debugger</p>
        <p id="p_00500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00500"> o</p>
        <p id="p_00501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00501"> Entering the Debugger o  Exiting the Debugger</p>
        <p id="p_00502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00502"> •  Listing Your Program</p>
        <p id="p_00503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00503"> o  The 1 command o  The - Command o  The  w  Command o  The / / and ? ? Commands</p>
        <p id="p_00504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00504"> o  The  S  Command</p>
        <p id="p_00505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00505"> •  Stepping Through Programs</p>
        <p id="p_00506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00506"> o  The s Command o  The  n  Command o  The  f  command o  The Carriage-Return Command</p>
        <p id="p_00507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00507"> o  The  r  Command</p>
        <p id="p_00508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00508"> •  Displaying Variable Values</p>
        <p id="p_00509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00509"> o  The  X  Command O  The  V  Command</p>
        <p id="p_00510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00510"> •  Breakpoints</p>
        <p id="p_00511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00511"> o  The  b  Command o  The  c  Command o  The  L  Command and Breakpoints</p>
        <p id="p_00512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00512"> o  The  d  and  D  Commands</p>
        <p id="p_00513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00513"> •  Tracing Program Execution</p>
        <p id="p_00514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00514"> •  Line Actions</p>
        <p id="p_00515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00515"> o  The  a  Command o  The  A  Command o  The &lt; and &gt; Commands</p>
        <p id="p_00516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00516"> o  Displaying Line Actions Using the  L  Command</p>
        <p id="p_00517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00517"> •  Other Debugging Commands</p>
        <p id="p_00518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00518"> o  Executing Other Perl Statements o  The  H  Command: Listing Preceding Commands</p>
        <p id="p_00519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00519"> o  The ! Command: Executing Previous Commands</p>
        <p id="p_00520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00520"> o  The  T  Command: Stack Tracing o  The  p  Command: Printing an Expression</p>
        <p id="p_00521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00521"> o  The = Command: Defining Aliases</p>
        <p id="p_00522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00522"> o  Predefining Aliases o  The  h  Command: Debugger Help</p>
        <p id="p_00523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00523"> •  Summary</p>
        <p id="p_00524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00524"> •  Q&amp;A</p>
        <p id="p_00525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00525"> •  Workshop</p>
        <p id="p_00526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00526"> o  Quiz</p>
        <p id="p_00527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00527"> Week  3  Week 3 in Review</p>
        <p id="p_00528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00528"> Appendix  A  Answers</p>
        <p id="p_00529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00529"> •  Answers for Day 1, ’’Getting Started"</p>
        <p id="p_00530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00530"> o  Quiz o  Exercises</p>
        <p id="p_00531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00531"> •  Answers for Day 2, "Basic Operators and Control Flow”</p>
        <p id="p_00532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00532"> o  Quiz o  Exercises</p>
        <p id="p_00533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00533"> •  Answers for Day 3, ’’Understanding Scalar Values”</p>
        <p id="p_00534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00534"> o  Quiz o  Exercises</p>
        <p id="p_00535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00535"> •  Answers for Day 4, ’’More Operators"</p>
        <p id="p_00536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00536"> o  Quiz o  Exercises</p>
        <p id="p_00537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00537"> •  Answers for Day 5, ’’Lists and Array Variables"</p>
        <p id="p_00538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00538"> o  Quiz o  Exercises</p>
        <p id="p_00539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00539"> •  Answers for Day 6, ’’Reading from and Writing to Files"</p>
        <p id="p_00540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00540"> o  Quiz o  Exercises</p>
        <p id="p_00541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00541"> •  Answers for Day 7, ’’Pattern Matching”</p>
        <p id="p_00542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00542"> o  Quiz o  Exercises</p>
        <p id="p_00543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00543"> •  Answers for Day 8, ’’More Control Structures”</p>
        <p id="p_00544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00544"> o  Quiz o  Exercises</p>
        <p id="p_00545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00545"> •  Answers for Day 9, "Using Subroutines”</p>
        <p id="p_00546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00546"> o  Quiz o  Exercises</p>
        <p id="p_00547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00547"> •  Answers for Day 10, "Associative Arrays"</p>
        <p id="p_00548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00548"> o  Quiz o  Exercises</p>
        <p id="p_00549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00549"> •  Answers for Day 11, ’’Formatting Your Output”</p>
        <p id="p_00550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00550"> o  Quiz o  Exercises</p>
        <p id="p_00551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00551"> •  Answers for Day 12, ’’Working with the File System”</p>
        <p id="p_00552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00552"> o  Quiz o  Exercises</p>
        <p id="p_00553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00553"> •  Answers for Day 13, ’’Process, String, and Mathematical Functions”</p>
        <p id="p_00554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00554"> o  Quiz o  Exercises</p>
        <p id="p_00555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00555"> •  Answers for Day 14, ’’Scalar-Conversion and List-Manipulation Functions"</p>
        <p id="p_00556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00556"> o  Quiz</p>
        <p id="p_00557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00557"> o  Exercises</p>
        <p id="p_00558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00558"> •  Answers for Day 15, ’’System Functions"</p>
        <p id="p_00559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00559"> o  Quiz o  Exercises</p>
        <p id="p_00560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00560"> •  Answers for Day 16, ’'Command-Line Options”</p>
        <p id="p_00561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00561"> o  Quiz o  Exercises</p>
        <p id="p_00562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00562"> •  Answers for Day 17, ’’System Variables”</p>
        <p id="p_00563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00563"> o  Quiz o  Exercises</p>
        <p id="p_00564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00564"> •  Answers for Day 18, ’’References in</p>
        <p id="p_00565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00565"> Perl 5”</p>
        <p id="p_00566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00566"> o  Quiz o  Exercises</p>
        <p id="p_00567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00567"> •  Answers for Day 19, "Object-Oriented Programming in Perl"</p>
        <p id="p_00568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00568"> o  Quiz o  Exercises</p>
        <p id="p_00569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00569"> •  Answers for Day 20, ’’Miscellaneous Features of Perl”</p>
        <p id="p_00570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00570"> o  Quiz o  Exercises</p>
        <p id="p_00571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00571"> •  Answers for Day 21, "The Perl Debugger”</p>
        <p id="p_00572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00572"> o  Quiz</p>
        <p id="p_00573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00573"> Appendix  B  ASCII Character Set</p>
        <p id="p_00574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00574"> Credits</p>
        <p id="p_00575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00575"> Copyright  © 1996 by Sams Publishing</p>
        <p id="p_00576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00576"> SECOND EDITION</p>
        <p id="p_00577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00577"> All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams Publishing, 201 W. 103rd St., Indianapolis, IN 46290.</p>
        <p id="p_00578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00578"> International Standard Book Number:  0-672-30894-0  HTML conversion by :</p>
        <p id="p_00579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00579"> M/s. LeafWriters (India) Pvt. Ltd.</p>
        <p id="p_00580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00580"> Website  :  http://leaf.stpn.soft.net e-mail :  leafwriters @ leaf.stpn.soft.net</p>
        <p id="p_00581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00581"> Publisher and President Development Manager Marketing Manager</p>
        <p id="p_00582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00582"> Acquisitions Editor</p>
        <p id="p_00583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00583"> Software Development Specialist</p>
        <p id="p_00584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00584"> Copy Editor Editorial Coordinator</p>
        <p id="p_00585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00585"> Formatter</p>
        <p id="p_00586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00586"> Cover Designer Copy Writer</p>
        <p id="p_00587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00587"> Production</p>
        <p id="p_00588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00588"> Richard K. Swadley Dean Miller John Pierce</p>
        <p id="p_00589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00589"> Chris Denny</p>
        <p id="p_00590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00590"> Steve Straiger</p>
        <p id="p_00591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00591"> Acquisitions Manager Managing Editor</p>
        <p id="p_00592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00592"> Assistant Marketing Manager</p>
        <p id="p_00593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00593"> Development Editors Production Editor</p>
        <p id="p_00594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00594"> Greg Wiegand Cindy Morrow Kristina Perry</p>
        <p id="p_00595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00595"> Angelique Brittingham, Keith Davenport</p>
        <p id="p_00596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00596"> Tonya R. Simpson</p>
        <p id="p_00597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00597"> Michael Brumitt, Charlotte Clapp, Jason Hand, Sonja Hart, Louisa Klucznik, Ayanna Lacey, Clint Lahnen, Paula Lowell, Laura Robbins, Bobbi Satterfield, Carol Sheehan, Chris Wilcox</p>
        <p id="p_00598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00598"> Acknowledgments</p>
        <p id="p_00599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00599"> I would like to thank the following people for their help:</p>
        <p id="p_00600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00600"> • David Macklem at Sietec Open Systems for allowing me to take the time off to work on the first edition of this book</p>
        <p id="p_00601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00601"> • Everyone at Sams Publishing, for their efforts and encouragement</p>
        <p id="p_00602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00602"> • Jim Gardner, for telling the people at Sams Publishing about me</p>
        <p id="p_00603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00603"> I'd also like to thank all those friends of mine (you know who you are) who tolerated my going stir-crazy as my deadlines approached.</p>
        <p id="p_00604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00604"> About the Authors</p>
        <p id="p_00605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00605"> David Till</p>
        <p id="p_00606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00606"> David Till is a technical writer working in Toronto, Ontario, Canada. He holds a master's degree in</p>
        <p id="p_00607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00607"> computer science from the University of Waterloo; programming languages was his major field of study. He also has worked in compiler development and on version-control software. He lists his hobbies as "writing, comedy, walking, duplicate bridge, and fanatical support of the Toronto Blue Jays."</p>
        <p id="p_00608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00608"> He can be reached via e-mail at  am671@f reenet. tor onto . on. ca  or  davet @klg. com,  or on the World Wide Web at  http : / /  www. inter log. com/-davet / .</p>
        <p id="p_00609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00609"> Kamran Husain</p>
        <p id="p_00610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00610"> Kamran Husain is a software consultant with experience in UNIX system programming. He has dabbled in all sorts of software for real-time systems applications, telecommunications, seismic data acquisition and navigation, X Window/Motif and Microsoft Windows applications. He refuses to divulge any more of his qualifications. Kamran offers consulting services and training classes through his company, MPS Inc., in Houston, Texas. He is an alumnus of the University of Texas at Austin.</p>
        <p id="p_00611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00611"> You can reach Kamran through Sams Publishing or via e-mail at  khusain@neosoft. com  or mpsi@aol.com.</p>
        <p id="p_00612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00612"> Introduction</p>
        <p id="p_00613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00613"> This book is designed to teach you the Perl programming language in just 21 days. When you finish reading this book, you will have learned why Perl is growing rapidly in popularity: It is powerful enough to perform many useful, sophisticated programming tasks, yet it is easy to learn and use.</p>
        <p id="p_00614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00614"> Who Should Read This Book?</p>
        <p id="p_00615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00615"> No previous programming experience is required for you to learn everything you need to know about programming with Perl from this book. In particular, no knowledge of the C programming language is required. If you are familiar with other programming languages, learning Perl will be a snap. The only assumption this book does make is that you are familiar with the basics of using the UNIX operating system.</p>
        <p id="p_00616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00616"> Special Features of This Book</p>
        <p id="p_00617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00617"> This book contains some special elements that help you understand Perl features and concepts as they are introduced:</p>
        <p id="p_00618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00618"> • Syntax boxes</p>
        <p id="p_00619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00619"> • DO/DON'T boxes</p>
        <p id="p_00620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00620"> • Notes</p>
        <p id="p_00621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00621"> • Warnings</p>
        <p id="p_00622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00622"> • Tips</p>
        <p id="p_00623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00623"> Syntax boxes  explain some of the more complicated features of Perl, such as the control structures. Each syntax box consists of a formal definition of the feature followed by an explanation of the elements of the</p>
        <p id="p_00624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00624"> feature. Here is an example of a syntax box:</p>
        <p id="p_00625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00625"> The syntax of the for statement is</p>
        <p id="p_00626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00626"> for (exprl; expr2; expr3) { statement block</p>
        <p id="p_00627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00627"> }</p>
        <p id="p_00628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00628"> exprl  is the loop initializer. It is evaluated only once, before the start of the loop.</p>
        <p id="p_00629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00629"> expr2  is the conditional expression that terminates the loop. The conditional expression in  expr2  behaves just like the ones in  while  and  if  statements: If its value is zero, the loop is terminated, and if its value is nonzero, the loop is executed.</p>
        <p id="p_00630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00630"> statement_block  is the collection of statements that is executed if (and when)  expr2  has a nonzero value.</p>
        <p id="p_00631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00631"> expr3  is executed once per iteration of the loop, and is executed after the last statement in</p>
        <p id="p_00632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00632"> statement_block  is executed.</p>
        <p id="p_00633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00633"> Don't try to understand this definition yet!</p>
        <p id="p_00634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00634"> DO/DON'T boxes  present the do's and don'ts for a particular task or feature. Here is an example of such a box:</p>
        <p id="p_00635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00635"> Do</p>
        <p id="p_00636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00636"> Don't</p>
        <p id="p_00637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00637"> DON'T  confuse the | operator (bitwise OR) with the | | operator (logical OR).</p>
        <p id="p_00638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00638"> DO  make sure you are using the proper bitwise operator. It's easy to slip and assume you want bitwise OR when you really want bitwise AND. (Trust me.</p>
        <p id="p_00639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00639"> Notes  are explanations of interesting properties of a particular program feature. Here is an example of a note:</p>
        <p id="p_00640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00640"> NOTE</p>
        <p id="p_00641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00641"> In left-justified output, the value being displayed appears at the left end of the value field. In right-justified output, the value being displayed appears at the right end of the value field.</p>
        <p id="p_00642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00642"> Warnings  warn you of programming pitfalls to avoid. Here is a typical warning:</p>
        <p id="p_00643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00643"> Warning</p>
        <p id="p_00644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00644"> You cannot use the  last  statement inside the  do  statement. The  do statement, although it behaves like the other control structures, is actually implemented differently.</p>
        <p id="p_00645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00645"> Tips  are hints on how to write your Perl programs better. Here is an example of a tip:</p>
        <p id="p_00646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00646"> TIP</p>
        <p id="p_00647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00647"> It is a good idea to use all uppercase letters for your file variable names. This makes it easier to distinguish file variable names from other variable names and from reserved words.</p>
        <p id="p_00648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00648"> Programming Examples</p>
        <p id="p_00649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00649"> Each feature of Perl is illustrated by examples of its use. In addition, each chapter of this book contains many useful programming examples complete with explanations; these examples show you how you can use Perl features in your own programs.</p>
        <p id="p_00650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00650"> Each example contains a listing of the program, the input required by and the output generated by the program, and an analysis of how the program works. Special icons are used to point out each part of the example: Type, Input-Output, and Analysis.</p>
        <p id="p_00651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00651"> In the Input-Output example following Listing IN.l, there are some special typographic conventions. The input you enter is shown in  bold monospace  type, and the output generated by the system or the program is shown in  plain monospace  type. The system prompt ($ in the examples in this book) is shown so that you know when a command is to be entered on the command line.</p>
        <p id="p_00652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00652"> Listing IN.l. A simple Perl program with comments.</p>
        <p id="p_00653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00653"> 1: #!/usr/local/bin/perl</p>
        <p id="p_00654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00654"> 2: # this program reads a line of input, and writes the line 3: # back out</p>
        <p id="p_00655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00655"> 4: $inputline = &lt;STDIN&gt;; # read a line of input</p>
        <p id="p_00656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00656"> 5: print( $inputline );</p>
        <p id="p_00657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00657"> # write the line out</p>
        <p id="p_00658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00658"> Analysis</p>
        <p id="p_00659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00659"> Line 1 is the header comment. Lines 2 and 3 are comments, not executable lines of code. Line 4 reads a line of input. Line 5 writes the line of input on your screen.</p>
        <p id="p_00660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00660"> End-of-Day Q&amp; A and Workshop</p>
        <p id="p_00661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00661"> Each day ends with a Q&amp;A section containing answers to common questions relating to that day's material. There also is a Workshop at the end of each day that consists of quiz questions and programming exercises. The exercises often include BUG BUSTER exercises that help you spot some of the common bugs that crop up in Perl programs. The answers to these quiz questions as well as sample solutions for the exercises are presented in Appendix A, "Answers."</p>
        <p id="p_00662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00662"> Conventions Used in This Book</p>
        <p id="p_00663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00663"> This book uses different typefaces to help you differentiate between Perl code and regular English, and also to help you identify important concepts.</p>
        <p id="p_00664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00664"> • Actual Perl code is typeset in a special  monospace  font. You'll see this font used in listings and the Input-Output examples, as well as in code snippets. In the explanations of Perl features, commands, filenames, statements, variables, and any text you see on the screen also are typeset in this font.</p>
        <p id="p_00665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00665"> • Command input and anything that you are supposed to enter appears in a  bold monospace  font. You'll see this mainly in the Input-Output examples.</p>
        <p id="p_00666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00666"> • Placeholders in syntax descriptions appear in an  italic monospace  font. Replace the placeholder with the actual filename, parameter, or whatever element it represents.</p>
        <p id="p_00667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00667"> •  Italics  highlight technical terms when they first appear in the text and are sometimes used to emphasize important points.</p>
        <p id="p_00668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00668"> What You'll Learn in 21 Days</p>
        <p id="p_00669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00669"> In your first week of learning Perl, you'll learn enough of the basics of Perl to write many useful Perl programs. Here's a summary of what you'll learn in Week 1:</p>
        <p id="p_00670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00670"> Day  1 ,  "Getting Started,"  tells you how to get Perl, how to run Perl programs, and how to</p>
        <p id="p_00671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00671"> read from your keyboard and write to your screen.</p>
        <p id="p_00672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00672"> Day 2 ,  "Basic Operators and Control Flow,"  teaches you about simple arithmetic, how to assign a value to a scalar variable, and how to control execution using conditional statements.</p>
        <p id="p_00673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00673"> Day  3 ,  "Understanding Scalar Values,"  teaches you about integers, floating-point numbers, and character strings. It also shows you that all three are interchangeable in Perl.</p>
        <p id="p_00674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00674"> Day 4 ,  "More Operators,"  tells you all about operators and expressions in Perl and talks about operator associativity and precedence.</p>
        <p id="p_00675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00675"> Day 5 ,  "Lists and Array Variables,"  introduces you to lists, which are collections of values, and to array variables, which store lists.</p>
        <p id="p_00676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00676"> Day  6 ,  "Reading from and Writing to Files,"  tells you how to interact with your file system by reading from input files, writing to output files, and testing for particular file attributes.</p>
        <p id="p_00677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00677"> Day 7 ,  "Pattern Matching,"  describes pattern-matching in Perl and shows how you can substitute values and translate sets of characters in text strings.</p>
        <p id="p_00678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00678"> By the end of Week 2, you'll have mastered almost all the features of Perl; you'll also have learned about many of the library functions supplied with the language. Here's a summary of what you'll learn:</p>
        <p id="p_00679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00679"> Day 8 ,  "More Control Structures,"  discusses the control flow statements not previously covered.</p>
        <p id="p_00680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00680"> Day 9 ,  "Using Subroutines,"  shows how you can break your program into smaller, more manageable, chunks.</p>
        <p id="p_00681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00681"> Day 10 ,  "Associative Arrays,"  introduces one of the most powerful and useful constructs in Perl-arrays-and it shows how you can use these arrays to simulate other data structures.</p>
        <p id="p_00682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00682"> Day 11 ,  "Formatting Your Output,"  shows how you can use Perl to produce tidy reports.</p>
        <p id="p_00683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00683"> Day 12 ,  "Working with the File System,"  shows how you can interact with your system's directory structure.</p>
        <p id="p_00684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00684"> Day 13 ,  "Process, String, and Mathematical Functions,"  describes the library functions that interact with processes running on the system. It also describes the functions that perform trigonometric and other mathematical operations, and the functions that operate on strings.</p>
        <p id="p_00685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00685"> Day 14 ,  "Scalar-Conversion and List-Manipulation Functions,"  describes the library functions that convert values from one form to another and the functions that work with lists and array variables.</p>
        <p id="p_00686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00686"> By the end of Week 3, you'll know all the features and capabilities of Perl. It covers the rest of the Perl</p>
        <p id="p_00687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00687"> library functions and describes some of the more esoteric concepts of the language. Here's a summary of what you'll learn:</p>
        <p id="p_00688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00688"> Day 15 ,  "System Functions,"  describes the functions that manipulate the Berkeley UNIX and UNIX System V environments.</p>
        <p id="p_00689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00689"> Day 16 ,  "Command-Line Options,"  describes the options you can supply with Perl to control how your program runs.</p>
        <p id="p_00690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00690"> Day 17 ,  "System Variables,"  describes the built-in variables that are included automatically as part of every Perl program.</p>
        <p id="p_00691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00691"> Day 18 ,  "References in Perl 5,"  describes the pointer and reference features of Perl 5, including multi-dimensional arrays.</p>
        <p id="p_00692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00692"> Day 19 ,  "Object-Oriented Programming in Perl,"  describes the object-oriented capabilities added to Perl 5. These enable you to hide information and divide your program into individual file modules.</p>
        <p id="p_00693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00693"> Day 20 ,  "Miscellaneous Features of Perl,"  covers some of the more exotic or obscure features of the language.</p>
        <p id="p_00694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00694"> Day 21 ,  "The Perl Debugger,"  shows you how to use the Perl debugger to discover errors quickly.</p>
        <p id="p_00695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00695"> Week at a Glance</p>
        <p id="p_00696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00696"> CONTENTS</p>
        <p id="p_00697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00697"> •  Where You’re Going</p>
        <p id="p_00698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00698"> In your first week of teaching yourself Perl, you'll learn enough of the basics to write many useful Perl programs. Although some experience in using a programming language will be an advantage as you read this book, it is not required. In particular, you don't need to know the C programming language before you read this book.</p>
        <p id="p_00699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00699"> To use this book effectively, you should be able to try out some of the features of Perl as you learn them. To do this, you should have Perl running on your system. If you don't have Perl,  Day  1 ,</p>
        <p id="p_00700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00700"> "Getting Started," tells how you can get it for free.</p>
        <p id="p_00701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00701"> Each chapter of this book contains quiz and exercise questions that test you on the material covered in the day’s lesson. These questions are answered in Appendix A, "Answers."</p>
        <p id="p_00702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00702"> Where You’re Going</p>
        <p id="p_00703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00703"> The first week covers the essentials of Perl. Here's a summary of what you'll learn.</p>
        <p id="p_00704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00704"> Day  1 , "Getting Started," tells you how to get Perl, how to run Perl programs, and how to read input from your keyboard and write output to your screen.</p>
        <p id="p_00705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00705"> Day  2 ,  "Basic Operators and Control Flow," teaches you about simple arithmetic, how to assign a value to a scalar variable, and how to control execution using conditional statements.</p>
        <p id="p_00706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00706"> Day  3 ,  "Understanding Scalar Values," teaches you about integers, floating-point numbers, and character strings. It also shows you that all three are interchangeable in Perl.</p>
        <p id="p_00707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00707"> Day  4 ,  "More Operators," tells you all about operators and expressions in Perl and talks about operator associativity and precedence.</p>
        <p id="p_00708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00708"> Day  5 ,  "Lists and Array Variables," introduces you to lists, which are collections of values, and to array variables, which store lists.</p>
        <p id="p_00709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00709"> Day  6 ,  "Reading from and Writing to Files," tells you how to interact with your file system by reading from input files, writing to output files, and testing for particular file attributes.</p>
        <p id="p_00710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00710"> Finally,  Day  7 ,  "Pattern Matching," describes pattern matching in Perl and shows how you can substitute values and translate sets of characters in text strings.</p>
        <p id="p_00711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00711"> This is quite a bit of material to learn in one week; however, by the end of the week you'll know most of the essentials of Perl and will be able to write many useful programs.</p>
        <p id="p_00712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00712"> Chapter 1</p>
        <p id="p_00713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00713"> Getting Started</p>
        <p id="p_00714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00714"> CONTENTS</p>
        <p id="p_00715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00715"> •  What Is Perl?</p>
        <p id="p_00716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00716"> •  How Do I Find Perl?</p>
        <p id="p_00717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00717"> o  Where Do I Get Perl? o  Other Places to Get Perl</p>
        <p id="p_00718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00718"> •  A Sample Perl Program</p>
        <p id="p_00719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00719"> •  Running a Perl Program</p>
        <p id="p_00720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00720"> o  If Something Goes Wrong</p>
        <p id="p_00721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00721"> •  The First Line of Your Perl Program: How Comments Work</p>
        <p id="p_00722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00722"> o  Comments</p>
        <p id="p_00723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00723"> •  Line 2: Statements, Tokens, and &lt;STDIN&gt;</p>
        <p id="p_00724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00724"> o  Statements and Tokens o  Tokens and White Space</p>
        <p id="p_00725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00725"> o  What the Tokens Do: Reading from Standard Input</p>
        <p id="p_00726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00726"> •  Line 3: Writing to Standard Output</p>
        <p id="p_00727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00727"> o  Function Invocations and Arguments</p>
        <p id="p_00728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00728"> •  Error Messages</p>
        <p id="p_00729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00729"> •  Interpretive Languages Versus Compiled Languages</p>
        <p id="p_00730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00730"> •  Summary</p>
        <p id="p_00731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00731"> •  O&amp;A</p>
        <p id="p_00732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00732"> •  Workshop</p>
        <p id="p_00733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00733"> o  Quiz o  Exercises</p>
        <p id="p_00734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00734"> Welcome to  Teach Yourself Perl 5 in 21 Days.  Today you'll learn about the following:</p>
        <p id="p_00735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00735"> • What Perl is and why Perl is useful</p>
        <p id="p_00736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00736"> • How to get Perl if you do not already have it</p>
        <p id="p_00737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00737"> • How to run Perl programs</p>
        <p id="p_00738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00738"> • How to write a very simple Perl program</p>
        <p id="p_00739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00739"> • The difference between interpretive and compiled programming languages</p>
        <p id="p_00740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00740"> • What an algorithm is and how to develop one</p>
        <p id="p_00741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00741"> What Is Perl?</p>
        <p id="p_00742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00742"> Perl is an acronym, short for Practical Extraction and  Re  port Language. It was designed by Larry Wall as a tool for writing programs in the UNIX environment and is continually being updated and maintained by him.</p>
        <p id="p_00743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00743"> For its many fans, Perl provides the best of several worlds. For instance:</p>
        <p id="p_00744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00744"> • Perl has the power and flexibility of a high-level programming language such as C. In fact, as you will see, many of the features of the language are borrowed from C.</p>
        <p id="p_00745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00745"> • Like shell script languages, Perl does not require a special compiler and linker to turn the programs you write into working code. Instead, all you have to do is write the program and tell Perl to run it. This means that Perl is ideal for producing quick solutions to small programming problems, or for creating prototypes to test potential solutions to larger problems.</p>
        <p id="p_00746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00746"> • Perl provides all the features of the script languages sed and awk, plus features not found in either of these two languages. Perl also supports a sed-to-Perl translator and an awk-to-Perl translator.</p>
        <p id="p_00747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00747"> In short, Perl is as powerful as C but as convenient as awk, sed, and shell scripts.</p>
        <p id="p_00748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00748"> As you'll see, Perl is very easy to learn. Indeed, if you are familiar with other programming languages, learning Perl is a snap. Even if you have very little programming experience, Perl can have you writing useful programs in a very short time. By the end of  Day 2 ,  "Basic Operators and Control Flow," you'll know enough about Perl to be able to solve many problems.</p>
        <p id="p_00749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00749"> How Do I Find Perl?</p>
        <p id="p_00750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00750"> To find out whether Perl already is available on your system, do the following:</p>
        <p id="p_00751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00751"> • If you are currently working in a UNIX programming environment, check to see whether the file</p>
        <p id="p_00752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00752"> /usr/local/bin/perl  exists.</p>
        <p id="p_00753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00753"> • If you are working in any other environment, check the place where you normally keep your executable programs, or check the directories accessible from your  PATH  environment variable.</p>
        <p id="p_00754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00754"> If you do not find Perl in this way, talk to your system administrator and ask whether she or he has Perl running somewhere else. If you don't have Perl running in your environment, don't despair-read on!</p>
        <p id="p_00755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00755"> Where Do I Get Perl?</p>
        <p id="p_00756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00756"> One of the reasons Perl is becoming so popular is that it is available free of charge to anyone who wants it. If you are on the Internet, you can obtain a copy of Perl with file-transfer protocol (FTP). The following is a sample FTP session that transfers a copy of the Perl distribution. The items shown in boldface type are what you would enter during the session.</p>
        <p id="p_00757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00757"> $ ftp prep.ai.mit.edu Connected to prep.ai.mit.edu.</p>
        <p id="p_00758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00758"> 220 aeneas FTP server (Version wu-2.4(l) Thu Apr 14 20:21:35 EDT 1994) ready.</p>
        <p id="p_00759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00759"> Name (prep.ai.mit.edu:dave): anonymous</p>
        <p id="p_00760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00760"> 331 Guest login ok, send your complete e-mail address as password. Password:</p>
        <p id="p_00761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00761"> 230-Welcome, archive user!</p>
        <p id="p_00762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00762"> 230-</p>
        <p id="p_00763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00763"> 230-If you have problems downloading and are seeing "Access denied" or</p>
        <p id="p_00764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00764"> 230-"Permission denied", please make sure that you started your FTP</p>
        <p id="p_00765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00765"> 230-client in a directory to which you have write permission.</p>
        <p id="p_00766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00766"> 230-</p>
        <p id="p_00767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00767"> 230-If you have any problems with the GNU software or its downloading,</p>
        <p id="p_00768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00768"> 230-please refer your questions to &lt;gnu@PREP.AI.MIT.EDU&gt;. If you have any</p>
        <p id="p_00769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00769"> 230-other unusual problems, please report them to &lt;root @aeneas.MIT.EDU&gt;.</p>
        <p id="p_00770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00770"> 230-</p>
        <p id="p_00771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00771"> 230-If you do have problems, please try using a dash (-) as the first</p>
        <p id="p_00772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00772"> 230-character of your password - this will turn off the continuation</p>
        <p id="p_00773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00773"> 230-messages that may be confusing your FTP client.</p>
        <p id="p_00774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00774"> 230-</p>
        <p id="p_00775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00775"> 230 Guest login ok, access restrictions apply.</p>
        <p id="p_00776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00776"> ftp&gt; cd pub/gnu</p>
        <p id="p_00777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00777"> 250-If you have problems downloading and are seeing "Access denied" or</p>
        <p id="p_00778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00778"> 250-"Permission denied", please make sure that you started your FTP 250-client in a directory to which you have write permission.</p>
        <p id="p_00779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00779"> 250-</p>
        <p id="p_00780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00780"> 250-Please note that all files ending in '.gz' are compressed with</p>
        <p id="p_00781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00781"> 250-'gzip', not with the unix 'compress' program. Get the file README</p>
        <p id="p_00782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00782"> 250- and read it for more information.</p>
        <p id="p_00783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00783"> 250-</p>
        <p id="p_00784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00784"> 250-Please read the file README</p>
        <p id="p_00785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00785"> 250- it was last modified on Thu Feb 1 15:00:50 1996 - 32 days ago 250-Please read the file README-about-.diff-files</p>
        <p id="p_00786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00786"> 250- it was last modified on Fri Feb 2 12:57:14 1996 - 31 days ago 250-Please read the file README-about-.gz-files</p>
        <p id="p_00787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00787"> 250- it was last modified on Wed Jun 14 16:59:43 1995 - 264 days ago</p>
        <p id="p_00788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00788"> 250 CWD command successful.</p>
        <p id="p_00789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00789"> ftp&gt; binary</p>
        <p id="p_00790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00790"> 200 Type set to I.</p>
        <p id="p_00791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00791"> ftp&gt; get perl-5.001.tar.gz</p>
        <p id="p_00792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00792"> 200 PORT command successful.</p>
        <p id="p_00793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00793"> 150 Opening ASCII mode data connection for perl-5.001.tar.gz (1130765 bytes).</p>
        <p id="p_00794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00794"> 226 Transfer complete.</p>
        <p id="p_00795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00795"> 1130765 bytes received in 9454 seconds (1.20 Kbytes/s) ftp&gt; quit 221 Goodbye.</p>
        <p id="p_00796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00796"> $</p>
        <p id="p_00797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00797"> The commands entered in this session are explained in the following steps. If some of these steps are not familiar to you, ask your system administrator for help.</p>
        <p id="p_00798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00798"> 1. The command</p>
        <p id="p_00799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00799"> $ ftp prep.ai.mit.edu</p>
        <p id="p_00800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00800"> connects you to the main Free Software Foundation source depository at MIT.</p>
        <p id="p_00801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00801"> 2.  The user ID  anonymous  tells FTP that you want to perform an anonymous FTP operation.</p>
        <p id="p_00802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00802"> 3. When FTP asks for a password, enter your user ID and network address. This lets the MIT system administrator know who is using the MIT archives. (For security reasons, the password is not actually displayed when you type it.)</p>
        <p id="p_00803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00803"> 4. The command  cd pub /gnu  sets your current working directory to be the directory containing the Perl source.</p>
        <p id="p_00804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00804"> 5. The binary command tells FTP that the file you'll be receiving is a file that contains unreadable (non-text) characters.</p>
        <p id="p_00805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00805"> 6.  The  get  command copies the file  perl-5.001.tar.gz  from the MIT source depository to your own site. (It's usually best to do this in off-peak hours to make things easier for other Internet users-it takes awhile.) This file is quite large because it contains all the source files for Perl bundled together into a single file.</p>
        <p id="p_00806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00806"> 7.  The  quit  command disconnects from the MIT source repository and returns you to your own system.</p>
        <p id="p_00807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00807"> Once you've retrieved the Perl distribution, do the following:</p>
        <p id="p_00808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00808"> 1. Create a directory and move the file you just received,  perl-5.001 .tar . gz,  to this directory. (Or, alternatively, move it to a directory already reserved for this purpose.)</p>
        <p id="p_00809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00809"> 2.  The  perl-5.0 01. tar . gz  file is compressed to save space. To uncompress it, enter the command</p>
        <p id="p_00810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00810"> $ gunzip perl-5.001.tar.gz</p>
        <p id="p_00811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00811"> gunzipis  the  GNU  uncompress program. If it's not available on your system, see your system administrator. (You can, in fact, retrieve it from  prep .  ai . mit . eduusing anonymous FTP with the same commands you used to retrieve the Perl distribution.)</p>
        <p id="p_00812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00812"> When you run gunzip, the file  perl-5.001 .tar .  gzwill be replaced by  perl-5.001 .tar, which is the uncompressed version of the Perl distribution file.</p>
        <p id="p_00813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00813"> 3. The next step is to unpack the Perl distribution. In other words, use the information in the Perl distribution to create the Perl source files. To do this, enter the following command:</p>
        <p id="p_00814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00814"> $ tar  xvf  - &lt;perl-5.001.tar</p>
        <p id="p_00815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00815"> As this command executes, it creates each source file in turn and displays the name and size of each file as it is created. The tarcommand also creates subdirectories where appropriate; this ensures that the Perl source files are organized in a logical way.</p>
        <p id="p_00816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00816"> 4. Using your favorite C compiler, compile the Perl source code using the makefile provided. (This makefile should have been created when the source files were unpacked in the last step.)</p>
        <p id="p_00817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00817"> 5. Place the compiled Perl executable into the directory where you normally keep your executables. On UNIX systems, this directory usually is called  /usr/local/bin,  and Perl usually is named</p>
        <p id="p_00818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00818"> /usr/local/bin/perl.</p>
        <p id="p_00819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00819"> You might need your system administrator's help to do this because you might not have the necessary permissions.</p>
        <p id="p_00820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00820"> Other Places to Get Perl</p>
        <p id="p_00821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00821"> If you cannot access the MIT site from where you are, you can get Perl from the following sites using anonymous FTP:</p>
        <p id="p_00822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00822"> North America</p>
        <p id="p_00823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00823"> Europe</p>
        <p id="p_00824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00824"> Australia</p>
        <p id="p_00825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00825"> South America</p>
        <p id="p_00826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00826"> You also can obtain Perl from most sites that store GNU source code, or from any site that archives the Usenet newsgroup  comp . sources . unix.</p>
        <p id="p_00827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00827"> A Sample Perl Program</p>
        <p id="p_00828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00828"> Now that Perl is available on your system, it's time to show you a simple program that illustrates how easy it is to use Perl. Listing 1.1 is a simple program that asks for a line of input and writes it out.</p>
        <p id="p_00829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00829"> Type</p>
        <p id="p_00830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00830"> Listing  1.1. A  simple Perl program that reads and writes a line of input.</p>
        <p id="p_00831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00831"> 1: #!/usr/local/bin/perl 2: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_00832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00832"> 3: print ( $inputline );</p>
        <p id="p_00833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00833"> Output</p>
        <p id="p_00834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00834"> $programl_l</p>
        <p id="p_00835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00835"> $</p>
        <p id="p_00836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00836"> Analysis</p>
        <p id="p_00837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00837"> Line 1 is the header comment. Line 2 reads a line of input. Line 3 writes the line of input back to your screen.</p>
        <p id="p_00838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00838"> The following sections describe how to create and run this program, and they describe it in more detail.</p>
        <p id="p_00839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00839"> Running a Perl Program</p>
        <p id="p_00840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00840"> To run the program shown in Listing 1.1, do the following:</p>
        <p id="p_00841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00841"> 1. Using your favorite editor, type the previous program and save it in a file called  programl_l.</p>
        <p id="p_00842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00842"> 2. Tell the system that this file contains executable statements. To do this in the UNIX environment, enter the command</p>
        <p id="p_00843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00843"> $ chmod +x programl_l</p>
        <p id="p_00844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00844"> 3. Run the program by entering the command</p>
        <p id="p_00845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00845"> $ programl_l</p>
        <p id="p_00846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00846"> When you run  programl_l,  it waits for you to enter a line of input. After you enter the line of input, programl_l  prints what you entered, as follows:</p>
        <p id="p_00847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00847"> $ programl_l</p>
        <p id="p_00848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00848"> $</p>
        <p id="p_00849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00849"> If Something Goes Wrong</p>
        <p id="p_00850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00850"> If Listing 1.1 is stored in the file  programl_l  and run according to the preceding steps, the program should run successfully. If the program doesn't run, one of two things has likely happened:</p>
        <p id="p_00851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00851"> • The system can't find the file  programl_l.</p>
        <p id="p_00852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00852"> • The system can't find Perl.</p>
        <p id="p_00853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00853"> If you receive the error message</p>
        <p id="p_00854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00854"> programl_l not found</p>
        <p id="p_00855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00855"> or something similar, your system couldn't find the file  programl_l.  To tell the system where programl_l  is located, you can do one of two things in a UNIX environment:</p>
        <p id="p_00856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00856"> • Enter the command .  /programl_l,  which gives the system the pathname of  programl_l relative to the current directory.</p>
        <p id="p_00857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00857"> • Add the current directory . to your  PATH  environment variable. This tells the system to search in the current directory when looking for executable programs such as  programl_l.</p>
        <p id="p_00858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00858"> If you receive the message</p>
        <p id="p_00859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00859"> /usr/local/bin/perl not found</p>
        <p id="p_00860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00860"> or something similar, this means that Perl is not installed properly on your machine. See the section "How Do I Find Perl?" earlier today, for more details.</p>
        <p id="p_00861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00861"> If you don't understand these instructions or are still having trouble running Listing 1.1, talk to your system administrator.</p>
        <p id="p_00862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00862"> The First Line of Your Perl Program: How Comments Work</p>
        <p id="p_00863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00863"> Now that you've run your first Perl program, let's look at each line of Listing 1.1 and figure out what it does. Line 1 of this program is a special line that tells the system that this is a Perl program:</p>
        <p id="p_00864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00864"> #!/usr/local/bin/perl</p>
        <p id="p_00865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00865"> Let's break this line down, one part at a time:</p>
        <p id="p_00866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00866"> • The first character in the line, the # character, is the Perl  comment character.  It tells the system that this line is not an executable instruction.</p>
        <p id="p_00867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00867"> • The ! character is a special character; it indicates what type of program this is. (You don't need to worry about the details of what the ! character does. All you have to do is remember to include it.)</p>
        <p id="p_00868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00868"> • The path  /usr/local/bin/perl  is the location of the Perl executable on your system. This executable  interprets  your program; in other words, it figures out what you want to do and then does it. Because the Perl executable has the job of interpreting Perl instructions, it usually is called the Perl interpreter.</p>
        <p id="p_00869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00869"> If, after reading this, you still don't understand the meaning of the line # !  /usr/local/bin/perl  don't worry. The actual specifics of what it does are not important for our puiposes in this book. Just remember to include it as the first line of your program, and Perl will take it from there.</p>
        <p id="p_00870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00870"> NOTE</p>
        <p id="p_00871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00871"> If you are running Perl on a system other than UNIX, you might need to replace the line # !  /usr/local/bin/perl  with some other line indi-cating the location of the Perl interpreter on your system. Ask your system administrator for details on what you need to include here.</p>
        <p id="p_00872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00872"> After you have found out what the proper first line is in your environment, include that line as the first line of every Perl program you write, and you're all set</p>
        <p id="p_00873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00873"> Comments</p>
        <p id="p_00874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00874"> As you have just seen, the first character of the line</p>
        <p id="p_00875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00875"> #!/usr/local/bin/perl</p>
        <p id="p_00876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00876"> is the  comment character , #. When the Perl interpreter sees the #, it ignores the rest of that line.</p>
        <p id="p_00877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00877"> Comments can be appended to lines containing code, or they can be lines of their own:</p>
        <p id="p_00878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00878"> $inputline = &lt;STDIN&gt;; # this line contains an appended comment # this entire line is a comment</p>
        <p id="p_00879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00879"> You can-and should-use comments to make your programs easier to understand. Listing 1.2 is the simple program you saw earlier, but it has been modified to include comments explaining what the program does.</p>
        <p id="p_00880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00880"> |note</p>
        <p id="p_00881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00881"> As you work through the lessons in this book and create your own programs-such as the one in Listing 1.2-you can, of course, name them anything you want. For illustration and discussion purposes, I've adopted the convention of using a name that corresponds to the listing number. For example, the program in Listing 1.2 is called programl_2.</p>
        <p id="p_00882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00882"> The program name is used in the Input-Output examples such as the one following this listing, as well as in the Analysis section where the listing is discussed in detail. When you follow the Input-Output example, just remember to substitute your program's name for the one shown in the example</p>
        <p id="p_00883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00883"> Listing 1.2. A simple Perl program with comments.</p>
        <p id="p_00884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00884"> 1: #!/usr/local/bin/perl</p>
        <p id="p_00885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00885"> 2: # this program reads a line of input, and writes the line 3: # back out</p>
        <p id="p_00886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00886"> 4: $inputline = &lt;STDIN&gt;; # read a line of input</p>
        <p id="p_00887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00887"> 5: print( $inputline ); # write the line out</p>
        <p id="p_00888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00888"> Output</p>
        <p id="p_00889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00889"> $ programl_2</p>
        <p id="p_00890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00890"> This is a line of input. This is a line of input. $</p>
        <p id="p_00891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00891"> Analysis</p>
        <p id="p_00892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00892"> The behavior of the program in Listing 1.2 is identical to that of Listing 1.1 because the actual code is the same. The only difference is that Listing 1.2 has comments in it</p>
        <p id="p_00893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00893"> Note that in an actual program, comments normally are used only to explain complicated code or to indicate that the following lines of code perform a specific task. Because Perl instructions usually are pretty straightforward, Perl programs don't need to have a lot of comments.</p>
        <p id="p_00894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00894"> Don't</p>
        <p id="p_00895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00895"> Do</p>
        <p id="p_00896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00896"> DO  use comments whenever you think that a line of code is not easy to understand.</p>
        <p id="p_00897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00897"> DON'T  clutter up your code with unnecessary comments. The goal is readability.  If  a comment makes a program easier to read, include it. Otherwise, don't bother.</p>
        <p id="p_00898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00898"> DON'T  put anything else after  /usr/local/bin/perl  in the first line: #!/usr/local/bin/perl</p>
        <p id="p_00899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00899"> This line is a special comment line, and it is not treated like the others.</p>
        <p id="p_00900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00900"> Line 2: Statements, Tokens, and  &lt;STDIN&gt;</p>
        <p id="p_00901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00901"> Now that you've learned what the first line of Listing 1.1 does, let's take a look at line 2:</p>
        <p id="p_00902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00902"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_00903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00903"> This is the first line of code that actually does any work. To understand what this line does, you need to know what a Perl statement is and what its components are.</p>
        <p id="p_00904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00904"> Statements and Tokens</p>
        <p id="p_00905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00905"> The line of code you have just seen is an example of a Perl  statement.  Basically, a statement is one task for the Perl interpreter to perform. A Perl program can be thought of as a collection of statements performed one at a time.</p>
        <p id="p_00906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00906"> When the Perl interpreter sees a statement, it breaks the statement down into smaller units of information. In this example, the smaller units of information are  $inputline, =, &lt;STDIN&gt;,  and ;. Each of these smaller units of information is called a  token.</p>
        <p id="p_00907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00907"> Tokens and White Space</p>
        <p id="p_00908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00908"> Tokens can normally be separated by as many spaces and tabs as you like. For example, the following statements are identical in Perl:</p>
        <p id="p_00909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00909"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_00910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00910"> $inputline=&lt;STDIN&gt;;</p>
        <p id="p_00911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00911"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_00912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00912"> Your statements can take up as many lines of code as you like. For example, the following statement is equivalent to the ones above:</p>
        <p id="p_00913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00913"> $inputline</p>
        <p id="p_00914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00914"> &lt;STDIN&gt;</p>
        <p id="p_00915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00915"> The collection of spaces, tabs, and new lines separating one token from another is known as  white space.</p>
        <p id="p_00916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00916"> When programming in Perl, you should use white space to make your programs more readable. The examples in this book use white space in the following ways:</p>
        <p id="p_00917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00917"> • New statements always start on a new line.</p>
        <p id="p_00918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00918"> • One blank space is used to separate one token from another (except in special cases, some of which you'll see today).</p>
        <p id="p_00919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00919"> What the Tokens Do: Reading from Standard Input</p>
        <p id="p_00920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00920"> As you've seen already, the statement</p>
        <p id="p_00921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00921"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_00922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00922"> consists of four tokens:  $inputline, =, &lt;STDIN&gt;,  and ;. The following subsections explain what each of these tokens does.</p>
        <p id="p_00923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00923"> The $ input line and = Tokens</p>
        <p id="p_00924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00924"> The first token in line 1, $  input line  (at the left of the statement), is an example of a  scalar variable.  In Perl, a scalar variable can store one piece of information.</p>
        <p id="p_00925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00925"> The = token, called the  assignment operator,  tells the Perl interpreter to store the item specified by the token to the right of the = in the place specified by the token to the left of the =. In this example, the item on the right of the assignment operator is the  &lt;STDIN&gt;  token, and the item to the left of the assignment operator is the  $inputline  token. Thus,  &lt;STDIN&gt;  is stored in the scalar variable  $inputline.</p>
        <p id="p_00926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00926"> Scalar variables and assignment operators are covered in more detail on  Day  2 ,  "Basic Operators and Control Flow."</p>
        <p id="p_00927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00927"> The &lt;STDIN&gt; Token and the Standard Input File</p>
        <p id="p_00928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00928"> The next token,  &lt;STDIN&gt;,  represents a line of input from the  standard input file.  The standard input file, or STDIN  for short, typically contains everything you enter when running a program.</p>
        <p id="p_00929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00929"> For example, when you run  programl_l  and enter</p>
        <p id="p_00930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00930"> This is a line of input.</p>
        <p id="p_00931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00931"> the line you enter is stored in the standard input file.</p>
        <p id="p_00932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00932"> The  &lt;STDIN&gt;  token tells the Perl interpreter to read one line from the standard input file, where a  line  is defined to be a set of characters terminated by a new line. In this example, when the Perl interpreter sees &lt;STDIN&gt;,  it reads in</p>
        <p id="p_00933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00933"> This is a line of input.</p>
        <p id="p_00934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00934"> If the Perl interpreter then sees another &lt;STDIN&gt;ina different statement, it reads another line of data from the standard input file. The line of data you read earlier is destroyed unless it has been copied somewhere else.</p>
        <p id="p_00935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00935"> |note</p>
        <p id="p_00936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00936"> If there are more lines of input than there are &lt;STDIN&gt; tokens, the extra lines of input are ignored</p>
        <p id="p_00937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00937"> Because the  &lt;STDIN&gt;  token is to the right of the assignment operator =, the line</p>
        <p id="p_00938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00938"> This is a line of input.</p>
        <p id="p_00939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00939"> is assigned to the scalar variable $  input line.</p>
        <p id="p_00940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00940"> The ; Token</p>
        <p id="p_00941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00941"> The ; token at the end of the statement is a special token that tells Perl the statement is complete. You can think of it as a punctuation mark that is like a period in English.</p>
        <p id="p_00942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00942"> Line 3: Writing to Standard Output</p>
        <p id="p_00943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00943"> Now that you understand what statements and tokens are, consider line 3 of Listing 1.1, which is</p>
        <p id="p_00944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00944"> print ($inputline);</p>
        <p id="p_00945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00945"> This statement refers to the  library function  that is called  print.  Library functions, such as  print,  are provided as part of the Perl interpreter; each library function performs a useful task.</p>
        <p id="p_00946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00946"> The  print  function's task is to send data to the  standard output file.  The standard output file stores data that is to be written to your screen. The standard output file sometimes appears in Perl programs under the name  STDOUT.</p>
        <p id="p_00947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00947"> In this example,  print  sends $  input line  to the standard output file. Because the second line of the Perl program assigns the line</p>
        <p id="p_00948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00948"> This is a line of input.</p>
        <p id="p_00949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00949"> to $  input line,  this is what  print  sends to the standard output file and what appears on your screen.</p>
        <p id="p_00950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00950"> Function Invocations and Arguments</p>
        <p id="p_00951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00951"> When a reference to  print  appears in a Perl program, the Perl interpreter  calls,  or  invokes,  the  print library function. This  function invocation  is similar to a function invocation in C, a  GOSUB  statement in BASIC, or a  PERFORM  statement in COBOL. When the Perl interpreter sees the  print  function invocation, it executes the code contained in  print  and returns to the program when  print  is finished.</p>
        <p id="p_00952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00952"> Most library functions require information to tell them what to do. For example, the  print  function needs to know what you want to print. In Perl, this information is supplied as a sequence of comma-separated items located between the parentheses of the function invocation. For example, the statement you've just seen:</p>
        <p id="p_00953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00953"> print ($inputline);</p>
        <p id="p_00954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00954"> supplies one piece of information that is passed to  print:  the variable $  input line.  This piece of information commonly is called an  argument.</p>
        <p id="p_00955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00955"> The following call to  print  supplies two arguments:</p>
        <p id="p_00956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00956"> print ($inputline, $inputline);</p>
        <p id="p_00957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00957"> You can supply  print  with as many arguments as you like; it prints each argument starting with the first one (the one on the left). In this case,  print  writes two copies of $  input line  to the standard output file.</p>
        <p id="p_00958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00958"> You also can tell  print  to write to any other specified file. You'll learn more about this on  Day  6 ,</p>
        <p id="p_00959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00959"> "Reading From and Writing To Files."</p>
        <p id="p_00960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00960"> Error Messages</p>
        <p id="p_00961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00961"> If you incorrectly type a statement when creating a Perl program, the Perl interpreter will detect the error and tell you where the error is located.</p>
        <p id="p_00962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00962"> For example, look at Listing 1.3. This program is identical to the program you've been seeing all along, except that it contains one small error. Can you spot it?</p>
        <p id="p_00963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00963"> Type</p>
        <p id="p_00964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00964"> Listing 1.3. A program containing an error.</p>
        <p id="p_00965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00965"> 1: #!/usr/local/bin/perl 2: $inputline = &lt;STDIN&gt; 3: print ($inputline);</p>
        <p id="p_00966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00966"> Type</p>
        <p id="p_00967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00967"> $ programl_3</p>
        <p id="p_00968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00968"> Syntax error in file programl_3 at line3, next char ( Execution of programl_3 aborted due to compilation errors. $</p>
        <p id="p_00969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00969"> Analysis</p>
        <p id="p_00970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00970"> When you try to run this program, an error message appears. The Perl interpreter has detected that line 2 of the program is missing its closing ; character. The error message from the interpreter tells you what the problem is and identifies the line on which the problem is located</p>
        <p id="p_00971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00971"> TIP</p>
        <p id="p_00972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00972"> You should fix errors starting from the beginning of your program and working down.</p>
        <p id="p_00973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00973"> When the Perl interpreter detects an error, it tries to figure out what you meant to say and carries on from there; this feature is known as error recovery.  Error recovery enables the interpreter to detect as many errors as possible at one time, which speeds up the development process.</p>
        <p id="p_00974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00974"> Sometimes, however, the Perl interpreter can get confused and think you meant to do one thing when you really meant to do another. In this situation, the interpreter might start trying to detect errors that don't really exist. This problem is known as  error cascading.</p>
        <p id="p_00975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00975"> It's usually pretty easy to spot error cascading. If the interpreter is telling you that errors exist on several consecutive lines, it usually means that the interpreter is confused. Fix the first error, and the others might very well go away</p>
        <p id="p_00976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00976"> Interpretive Languages Versus Compiled Languages</p>
        <p id="p_00977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00977"> As you've seen, running a Perl program is easy. All you need to do is create the program, mark it as executable, and run it. The Perl interpreter takes care of the rest. Languages such as Perl that are processed by an interpreter are known as  interpretive languages.</p>
        <p id="p_00978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00978"> Some programming languages require more complicated processing. If a language is a  compiled language, the program you write must be translated into machine-readable code by a special program known as a compiler.  In addition, library code might need to be added by another special program known as a  linker. After the compiler and linker have done their jobs, the result is a program that can be executed on your machine-assuming, of course, that you have written the program correctly. If not, you have to compile and link the program all over again.</p>
        <p id="p_00979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00979"> Interpretive languages and compiled languages both have advantages and disadvantages, as follows:</p>
        <p id="p_00980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00980"> • As you've seen with Perl, it takes very little time to run a program in an interpretive language.</p>
        <p id="p_00981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00981"> • Interpretive languages, however, cannot run unless the interpreter is available. Compiled programs, on the other hand, can be transferred to any machine that understands them.</p>
        <p id="p_00982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00982"> As you'll see, Perl is as powerful as a compiled language. This means that you can do a lot of work quickly and easily.</p>
        <p id="p_00983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00983"> Summary</p>
        <p id="p_00984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00984"> Today you learned that Perl is a programming language that provides many of the capabilities of a high-level programming language such as C. You also learned that Perl is easy to use; basically, you just write the program and run it.</p>
        <p id="p_00985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00985"> You saw a very simple Perl program that reads a line of input from the standard input file and writes the line to the standard output file. The standard input file stores everything you type from your keyboard, and the standard output file stores everything your Perl program sends to your screen.</p>
        <p id="p_00986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00986"> You learned that Perl programs contain a header comment, which indicates to the system that your program is written in Perl. Perl programs also can contain other comments, each of which must be preceded by a #.</p>
        <p id="p_00987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00987"> Perl programs consist of a series of statements, which are executed one at a time. Each statement consists of a collection of tokens, which can be separated by white space.</p>
        <p id="p_00988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00988"> Perl programs call library functions to perform certain predefined tasks. One example of a library function is print,  which writes to the standard output file. Library functions are passed chunks of information called</p>
        <p id="p_00989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00989"> arguments; these arguments tell a function what to do.</p>
        <p id="p_00990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00990"> The Perl interpreter executes the Perl programs you write. If it detects an error in your program, it displays an error message and uses the error-recovery process to try to continue processing your program. If Perl gets confused, error cascading can occur, and the Perl interpreter might display inappropriate error messages.</p>
        <p id="p_00991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00991"> Finally, you learned about the differences between interpretive languages and compiled languages, and that Perl is an example of an interpretive language.</p>
        <p id="p_00992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00992"> Q&amp;A</p>
        <p id="p_00993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00993"> Q: Is there any particular editor I need to use with Perl?</p>
        <p id="p_00994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00994"> A: No. Perl programs are ordinary text files. You can use any text editor you like.</p>
        <p id="p_00995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00995"> Q: Why do I need to enter the chmod +x command before running my program?</p>
        <p id="p_00996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00996"> A: Because Perl programs are ordinary text files, the UNIX operating system does not know that they</p>
        <p id="p_00997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00997"> are executable programs. By default, text files have read and write permissions granted, which means you can look at your file or change it. The  chmod +x  command adds execute permission to the file; when this permission is granted, the system knows that this is an executable program.</p>
        <p id="p_00998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00998"> Q: Can I use print to print other things besides input lines?</p>
        <p id="p_00999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_00999"> A: Yes. You'll learn more about how you can use  print  on  Day  3 ,  "Understanding Scalar Values."</p>
        <p id="p_01000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01000"> Q: Why is Perl available for free?</p>
        <p id="p_01001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01001"> A: This encourages the dissemination of computer knowledge and capabilities.</p>
        <p id="p_01002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01002"> It works like this: You can get Perl for free, and you can use it to write interesting and useful programs. If you want, you can then give these programs away and let other people write interesting and useful programs based on your programs. This way, everybody benefits.</p>
        <p id="p_01003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01003"> You also can modify the source for Perl, provided you tell everybody that your version is a modification of the original. This means that if you think of a clever thing you want Perl to do, you can add it yourself. (However, you can't blame anybody else if your modification breaks something or if it doesn't work.)</p>
        <p id="p_01004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01004"> Of course, you don't have to give your Perl programs away for free. In fact, you even can sell your Perl programs, provided you don't borrow anything from somebody else's program.</p>
        <p id="p_01005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01005"> Workshop</p>
        <p id="p_01006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01006"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try to understand the quiz and exercise answers before continuing to the next day.</p>
        <p id="p_01007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01007"> Quiz</p>
        <p id="p_01008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01008"> 1. What do Perl's fans appreciate about Perl?</p>
        <p id="p_01009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01009"> 2. What does the Perl interpreter do?</p>
        <p id="p_01010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01010"> 3.  Define the following terms:</p>
        <p id="p_01011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01011"> a statement b token c argument d error recovery e standard input file</p>
        <p id="p_01012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01012"> 4. What is a comment, and where can it appear?</p>
        <p id="p_01013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01013"> 5. Where is Perl usually located on a UNIX machine?</p>
        <p id="p_01014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01014"> 6. What is a header comment, and where does it appear in a program?</p>
        <p id="p_01015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01015"> 7. What is a library function?</p>
        <p id="p_01016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01016"> Exercises</p>
        <p id="p_01017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01017"> 1. Modify  programl_l  to print the input line twice.</p>
        <p id="p_01018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01018"> 2.  Modify  programl_l  to read and print two different input lines.</p>
        <p id="p_01019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01019"> 3.  Modify  programl_l  to read two input lines and print only the second one.</p>
        <p id="p_01020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01020"> 4.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_01021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01021"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;; print ($inputline)</p>
        <p id="p_01022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01022"> 5.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_01023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01023"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;;</p>
        <p id="p_01024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01024"> # print my line! print($inputline);</p>
        <p id="p_01025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01025"> 6. What does the following program do?</p>
        <p id="p_01026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01026"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;;</p>
        <p id="p_01027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01027"> $inputline2 = &lt;STDIN&gt;; print ($inputline2); print ($inputline);</p>
        <p id="p_01028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01028"> Chapter 2</p>
        <p id="p_01029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01029"> Basic Operators and Control Flow</p>
        <p id="p_01030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01030"> CONTENTS</p>
        <p id="p_01031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01031"> •  Storing in Scalar Variables Assignment</p>
        <p id="p_01032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01032"> o  The Definition of a Scalar Variable</p>
        <p id="p_01033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01033"> o  Scalar Variable Syntax o  Assigning a Value to a Scalar Variable</p>
        <p id="p_01034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01034"> •  Performing Arithmetic</p>
        <p id="p_01035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01035"> o  Example of Miles-to-Kilometers Conversion</p>
        <p id="p_01036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01036"> o  The chop Library Function</p>
        <p id="p_01037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01037"> •  Expressions</p>
        <p id="p_01038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01038"> o  Assignments and Expressions</p>
        <p id="p_01039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01039"> •  Other Perl Operators</p>
        <p id="p_01040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01040"> •  Introduction to Conditional Statements</p>
        <p id="p_01041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01041"> •  The if Statement</p>
        <p id="p_01042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01042"> o  The Conditional Expression</p>
        <p id="p_01043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01043"> o  The Statement Block o  Testing for Equality Using ==</p>
        <p id="p_01044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01044"> o  Other Comparison Operators</p>
        <p id="p_01045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01045"> •  Two-Way Branching Using if and else</p>
        <p id="p_01046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01046"> •  Multi-Way Branching Using e 1 s i f</p>
        <p id="p_01047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01047"> •  Writing Loops Using the while Statement</p>
        <p id="p_01048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01048"> •  Nesting Conditional Statements</p>
        <p id="p_01049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01049"> •  Looping Using the until Statement</p>
        <p id="p_01050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01050"> •  Summary</p>
        <p id="p_01051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01051"> •  Q&amp;A</p>
        <p id="p_01052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01052"> •  Workshop</p>
        <p id="p_01053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01053"> o  Quiz o  Exercises</p>
        <p id="p_01054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01054"> Today's lesson gives you the information you need to write some simple Perl programs. You'll learn the following:</p>
        <p id="p_01055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01055"> • More about scalar variables and how to assign values to them</p>
        <p id="p_01056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01056"> • The basic arithmetic operators and how they work with scalar variables</p>
        <p id="p_01057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01057"> • What an expression is</p>
        <p id="p_01058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01058"> • How to use the  i f  statement and the == operator to test for simple conditions</p>
        <p id="p_01059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01059"> • How to specify two-way and multi-way branches using  else  and  e 1 s i f</p>
        <p id="p_01060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01060"> • How to write simple loops using the  while  and  until  statements</p>
        <p id="p_01061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01061"> Storing in Scalar Variables Assignment</p>
        <p id="p_01062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01062"> In yesterday's lesson, you saw the following statement, which assigns a line of input from the keyboard to the variable  $inputline:</p>
        <p id="p_01063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01063"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_01064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01064"> This section tells you more about variables such as $  input line  and how to assign values to these variables.</p>
        <p id="p_01065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01065"> The Definition of a Scalar Variable</p>
        <p id="p_01066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01066"> The variable $  input line  is an example of a  scalar variable.  A scalar variable stores exactly one item-a line of input, a piece of text, or a number, for example. Items that can be stored in scalar variables are called  scalar values.</p>
        <p id="p_01067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01067"> You'll learn more about scalar values on  Day  3 ,  "Understanding Scalar Values." For today, all you need to remember is that a scalar variable stores exactly one value, which is a scalar value.</p>
        <p id="p_01068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01068"> Scalar Variable Syntax</p>
        <p id="p_01069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01069"> The name of a scalar variable consists of the character $ followed by at least one letter, which is followed by any number of letters, digits, or underscore characters (that is, the _ character).</p>
        <p id="p_01070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01070"> The following are examples of legal scalar variable names:</p>
        <p id="p_01071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01071"> $x</p>
        <p id="p_01072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01072"> $var</p>
        <p id="p_01073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01073"> $my_variable</p>
        <p id="p_01074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01074"> $var2</p>
        <p id="p_01075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01075"> $a_new_variable</p>
        <p id="p_01076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01076"> These, however, are not legal scalar variable names:</p>
        <p id="p_01077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01077"> variable</p>
        <p id="p_01078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01078"> $</p>
        <p id="p_01079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01079"> $47x $_var $variable! $new.var</p>
        <p id="p_01080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01080"> # the $ character is missing</p>
        <p id="p_01081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01081"> # there must be at least one letter in the name</p>
        <p id="p_01082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01082"> # second character must be a letter</p>
        <p id="p_01083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01083"> # again, the second character must be a letter</p>
        <p id="p_01084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01084"> # you can't have a ! in a variable name</p>
        <p id="p_01085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01085"> # you can't have a . in a variable name</p>
        <p id="p_01086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01086"> Perl variables are case-sensitive. This means that the following variables are different:</p>
        <p id="p_01087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01087"> $VAR</p>
        <p id="p_01088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01088"> $var</p>
        <p id="p_01089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01089"> $Var</p>
        <p id="p_01090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01090"> Your variable name can be as long as you want.</p>
        <p id="p_01091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01091"> $this_is_a_really_long_but_legal_name</p>
        <p id="p_01092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01092"> $this_is_a_really_long_but_legal_name_that_is_different</p>
        <p id="p_01093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01093"> The $ character is necessary because it ensures that the Perl interpreter can distinguish scalar variables from other kinds of Perl variables, which you'll see on later days.</p>
        <p id="p_01094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01094"> TIP</p>
        <p id="p_01095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01095"> Variable names should be long enough to be self-explanatory but short enough to be easy to read and type.</p>
        <p id="p_01096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01096"> Assigning a Value to a Scalar Variable</p>
        <p id="p_01097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01097"> The following statement contains the Perl  assignment operator , which is the = character:</p>
        <p id="p_01098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01098"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_01099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01099"> Remember that this statement tells Perl that the line of text read from the standard input file, represented by  &lt;STDIN&gt;,  is to become the new value of the scalar variable  $inputline.</p>
        <p id="p_01100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01100"> You can use the assignment operator to assign other values to scalar variables as well. For example, the following statement, the number  4 2  is assigned to the scalar variable  $var:</p>
        <p id="p_01101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01101"> $var = 42;</p>
        <p id="p_01102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01102"> A second assignment to a scalar variable supersedes any previous assignments. In these two statements:</p>
        <p id="p_01103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01103"> $var = 42;</p>
        <p id="p_01104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01104"> $var = 113;</p>
        <p id="p_01105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01105"> the old value of  $var, 42,  is destroyed, and the value of  $var  becomes  113.</p>
        <p id="p_01106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01106"> Assignment statements can assign text to scalar variables as well. Consider the following statement:</p>
        <p id="p_01107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01107"> $name = "inputdata";</p>
        <p id="p_01108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01108"> In this statement, the text  inputdata  is assigned to the scalar variable  $name.</p>
        <p id="p_01109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01109"> Note that the quotation marks (the " characters) on either end of the text are not part of the text assigned to  $name.  This is because the " characters are just there to enclose the text.</p>
        <p id="p_01110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01110"> Spaces or tabs contained inside the pair of " characters are treated as part of the text:</p>
        <p id="p_01111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01111"> $name = "John Q Hacker";</p>
        <p id="p_01112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01112"> Here, the spaces on either side of the  Q  are considered part of the text.</p>
        <p id="p_01113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01113"> In Perl, enclosed text such as  John Q Hacker  is known as a  character string,  and the surrounding " characters are an example of  string delimiters.  You learn more about character strings on  Day  3 ;  for now, all you need to know is that everything inside the " characters is treated as a single unit.</p>
        <p id="p_01114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01114"> Performing Arithmetic</p>
        <p id="p_01115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01115"> As you've seen, the assignment operator = takes the value to the right of the = sign and assigns it to the variable on the left of the =:</p>
        <p id="p_01116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01116"> $var = 42;</p>
        <p id="p_01117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01117"> Here, the value  4 2  is assigned to the scalar variable  $var.</p>
        <p id="p_01118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01118"> In Perl, the assignment operator is just one of many  operators  that perform tasks, or  operations.  Each operation consists of the following components:</p>
        <p id="p_01119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01119"> • The operator, such as the assignment operator (=)</p>
        <p id="p_01120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01120"> • One or more  operands,  such as  $var  and  4 2</p>
        <p id="p_01121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01121"> This might sound a little confusing, but it's really quite straightforward. To illustrate, Table 2.1 lists some of the basic arithmetic operators that Perl supports.</p>
        <p id="p_01122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01122"> Table 2.1. Basic arithmetic operators.</p>
        <p id="p_01123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01123"> You use these operators in the same way you use +, -, and so on when you do arithmetic on paper. For example, the following statement adds  17  and  5  and then assigns the result,  2 2 , to the scalar variable</p>
        <p id="p_01124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01124"> $var:</p>
        <p id="p_01125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01125"> $var  =17+5;</p>
        <p id="p_01126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01126"> You can perform more than one arithmetic operation in a single statement like this one, which assigns 19 to  $var:</p>
        <p id="p_01127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01127"> $var  =17+5</p>
        <p id="p_01128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01128"> 3;</p>
        <p id="p_01129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01129"> You can use the value of a variable in an arithmetic operation, as follows:</p>
        <p id="p_01130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01130"> $varl = 11;</p>
        <p id="p_01131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01131"> $var2 = $varl * 6;</p>
        <p id="p_01132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01132"> The second statement takes the value currently stored in  $var 1, 11,  and multiplies it by  6.  The result,  6 6,  is assigned to  $var2.</p>
        <p id="p_01133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01133"> Now examine the following statements:</p>
        <p id="p_01134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01134"> $var = 11;</p>
        <p id="p_01135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01135"> $var = $var * 6;</p>
        <p id="p_01136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01136"> As you can see,  $var  appears twice in the second statement. What Perl does in this case is straightforward:</p>
        <p id="p_01137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01137"> 1. The first statement assigns the value 11 to  $var.</p>
        <p id="p_01138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01138"> 2.  In the second statement, the Perl interpreter retrieves the current value of  $var,  11 ,  and multiplies it by 6, producing the result 6 6.</p>
        <p id="p_01139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01139"> 3.  This result,  6 6,  is then assigned to  $var  (destroying the old value, 11).</p>
        <p id="p_01140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01140"> As you can see, there is no ambiguity. Perl uses the old value of  $var  in the arithmetic operation, and then it assigns the result of the operation to  $var.</p>
        <p id="p_01141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01141"> NOTE</p>
        <p id="p_01142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01142"> Perl always performs multiplication and division before addition and subtraction-even if the addition or subtraction operator appears first. Perl does this to conform to the rules of arithmetic. For example, in the following statement:</p>
        <p id="p_01143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01143"> $var  =5+6*4;</p>
        <p id="p_01144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01144"> $var  is assigned  2 9: 6  is multiplied by  4,  and then  5  is added to the result</p>
        <p id="p_01145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01145"> Example of Miles-to-Kilometers Conversion</p>
        <p id="p_01146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01146"> To see how arithmetic operators work, look at Listing 2.1, which performs a simple miles-to-kilometers and kilometers-to-miles conversion.</p>
        <p id="p_01147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01147"> Type</p>
        <p id="p_01148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01148"> Listing 2.1. Miles-to-kilometers converter.</p>
        <p id="p_01149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01149"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01150"> 2 :</p>
        <p id="p_01151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01151"> 3: print ("Enter the distance to be converted:\n");</p>
        <p id="p_01152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01152"> 4: $originaldist = &lt;STDIN&gt;;</p>
        <p id="p_01153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01153"> 5: chop ($originaldist) ;</p>
        <p id="p_01154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01154"> 6: $miles = $originaldist * 0.6214;</p>
        <p id="p_01155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01155"> 7: $kilometers = $originaldist * 1.609;</p>
        <p id="p_01156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01156"> 8: print ($originaldist, " kilometers = ", $miles,</p>
        <p id="p_01157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01157"> 9 : " miles\n");</p>
        <p id="p_01158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01158"> 10: print ($originaldist, " miles = ", $kilometers,</p>
        <p id="p_01159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01159"> 11: " kilometers\n");</p>
        <p id="p_01160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01160"> Output</p>
        <p id="p_01161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01161"> $ program2_l</p>
        <p id="p_01162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01162"> Enter the distance to be converted:</p>
        <p id="p_01163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01163"> 10</p>
        <p id="p_01164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01164"> 10 kilometers</p>
        <p id="p_01165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01165"> 6.2139999999999995 miles</p>
        <p id="p_01166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01166"> 10 miles =  16.09  kilometers $</p>
        <p id="p_01167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01167"> Analysis</p>
        <p id="p_01168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01168"> Line 3 of this program asks for a distance to convert. To do this, it prints the following text on your screen</p>
        <p id="p_01169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01169"> Enter the distance to be converted:</p>
        <p id="p_01170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01170"> Note that the  \n  at the end of the text is not printed. The  \n  is a special sequence of characters that represents the newline character; when the  print  library function sees  \n,  it starts a new line of output on your screen. (You'll learn more about special sequences of characters such as  \n  on  Day  3 .)</p>
        <p id="p_01171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01171"> At this point, you can enter any number you want in response to the program's request for a distance. The input/output example shows an entry of 10.</p>
        <p id="p_01172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01172"> Line 4 retrieves the line of input you entered and then assigns it to the variable named</p>
        <p id="p_01173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01173"> $originaldist.</p>
        <p id="p_01174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01174"> Line 5 calls the library function  chop,  which gets rid of the closing newline character that is part of the input line you entered. The  chop  library function is described in the following section, "The chop  Library Function."</p>
        <p id="p_01175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01175"> Line 6 determines the number of miles that is equivalent to 10 kilometers and assigns this number to the variable $miles.</p>
        <p id="p_01176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01176"> Line 7 determines the number of kilometers that is equivalent to 10 miles and assigns this number to the variable  $kilometers.</p>
        <p id="p_01177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01177"> Lines 8-11 print the values of the variables  $miles  and  $kilometers.</p>
        <p id="p_01178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01178"> NOTE</p>
        <p id="p_01179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01179"> Different machines handle floating-point numbers (numbers containing a decimal point) in different ways. Because of this, the numbers displayed in your Listing 2.1 output might not be exactly the same as the numbers shown here. These minor differences will appear whenever a floating-point number is printed.</p>
        <p id="p_01180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01180"> For more information on difficulties with floating-point numbers, refer to the discussion of round-off errors on  Day  3 , "Understanding Scalar Values.</p>
        <p id="p_01181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01181"> The chop Library Function</p>
        <p id="p_01182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01182"> The program shown in Listing 2.1 calls a special library function,  chop.  This function assumes that a line of text is stored in the variable passed to it;  chop's  job is to delete the character at the right end of the line of text. Consider this example:</p>
        <p id="p_01183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01183"> $line = "This is my line"; chop ($line);</p>
        <p id="p_01184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01184"> After  chop  is called, the value of  $line  becomes</p>
        <p id="p_01185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01185"> This is my lin</p>
        <p id="p_01186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01186"> Here's why Listing 2.1 uses  chop.  The statement</p>
        <p id="p_01187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01187"> $originaldist = &lt;STDIN&gt;;</p>
        <p id="p_01188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01188"> assigns a line of input from the standard input file to the variable  $originaldist.  When you type 10 and press Enter, the line of input assigned to  $originaldist  consists of three characters: the 1, the  0,  and a newline character. When  chop  is called, the newline character is removed, and $originaldist  now contains the value 10, which can be used in arithmetic operations.</p>
        <p id="p_01189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01189"> You'll learn more about using lines of input in arithmetic operations and about conversions from lines of input to numbers on  Day  3 .  For now, just remember to call  chop  after reading a number from the standard input file.</p>
        <p id="p_01190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01190"> $originaldist = &lt;STDIN&gt;; chop ($originaldist);</p>
        <p id="p_01191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01191"> Expressions</p>
        <p id="p_01192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01192"> Now that you know a little more about operators, operands, and how they both work, it's time to learn some more terminology as well as the details about exactly what Perl is doing when it evaluates operators such as the arithmetic operators and the assignment operator.</p>
        <p id="p_01193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01193"> In Perl, a collection of operators and operands is known as an  expression.  Each expression yields a result,  which is the value you get when the Perl interpreter  evaluates  the expression (that is, when the Perl interpreter performs the specified operations). For example, in the simple expression</p>
        <p id="p_01194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01194"> 4*5</p>
        <p id="p_01195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01195"> the result is 20, or 4 times 5.</p>
        <p id="p_01196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01196"> You can think of an expression as a set of subordinate expressions. Consider this example:</p>
        <p id="p_01197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01197"> 4 * 5 + 3 * 6</p>
        <p id="p_01198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01198"> When the Perl interpreter evaluates this expression, it first evaluates the subexpressions  4*5  and  3 * 6, yielding the results 20 and 18. These results are then (effectively) substituted for the subexpressions, leaving the following:</p>
        <p id="p_01199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01199"> 20 + 18</p>
        <p id="p_01200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01200"> The Perl interpreter then performs the addition operation, and the final result of the expression is 38. Consider the following statement:</p>
        <p id="p_01201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01201"> $var  =4*5+3;</p>
        <p id="p_01202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01202"> As you can see, the Perl interpreter multiplies 4 by 5, adds 3, and assigns the result, 23, to $var. Here's what the Perl interpreter is doing, more formally, when it evaluates this expression ($var =</p>
        <p id="p_01203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01203"> 4*5 + 3):</p>
        <p id="p_01204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01204"> 1. The subexpression  4 * 5  is evaluated, yielding the result  2 0.  The expression being evaluated is now</p>
        <p id="p_01205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01205"> $var  =20+3</p>
        <p id="p_01206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01206"> because the multiplication operation has been replaced by its result.</p>
        <p id="p_01207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01207"> 2. The subexpression  2 0 + 3  is evaluated, yielding  2 3.  The expression is now</p>
        <p id="p_01208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01208"> $var = 23</p>
        <p id="p_01209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01209"> 3. Finally, the value  2 3  is assigned to  $var.</p>
        <p id="p_01210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01210"> Here's one more example, this time using the value of a variable in an expression:</p>
        <p id="p_01211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01211"> $varl = 15;</p>
        <p id="p_01212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01212"> $var2 = $varl - 11;</p>
        <p id="p_01213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01213"> When the Perl interpreter evaluates the second expression, it does the following:</p>
        <p id="p_01214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01214"> 1. It retrieves the value currently stored in $  var  1 ,  which is  1 5,  and replaces the variable with its value. This means the expression is now</p>
        <p id="p_01215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01215"> $var2  =15-11</p>
        <p id="p_01216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01216"> and  $varl  is out of the picture.</p>
        <p id="p_01217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01217"> 2. The Perl interpreter performs the subtraction operation, yielding</p>
        <p id="p_01218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01218"> $var2 = 4</p>
        <p id="p_01219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01219"> 3. $ var 2  is thus assigned the value  4.</p>
        <p id="p_01220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01220"> NOTE</p>
        <p id="p_01221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01221"> An expression and a statement are two different things. A statement, however, can contain a Perl expression. For example, the statement</p>
        <p id="p_01222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01222"> $var2 = 4;</p>
        <p id="p_01223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01223"> contains the Perl expression</p>
        <p id="p_01224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01224"> $var2 = 4</p>
        <p id="p_01225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01225"> and is terminated by a semicolon (;).</p>
        <p id="p_01226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01226"> The distinction between statements and expressions will become clearer when you encounter other places where Perl statements use expressions. For example, expressions are used in conditional</p>
        <p id="p_01227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01227"> statements, which you'll see later today.</p>
        <p id="p_01228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01228"> Assignments and Expressions</p>
        <p id="p_01229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01229"> The assignment operator, like all Perl operators, yields a result. The result of an assignment operation is the value assigned. For example, in the expression</p>
        <p id="p_01230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01230"> $var = 42</p>
        <p id="p_01231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01231"> the result of the expression is  4 2,  which is the value assigned to  $var.</p>
        <p id="p_01232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01232"> Because the assignment operator yields a value, you can use more than one assignment operator in a single expression:</p>
        <p id="p_01233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01233"> $varl = $var2 = 42;</p>
        <p id="p_01234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01234"> In this example, the subexpression</p>
        <p id="p_01235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01235"> $var2 = 42</p>
        <p id="p_01236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01236"> is performed first. (You'll learn why on  Day  4 ,  "More Operators," in the lesson about operator precedence.) The result of this subexpression is  4 2,  and the expression is now</p>
        <p id="p_01237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01237"> $varl = 42</p>
        <p id="p_01238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01238"> At this point,  4 2  is assigned to  $varl.</p>
        <p id="p_01239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01239"> Other Perl Operators</p>
        <p id="p_01240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01240"> So far, you have encountered the following Perl operators, which are just a few of the many operators Perl supports:</p>
        <p id="p_01241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01241"> The assignment operator, =.</p>
        <p id="p_01242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01242"> The arithmetic operators +, -, *, and /.</p>
        <p id="p_01243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01243"> You'll learn about additional Perl operators on  Day 4 .</p>
        <p id="p_01244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01244"> Introduction to Conditional Statements</p>
        <p id="p_01245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01245"> So far, the Perl programs you've seen have had their statements executed in sequential order. For example, consider the kilometer-to-mile conversion program you saw in Listing 2.1:</p>
        <p id="p_01246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01246"> #!/usr/local/bin/perl</p>
        <p id="p_01247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01247"> print ("Enter the distance to be converted:\n");</p>
        <p id="p_01248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01248"> $originaldist = &lt;STDIN&gt;; chop ($originaldist);</p>
        <p id="p_01249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01249"> $miles = $originaldist * 0.6214;</p>
        <p id="p_01250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01250"> $kilometers = $originaldist * 1.609;</p>
        <p id="p_01251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01251"> print ($originaldist, " kilometers = ", $miles,</p>
        <p id="p_01252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01252"> " miles\n");</p>
        <p id="p_01253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01253"> print ($originaldist, " miles = ", $kilometers,</p>
        <p id="p_01254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01254"> " kilometers\n");</p>
        <p id="p_01255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01255"> When the Perl interpreter executes this program, it starts at the top of the program and executes each statement in turn. When the final statement is executed, the program is terminated.</p>
        <p id="p_01256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01256"> All the statements in this program are  unconditional statements -that is, they always are executed sequentially, regardless of what is happening in the program. In some situations, however, you might want to have statements that are executed only when certain conditions are true. These statements are known as  conditional statements.</p>
        <p id="p_01257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01257"> Perl supports a variety of conditional statements. In the following sections, you'll learn about these conditional statements:</p>
        <p id="p_01258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01258"> Perl also has other conditional statements, which you'll learn about on  Day  8 ,  "More Control Structures."</p>
        <p id="p_01259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01259"> The if Statement</p>
        <p id="p_01260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01260"> The  i f  statement is the simplest conditional statement used in Perl. The easiest way to explain how the if statement works is to show you a simple example:</p>
        <p id="p_01261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01261"> if ($number) {</p>
        <p id="p_01262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01262"> print ("The number is not  zero.\n");</p>
        <p id="p_01263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01263"> }</p>
        <p id="p_01264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01264"> The if statement consists of (closing brace character):</p>
        <p id="p_01265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01265"> This statement consists of two parts:</p>
        <p id="p_01266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01266"> • The code between the if and the open brace character ({).</p>
        <p id="p_01267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01267"> • The code between the { and the }.</p>
        <p id="p_01268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01268"> The first part is known as a  conditional expression ; the second part is a set of one or more statements called a  statement block.   Let's look at each part in detail.</p>
        <p id="p_01269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01269"> The Conditional Expression</p>
        <p id="p_01270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01270"> The first part of an  i f  statement-the part between the parentheses-is the  conditional expression associated with the  i f  statement. This conditional expression is just like any other expression you've seen so far; in fact, you can use any legal Perl expression as a conditional expression.</p>
        <p id="p_01271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01271"> When the Perl interpreter sees a conditional expression, it evaluates the expression. The result of the expression is then placed in one of two classes:</p>
        <p id="p_01272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01272"> • If the result is a nonzero value, the conditional expression is  true.</p>
        <p id="p_01273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01273"> • If the result is zero, the conditional expression is  false.</p>
        <p id="p_01274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01274"> The Perl interpreter uses the value of the conditional expression to decide whether to execute the</p>
        <p id="p_01275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01275"> statements between the { and } characters. If the conditional expression is true, the statements are executed. If the conditional expression is false, the statements are not executed.</p>
        <p id="p_01276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01276"> In the example you have just seen,</p>
        <p id="p_01277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01277"> if ($number) {</p>
        <p id="p_01278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01278"> print ("The number is not zero.\n");</p>
        <p id="p_01279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01279"> }</p>
        <p id="p_01280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01280"> the conditional expression consists of the value of the variable $  number.  If $  number  contains something other than zero, the conditional expression is true, and the statement</p>
        <p id="p_01281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01281"> print ("The value is not zero.\n");</p>
        <p id="p_01282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01282"> is executed. If  $number  currently is set to zero, the conditional expression is false, and the  print statement is not executed.</p>
        <p id="p_01283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01283"> Listing  2.2  is a program that contains this simple  i f  statement.</p>
        <p id="p_01284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01284"> Type</p>
        <p id="p_01285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01285"> Listing 2.2. A program containing a simple example of an if statement.</p>
        <p id="p_01286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01286"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01287"> 2 :</p>
        <p id="p_01288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01288"> 3: print ("Enter a number:\n");</p>
        <p id="p_01289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01289"> 4: $number = &lt;STDIN&gt;;</p>
        <p id="p_01290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01290"> 5: chop ($number);</p>
        <p id="p_01291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01291"> 6: if ($number) {</p>
        <p id="p_01292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01292"> 7 :</p>
        <p id="p_01293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01293"> 8 : }</p>
        <p id="p_01294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01294"> print ("The number is not zero.\n") ;</p>
        <p id="p_01295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01295"> 9: print ("This is the last line of the program.\n");</p>
        <p id="p_01296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01296"> $ program2_2</p>
        <p id="p_01297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01297"> Enter a number:</p>
        <p id="p_01298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01298"> 5</p>
        <p id="p_01299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01299"> The number is not zero.</p>
        <p id="p_01300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01300"> This is the last line of the program. $</p>
        <p id="p_01301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01301"> Analysis</p>
        <p id="p_01302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01302"> Lines 3, 4, and 5 of Listing 2.2 are similar to lines you've seen before. Line 3 tells you to enter a number; line 4 assigns the line you've entered to the variable $  number;  and line 5 throws away the trailing newline character</p>
        <p id="p_01303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01303"> Lines  6-8  constitute the  i f  statement itself. As you have seen, this statement evaluates the conditional expression consisting of the variable $  number.  If $  number  is not zero, the expression is true, and the call to  print  is executed. If  $number  is zero, the expression is false, and the call to  print  is skipped; the Perl interpreter thus jumps to line 9.</p>
        <p id="p_01304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01304"> The Perl interpreter executes line 9 and prints the following regardless of whether the conditional expression in line 6 is true or false:</p>
        <p id="p_01305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01305"> This is the last line of the program.</p>
        <p id="p_01306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01306"> Now that you understand how an  if  statement works, you're ready to see the formal syntax definition for the  i f  statement.</p>
        <p id="p_01307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01307"> The syntax for the  i f  statement is</p>
        <p id="p_01308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01308"> if  (expr)  {</p>
        <p id="p_01309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01309"> statement block</p>
        <p id="p_01310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01310"> }</p>
        <p id="p_01311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01311"> This formal definition doesn't tell you anything you don't already know,  expr  refers to the conditional expression, which evaluates to either true or false.  statement_block  is the group of statements that is executed when  expr  evaluates to true.</p>
        <p id="p_01312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01312"> Warning</p>
        <p id="p_01313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01313"> If you are familiar with the C programming language, you probably have noticed that the i f statement in Perl is syntactically similar to the if statement in C. There is one important difference, however: In Perl, the braces ({ and }) must be present</p>
        <p id="p_01314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01314"> The following statement is illegal in Perl because the { and } are missing:</p>
        <p id="p_01315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01315"> if ($number)</p>
        <p id="p_01316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01316"> print ("The value is not  zero.\n");</p>
        <p id="p_01317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01317"> Perl does support a syntax for single-line conditional statements. This is discussed on  Day  8 .</p>
        <p id="p_01318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01318"> The Statement Block</p>
        <p id="p_01319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01319"> The second part of the i f statement, the part between the { and the }, is called a  statement block.  A statement block consists of any number of legal Perl statements (including no statements, if you like).</p>
        <p id="p_01320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01320"> In the following example, the statement block consists of one statement:</p>
        <p id="p_01321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01321"> print  ("The  value is not  zero.\n");</p>
        <p id="p_01322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01322"> NOTE</p>
        <p id="p_01323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01323"> A statement block can be completely empty. In this statement, for example:</p>
        <p id="p_01324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01324"> if ($number == 21) {</p>
        <p id="p_01325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01325"> }</p>
        <p id="p_01326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01326"> there is nothing between the { and }, so the statement block is empty. This is perfectly legal Perl code, although it's not particularly useful</p>
        <p id="p_01327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01327"> Testing for Equality Using ==</p>
        <p id="p_01328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01328"> So far, the only conditional expression you've seen is an expression consisting of a single variable. Although you can use any expression you like and any operators you like, Perl provides special operators that are designed for use in conditional expressions. One such operator is the  equality comparison operator , ==.</p>
        <p id="p_01329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01329"> The == operator, like the other operators you've seen so far, requires two operands or subexpressions. Unlike the other operators, however, it yields one of two possible results: true or false. (The other operators you've seen yield a numeric value as a result.) The == operator works like this:</p>
        <p id="p_01330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01330"> • If the two subexpressions evaluate to the same numeric value, the == operator yields the result true.</p>
        <p id="p_01331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01331"> • If the two subexpressions have different values, the == operator yields the result  false.</p>
        <p id="p_01332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01332"> Because the == operator returns either true or false, it is ideal for use in conditional expressions, because conditional expressions are expected to evaluate to either true or false. For an example, look at Listing 2.3, which compares two numbers read in from the standard input file.</p>
        <p id="p_01333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01333"> Type</p>
        <p id="p_01334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01334"> Listing 2.3. A program that uses the equality-comparison operator to compare two numbers entered at the keyboard.</p>
        <p id="p_01335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01335"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01336"> 2 :</p>
        <p id="p_01337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01337"> 3 :</p>
        <p id="p_01338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01338"> print ("Enter a number:\n");</p>
        <p id="p_01339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01339"> $numberl = &lt;STDIN&gt;;</p>
        <p id="p_01340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01340"> 4 :</p>
        <p id="p_01341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01341"> 5: chop ($numberl);</p>
        <p id="p_01342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01342"> 6: print ("Enter another number:\n");</p>
        <p id="p_01343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01343"> 7: $number2 = &lt;STDIN&gt;;</p>
        <p id="p_01344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01344"> 8: chop ($number2);</p>
        <p id="p_01345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01345"> 9: if ($numberl == $number2) {</p>
        <p id="p_01346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01346"> 10: print ("The two numbers are equal.\n");</p>
        <p id="p_01347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01347"> 11 : }</p>
        <p id="p_01348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01348"> 12: print ("This is the last line of the program.\n");</p>
        <p id="p_01349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01349"> Output</p>
        <p id="p_01350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01350"> $ program2_3</p>
        <p id="p_01351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01351"> Enter a number:</p>
        <p id="p_01352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01352"> 17</p>
        <p id="p_01353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01353"> Enter another number:</p>
        <p id="p_01354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01354"> 17</p>
        <p id="p_01355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01355"> The two numbers are equal.</p>
        <p id="p_01356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01356"> This is the last line of the program.</p>
        <p id="p_01357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01357"> $</p>
        <p id="p_01358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01358"> Lines 3-5 are again similar to statements you’ve seen before. They print a message on your screen, read a number into the variable  $ number  1 ,  and chop the newline character from the number</p>
        <p id="p_01359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01359"> Analyse</p>
        <p id="p_01360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01360"> Lines 6-8 repeat the preceding process for a second number, which is stored in  $number2.</p>
        <p id="p_01361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01361"> Lines 9-11 contain the if statement that compares the two numbers. Line 9 contains the conditional expression</p>
        <p id="p_01362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01362"> $numberl == $number2</p>
        <p id="p_01363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01363"> If the two numbers are equal, the conditional expression is true, and the  print  statement in line 10 executed. If the two numbers are not equal, the conditional expression is false, so the  print statement in line 10 is not executed; in this case, the Perl interpreter skips to the first statement after the if statement, which is line 12.</p>
        <p id="p_01364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01364"> Line 12 is executed regardless of whether or not the conditional expression in line 9 is true. It prints the following message on the screen:</p>
        <p id="p_01365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01365"> This is the last line of the program.</p>
        <p id="p_01366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01366"> Warning</p>
        <p id="p_01367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01367"> Make sure that you don't confuse the = and == operators.</p>
        <p id="p_01368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01368"> Because any expression can be used as a conditional expression,</p>
        <p id="p_01369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01369"> Perl is quite happy to accept statements such as</p>
        <p id="p_01370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01370"> if ($number = 5) {</p>
        <p id="p_01371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01371"> print ("The number is  five.\n");</p>
        <p id="p_01372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01372"> }</p>
        <p id="p_01373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01373"> Here, the if statement is evaluated as follows:</p>
        <p id="p_01374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01374"> 1. The number 5 is assigned to $number, and the following expression yields the result 5:</p>
        <p id="p_01375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01375"> $number = 5</p>
        <p id="p_01376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01376"> 2. The value 5 is nonzero, so the conditional expression is true.</p>
        <p id="p_01377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01377"> 3. Because the conditional expression is true, this statement is executed:</p>
        <p id="p_01378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01378"> print  ("The  number is  five.\n");</p>
        <p id="p_01379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01379"> Note that the  print  statement is executed regardless of what the value of $  number  was before the  if  statement. This is because the value  5  is assigned to $  number  by the conditional expression.</p>
        <p id="p_01380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01380"> To repeat: Be careful when you use the == operator</p>
        <p id="p_01381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01381"> Other Comparison Operators</p>
        <p id="p_01382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01382"> The == operator is just one of many comparison operators that you can use in conditional expressions. For a complete list, refer to  Day 4 .</p>
        <p id="p_01383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01383"> Two-Way Branching Using if and else</p>
        <p id="p_01384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01384"> When you examine Listing 2.3 (shown previously), you might notice a problem. What happens if the two numbers are not equal? In this case, the statement</p>
        <p id="p_01385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01385"> print ("The two numbers are equal.\n");</p>
        <p id="p_01386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01386"> is not printed. In fact, nothing is printed.</p>
        <p id="p_01387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01387"> Suppose you want to modify Listing 2.3 to print one message if the two numbers are equal and another message if the two numbers are not equal. One convenient way of doing this is with the  if-else  statement.</p>
        <p id="p_01388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01388"> Listing 2.4 is a modification of the program in Listing 2.3. It uses the  if-else  statement to print one of two messages, depending on whether the numbers are equal.</p>
        <p id="p_01389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01389"> Type</p>
        <p id="p_01390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01390"> Listing 2.4. A program that uses the if-else statement.</p>
        <p id="p_01391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01391"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01392"> 2 :</p>
        <p id="p_01393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01393"> 3: print ("Enter a number:\n");</p>
        <p id="p_01394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01394"> $numberl = &lt;STDIN&gt;;</p>
        <p id="p_01395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01395"> 4 :</p>
        <p id="p_01396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01396"> 5: chop ($numberl);</p>
        <p id="p_01397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01397"> 6: print ("Enter another number:\n");</p>
        <p id="p_01398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01398"> 7: $number2 = &lt;STDIN&gt;;</p>
        <p id="p_01399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01399"> 8: chop ($number2);</p>
        <p id="p_01400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01400"> 9: if ($numberl == $number2) {</p>
        <p id="p_01401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01401"> 10: print ("The two numbers are equal.\n");</p>
        <p id="p_01402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01402"> 11: } else {</p>
        <p id="p_01403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01403"> 12: print ("The two numbers are not equal.\n");</p>
        <p id="p_01404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01404"> 13: }</p>
        <p id="p_01405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01405"> 14: print ("This is the last line of the program.\n");</p>
        <p id="p_01406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01406"> OuTPUt</p>
        <p id="p_01407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01407"> $ program2_4</p>
        <p id="p_01408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01408"> Enter a number:</p>
        <p id="p_01409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01409"> 17</p>
        <p id="p_01410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01410"> Enter another number:</p>
        <p id="p_01411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01411"> 18</p>
        <p id="p_01412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01412"> The two numbers are not equal.</p>
        <p id="p_01413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01413"> This is the last line of the program.</p>
        <p id="p_01414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01414"> $</p>
        <p id="p_01415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01415"> Analysis</p>
        <p id="p_01416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01416"> Lines 3-8 are identical to those in Listing 2.3. They read in two numbers, assign them to</p>
        <p id="p_01417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01417"> $numberl  and  $number2,  and chop their newline characters</p>
        <p id="p_01418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01418"> Line 9 compares the value stored in $  number  1 to the value stored in  $n umber 2.  If the two values are equal, line 10 is executed, and the following message is printed:</p>
        <p id="p_01419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01419"> The two numbers are equal.</p>
        <p id="p_01420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01420"> The Perl interpreter then jumps to the first statement after the  if-else  statement-line  14.</p>
        <p id="p_01421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01421"> If the two values are not equal, line 12 is executed, and the following message is printed:</p>
        <p id="p_01422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01422"> The two numbers are not equal.</p>
        <p id="p_01423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01423"> The interpreter then continues with the first statement after the  if-e   Is  e-line 14.</p>
        <p id="p_01424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01424"> In either case, the Perl interpreter executes line 14, which prints the following message:</p>
        <p id="p_01425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01425"> This is the last line of the program.</p>
        <p id="p_01426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01426"> The syntax for the  if-else  statement is</p>
        <p id="p_01427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01427"> if ( expr)  {</p>
        <p id="p_01428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01428"> statement_block_l</p>
        <p id="p_01429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01429"> } else {</p>
        <p id="p_01430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01430"> statement block 2</p>
        <p id="p_01431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01431"> }</p>
        <p id="p_01432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01432"> As in the  i f  statement,  expr  is any expression (it is usually a conditional expression). statement_block_l  is the block of statements that the Perl interpreter executes if  expr  is true, and  statement_block_2  is the block of statements that are executed if  expr  is false.</p>
        <p id="p_01433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01433"> Note that the  else  part of the  if-else  statement cannot appear by itself; it must always follow an if.</p>
        <p id="p_01434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01434"> TIP</p>
        <p id="p_01435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01435"> In Perl, as you’ve learned, you can use any amount of white space to separate tokens. This means that you can present conditional statements in a variety of ways.</p>
        <p id="p_01436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01436"> The examples in this book use what is called the  one true brace style:</p>
        <p id="p_01437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01437"> if ($number == 0) {</p>
        <p id="p_01438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01438"> print ("The number is zero.\n");</p>
        <p id="p_01439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01439"> } else {</p>
        <p id="p_01440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01440"> print ("The number is not zero.\n");</p>
        <p id="p_01441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01441"> }</p>
        <p id="p_01442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01442"> In this brace style, the opening brace ({) appears on the same line as the  if  or  else,  and the closing brace (}) starts a new line.</p>
        <p id="p_01443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01443"> Other programmers insist on putting the braces on separate lines:</p>
        <p id="p_01444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01444"> if ($number == 0)</p>
        <p id="p_01445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01445"> {</p>
        <p id="p_01446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01446"> print ("The number is zero.\n");</p>
        <p id="p_01447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01447"> }</p>
        <p id="p_01448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01448"> else</p>
        <p id="p_01449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01449"> {</p>
        <p id="p_01450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01450"> print ("The number is not zero.\n");</p>
        <p id="p_01451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01451"> }</p>
        <p id="p_01452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01452"> Still others prefer to indent their braces:</p>
        <p id="p_01453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01453"> if ($number == 0)</p>
        <p id="p_01454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01454"> {</p>
        <p id="p_01455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01455"> print ("The number is not zero.\n");</p>
        <p id="p_01456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01456"> }</p>
        <p id="p_01457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01457"> I prefer the one true brace style because it is both legible and compact. However, it doesn't really matter what brace style you choose, provided that you follow these rules:</p>
        <p id="p_01458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01458"> The brace style is consistent. Every  i f  and  else  that appears in your program should have its braces displayed in the same way.</p>
        <p id="p_01459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01459"> • The brace style is easy to follow.</p>
        <p id="p_01460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01460"> • The statement blocks inside the braces always should be indented in the same way.</p>
        <p id="p_01461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01461"> If you do not follow a consistent style, and you write statements such as</p>
        <p id="p_01462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01462"> if ($number == 0) { print ("The number is</p>
        <p id="p_01463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01463"> zero"); }</p>
        <p id="p_01464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01464"> you'll find that your code is difficult to understand, especially when you start writing longer Perl programs</p>
        <p id="p_01465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01465"> Multi-Way Branching Using elsif</p>
        <p id="p_01466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01466"> Listing 2.4 (which you've just seen) shows how to write a program that chooses between two alternatives. Perl also provides a conditional statement, the  if-elsif-else  statement, which selects one of more than two alternatives. Listing 2.5 illustrates the use of  elsif.</p>
        <p id="p_01467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01467"> Listing  2.5. A  program that uses the  if-elsif-else  statement.</p>
        <p id="p_01468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01468"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01469"> 2 :</p>
        <p id="p_01470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01470"> 3: print ("Enter a number:\n");</p>
        <p id="p_01471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01471"> 4: $numberl = &lt;STDIN&gt;;</p>
        <p id="p_01472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01472"> 5: chop ($numberl);</p>
        <p id="p_01473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01473"> 6: print ("Enter another number:\n");</p>
        <p id="p_01474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01474"> 7: $number2 = &lt;STDIN&gt;;</p>
        <p id="p_01475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01475"> 8: chop ($number2);</p>
        <p id="p_01476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01476"> 9: if ($numberl == $number2) {</p>
        <p id="p_01477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01477"> print ("The two numbers are equal.\n");</p>
        <p id="p_01478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01478"> 10  :</p>
        <p id="p_01479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01479"> 11: } elsif ($numberl == $number2 + 1) {</p>
        <p id="p_01480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01480"> 12: print ("The first number is greater by one.\n");</p>
        <p id="p_01481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01481"> 13: } elsif ($numberl + 1 == $number2) {</p>
        <p id="p_01482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01482"> 14: print ("The second number is greater by one.\n");</p>
        <p id="p_01483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01483"> 15: } else {</p>
        <p id="p_01484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01484"> 16: print ("The two numbers are not equal.\n");</p>
        <p id="p_01485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01485"> 17: }</p>
        <p id="p_01486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01486"> 18: print ("This is the last line of the program.\n");</p>
        <p id="p_01487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01487"> $ program2_5</p>
        <p id="p_01488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01488"> Enter a number:</p>
        <p id="p_01489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01489"> 17</p>
        <p id="p_01490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01490"> Enter another number:</p>
        <p id="p_01491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01491"> 18</p>
        <p id="p_01492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01492"> The second number is greater by one. This is the last line of the program. $</p>
        <p id="p_01493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01493"> You already are familiar with lines 3-8. They obtain two numbers from the standard input file and assign them to  $number 1  and  $number2,  chopping the terminating newline character in the process</p>
        <p id="p_01494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01494"> Line 9 checks whether the two numbers are equal. If the numbers are equal, line 10 is executed, and</p>
        <p id="p_01495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01495"> Analysis</p>
        <p id="p_01496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01496"> the following message is printed:</p>
        <p id="p_01497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01497"> The two numbers are equal.</p>
        <p id="p_01498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01498"> The Perl interpreter then jumps to the first statement after the  if-elsif-else  statement, which is line 18.</p>
        <p id="p_01499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01499"> If the two numbers are not equal, the Perl interpreter goes to line 11. Line 11 performs another comparison. It adds 1 to the value of $  number 2  and compares it with the value of $  number  1 .  If the two values are equal, the Perl interpreter executes line 12, printing the message</p>
        <p id="p_01500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01500"> The first number is greater by one.</p>
        <p id="p_01501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01501"> The interpreter then jumps to line 18-the statement following the  if-elsif-else  statement.</p>
        <p id="p_01502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01502"> If the conditional expression in line 11 is false, the interpreter jumps to line 13. Line 13 adds 1 to the value of $  number 1  and compares it with the value of  $n umber 2.  If these two values are equal, the Perl interpreter executes line 14, which prints</p>
        <p id="p_01503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01503"> The second number is greater by one.</p>
        <p id="p_01504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01504"> on the screen. The interpreter then jumps to line 18.</p>
        <p id="p_01505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01505"> If the conditional expression in line 13 is false, the Perl interpreter jumps to line 15 and executes line 16, which prints</p>
        <p id="p_01506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01506"> The two numbers are not equal.</p>
        <p id="p_01507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01507"> on the screen. The Perl interpreter continues with the next statement, which is line 18.</p>
        <p id="p_01508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01508"> If you have followed the program logic to this point, you've realized that the Perl interpreter eventually reaches line 18 in every case. Line 18 prints this statement:</p>
        <p id="p_01509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01509"> This is the last line of the program.</p>
        <p id="p_01510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01510"> The syntax of the  if-elsif-else  statement is as follows:</p>
        <p id="p_01511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01511"> if ( expr_l) {</p>
        <p id="p_01512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01512"> statement_block_l }  elsif ( expr_2) {</p>
        <p id="p_01513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01513"> statemen t_block_2 } elsif ( expr_3 ) {</p>
        <p id="p_01514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01514"> statement_block_3</p>
        <p id="p_01515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01515"> } else {</p>
        <p id="p_01516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01516"> defaul t_statement_block</p>
        <p id="p_01517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01517"> }</p>
        <p id="p_01518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01518"> Here,  expr_l, expr_2,   and  expr_3   are conditional expressions.  statement_block_l, statement_block_2, statement_block_3,   and  default_statement_block   are</p>
        <p id="p_01519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01519"> blocks of statements.</p>
        <p id="p_01520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01520"> The . . . indicates that you can have as many  elsif  statements as you like. Each  elsif  statement has the same form:</p>
        <p id="p_01521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01521"> } elsif ( expr ) {</p>
        <p id="p_01522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01522"> statement_block</p>
        <p id="p_01523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01523"> }</p>
        <p id="p_01524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01524"> Syntactically, an  if-else  statement is just an  if-elsif-else  statement with no  elsif  parts.</p>
        <p id="p_01525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01525"> If you want, you can leave out the  else  part of the  if-elsif-else  statement, as follows:</p>
        <p id="p_01526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01526"> if ( expr_l)  {</p>
        <p id="p_01527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01527"> statement_block_l }  elsif ( expr_2) {</p>
        <p id="p_01528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01528"> statement_block_2 } elsif  ( expr_3) {</p>
        <p id="p_01529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01529"> statement block 3</p>
        <p id="p_01530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01530"> }</p>
        <p id="p_01531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01531"> Here, if none of the expressions-expr_l, expr_2, expr_3,  and so on-are true, the Perl interpreter just skips to the first statement following the  if-elsif-else  statement.</p>
        <p id="p_01532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01532"> NOTE</p>
        <p id="p_01533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01533"> The  elsif  parts of the  if-elsif-else  statement must appear between the  i f  part and the  else  part</p>
        <p id="p_01534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01534"> Writing Loops Using the while Statement</p>
        <p id="p_01535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01535"> The conditional statements you've seen so far enable the Perl interpreter to decide between alternatives. However, each statement in the Perl programs that you have seen is either not executed or is executed only once.</p>
        <p id="p_01536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01536"> Perl also enables you to write conditional statements that tell the Perl interpreter to repeat a block of statements a specified number of times. A block of statements that can be repeated is known as a  loop.</p>
        <p id="p_01537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01537"> The simplest way to write a loop in Perl is with the  while  statement. Here is a simple example of a while  statement:</p>
        <p id="p_01538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01538"> while ($number == 5) {</p>
        <p id="p_01539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01539"> print ("The number is still  5!\n");</p>
        <p id="p_01540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01540"> }</p>
        <p id="p_01541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01541"> The  while  statement is structurally similar to the  if  statement, but it works in a slightly different way. Here's how:</p>
        <p id="p_01542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01542"> • First, the conditional expression located between the parentheses is tested.</p>
        <p id="p_01543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01543"> • If the conditional expression is true, the statement block between the { and } is executed. If</p>
        <p id="p_01544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01544"> the expression is false, the statement block is skipped, and the Perl interpreter jumps to the statement following the  while  statement. (This is called  exiting the loop.)</p>
        <p id="p_01545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01545"> • If the statement block is executed, the Perl interpreter jumps back to the start of the  while statement and tests the conditional expression over again. (This is the looping part of the while  statement, because at this point the Perl interpreter is executing a statement it has executed before.)</p>
        <p id="p_01546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01546"> The statement block in the  while  statement is repeated until the conditional expression becomes false. This means that the statement</p>
        <p id="p_01547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01547"> while ($number == 5) {</p>
        <p id="p_01548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01548"> print ("The number is still  5!\n");</p>
        <p id="p_01549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01549"> }</p>
        <p id="p_01550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01550"> loops forever (which is referred to as going into an  infinite loop ) if the value of $  number  is 5, because the value of $  number  never changes and the following conditional expression is always true:</p>
        <p id="p_01551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01551"> $number == 5</p>
        <p id="p_01552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01552"> For a more useful example of a  while  statement-one that does not go into an infinite loop-take a look at Listing 2.6.</p>
        <p id="p_01553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01553"> Type</p>
        <p id="p_01554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01554"> Listing 2.6.  A  program that demonstrates the while statement.</p>
        <p id="p_01555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01555"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01556"> 2 :</p>
        <p id="p_01557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01557"> 3: $done = 0;</p>
        <p id="p_01558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01558"> 4: $count = 1;</p>
        <p id="p_01559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01559"> 5 :</p>
        <p id="p_01560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01560"> print ("This line is printed before the loop  starts.\n");</p>
        <p id="p_01561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01561"> 6:</p>
        <p id="p_01562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01562"> while ($done == 0) {</p>
        <p id="p_01563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01563"> 7: print ("The value of count is ", $count, "\n");</p>
        <p id="p_01564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01564"> 8: if ($count == 3) {</p>
        <p id="p_01565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01565"> 9: $done = 1;</p>
        <p id="p_01566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01566"> 10 : }</p>
        <p id="p_01567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01567"> 11: $count = $count + 1;</p>
        <p id="p_01568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01568"> 12 : }</p>
        <p id="p_01569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01569"> 13: print ("End of loop.\n");</p>
        <p id="p_01570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01570"> Output</p>
        <p id="p_01571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01571"> $ program2_6</p>
        <p id="p_01572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01572"> This line is printed before the loop starts</p>
        <p id="p_01573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01573"> The value of count is 1</p>
        <p id="p_01574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01574"> The value of count is 2</p>
        <p id="p_01575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01575"> The value of count is 3</p>
        <p id="p_01576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01576"> End of loop</p>
        <p id="p_01577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01577"> Lines 3-5 prepare the program for looping. Line 3 assigns the value 0 to the variable $done.  (As you'll see, the program uses  $done  to indicate whether or not to continue looping.) Line 4 assigns the value 1 to the variable $  count.  Line 5 prints the following line to the screen</p>
        <p id="p_01578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01578"> This line is printed before the loop starts.</p>
        <p id="p_01579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01579"> Analyse</p>
        <p id="p_01580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01580"> The  while  statement appears in lines 6-12. Line 6 contains a conditional expression to be tested. If</p>
        <p id="p_01581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01581"> the conditional expression is true, the statement block in lines 7-11 is executed. At this point, the conditional expression is true, so the Perl interpreter continues with line 7.</p>
        <p id="p_01582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01582"> Line 7 prints the current value of the variable $  count.  At present, $  count  is set to 1. This means that line 7 prints the following on the screen:</p>
        <p id="p_01583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01583"> The value of count is 1</p>
        <p id="p_01584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01584"> Lines 8-10 test whether $  count  has reached the value  3.  Because $  count  is 1 at the moment, the conditional expression in line 8 is false, and the Perl interpreter skips to line 11.</p>
        <p id="p_01585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01585"> Line 11 adds 1 to the current value of $  count,  setting it to  2.</p>
        <p id="p_01586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01586"> Line  12  is the bottom of the  while  statement. The Perl interpreter now jumps back to line  6,  and the whole process is repeated. Here's how the Perl interpreter continues from here:</p>
        <p id="p_01587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01587"> • Line 6:  $done  ==  0  is true, so continue.</p>
        <p id="p_01588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01588"> • Line 7: Print  The value of count is 2  on the screen.</p>
        <p id="p_01589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01589"> • Line  8: $count  is  2; $count == 3  is false, so skip to line 11.</p>
        <p id="p_01590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01590"> • Line 11: 1 is added to $  count; $ count  is now  3.</p>
        <p id="p_01591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01591"> • Line 12: Jump back to the start of the loop, which is line 6.</p>
        <p id="p_01592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01592"> • Line 6:  $done  ==  0  is true, so continue.</p>
        <p id="p_01593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01593"> • Line 7: Print  The value of count is 3  on the screen.</p>
        <p id="p_01594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01594"> • Line 8: $  count  is  3;  $  count  ==  3  is true, and the  if  statement block is executed.</p>
        <p id="p_01595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01595"> • Line 9:  $done  is set to 1. Execution continues with the first statement after the  if,  which is line 11.</p>
        <p id="p_01596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01596"> • Line 11: $  count  is set to  4.</p>
        <p id="p_01597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01597"> • Line 12: Jump back to line 6.</p>
        <p id="p_01598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01598"> • Line 6:  $done  ==  0  is now false, because the value of  $done  is 1. The Perl interpreter exits the loop and continues with the first statement after  while,  which is line 13.</p>
        <p id="p_01599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01599"> Line 13 prints the following message on the screen:</p>
        <p id="p_01600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01600"> End of loop.</p>
        <p id="p_01601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01601"> At this point, program execution terminates because there are no more statements to execute. The syntax for the  while  statement is</p>
        <p id="p_01602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01602"> while ( expr)  {</p>
        <p id="p_01603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01603"> statement block</p>
        <p id="p_01604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01604"> }</p>
        <p id="p_01605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01605"> As you can see, the  while  statement is syntactically similar to the  if  statement,  expr  is a conditional expression to be evaluated, and  statement_block  is a block of statements to be executed while  expr  is true.</p>
        <p id="p_01606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01606"> Nesting Conditional Statements</p>
        <p id="p_01607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01607"> The  i f  statement in Listing 2.6 (shown previously) is an example of a  nested conditional statement.  It is contained inside another conditional statement (the  while  statement). In Perl, you can nest any conditional statement inside another. For example, you can have a  while  statement inside another while  statement, as follows:</p>
        <p id="p_01608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01608"> while ( expr_l)  {</p>
        <p id="p_01609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01609"> some_statements while ( expr_2)  {</p>
        <p id="p_01610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01610"> lnner_statement_block</p>
        <p id="p_01611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01611"> }</p>
        <p id="p_01612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01612"> some_more_statements</p>
        <p id="p_01613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01613"> }</p>
        <p id="p_01614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01614"> Similarly, you can have an  if  statement inside another  if  statement, or you can have  awhile statement inside an  i f  statement.</p>
        <p id="p_01615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01615"> You can nest conditional statements inside  elsif  and  else  parts of  if  statements as well:</p>
        <p id="p_01616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01616"> if ($number == 0) {</p>
        <p id="p_01617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01617"> # some statements go here } elsif ($number == 1) {</p>
        <p id="p_01618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01618"> while ($number2</p>
        <p id="p_01619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01619"> 19) {</p>
        <p id="p_01620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01620"> # here is a place for a statement block</p>
        <p id="p_01621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01621"> }</p>
        <p id="p_01622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01622"> } else {</p>
        <p id="p_01623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01623"> while ($number2 == 33) {</p>
        <p id="p_01624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01624"> # here is a place for another statement block</p>
        <p id="p_01625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01625"> }</p>
        <p id="p_01626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01626"> }</p>
        <p id="p_01627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01627"> The braces ({ and }) around the statement block for each conditional statement ensure that the Perl interpreter never gets confused.</p>
        <p id="p_01628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01628"> TIP</p>
        <p id="p_01629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01629"> If you plan to nest conditional statements, it's a good idea to indent each statement block to indicate how many levels of nesting you are using. If you write code such as the following, it's easy to get confused:</p>
        <p id="p_01630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01630"> while ($done == 0) {</p>
        <p id="p_01631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01631"> print ("The value of count  is",  $count,</p>
        <p id="p_01632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01632"> "\n");</p>
        <p id="p_01633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01633"> if  ($count == 3) {</p>
        <p id="p_01634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01634"> $done = 1;</p>
        <p id="p_01635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01635"> }</p>
        <p id="p_01636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01636"> $count = $count + 1;</p>
        <p id="p_01637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01637"> }</p>
        <p id="p_01638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01638"> Although this code is correct, it's not easy to see that the statement</p>
        <p id="p_01639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01639"> $done = 1;</p>
        <p id="p_01640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01640"> is actually inside an  if  statement that is inside a  while statement. Larger and more complicated programs rapidly become unreadable if you do not indent properly.</p>
        <p id="p_01641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01641"> Looping Using the until Statement</p>
        <p id="p_01642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01642"> Another way to loop in Perl is with the  until  statement. It is similar in appearance to the  while statement, but it works in a slightly different way.</p>
        <p id="p_01643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01643"> • The  while  statement loops  while  its conditional expression is true.</p>
        <p id="p_01644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01644"> • The  until  statement loops  until  its conditional expression is true (that is, it loops as long as its conditional expression is  false).</p>
        <p id="p_01645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01645"> Listing  2.7  contains an example of the  until  statement.</p>
        <p id="p_01646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01646"> Type</p>
        <p id="p_01647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01647"> Listing 2.7. A program that uses the until statement.</p>
        <p id="p_01648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01648"> Output</p>
        <p id="p_01649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01649"> $ program2_7</p>
        <p id="p_01650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01650"> What is 17 plus 26?</p>
        <p id="p_01651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01651"> 39</p>
        <p id="p_01652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01652"> Wrong! Keep trying!</p>
        <p id="p_01653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01653"> 43</p>
        <p id="p_01654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01654"> You've got it!</p>
        <p id="p_01655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01655"> $</p>
        <p id="p_01656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01656"> Analysis</p>
        <p id="p_01657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01657"> Lines 3 and 4 set up the loop. Line 3 prints the following question on the screen</p>
        <p id="p_01658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01658"> What is 17 plus 26?</p>
        <p id="p_01659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01659"> Line 4 assigns the correct answer,  4 3,  to  $correct_answer.</p>
        <p id="p_01660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01660"> Lines 5 and 6 retrieve the first attempt at the answer. Line 5 reads a line of input and stores it in $input_answer.  Line  6  chops off the newline character.</p>
        <p id="p_01661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01661"> Line 7 tests whether the answer entered is correct by comparing  $input_answer  with $correct_answer.  If the two are not equal, the Perl interpreter continues with lines 8-10; if they are equal, the interpreter skips to line 12.</p>
        <p id="p_01662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01662"> Line 8 prints the following on the screen:</p>
        <p id="p_01663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01663"> Wrong! Keep trying!</p>
        <p id="p_01664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01664"> Line 9 reads another attempt from the standard input file and stores it in  $input_answer.</p>
        <p id="p_01665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01665"> Line 10 chops off the newline character. At this point, the Perl interpreter jumps back to line 7 and tests the new attempt.</p>
        <p id="p_01666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01666"> The interpreter reaches line 12 when the answer is correct. At this point, the following message appears on the screen, and the program terminates:</p>
        <p id="p_01667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01667"> You've got it!</p>
        <p id="p_01668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01668"> The syntax for the  until  statement is</p>
        <p id="p_01669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01669"> until  ( expr)  {</p>
        <p id="p_01670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01670"> statement block</p>
        <p id="p_01671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01671"> }</p>
        <p id="p_01672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01672"> As in the  while  statement,  expr  is a conditional expression, and  statement_block  is a statement block.</p>
        <p id="p_01673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01673"> Summary</p>
        <p id="p_01674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01674"> Today, you learned about scalar variables and how to assign values to them.</p>
        <p id="p_01675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01675"> Scalar variables and values can be used by the arithmetic operators to perform the basic arithmetic operations of addition, subtraction, multiplication, and division. The  chop  library function removes the trailing newline character from a line, which enables you to read scalar values from the standard input file.</p>
        <p id="p_01676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01676"> A collection of operations and their values is known as an expression. The values operated on by a particular operator are called the operands of the operator. Each operator yields a result, which then can be used in other operations.</p>
        <p id="p_01677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01677"> An expression can be divided into subexpressions, each of which is evaluated in turn.</p>
        <p id="p_01678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01678"> Today you were introduced to the idea of a conditional statement. A conditional statement consists of two components: a conditional expression, which yields a result of either true or false; and a statement block, which is a group of statements that is executed only when the conditional expression is true.</p>
        <p id="p_01679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01679"> Some conditional expressions contain the == operator, which returns true if its operands are numerically equal, and returns false if its operands are not.</p>
        <p id="p_01680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01680"> The following conditional statements were described today:</p>
        <p id="p_01681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01681"> • The  i f  statement, which is executed only if its conditional expression is true</p>
        <p id="p_01682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01682"> • The  if-else  statement, which chooses between two alternatives</p>
        <p id="p_01683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01683"> • The  if-elsif-else  statement, which chooses between multiple alternatives</p>
        <p id="p_01684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01684"> • The  while  statement, which loops while a condition is true</p>
        <p id="p_01685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01685"> The  until  statement, which loops until a condition is true</p>
        <p id="p_01686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01686"> You also learned about nesting conditional statements, as well as about infinite loops and how to</p>
        <p id="p_01687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01687"> avoid them.</p>
        <p id="p_01688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01688"> Q&amp;A</p>
        <p id="p_01689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01689"> Q: Which should I use, the while statement or the until statement?</p>
        <p id="p_01690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01690"> A: It doesn't matter, really; it just depends on which, in your judgment, is easier to read.</p>
        <p id="p_01691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01691"> Once you learn about the other comparison operators on  Day  4 ,  "More Operators," you'll be able to use the  while  statement wherever you can use an  until  statement, and vice versa.</p>
        <p id="p_01692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01692"> Q: In Listing 2.7, you read input from the standard input file in two separate places. Is</p>
        <p id="p_01693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01693"> there any way I can reduce this to one?</p>
        <p id="p_01694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01694"> A: Yes, by using the  do  statement, which you'll encounter on  Day  8 ,  "More Control Structures."</p>
        <p id="p_01695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01695"> Q: Do I really need both a $done variable and a $ count variable in Listing 2.6?</p>
        <p id="p_01696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01696"> A: No. On  Day 4   you'll learn about comparison operators, which enable you to test whether a</p>
        <p id="p_01697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01697"> variable is less than or greater than a particular value. At that point, you won't need the $done  variable.</p>
        <p id="p_01698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01698"> Q: How many elsif parts can I have in an if-elsif-else statement?</p>
        <p id="p_01699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01699"> A: Effectively, as many as you like. (There is an upper limit, but it's so large that you are not</p>
        <p id="p_01700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01700"> likely ever to reach it.)</p>
        <p id="p_01701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01701"> Q: How much nesting of conditional statements does Perl allow? Can I put an if inside a</p>
        <p id="p_01702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01702"> while that is inside an if that is inside an until?</p>
        <p id="p_01703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01703"> A: Yes. You can nest as many levels deep as you like. Generally, though, you don't want to go</p>
        <p id="p_01704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01704"> too many levels down because your program will become difficult to read.</p>
        <p id="p_01705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01705"> The logical operators, which you'll learn about on  Day  4 ,  make it possible to produce more complicated conditional expressions. They'll eliminate the need for too much nesting.</p>
        <p id="p_01706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01706"> Workshop</p>
        <p id="p_01707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01707"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_01708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01708"> Quiz</p>
        <p id="p_01709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01709"> 1. Define the following terms:</p>
        <p id="p_01710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01710"> a. expression</p>
        <p id="p_01711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01711"> b. operand</p>
        <p id="p_01712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01712"> c. conditional statement</p>
        <p id="p_01713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01713"> d. statement block</p>
        <p id="p_01714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01714"> e. infinite loop</p>
        <p id="p_01715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01715"> 2. When does a while statement stop looping?</p>
        <p id="p_01716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01716"> 3. When does an until statement stop looping?</p>
        <p id="p_01717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01717"> 4. What does the == operator do?</p>
        <p id="p_01718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01718"> 5. What is the result when the following expression is evaluated?</p>
        <p id="p_01719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01719"> 14 +6*3- 10/2</p>
        <p id="p_01720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01720"> 6. Which of the following are legal scalar variable names?</p>
        <p id="p_01721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01721"> a.  $hello</p>
        <p id="p_01722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01722"> b.  $_test</p>
        <p id="p_01723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01723"> c. $now_is_the_time_to_come_to_the_aid_of_the_party</p>
        <p id="p_01724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01724"> d.  $fries&amp;gravy</p>
        <p id="p_01725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01725"> e.  $96tears</p>
        <p id="p_01726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01726"> f.  $tea_for_2</p>
        <p id="p_01727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01727"> Exercises</p>
        <p id="p_01728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01728"> 1. Write a Perl program that reads in a number, multiplies it by 2, and prints the result.</p>
        <p id="p_01729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01729"> 2. Write a Perl program that reads in two numbers and does the following:</p>
        <p id="p_01730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01730"> o It prints  Error  :  can't divide by zero  if the second number is 0. o If the first number is 0 or the second number is 1, it just prints the first number (because no division is necessary).</p>
        <p id="p_01731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01731"> o In all other cases, it divides the first number by the second number and prints the result.</p>
        <p id="p_01732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01732"> 3. Write a Perl program that uses the while statement to print out the first 10 numbers (1-10) in ascending order.</p>
        <p id="p_01733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01733"> 4. Write a Perl program that uses the until statement to print out the first 10 numbers in descending order (10-1).</p>
        <p id="p_01734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01734"> 5.  BUG BUSTER:  What is wrong with the following program? (Hint: there might  be more than one bug! )</p>
        <p id="p_01735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01735"> #!/usr/local/bin/perl $value = &lt;STDIN&gt;; if ($value = 17) {</p>
        <p id="p_01736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01736"> print ("You typed the number 17.\n"); else {</p>
        <p id="p_01737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01737"> print ("You did not type the number 17.\n");</p>
        <p id="p_01738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01738"> 6.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_01739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01739"> #!/usr/local/bin/perl</p>
        <p id="p_01740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01740"> # program which prints the next five numbers after the</p>
        <p id="p_01741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01741"> # number typed in $input = &lt;STDIN&gt;; chop ( $input);</p>
        <p id="p_01742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01742"> $input = $input +1; # start with the next number;</p>
        <p id="p_01743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01743"> $input = $terminate + 5; # we want to loop five times until ($input == $terminate) {</p>
        <p id="p_01744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01744"> print ("The next number is ", $terminate,  "\n");</p>
        <p id="p_01745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01745"> Chapter 3</p>
        <p id="p_01746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01746"> Understanding Scalar Values</p>
        <p id="p_01747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01747"> CONTENTS</p>
        <p id="p_01748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01748"> •  What Is a Scalar Value?</p>
        <p id="p_01749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01749"> •  Integer Scalar Values</p>
        <p id="p_01750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01750"> o  Integer Scalar Value Limitations</p>
        <p id="p_01751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01751"> •  Floating-Point Scalar Values</p>
        <p id="p_01752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01752"> o  Floating-Point Arithmetic and Round-Off Error</p>
        <p id="p_01753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01753"> •  Using Octal and Hexadecimal Notation</p>
        <p id="p_01754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01754"> o  Decimal Notation o  Octal Notation o  Hexadecimal Notation o  Why Bother?</p>
        <p id="p_01755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01755"> •  Character Strings</p>
        <p id="p_01756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01756"> o  Using Double-Quoted Strings</p>
        <p id="p_01757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01757"> o  Escape Sequences o  Single-Quoted Strings</p>
        <p id="p_01758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01758"> •  Interchangeability of Strings and Numeric Values</p>
        <p id="p_01759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01759"> o  Initial Values of Scalar Variables</p>
        <p id="p_01760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01760"> •  Summary</p>
        <p id="p_01761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01761"> •  Q&amp;A</p>
        <p id="p_01762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01762"> •  Workshop</p>
        <p id="p_01763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01763"> o  Quiz o  Exercises</p>
        <p id="p_01764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01764"> Today's lesson describes everything you need to know about scalar values in Perl. Today, you learn about the following:</p>
        <p id="p_01765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01765"> • Scalar values</p>
        <p id="p_01766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01766"> • How integers are represented</p>
        <p id="p_01767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01767"> • Floating-point values</p>
        <p id="p_01768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01768"> • The octal and hexadecimal notations</p>
        <p id="p_01769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01769"> • Character strings, and using the double-quote and single-quote characters to enclose them</p>
        <p id="p_01770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01770"> • Escape sequences</p>
        <p id="p_01771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01771"> • The interchangeability of character strings and numeric values</p>
        <p id="p_01772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01772"> What Is a Scalar Value?</p>
        <p id="p_01773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01773"> Basically, a  scalar value  is one unit of data. This unit of data can be either a number or a chunk of text.</p>
        <p id="p_01774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01774"> There are several types of scalar values that Perl understands. Today's lesson describes each of them in turn and shows you how you can use them.</p>
        <p id="p_01775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01775"> Integer Scalar Values</p>
        <p id="p_01776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01776"> The most common scalar values in Perl programs are integer scalar values, also known as  integer constants  or  integer literals.</p>
        <p id="p_01777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01777"> An integer scalar value consists of one or more digits, optionally preceded by a plus or minus sign and optionally containing underscores.</p>
        <p id="p_01778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01778"> Here are a few examples:</p>
        <p id="p_01779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01779"> 14</p>
        <p id="p_01780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01780"> 10000000000</p>
        <p id="p_01781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01781"> -27</p>
        <p id="p_01782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01782"> 1   000   000</p>
        <p id="p_01783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01783"> You can use integer scalar values in expressions or assign them to scalar variables, as follows:</p>
        <p id="p_01784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01784"> $ x = 12345;</p>
        <p id="p_01785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01785"> if (1217 + 116 == 1333) {</p>
        <p id="p_01786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01786"> # statement block goes here</p>
        <p id="p_01787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01787"> }</p>
        <p id="p_01788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01788"> Integer Scalar Value Limitations</p>
        <p id="p_01789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01789"> In Perl, there is a limit on the size of integers included in a program. To see what this limit is and how</p>
        <p id="p_01790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01790"> it works, take a look at Listing 3.1, which prints out integers of various sizes.</p>
        <p id="p_01791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01791"> Type</p>
        <p id="p_01792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01792"> Listing 3.1. A program that displays integers and illustrates their size limitations.</p>
        <p id="p_01793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01793"> $ program3_l</p>
        <p id="p_01794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01794"> first value is 1234567890 second value is 1234567890123456 third value is 12345678901234567168 $</p>
        <p id="p_01795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01795"> Analysis</p>
        <p id="p_01796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01796"> This program assigns integer scalar values to the variable $  value,  and then prints</p>
        <p id="p_01797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01797"> $value</p>
        <p id="p_01798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01798"> Lines 3 and 4 store and print the value  1234567890  without any difficulty. Similarly, lines 5 and 6 successfully store and print the value  1234567890123456.</p>
        <p id="p_01799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01799"> Line 7 attempts to assign the value  12345678901234567890  to  $value.  Unfortunately, this number is too big for Perl to understand. When line 8 prints out the value assigned to $  value,  it prints out</p>
        <p id="p_01800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01800"> 12345678901234567168</p>
        <p id="p_01801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01801"> As you can see, the last three digits have been replaced with different values.</p>
        <p id="p_01802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01802"> Here's what has happened: Perl actually stores integers in the floating-point registers on your machine. In other words, integers are treated as if they are floating-point numbers (numbers containing decimal points).</p>
        <p id="p_01803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01803"> On most machines, floating-point registers can store approximately 16 digits before running out of space. As the output from line  8  shows, the first 17 digits of the number  12345678901234567890 are remembered and stored by the Perl interpreter, and the rest are thrown away. This means that the value printed by line 8 is not the same as the value assigned in line 7.</p>
        <p id="p_01804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01804"> This somewhat annoying limitation on the number of digits in an integer can be found in almost all programming languages. In fact, many programming languages have an upper integer limit of 4294967295  (which is equal to 232 minus 1).</p>
        <p id="p_01805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01805"> The number of digits that can be stored varies from machine to machine. For a more detailed explanation, refer to the discussion of precision in the following section, "Floating-Point Scalar Values."</p>
        <p id="p_01806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01806"> Warning</p>
        <p id="p_01807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01807"> An integer constant that starts with a 0 is a special case:</p>
        <p id="p_01808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01808"> $ x = 012345;</p>
        <p id="p_01809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01809"> The  0  at the beginning of the constant (also known as a  leading zero)  tells the Perl interpreter to treat this as an  octal integer constant.  To find out about octal integer constants, refer to the section called "Using Octal and Hexadecimal Notation" later today</p>
        <p id="p_01810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01810"> Floating-Point Scalar Values</p>
        <p id="p_01811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01811"> As you have just seen, integers in Perl actually are represented as floating-point numbers. This means that an integer scalar value is actually a special kind of floating-point scalar value.</p>
        <p id="p_01812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01812"> In Perl, a floating-point scalar value consists of all of the following:</p>
        <p id="p_01813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01813"> • An optional minus sign (-)</p>
        <p id="p_01814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01814"> • A sequence of digits, optionally containing a decimal point</p>
        <p id="p_01815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01815"> • An optional exponent</p>
        <p id="p_01816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01816"> Here are some simple examples of floating-point scalar values:</p>
        <p id="p_01817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01817"> 11.4 -275 -0 . 3</p>
        <p id="p_01818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01818"> .3</p>
        <p id="p_01819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01819"> 3.</p>
        <p id="p_01820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01820"> The optional exponent tells the Perl interpreter to multiply or divide the scalar value by a power of ten. An exponent consists of all of the following:</p>
        <p id="p_01821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01821"> • The letter e (E is also acceptable)</p>
        <p id="p_01822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01822"> • An optional + or -</p>
        <p id="p_01823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01823"> • A one-, two-, or three-digit number</p>
        <p id="p_01824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01824"> The number in the exponent represents the value by which to multiply or divide, represented as a power of 10. For example, the exponent  e+01  tells the Perl interpreter to multiply the scalar value by 10 to the power of 1, or 10. This means that the scalar value  8 e+01  is equivalent to 8 multiplied by 10, or 80.</p>
        <p id="p_01825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01825"> Similarly, the exponent  e+0 2  is equivalent to multiplying by 100,  e+0 3  is equivalent to multiplying by 1,000, and so on. The following scalar values are all equal:</p>
        <p id="p_01826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01826"> 54le+01 54.le+02</p>
        <p id="p_01827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01827"> 5.41e+03</p>
        <p id="p_01828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01828"> A negative exponent tells the Perl interpreter to divide by 10. For example, the value  54e-01  is equivalent to 54 divided by 10, or 5.4. Similarly,  e-02  tells the Perl interpreter to divide by 100,  e-0 3  to divide by 1,000, and so on.</p>
        <p id="p_01829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01829"> The exponent  e+0 0  is equivalent to multiplying by 1, which does nothing. Therefore, the following values are equal:</p>
        <p id="p_01830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01830"> 5.12e + 0 0 5.12</p>
        <p id="p_01831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01831"> If you want, you can omit the + when you multiply by a power of ten.</p>
        <p id="p_01832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01832"> 5.4 7e + 0 3 5.47e03</p>
        <p id="p_01833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01833"> Listing 3.2 shows how Perl works with and prints out floating-point scalar values.</p>
        <p id="p_01834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01834"> Type</p>
        <p id="p_01835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01835"> Listing 3.2. A program that displays various floating-point scalar values.</p>
        <p id="p_01836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01836"> 9: $value = 123456789000000000000000000000;</p>
        <p id="p_01837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01837"> 10: print ("fourth value is ", $value,  "\n"); 11: $value = 1.23e+999;</p>
        <p id="p_01838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01838"> 12: print ("fifth value is ", $value,  "\n"); 13: $value = 1.23e-999;</p>
        <p id="p_01839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01839"> 14: print ("sixth value is ", $value,  "\n");</p>
        <p id="p_01840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01840"> Output</p>
        <p id="p_01841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01841"> $ program3_2 first value is 34</p>
        <p id="p_01842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01842"> second value is 11.460000000000001 third value is 1.7826300000000001e+21 fourth value is 1.2345678899999999e+29 fifth value is Infinity sixth value is 0 $</p>
        <p id="p_01843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01843"> Analysis</p>
        <p id="p_01844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01844"> As in Listing 3.1, this program stores and prints various scalar values. Line 3 assigns the floating-point value  34.0  to $ value. Line 4 then prints this value. Note that because there are no significant digits after the decimal point, the Perl interpreter treats  34.0  as if it is an integer</p>
        <p id="p_01845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01845"> Line 5 assigns 114.6e-01 to $value, and line 6 prints this value. Whenever possible, the Perl interpreter removes any exponents, shifting the decimal point appropriately. As a result, line 6 prints out</p>
        <p id="p_01846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01846"> 11.460000000000001</p>
        <p id="p_01847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01847"> which is 114.6e-01 with the exponent e-01 removed and the decimal point shifted one place to the left (which is equivalent to dividing by 10).</p>
        <p id="p_01848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01848"> Note that the number printed by line 6 is not exactly equal to the value assigned in line 5. This is a result of  round-off error.  The floating-point register cannot contain the exact value 11.46, so it comes as close as it can. It comes pretty close-in fact, the first 16 digits are correct. This number of correct digits is known as the  precision , and it is a property of the machine on which you are working; the precision of a floating-point number varies from machine to machine. (The machine on which I ran these test examples supports a floating-point precision of 16 or 17 digits. This is about normal.)</p>
        <p id="p_01849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01849"> NOTE</p>
        <p id="p_01850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01850"> The size of an integer is roughly equivalent to the supported floating-point precision. If a machine supports a floating-point precision of 16 digits, an integer can be approximately 16 digits long.</p>
        <p id="p_01851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01851"> Line 6 shows that a floating-point value has its exponent removed whenever possible. Lines 7 and 8 show what happens when a number is too large to be conveniently displayed without the exponent. In this case, the number is displayed in scientific notation.</p>
        <p id="p_01852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01852"> In  scientific notation , one digit appears before the decimal point, and all the other significant digits (the rest of the machine's precision) follow the decimal point. The exponent is adjusted to reflect this. In this example, the number</p>
        <p id="p_01853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01853"> 178.263e+19</p>
        <p id="p_01854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01854"> is converted into scientific notation and becomes</p>
        <p id="p_01855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01855"> 1.7826300000000001e+21</p>
        <p id="p_01856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01856"> As you can see, the decimal point has been shifted two places to the left, and the exponent has, as a consequence, been adjusted from 19 to 21. As before, the 1 at the end is an example of round¬ off error.</p>
        <p id="p_01857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01857"> If an integer is too large to be displayed conveniently, the Perl interpreter converts it to scientific notation. Lines 9 and 10 show this. The number</p>
        <p id="p_01858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01858"> 123456789000000000000000000000</p>
        <p id="p_01859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01859"> is  converted to</p>
        <p id="p_01860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01860"> 1.2345678899999999e+29</p>
        <p id="p_01861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01861"> Here, scientific notation becomes useful. At a glance, you can tell approximately how large the number is. (In conventional notation, you can't do this without counting the zeros.)</p>
        <p id="p_01862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01862"> Lines 11 and 12 show what happens when the Perl interpreter is given a number that is too large to fit into the machine's floating-point register. In this case, Perl just prints the word Infinity.</p>
        <p id="p_01863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01863"> The maximum size of a floating-point number varies from machine to machine. Generally, the largest possible exponent that can be stored is about  e+308.</p>
        <p id="p_01864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01864"> Lines 13 and 14 illustrate the case of a number having a negative exponent that is too large (that is, it's too small to store). In such cases, Perl either gets as close as it can or just prints 0.</p>
        <p id="p_01865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01865"> The largest negative exponent that produces reliable values is about  e-309.  Below that, accuracy diminishes.</p>
        <p id="p_01866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01866"> Floating-Point Arithmetic and Round-Off Error</p>
        <p id="p_01867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01867"> The arithmetic operations you saw on  Day  2 ,  "Basic Operators and Control Flow," also work on floating-point values. On that day, you saw an example of a miles-to-kilometers conversion program that uses floating-point arithmetic.</p>
        <p id="p_01868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01868"> When you perform floating-point arithmetic, you must remember the problems with precision and round-off error. Listing 3.3 illustrates what can go wrong and shows you how to attack this problem.</p>
        <p id="p_01869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01869"> :  Type</p>
        <p id="p_01870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01870"> Listing 3.3. A program that illustrates round-off error problems in floating-point arithmetic.</p>
        <p id="p_01871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01871"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01872"> 2 :</p>
        <p id="p_01873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01873"> 3: $value = 9.01e+21 + 0.01 - 9.01e+21;</p>
        <p id="p_01874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01874"> 4: print ("first value is ", $value,  "\n");</p>
        <p id="p_01875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01875"> 5:  $value = 9.01e+21 - 9.01e+21 + 0.01;</p>
        <p id="p_01876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01876"> 6: print ("second value is ", $value,  "\n");</p>
        <p id="p_01877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01877"> Output</p>
        <p id="p_01878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01878"> $ program3_3 first value is 0 second value is  0.01 $</p>
        <p id="p_01879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01879"> Analysis</p>
        <p id="p_01880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01880"> Line 3 and line 5 both subtract 9.01e+21 from itself and add 0.01. However, as you can see when you examine the output produced by line 4 and line 6, the order in which you perform the addition and subtraction has a significant effect</p>
        <p id="p_01881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01881"> In line 3, a very small number, 0.01, is added to a very large number, 9.01e+21. If you work it out yourself, you see that the result is 9.01000000000000000000001e+21.</p>
        <p id="p_01882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01882"> The final 1 in the preceding number can be retained only on machines that support 24 digits of precision in their floating-point numbers. Most machines, as you've seen, handle only 16 or 17 digits. As a result, the final 1, along with some of the zeros, is lost, and the number instead is stored as 9.0100000000000000e+21.</p>
        <p id="p_01883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01883"> This is the same as 9.01e+21, which means that subtracting 9.01e+21 yields zero. The  0.01 is lost along the way.</p>
        <p id="p_01884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01884"> Line 5, however, doesn't have this problem. The two large numbers are operated on first, yielding 0, and then  0.01  is added. The result is what you expect:  0.01.</p>
        <p id="p_01885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01885"> The moral of the story: Floating-point arithmetic is accurate only when you bunch together operations on large numbers. If the arithmetic operations are on values stored in variables, it might not be as easy to spot this problem.</p>
        <p id="p_01886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01886"> $result = $numberl + $number2 - $number3;</p>
        <p id="p_01887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01887"> If $numberl and $number3 contain large numbers and $number2 is small, $result is likely to contain an incorrect value because of the problem demonstrated in Listing 3.3.</p>
        <p id="p_01888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01888"> Using Octal and Hexadecimal Notation</p>
        <p id="p_01889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01889"> So far, all the integer scalar values you've seen have been in what normally is called  base 10  or decimal notation.  Perl also enables you to use two other notations to represent integer scalar values:</p>
        <p id="p_01890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01890"> • Base 8 notation, or  octal</p>
        <p id="p_01891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01891"> •  Base 16 notation, or  hexadecimal  (sometimes shortened to  hex)</p>
        <p id="p_01892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01892"> To use octal notation, put a zero in front of your integer scalar value:</p>
        <p id="p_01893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01893"> $result = 047;</p>
        <p id="p_01894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01894"> This assigns 47 octal, or 39 decimal, to $result.</p>
        <p id="p_01895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01895"> To use hexadecimal notation, put Ox in front of your integer scalar value, as follows:</p>
        <p id="p_01896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01896"> $result = Oxlf;</p>
        <p id="p_01897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01897"> This assigns If hexadecimal, or 31 decimal, to $result.</p>
        <p id="p_01898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01898"> Perl accepts either uppercase letters or lowercase letters as representations of the digits a through f:</p>
        <p id="p_01899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01899"> $result = Oxe;</p>
        <p id="p_01900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01900"> $result = OxE;</p>
        <p id="p_01901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01901"> Both of the preceding statements assign 14 (decimal) to $result.</p>
        <p id="p_01902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01902"> If you are not familiar with octal and hexadecimal notations and would like to learn more, read the following sections. These sections explain how to convert numbers to different bases. If you are familiar with this concept, you can skip to the section called "Character Strings."</p>
        <p id="p_01903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01903"> Decimal Notation</p>
        <p id="p_01904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01904"> To understand how the octal and hexadecimal notations work, take a closer look at what the standard decimal notation actually represents.</p>
        <p id="p_01905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01905"> In decimal notation, each digit in a number has one of 10 values: the standard numbers 0 through 9. Each digit in a number in decimal notation corresponds to a power of 10. Mathematically, the value of a digit  x  in a number is</p>
        <p id="p_01906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01906"> x * 10 to the exponent n,</p>
        <p id="p_01907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01907"> where n is the number of digits you have to skip before reaching x.</p>
        <p id="p_01908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01908"> This might sound complicated, but it's really straightforward. For example, the number 243 can be expressed as follows:</p>
        <p id="p_01909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01909"> • 2 * 10 to the exponent 2 (which is 200), plus</p>
        <p id="p_01910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01910"> • 4 * 10 to the exponent 1 (which is 40), plus</p>
        <p id="p_01911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01911"> • 3 * 10 to the exponent 0 (which is 3 * 1, which is 3)</p>
        <p id="p_01912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01912"> Adding the three numbers together yields 243.</p>
        <p id="p_01913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01913"> Octal Notation</p>
        <p id="p_01914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01914"> Working through these steps might seem like a waste of time when you are dealing with decimal notation. However, once you understand this method, reading numbers in other notations becomes simple.</p>
        <p id="p_01915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01915"> For example, in octal notation, each digit  x  in a number is</p>
        <p id="p_01916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01916"> x * 8 to the exponent n</p>
        <p id="p_01917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01917"> where x is the value of the digit, and n is the number of digits to skip before reaching x. This is the same formula as in decimal notation, but with the 10 replaced by 8.</p>
        <p id="p_01918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01918"> Using this method, here's how to determine the decimal equivalent of 243 octal:</p>
        <p id="p_01919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01919"> • 2 * 8 to the exponent 2, which is 2 * 64, or 128, plus</p>
        <p id="p_01920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01920"> • 4 * 8 to the exponent 1, which is 4 * 8, or 32, plus</p>
        <p id="p_01921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01921"> • 3 * 8 to the exponent 0, which is 3 * 1, or 3</p>
        <p id="p_01922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01922"> Adding 128, 32 and 3 yields 163, which is the decimal notation equivalent of 243 octal.</p>
        <p id="p_01923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01923"> Hexadecimal Notation</p>
        <p id="p_01924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01924"> Hexadecimal notation works the same way, but with 16 as the base instead of 10 or 8. For example, here's how to convert 243 hexadecimal to decimal notation:</p>
        <p id="p_01925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01925"> • 2 * 16 to the exponent 2, which is 2 * 256, or 512, plus</p>
        <p id="p_01926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01926"> • 4 * 16 to the exponent 1, which is 4 * 16, or 64, plus</p>
        <p id="p_01927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01927"> • 3 * 16 to the exponent 0, which is 3 * 1, or 3</p>
        <p id="p_01928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01928"> Adding these three numbers together yields 579.</p>
        <p id="p_01929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01929"> Note that the letters a through f represent the numbers 10 through 15, respectively. For example, here's the hexadecimal number fe in decimal notation:</p>
        <p id="p_01930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01930"> • 15 * 16 to the exponent 1, which is 15 * 16, or 240, plus</p>
        <p id="p_01931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01931"> • 14 * 16 to the exponent 0, which is 14 * 1, or 14</p>
        <p id="p_01932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01932"> Adding 240 and 14 yields 254, which is the decimal equivalent of fe.</p>
        <p id="p_01933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01933"> Why Bother?</p>
        <p id="p_01934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01934"> You might be wondering why Perl bothers supporting octal and hexadecimal notation. Here's the answer: Computers store numbers in memory in binary (base 2) notation, not decimal (base 10) notation. Because 8 and 16 are multiples of 2, it is easier to represent stored computer memory in base 8 or base 16 than in base 10. (You could use base 2, of course; however, base 2 numbers are clumsy because they are very long.)</p>
        <p id="p_01935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01935"> NOTE</p>
        <p id="p_01936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01936"> Perl supports base-2 operations on integer scalar values. These operations, called bit-manipulation operations, are discussed on Day  4 ,  "More Operators.</p>
        <p id="p_01937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01937"> Character Strings</p>
        <p id="p_01938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01938"> On previous days, you've seen that Perl enables you to assign text to scalar variables. In the following statement, for instance</p>
        <p id="p_01939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01939"> $var = "This is some text";</p>
        <p id="p_01940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01940"> the text  This is some text  is an example of what is called a  character string  (frequently shortened to just  string).  A character string is a sequence of one or more letters, digits, spaces, or special characters.</p>
        <p id="p_01941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01941"> The following subsections show you</p>
        <p id="p_01942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01942"> • How you can substitute for scalar variables in character strings</p>
        <p id="p_01943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01943"> • How to add escape sequences to your character strings</p>
        <p id="p_01944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01944"> • How to tell the Perl interpreter not to substitute for scalar variables</p>
        <p id="p_01945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01945"> NOTE</p>
        <p id="p_01946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01946"> C programmers should be advised that character strings in Perl do not contain a hidden null character at the end of the string. In Perl, null characters can appear anywhere in a string. (See the discussion of escape sequences later today for more details.</p>
        <p id="p_01947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01947"> Using Double-Quoted Strings</p>
        <p id="p_01948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01948"> Perl supports  scalar variable substitution  in character strings enclosed by double quotation-mark characters. For example, consider the following assignments:</p>
        <p id="p_01949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01949"> $number = 11;</p>
        <p id="p_01950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01950"> $text = "This text contains the number $number.";</p>
        <p id="p_01951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01951"> When the Perl interpreter sees $ number inside the string in the second statement, it replaces $number with its current value. This means that the string assigned to $text is actually</p>
        <p id="p_01952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01952"> This text contains the number 11.</p>
        <p id="p_01953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01953"> The most immediate practical application of this is in the print statement. So far, many of the</p>
        <p id="p_01954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01954"> print statements you have seen contain several arguments, as in the following:</p>
        <p id="p_01955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01955"> print ("The final result is ", $result, "\n");</p>
        <p id="p_01956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01956"> Because Perl supports scalar variable substitution, you can combine the three arguments to print into a single argument, as in the following:</p>
        <p id="p_01957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01957"> print ("The final result is $result\n");</p>
        <p id="p_01958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01958"> NOTE</p>
        <p id="p_01959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01959"> From now on, examples and listings that call  print  use scalar variable substitution because it is easier to read</p>
        <p id="p_01960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01960"> Escape Sequences</p>
        <p id="p_01961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01961"> Character strings that are enclosed in double quotes accept  escape sequences  for special characters. These escape sequences consist of a backslash (\) followed by one or more characters. The most common escape sequence is \n, which represents the newline character shown in this example:</p>
        <p id="p_01962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01962"> $text = "This is a string terminated by a newline\n";</p>
        <p id="p_01963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01963"> Table 3.1 lists the escape sequences recognized in double-quoted strings.</p>
        <p id="p_01964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01964"> Table 3.1. Escape sequences in strings.</p>
        <p id="p_01965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01965"> The \Q escape sequence is useful only when the string is used as a pattern. Patterns are described on  Day  7 ,  "Pattern Matching."</p>
        <p id="p_01966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01966"> The escape sequences  \L,  \U, and \Q can be turned off by  \E,  as follows:</p>
        <p id="p_01967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01967"> $a = "T\LHIS IS A \ESTRING"; # same as "This is a STRING"</p>
        <p id="p_01968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01968"> To include a backslash or double quote in a double-quoted string, precede the backslash or quote with another backslash:</p>
        <p id="p_01969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01969"> $result = "A quote \" in a string";</p>
        <p id="p_01970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01970"> $result = "A backslash \\ in a string";</p>
        <p id="p_01971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01971"> A backslash also enables you to include a $ character in a string. For example, the statements</p>
        <p id="p_01972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01972"> $result = 14;</p>
        <p id="p_01973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01973"> print("The value of \$result is $result.\n");</p>
        <p id="p_01974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01974"> print the following on your screen:</p>
        <p id="p_01975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01975"> The value of $result is 14.</p>
        <p id="p_01976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01976"> You can specify the ASCII value for a character in base 8 or octal notation using \nnn, where each n is an octal digit; for example:</p>
        <p id="p_01977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01977"> $result = "\377"; # this is the character 255, or EOF</p>
        <p id="p_01978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01978"> You can also use hexadecimal notation to specify the ASCII value for a character. To do this, use the sequence \xnn, where each n is a hexadecimal digit.</p>
        <p id="p_01979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01979"> $result =  "\xff";  # this is also 255</p>
        <p id="p_01980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01980"> Listing 3.4 is an example of a program that uses escape sequences. This program takes a line of input and converts it to a variety of cases.</p>
        <p id="p_01981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01981"> Type</p>
        <p id="p_01982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01982"> Listing 3.4. A case-conversion program.</p>
        <p id="p_01983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01983"> 1: #!/usr/local/bin/perl</p>
        <p id="p_01984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01984"> 2 :</p>
        <p id="p_01985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01985"> 3: print ("Enter a line of input:\n");</p>
        <p id="p_01986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01986"> 4: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_01987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01987"> 5: print ("uppercase: \U$inputline\E\n");</p>
        <p id="p_01988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01988"> 6: print ("lowercase: \L$inputline\E\n");</p>
        <p id="p_01989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01989"> 7: print ("as a sentence: \L\u$inputline\E\n");</p>
        <p id="p_01990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01990"> Output</p>
        <p id="p_01991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01991"> $ program3_4</p>
        <p id="p_01992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01992"> Enter a line of input: tHis Is My INpUT LiNE. uppercase: THIS IS MY INPUT LINE, lowercase: this is my input line. as a sentence: This is my input line. $</p>
        <p id="p_01993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01993"> Analysis</p>
        <p id="p_01994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01994"> Line 3 of this program reads a line of input and stores it in the scalar variable $inputline</p>
        <p id="p_01995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01995"> Line 5 replaces the string $ input line with the current value of the scalar variable $ input line. The escape character \U tells the Perl interpreter to convert everything in the string into uppercase until it sees a \E character; as a result, line 4 writes the contents of $ input line in uppercase.</p>
        <p id="p_01996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01996"> Similarly, line 6 writes the input line in all lowercase characters by specifying the escape character  \L  in the string.</p>
        <p id="p_01997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01997"> Line 7 combines the escape characters  \L  and \u. The  \L  specifies that everything in the string is to be in lowercase; however, the \u special character temporarily overrides this and tells the Perl interpreter that the next character is to be in uppercase. When this character-the first character in the line-is printed, the  \L  escape character remains in force, and the rest of the line is printed in lowercase. The result is as if the input line is a single sentence in English. The first character is capitalized, and the remainder is in lowercase.</p>
        <p id="p_01998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01998"> Single-Quoted Strings</p>
        <p id="p_01999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_01999"> Perl also enables you to enclose strings using the ' (single quotation mark) character:</p>
        <p id="p_02000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02000"> $text = 'This is a string in single quotes';</p>
        <p id="p_02001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02001"> There are two differences between double-quoted strings and single-quoted strings. The first difference is that scalar variables are replaced by their values in double-quoted strings but not in single-quoted strings. The following is an example:</p>
        <p id="p_02002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02002"> $string = "a string";</p>
        <p id="p_02003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02003"> $text = "This is $string"; # becomes "This is a string"</p>
        <p id="p_02004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02004"> $text = 'This is $string'; # remains 'This is $string'</p>
        <p id="p_02005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02005"> The second difference is that the backslash character, \, does not have a special meaning in single-quoted strings. This means that the statement</p>
        <p id="p_02006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02006"> $text = 'This is a string.\n';</p>
        <p id="p_02007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02007"> assigns the following string to $text:</p>
        <p id="p_02008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02008"> This is a string.\n</p>
        <p id="p_02009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02009"> The \ character is special in only two instances for single-quoted strings. The first is when you want to include a single-quote character ' in a string.</p>
        <p id="p_02010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02010"> $text = 'This string contains \', a quote character';</p>
        <p id="p_02011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02011"> The preceding line of code assigns the following string to $text:</p>
        <p id="p_02012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02012"> This string contains ', a quote character</p>
        <p id="p_02013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02013"> The second instance is to escape the backslash itself.</p>
        <p id="p_02014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02014"> $text = 'This string ends with a backslash  \\';</p>
        <p id="p_02015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02015"> The preceding code line assigns the following string to $text:</p>
        <p id="p_02016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02016"> This string ends with a backslash \</p>
        <p id="p_02017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02017"> As you can see, the double backslash makes it possible for the backslash character (\) to be the last character in a string.</p>
        <p id="p_02018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02018"> Warning</p>
        <p id="p_02019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02019"> Single-quoted strings can be spread over multiple lines. The statement</p>
        <p id="p_02020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02020"> $text = 'This is two lines of text</p>
        <p id="p_02021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02021"> f . r</p>
        <p id="p_02022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02022"> is equivalent to the statement</p>
        <p id="p_02023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02023"> $text = "This is two\nlines of text\n";</p>
        <p id="p_02024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02024"> This means that if you forget the closing ' for a string, the Perl interpreter is likely to get quite confused because it won't detect an error until after it starts processing the next line</p>
        <p id="p_02025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02025"> Interchangeability of Strings and Numeric Values</p>
        <p id="p_02026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02026"> As you've seen, you can use a scalar variable to store a character string, an integer, or a floating¬ point value. In scalar variables, a value that was assigned as a string can be used as an integer whenever it makes sense to do so, and vice versa. In the following example:</p>
        <p id="p_02027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02027"> $string = "43"; $number = 28;</p>
        <p id="p_02028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02028"> $result</p>
        <p id="p_02029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02029"> $string + $number;</p>
        <p id="p_02030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02030"> the value of $ string is converted to an integer and added to the value of $number. The result of the addition, 71, is assigned to $result.</p>
        <p id="p_02031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02031"> Another instance in which strings are converted to integers is when you are reading a number from the standard input file. The following is some code similar to code you've seen before:</p>
        <p id="p_02032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02032"> $number = &lt;STDIN&gt;;</p>
        <p id="p_02033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02033"> chop ($number);</p>
        <p id="p_02034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02034"> $result = $number + 1;</p>
        <p id="p_02035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02035"> This is what is happening: When $ number is assigned a line of standard input, it really is being assigned a string. For instance, if you enter 22, $ number is assigned the string 22 \n (the \n represents the newline character). The chop function removes the \n, leaving the string 22, and this string is converted to the number 22 in the arithmetic expression.</p>
        <p id="p_02036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02036"> Warning</p>
        <p id="p_02037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02037"> If a string contains characters that are not digits, the string is converted to  0  when used in an integer context. For example:</p>
        <p id="p_02038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02038"> $result = "hello" * 5;</p>
        <p id="p_02039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02039"> # this assigns 0 to $result, since "hello" becomes 0</p>
        <p id="p_02040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02040"> This is true even if the string is a valid hexadecimal integer if the quotes are removed, as in the following:</p>
        <p id="p_02041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02041"> $result =  "Oxff"  + 1;</p>
        <p id="p_02042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02042"> In cases like this, Perl does not tell you that anything has gone wrong, and your results might not be what you expect.</p>
        <p id="p_02043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02043"> Also, strings containing misprints might not contain what you expect. For example:</p>
        <p id="p_02044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02044"> $result = "12034"; # the letter 0, not the number 0</p>
        <p id="p_02045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02045"> When converting from a string to an integer, Perl starts at the left and continues until it sees a letter that is not a digit. In the preceding instance,  12034  is converted to the integer 12, not 12034</p>
        <p id="p_02046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02046"> Initial Values of Scalar Variables</p>
        <p id="p_02047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02047"> In Perl, all scalar variables have an initial value of the null string, " ". This means that you do not need to define a value for a scalar variable.</p>
        <p id="p_02048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02048"> #!/usr/local/bin/perl</p>
        <p id="p_02049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02049"> $result = $undefined +2; # $undefined is not defined</p>
        <p id="p_02050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02050"> print ("The value of \$result is $result.\n");</p>
        <p id="p_02051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02051"> This short program is perfectly legal Perl. The output is</p>
        <p id="p_02052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02052"> The value of $result is 2.</p>
        <p id="p_02053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02053"> Because $undefined is not defined, the Perl interpreter assumes that its value is the null string. This null string is then converted to 0, because it is being used in an addition operation. The result of the addition, 2, is assigned to $result.</p>
        <p id="p_02054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02054"> TIP</p>
        <p id="p_02055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02055"> Although you can use uninitialized variables in your Perl programs, you shouldn't. If your Perl program gets to be large (as many complicated programs do), it might be difficult to determine whether a particular variable is supposed to be appearing for the first time or whether it is a spelling mistake that should be fixed. To avoid ambiguity and to make life easier for yourself, initialize every scalar variable before using it</p>
        <p id="p_02056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02056"> Summary</p>
        <p id="p_02057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02057"> Perl supports three kinds of scalar values: integers, floating-point numbers, and character strings.</p>
        <p id="p_02058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02058"> Integers can be in three notations: standard (decimal) notation, octal notation, and hexadecimal notation. Octal notation is indicated by a leading 0, and hexadecimal notation is indicated by a leading Ox. Integers are stored as floating-point values and can be as long as the machine's floating-point precision (usually 16 digits or so).</p>
        <p id="p_02059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02059"> Floating-point numbers can consist of a string of digits that contain a decimal point and an optional exponent. The exponent's range can be anywhere from about e-309 to e+308. (This value might be different on some machines.) When possible, floating-point numbers are displayed without the exponent; failing that, they are displayed in scientific notation (one digit before the decimal point).</p>
        <p id="p_02060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02060"> When you use floating-point arithmetic, be alert for round-off errors. Performing arithmetic operations in the proper order-operating on large numbers first-might yield better results.</p>
        <p id="p_02061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02061"> You can enclose character strings in either double quotes (") or single quotes  (').  If a scalar variable name appears in a character string enclosed in double quotes, the value of the variable is substituted for its name. Escape characters are recognized in strings enclosed in double quotes; these characters are indicated by a backslash (\).</p>
        <p id="p_02062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02062"> Character strings in single quotes do not support escape characters, with the exception of \\ and \ '. Scalar variable names are not replaced by their values.</p>
        <p id="p_02063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02063"> Strings and integers are freely interchangeable in Perl whenever it is logically possible to do so.</p>
        <p id="p_02064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02064"> Q&amp;A</p>
        <p id="p_02065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02065"> Q: If Perl character strings are not terminated by null characters, how does the Perl</p>
        <p id="p_02066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02066"> interpreter know the length of a string?</p>
        <p id="p_02067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02067"> A: The Perl interpreter keeps track of the length of a string as well as its contents. In Perl, you do</p>
        <p id="p_02068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02068"> not need to use a null character to indicate "end of string."</p>
        <p id="p_02069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02069"> Q: Why does Perl use floating-point registers for floating-point arithmetic even though they</p>
        <p id="p_02070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02070"> cause round-off errors?</p>
        <p id="p_02071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02071"> A: Basically, it's a performance issue. It's possible to write routines that store floating-point</p>
        <p id="p_02072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02072"> numbers as strings and convert parts of these strings to numbers as necessary; however, you often don't need more than 16 or so digits of precision anyway.</p>
        <p id="p_02073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02073"> Applications that need to do high-speed arithmetic calculations of great precision usually run on special computers designed for that purpose.</p>
        <p id="p_02074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02074"> Q: What happens if I forget to call chop when reading a number from the standard input</p>
        <p id="p_02075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02075"> file?</p>
        <p id="p_02076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02076"> A: As it happens, nothing. Perl is smart enough to ignore white space at the end of a line that</p>
        <p id="p_02077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02077"> consists only of a number. However, it's a good idea to get into the habit of using  chop  to get rid of a trailing newline at all times, because the trailing newline becomes significant when you start doing string comparisons. (You'll learn about string comparisons on  Day  4 ,  "More Operators.")</p>
        <p id="p_02078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02078"> Workshop</p>
        <p id="p_02079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02079"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_02080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02080"> Quiz</p>
        <p id="p_02081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02081"> 1. Define the following terms:</p>
        <p id="p_02082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02082"> a round-off error</p>
        <p id="p_02083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02083"> b octal notation</p>
        <p id="p_02084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02084"> c precision</p>
        <p id="p_02085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02085"> d scientific notation</p>
        <p id="p_02086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02086"> 2. Convert the following numbers from octal notation to decimal:</p>
        <p id="p_02087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02087"> a 0377</p>
        <p id="p_02088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02088"> b 06</p>
        <p id="p_02089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02089"> c 01131</p>
        <p id="p_02090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02090"> 3. Convert the following numbers from hexadecimal notation to decimal notation:</p>
        <p id="p_02091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02091"> a Oxf f</p>
        <p id="p_02092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02092"> b 0x11</p>
        <p id="p_02093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02093"> c Oxbead</p>
        <p id="p_02094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02094"> 4. What does the following line print?</p>
        <p id="p_02095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02095"> print ("I am bored\b\b\b\b\bhappy!\n");</p>
        <p id="p_02096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02096"> 5. Suppose the value of $num is 21. What string is assigned to $text in each of the following cases?</p>
        <p id="p_02097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02097"> a $text = "This string contains $num.";</p>
        <p id="p_02098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02098"> b $text = "\\$num is my favorite number.";</p>
        <p id="p_02099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02099"> c $text = 'Assign \$num to this string.';</p>
        <p id="p_02100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02100"> 6. Convert the following numbers to scientific notation:</p>
        <p id="p_02101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02101"> a 43.71</p>
        <p id="p_02102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02102"> b 0.000006e-02</p>
        <p id="p_02103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02103"> c 3</p>
        <p id="p_02104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02104"> d -1.04</p>
        <p id="p_02105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02105"> Exercises</p>
        <p id="p_02106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02106"> 1. Write a program that prints every number from 0 to 1 that has a single digit after the decimal place (that is, 0.1, 0.2, and so on).</p>
        <p id="p_02107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02107"> 2. Write a program that reads a line of input and prints out the following:</p>
        <p id="p_02108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02108"> o  1 if the line consists of a non-zero integer o  0 if the line consists of 0 or a string</p>
        <p id="p_02109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02109"> (Hint: Remember that character strings are converted to 0 when they are converted to integers.)</p>
        <p id="p_02110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02110"> 3. Write a program that asks for a number and keeps trying until you enter the number 47. At that point, it prints  Correct ! and rings a bell.</p>
        <p id="p_02111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02111"> 4. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_02112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02112"> #!/usr/local/bin/perl</p>
        <p id="p_02113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02113"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_02114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02114"> print ('here is the value of \$inputline\', ": $inputline");</p>
        <p id="p_02115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02115"> 5. BUG BUSTER: What is wrong with the following code fragment?</p>
        <p id="p_02116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02116"> $numl = 6.02e+23;</p>
        <p id="p_02117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02117"> $num2 = 11.4;</p>
        <p id="p_02118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02118"> $num3 = 5.171e+22;</p>
        <p id="p_02119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02119"> $num4 =  -2.5;</p>
        <p id="p_02120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02120"> $result = $numl + $num2 - $num3 + $num4;</p>
        <p id="p_02121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02121"> 6. BUG BUSTER: What is wrong with the following statement? $result =  "26" + "Oxce" + "1";</p>
        <p id="p_02122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02122"> Chapter 4</p>
        <p id="p_02123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02123"> More Operators</p>
        <p id="p_02124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02124"> CONTENTS</p>
        <p id="p_02125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02125"> •  Using the Arithmetic Operators</p>
        <p id="p_02126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02126"> o  Exponentiation o  The Remainder Operator</p>
        <p id="p_02127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02127"> o  Unary Negation</p>
        <p id="p_02128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02128"> •  Using Comparison Operators</p>
        <p id="p_02129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02129"> o  Integer-Comparison Operators</p>
        <p id="p_02130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02130"> o  String-Comparison Operators o  String Comparison Versus Integer Comparison</p>
        <p id="p_02131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02131"> o  Comparison and Floating-Point Numbers</p>
        <p id="p_02132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02132"> •  Using Logical Operators</p>
        <p id="p_02133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02133"> o  Evaluation Within Logical Operators</p>
        <p id="p_02134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02134"> o  Logical Operators as Subexpressions</p>
        <p id="p_02135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02135"> •  Using Bit-Manipulation Operators</p>
        <p id="p_02136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02136"> o  What Bits Are and How They Are Used</p>
        <p id="p_02137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02137"> o  The Bit-Manipulation Operators</p>
        <p id="p_02138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02138"> •  Using the Assignment Operators</p>
        <p id="p_02139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02139"> o  Assignment Operators as Subexpressions</p>
        <p id="p_02140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02140"> •  Using Autoincrement and Autodecrement</p>
        <p id="p_02141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02141"> o  The Autoincrement Operator Pre-Increment</p>
        <p id="p_02142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02142"> o  The Autoincrement Operator Post-Increment</p>
        <p id="p_02143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02143"> o  The Autodecrement Operator o  Using Autoincrement With Strings</p>
        <p id="p_02144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02144"> •  The String Concatenation and Repetition Operators</p>
        <p id="p_02145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02145"> o  The String-Concatenation Operator</p>
        <p id="p_02146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02146"> o  The String-Repetition Operator</p>
        <p id="p_02147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02147"> o  Concatenation and Assignment</p>
        <p id="p_02148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02148"> •  Other Perl Operators</p>
        <p id="p_02149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02149"> o  The Comma Operator o  The Conditional Operator</p>
        <p id="p_02150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02150"> •  The Order of Operations</p>
        <p id="p_02151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02151"> o  Precedence o  Associativity</p>
        <p id="p_02152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02152"> o  Forcing Precedence Using Parentheses</p>
        <p id="p_02153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02153"> •  Summary</p>
        <p id="p_02154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02154"> •  Q&amp;A</p>
        <p id="p_02155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02155"> •  Workshop o  Quiz o  Exercises</p>
        <p id="p_02156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02156"> On  Day 2 ,  "Basic Operators and Control Flow," you learned about the following operators:</p>
        <p id="p_02157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02157"> • The arithmetic operators +, *, and /</p>
        <p id="p_02158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02158"> • The comparison operator ==</p>
        <p id="p_02159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02159"> • The assignment operator =</p>
        <p id="p_02160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02160"> Today, you learn about the rest of the operators that Perl provides, as well as about operator associativity and precedence. The operators are</p>
        <p id="p_02161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02161"> • The arithmetic operators **, %, and - (unary negation)</p>
        <p id="p_02162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02162"> • The other integer- and string-comparison operators</p>
        <p id="p_02163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02163"> • The logical operators</p>
        <p id="p_02164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02164"> • The bit-manipulation operators</p>
        <p id="p_02165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02165"> • The assignment operators</p>
        <p id="p_02166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02166"> • Autoincrement and autodecrement</p>
        <p id="p_02167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02167"> • Concatenating and repeating strings</p>
        <p id="p_02168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02168"> • The comma and conditional operators</p>
        <p id="p_02169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02169"> Using the Arithmetic Operators</p>
        <p id="p_02170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02170"> The arithmetic operators that you have seen so far-the +, -, *, and / operators-work the way you expect them to: They perform the operations of addition, subtraction, multiplication, and division.</p>
        <p id="p_02171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02171"> Perl also supports three other arithmetic operations:</p>
        <p id="p_02172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02172"> • Exponentiation</p>
        <p id="p_02173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02173"> • The modulo or remainder operation</p>
        <p id="p_02174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02174"> • Unary negation</p>
        <p id="p_02175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02175"> Although these operators aren't as intuitively obvious as the ones you've already seen, they are quite easy to use.</p>
        <p id="p_02176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02176"> Exponentiation</p>
        <p id="p_02177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02177"> The  exponentiation operator,  **, provides a convenient way to multiply a number by itself repeatedly. For example, here is a simple Perl statement that uses the exponentiation operator:</p>
        <p id="p_02178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02178"> $x = 2 ** 4;</p>
        <p id="p_02179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02179"> The expression  2**4   means "take four copies of two and multiply them." This statement assigns 16 to the scalar variable  $x.</p>
        <p id="p_02180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02180"> Note that the following statements are equivalent, but the first statement is much more concise:</p>
        <p id="p_02181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02181"> $x  = 2  **   7;</p>
        <p id="p_02182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02182"> $x=2*2*2*2*2*2*2;</p>
        <p id="p_02183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02183"> When an exponentiation operator is employed, the  base  value (the value to the left of the * *) is the number to be repeatedly multiplied. The number to the right, called the  exponent , is the number of times the multiplication is to be performed. Here are some other simple examples of the exponentiation operator:</p>
        <p id="p_02184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02184"> The * * operator also works on the values stored in variables:</p>
        <p id="p_02185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02185"> $x = $y ** 2;</p>
        <p id="p_02186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02186"> Here, the value stored in  $y  is multiplied by itself, and the result is stored in  $x. $y  is not changed by this operation.</p>
        <p id="p_02187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02187"> $x = 2 ** $y;</p>
        <p id="p_02188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02188"> In this case, the value stored in  $y  becomes the exponent, and  $x  is assigned  2  multiplied by itself  $y times.</p>
        <p id="p_02189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02189"> You can use the exponent operator with non-integer or negative exponents:</p>
        <p id="p_02190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02190"> 2 ** -5 # this is the fraction 1/32</p>
        <p id="p_02191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02191"> 5 ** 2.5 # this is 25 * the square root of 5</p>
        <p id="p_02192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02192"> Listing 4.1 shows an example of a simple program that uses the exponential operator. It prompts for a number,  $exponent,  and prints out  2 ** $exponent.</p>
        <p id="p_02193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02193"> Type</p>
        <p id="p_02194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02194"> Listing  4.1. A  program that prints out the powers of two.</p>
        <p id="p_02195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02195"> 1: #!/usr/local/bin/perl</p>
        <p id="p_02196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02196"> 2  :</p>
        <p id="p_02197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02197"> 3: # this program asks for a number, n, and prints 2 to the</p>
        <p id="p_02198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02198"> 4: # exponent n</p>
        <p id="p_02199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02199"> 5:</p>
        <p id="p_02200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02200"> 6: print ("Enter the exponent to use:\n");</p>
        <p id="p_02201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02201"> 7: $exponent = &lt;STDIN&gt;;</p>
        <p id="p_02202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02202"> 8: chop ($exponent);</p>
        <p id="p_02203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02203"> 9: print ("Two to the power $exponent is  ",</p>
        <p id="p_02204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02204"> 10: 2 ** $exponent,  "\n");</p>
        <p id="p_02205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02205"> Output</p>
        <p id="p_02206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02206"> $ program4_l</p>
        <p id="p_02207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02207"> Enter the exponent to use:</p>
        <p id="p_02208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02208"> 16</p>
        <p id="p_02209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02209"> Two to the power 16 is 65536</p>
        <p id="p_02210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02210"> $</p>
        <p id="p_02211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02211"> Analysis</p>
        <p id="p_02212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02212"> The program shown in Listing 4.1 is useful if you have to use, or be aware of, numbers such as 4,294,967,295 (the largest number that can be stored in a 32-bit unsigned integer) and 2,147,483,647 (the largest number that can be stored in a 32-bit signed integer). The former is equivalent to  2 * * 32 - 1, and the latter is equivalent to  2 * * 31 - 1</p>
        <p id="p_02213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02213"> DON'T  use the exponent operator with a negative base and a non-integer exponent:</p>
        <p id="p_02214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02214"> (-5) ** 2.5 # error</p>
        <p id="p_02215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02215"> The result of this expression is a complex (non-real) number (just as, for instance, the square root of -2 is a complex number). Perl does not understand complex numbers.</p>
        <p id="p_02216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02216"> DON'T  produce a result that is larger than the largest floating-point number your machine can understand:</p>
        <p id="p_02217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02217"> 10  **  999999  # error</p>
        <p id="p_02218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02218"> In this example, the exponent is too large to be stored on most machines.</p>
        <p id="p_02219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02219"> The Remainder Operator</p>
        <p id="p_02220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02220"> The  remainder operator  retrieves the remainder resulting from the division of one integer by another. Consider the following simple example:</p>
        <p id="p_02221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02221"> $x — 2 5 % 4;</p>
        <p id="p_02222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02222"> In this case, 25 divided by 4 yields 6, with a remainder of 1. The remainder, 1, is assigned to  $x.</p>
        <p id="p_02223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02223"> The % operator does not work on values that are not integers. Non-integers are converted to integers, as follows:</p>
        <p id="p_02224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02224"> $x = 24.77 % 4.21; #  same  as 25 % 4</p>
        <p id="p_02225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02225"> Because division by 0 is impossible, you can't put a 0 to the right of a % operator.</p>
        <p id="p_02226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02226"> $x = 25 % 0; # error: can't  divide   by  0</p>
        <p id="p_02227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02227"> $x = 25 % 0.1; # error: 0.1 is  converted to 0</p>
        <p id="p_02228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02228"> Unary Negation</p>
        <p id="p_02229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02229"> The  unary negation operator  is a - character in front of a single value. (This distinguishes it from the</p>
        <p id="p_02230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02230"> subtraction operator, which appears between two values.) It is equivalent to multiplying the value by -1, as illustrated by this example:</p>
        <p id="p_02231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02231"> - 5; # identical to the integer -5</p>
        <p id="p_02232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02232"> - $y; # equivalent to $y * -1</p>
        <p id="p_02233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02233"> Using Comparison Operators</p>
        <p id="p_02234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02234"> On  Day 2 ,  "Basic Operators and Control Flow," you learned about the equality comparison operator (==), which compares two values and tests whether they are equal.</p>
        <p id="p_02235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02235"> $x = $a == $b;</p>
        <p id="p_02236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02236"> Recall that the value of  $x  depends on the values stored in  $a  and  $b:</p>
        <p id="p_02237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02237"> • If  $a  equals  $b, $a == $b  is true, and  $x  is assigned a nonzero value.</p>
        <p id="p_02238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02238"> • If $  a  is not equal to  $b, $ a == $b  is false, and  $x  is assigned 0.</p>
        <p id="p_02239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02239"> The == operator is an example of a  comparison operator.  Comparison operators are most commonly used control statements such as the if statement, as follows:</p>
        <p id="p_02240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02240"> if ($a == $b) {</p>
        <p id="p_02241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02241"> print("$a is equal to $b\n");</p>
        <p id="p_02242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02242"> }</p>
        <p id="p_02243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02243"> In Perl, the comparison operators are divided into two classes:</p>
        <p id="p_02244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02244"> • Comparison operators that work with numbers</p>
        <p id="p_02245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02245"> • Comparison operators that work with strings</p>
        <p id="p_02246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02246"> Integer-Comparison Operators</p>
        <p id="p_02247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02247"> Table 4.1 defines the integer-comparison operators available in Perl.</p>
        <p id="p_02248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02248"> Table 4.1. Integer-comparison operators.</p>
        <p id="p_02249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02249"> Here are simple examples of each of the first six operators in Table 4.1:</p>
        <p id="p_02250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02250"> Each of these operators yields one of two values:</p>
        <p id="p_02251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02251"> • True, or nonzero</p>
        <p id="p_02252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02252"> • False, or zero</p>
        <p id="p_02253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02253"> The &lt;=&gt; operator is a special case. Unlike the other integer comparison operators, &lt;=&gt; returns one of three values:</p>
        <p id="p_02254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02254"> • 0, if the two values being compared are equal</p>
        <p id="p_02255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02255"> • 1, if the first value is greater</p>
        <p id="p_02256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02256"> • -1, if the second value is greater</p>
        <p id="p_02257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02257"> For example, consider the following statement:</p>
        <p id="p_02258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02258"> $y = $x &lt;=&gt; 10;</p>
        <p id="p_02259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02259"> These are the possible results:</p>
        <p id="p_02260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02260"> • If  $x  is greater than 10, the first value in the comparison is greater, and  $y  is assigned 1.</p>
        <p id="p_02261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02261"> • If  $x  is less than 10, the second value in the comparison is greater, and  $y  is assigned -1.</p>
        <p id="p_02262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02262"> • If  $x  is equal to 10,  $y  is assigned 0.</p>
        <p id="p_02263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02263"> Integer Comparisons and Readability</p>
        <p id="p_02264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02264"> In any given statement, it's best to use the comparison that can be most easily read. For example, consider the following:</p>
        <p id="p_02265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02265"> if (3.2 &lt; $x) {</p>
        <p id="p_02266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02266"> # conditionally executed stuff goes here</p>
        <p id="p_02267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02267"> }</p>
        <p id="p_02268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02268"> Although the expression  3.2 &lt; $x&lt;  is perfectly valid, it isn't easy to read because variables usually appear first in comparisons. Instead, it would be better to use</p>
        <p id="p_02269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02269"> if ($x &gt;=  3.2) {</p>
        <p id="p_02270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02270"> because this is easier to understand. I'm not sure exactly why this is true; I think it's related to the way the English language is spoken. (Normally, we say, "If I had five dollars, I'd buy some milk," instead of, "If five dollars had I, I'd buy some milk," even though both are correct.)</p>
        <p id="p_02271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02271"> String-Comparison Operators</p>
        <p id="p_02272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02272"> For every numeric-comparison operator, Perl defines an equivalent string-comparison operator. Table 4.2 displays each string-comparison operator, the comparison it performs, and the equivalent numeric-comparison operator.</p>
        <p id="p_02273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02273"> Table 4.2. String- and numeric-comparison operators.</p>
        <p id="p_02274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02274"> Perl compares strings by determining their places in an alphabetical order. For example, the string  aaa  is less than the string  bbb,  because  aaa  appears before  bbb  when they are sorted alphabetically.</p>
        <p id="p_02275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02275"> Here are some examples of string-comparison operators in action:</p>
        <p id="p_02276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02276"> If you are familiar with the C programming language, you might have noticed that the behavior of the  cmp operator is identical to that of the C function  strcmp ().</p>
        <p id="p_02277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02277"> String Comparison Versus Integer Comparison</p>
        <p id="p_02278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02278"> You might be thinking: If strings and integers are equivalent in Perl, why do we need two kinds of comparison operators?</p>
        <p id="p_02279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02279"> To answer this, consider the strings 12 3 and 4 5. The result when these two strings are compared depends on whether a string or integer comparison is being performed.</p>
        <p id="p_02280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02280"> $result = "123" &lt;  "45";</p>
        <p id="p_02281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02281"> $result = "123" It  "45";</p>
        <p id="p_02282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02282"> In the first statement, the strings 12 3 and 4 5 are converted to integers, and 123 is compared to 45. The result is false and $result is assigned 0, because 123 is not less than 45.</p>
        <p id="p_02283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02283"> In the second statement, 12 3 is alphabetically compared to 4 5. Because 12 3 is alphabetically less than 4 5, the result in this case is true, and $  result  is assigned a nonzero value.</p>
        <p id="p_02284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02284"> Because these results are different, you must ensure that you are using the proper comparison operator every time. If you don't, your program can contain errors that are not easy to spot. For instance, consider the following:</p>
        <p id="p_02285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02285"> $varl = "string 1";</p>
        <p id="p_02286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02286"> $var2 = "string 2";</p>
        <p id="p_02287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02287"> $result = $varl == $var2;</p>
        <p id="p_02288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02288"> # this statement is bad</p>
        <p id="p_02289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02289"> Because == is a numeric-comparison operator, the values  string  1 and  string 2  are converted to integers before the comparison is performed. Because both strings are non-numeric, they are both converted to the integer 0, and the following comparison becomes true:</p>
        <p id="p_02290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02290"> $varl == $var2</p>
        <p id="p_02291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02291"> This is probably not what you want.</p>
        <p id="p_02292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02292"> Comparison and Floating-Point Numbers</p>
        <p id="p_02293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02293"> There is one thing to keep in mind when you use comparison operators: Floating-point numbers don't always behave properly in comparisons.</p>
        <p id="p_02294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02294"> Take a look at Listing 4.2.</p>
        <p id="p_02295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02295"> Type</p>
        <p id="p_02296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02296"> Listing 4.2. A program that contains a floating-point comparison.</p>
        <p id="p_02297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02297"> 1 : #!/usr/local/bin/perl</p>
        <p id="p_02298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02298"> 2  :</p>
        <p id="p_02299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02299"> 3: $valuel = 14.3;</p>
        <p id="p_02300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02300"> 4: $value2 = 100 + 14.3 - 100;</p>
        <p id="p_02301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02301"> 5: if ($valuel == $value2) {</p>
        <p id="p_02302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02302"> 6 : print("value 1 equals value 2 \n");</p>
        <p id="p_02303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02303"> 7:  } else {</p>
        <p id="p_02304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02304"> 8  :</p>
        <p id="p_02305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02305"> 9: }</p>
        <p id="p_02306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02306"> print("value 1 does not equal value 2 \n");</p>
        <p id="p_02307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02307"> Output</p>
        <p id="p_02308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02308"> $ program4_2</p>
        <p id="p_02309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02309"> value 1 does not equal value 2</p>
        <p id="p_02310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02310"> $</p>
        <p id="p_02311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02311"> Analysis</p>
        <p id="p_02312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02312"> At first glance, you might think that $valuel and $value2 are identical. However, when you run this program, you get the following:</p>
        <p id="p_02313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02313"> value 1 does not equal value 2</p>
        <p id="p_02314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02314"> What is wrong? To find out, print out the values of $ value 1 and $value2 before doing the comparison.</p>
        <p id="p_02315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02315"> #!/usr/local/bin/perl $valuel = 14.3;</p>
        <p id="p_02316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02316"> $value2 = 100 + 14.3 - 100;</p>
        <p id="p_02317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02317"> print("value 1 is $valuel, value2 is $value2\n"); if ($valuel == $value2) {</p>
        <p id="p_02318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02318"> print("value 1 equals value  2\n");</p>
        <p id="p_02319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02319"> } else {</p>
        <p id="p_02320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02320"> print("value 1 does not equal value  2\n");</p>
        <p id="p_02321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02321"> }</p>
        <p id="p_02322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02322"> When you run this program, you get the following output:</p>
        <p id="p_02323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02323"> value 1 is  14.300000000000001,  value 2 is  14.299999999999997 value 1 does not equal value 2</p>
        <p id="p_02324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02324"> Well, Perl isn't lying: $ value 1 and $value2  are  different. What happened?</p>
        <p id="p_02325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02325"> To understand what's going on, consider what happens when you take an ordinary calculator and tell it to divide 8 by 3. The actual answer is</p>
        <p id="p_02326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02326"> 2 . 6666666 . . .</p>
        <p id="p_02327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02327"> with the number of 6s being infinite. Because your calculator can't display an infinite number of 6s, what it displays is something like the following:</p>
        <p id="p_02328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02328"> 2.6666666667</p>
        <p id="p_02329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02329"> This is as close to the actual number as your calculator can get. The difference between the actual number and the number displayed is an example of a  round-off error.</p>
        <p id="p_02330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02330"> Round-off errors often occur when Perl (or almost any other programming language) stores a floating¬ point number or adds a number to a floating-point number. The statement</p>
        <p id="p_02331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02331"> $valuel = 14.3;</p>
        <p id="p_02332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02332"> actually assigns</p>
        <p id="p_02333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02333"> 14.300000000000001</p>
        <p id="p_02334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02334"> to $valuel, because  14.3  cannot be exactly represented in the machine's floating-point storage. When 100 is added to this number and subtracted again, the result is</p>
        <p id="p_02335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02335"> 14.299999999999997</p>
        <p id="p_02336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02336"> Note that both numbers are very close to 14.3 but aren't exactly 14.3 due to round-off errors. What's worse, each number is affected by a different set of round-off errors, so the two numbers are not identical.</p>
        <p id="p_02337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02337"> The moral of the story? Be very careful when you use floating-point numbers in comparisons, because round-off errors might affect your results.</p>
        <p id="p_02338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02338"> Using Logical Operators</p>
        <p id="p_02339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02339"> The comparison operators you've seen so far are sufficient if you need to test for only one condition before executing a particular code segment, as in this example:</p>
        <p id="p_02340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02340"> if ($value  ==26) {</p>
        <p id="p_02341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02341"> # the code to execute if the condition is true</p>
        <p id="p_02342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02342"> }</p>
        <p id="p_02343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02343"> Suppose, however, that a particular section of code is to be executed only when a variety of conditions are true. You can use a sequence of if statements to test for the conditions, as follows:</p>
        <p id="p_02344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02344"> if ($valuel == 26) {</p>
        <p id="p_02345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02345"> if ($value2 &gt;0) {</p>
        <p id="p_02346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02346"> if ($stringl eq "ready") {</p>
        <p id="p_02347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02347"> print("all three conditions are true!\n");</p>
        <p id="p_02348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02348"> }</p>
        <p id="p_02349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02349"> }</p>
        <p id="p_02350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02350"> }</p>
        <p id="p_02351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02351"> This is tiresome to write and not particularly easy to read.</p>
        <p id="p_02352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02352"> Fortunately, Perl provides an easier way to deal with multiple conditions: the  logical operators.  The following logical operators are defined:</p>
        <p id="p_02353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02353"> $a || $b $a &amp;&amp; $b ! $a</p>
        <p id="p_02354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02354"> # logical or:</p>
        <p id="p_02355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02355"> # logical and:</p>
        <p id="p_02356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02356"> # logical not:</p>
        <p id="p_02357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02357"> true if either is nonzero true only if both are nonzero true if $a is zero</p>
        <p id="p_02358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02358"> Perl 5 also defines these logical operators:</p>
        <p id="p_02359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02359"> $a or $b</p>
        <p id="p_02360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02360"> $a and $b</p>
        <p id="p_02361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02361"> not $a</p>
        <p id="p_02362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02362"> $a xor $b nonzero, but not</p>
        <p id="p_02363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02363"> # another form</p>
        <p id="p_02364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02364"> # another form</p>
        <p id="p_02365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02365"> # another form</p>
        <p id="p_02366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02366"> # logical xor: both</p>
        <p id="p_02367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02367"> of logical or</p>
        <p id="p_02368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02368"> of logical and</p>
        <p id="p_02369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02369"> of logical not</p>
        <p id="p_02370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02370"> true if either $a or $b is</p>
        <p id="p_02371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02371"> The or, and, and not operators listed are identical to | |, &amp;&amp;, and !, except that their precedence is lower.  (Operator precedence  determines the order in which operators are evaluated, and is discussed later today.)</p>
        <p id="p_02372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02372"> In each case, the result of the operation performed by a logical operator is nonzero if true and 0 if false.</p>
        <p id="p_02373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02373"> These logical operators enable you to test for multiple conditions more conveniently. Instead of writing, for example, this code:</p>
        <p id="p_02374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02374"> if ($valuel == 26) {</p>
        <p id="p_02375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02375"> if ($value2 &gt;0) {</p>
        <p id="p_02376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02376"> if ($stringl eq "ready") {</p>
        <p id="p_02377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02377"> print("all three conditions are true!\n");</p>
        <p id="p_02378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02378"> }</p>
        <p id="p_02379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02379"> }</p>
        <p id="p_02380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02380"> }</p>
        <p id="p_02381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02381"> you now can write this code instead:</p>
        <p id="p_02382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02382"> if ($value == 26 &amp;&amp; $value2 &gt; 0 &amp;&amp; $stringl eq "ready") { print("all three conditions are true!\n");</p>
        <p id="p_02383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02383"> }</p>
        <p id="p_02384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02384"> In each case, the result is the same: the print operation is performed only when $value is 26, $value2 is greater than 0, and $stringl is "ready."</p>
        <p id="p_02385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02385"> Evaluation Within Logical Operators</p>
        <p id="p_02386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02386"> When Perl sees a logical AND operator or a logical OR operator, the expression on the left side of the operator is always evaluated first. For example, consider the following:</p>
        <p id="p_02387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02387"> $a = 0;</p>
        <p id="p_02388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02388"> $b = 106;</p>
        <p id="p_02389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02389"> $result = $a &amp;&amp; $b;</p>
        <p id="p_02390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02390"> When Perl is evaluating the expression $ a &amp;&amp; $b, it first checks whether $ a is 0. If $ a is 0, $ a &amp;&amp; $b must be false regardless of the value of $b, so Perl doesn't bother checking the value of $b. (This is called  short-circuit evaluation .)</p>
        <p id="p_02391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02391"> Similarly, in the following example, Perl doesn't bother checking $b, because $a is nonzero and therefore $ a || $b must be true:</p>
        <p id="p_02392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02392"> $a = 43;</p>
        <p id="p_02393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02393"> $b = 11;</p>
        <p id="p_02394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02394"> $result = $a || $b;</p>
        <p id="p_02395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02395"> You can take advantage of the order of evaluation of expressions in | | or &amp;&amp; to safeguard your code.</p>
        <p id="p_02396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02396"> $x  == 0 || $y /  $x  &gt; 5</p>
        <p id="p_02397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02397"> Here is how the preceding statement protects you from division-by-zero errors:</p>
        <p id="p_02398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02398"> • If  $x  is not 0,  $x ==  0 is false, so Perl evaluates  $y / $x &gt;  5. This cannot produce a division-by-zero error, because  $x  is guaranteed to be some value other than 0.</p>
        <p id="p_02399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02399"> • If  $x  is 0,  $x ==  0 is true. This means that $x ==  0 || $y  / $x &gt;  5</p>
        <p id="p_02400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02400"> is true, so Perl doesn't bother evaluating the expression to the right of the | |. As a result, the</p>
        <p id="p_02401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02401"> expression</p>
        <p id="p_02402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02402"> $y / $x &gt; 5</p>
        <p id="p_02403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02403"> is not evaluated when $x is 0, and the division-by-zero error is avoided.</p>
        <p id="p_02404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02404"> Logical Operators as Subexpressions</p>
        <p id="p_02405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02405"> Expressions that contain logical operators can be contained in larger expressions. The following is an example:</p>
        <p id="p_02406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02406"> $myval = $a || $b || $c;</p>
        <p id="p_02407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02407"> Here, Perl evaluates the expression $a | | $b | | $c and assigns its value to $myval.</p>
        <p id="p_02408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02408"> To understand the behavior of this statement, recall that the | | operator evaluates its subexpressions in the order given, and evaluates a subexpression only if the previous subexpression is zero. This means that $b is evaluated only if $ a is zero.</p>
        <p id="p_02409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02409"> When the logical OR operator is used in a larger expression, its value is the last subexpression actually evaluated, which is the first subexpression of the logical OR operator that is nonzero. This means that</p>
        <p id="p_02410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02410"> $myval = $a || $b || $c;</p>
        <p id="p_02411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02411"> is equivalent to</p>
        <p id="p_02412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02412"> if  ($a != 0) {</p>
        <p id="p_02413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02413"> $myvalue = $a;</p>
        <p id="p_02414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02414"> }  elsif  ($b != 0) {</p>
        <p id="p_02415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02415"> $myvalue = $b;</p>
        <p id="p_02416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02416"> } else {</p>
        <p id="p_02417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02417"> $myvalue = $c;</p>
        <p id="p_02418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02418"> }</p>
        <p id="p_02419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02419"> The logical AND operator works in the same way, but isn't as useful. The statement</p>
        <p id="p_02420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02420"> $myval = $a &amp;&amp; $b &amp;&amp; $c;</p>
        <p id="p_02421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02421"> is equivalent to</p>
        <p id="p_02422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02422"> if ($a == 0) {</p>
        <p id="p_02423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02423"> $myvalue = $a; } elsif ($b == 0) {</p>
        <p id="p_02424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02424"> $myvalue = $b;</p>
        <p id="p_02425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02425"> } else {</p>
        <p id="p_02426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02426"> $myvalue = $c;</p>
        <p id="p_02427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02427"> }</p>
        <p id="p_02428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02428"> This means that $myval is set to either 0 or the value of $c.</p>
        <p id="p_02429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02429"> Using Bit-Manipulation Operators</p>
        <p id="p_02430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02430"> Perl enables you to manipulate the binary digits (or bits) of an integer. To understand how Perl does this, first look at what a bit is and how computers store integers. Once you understand how bits work, you can easily figure out how the bit-manipulation operators work. (If you are familiar with binary notation and the computer representation of an integer, feel free to skip the following section.)</p>
        <p id="p_02431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02431"> What Bits Are and How They Are Used</p>
        <p id="p_02432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02432"> On  Day  3 ,  "Understanding Scalar Values," you learned that Perl understands three different notations for integers:</p>
        <p id="p_02433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02433"> • Standard notation, or base 10</p>
        <p id="p_02434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02434"> • Octal notation, or base 8</p>
        <p id="p_02435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02435"> • Hexadecimal notation, or base 16</p>
        <p id="p_02436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02436"> However, when a computer stores an integer, it uses none of these notations; instead, it uses base 2, or binary notation.</p>
        <p id="p_02437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02437"> In binary notation, every number is represented as a series of Os and Is. For instance, the number 124 is represented as</p>
        <p id="p_02438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02438"> 01111100</p>
        <p id="p_02439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02439"> To understand how to get from base-10 notation to binary notation, recall what the number 124 represents. When we write "124," what we really mean is the following:</p>
        <p id="p_02440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02440"> • 4 multiplied by 1, plus</p>
        <p id="p_02441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02441"> • 2 multiplied by 10, plus</p>
        <p id="p_02442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02442"> • 1 multiplied by 100</p>
        <p id="p_02443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02443"> In grade school, your teacher probably said these digits represented the "ones place," the "tens place," and the "hundreds place." Each "place" is ten times larger than the place to its right. This means that you also can think of 124 as follows:</p>
        <p id="p_02444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02444"> • 4 multiplied by 1 (or 10 to the exponent 0), plus</p>
        <p id="p_02445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02445"> • 2 multiplied by 10 to the exponent 1, plus</p>
        <p id="p_02446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02446"> • 1 multiplied by 10 to the exponent 2</p>
        <p id="p_02447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02447"> In binary notation, you can use this same method, but replace the 10s with 2s. Here's how to use this method to figure out that the binary number 01111100 is equivalent to 124 in standard notation. Starting from the right, you have:</p>
        <p id="p_02448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02448"> • 0 multiplied by 2 to the exponent 0, which is 0</p>
        <p id="p_02449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02449"> • 0 multiplied by 2 to the exponent 1, which is 0</p>
        <p id="p_02450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02450"> • 1 multiplied by 2 to the exponent 2, which is 4</p>
        <p id="p_02451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02451"> • 1 multiplied by 2 to the exponent 3, which is 8</p>
        <p id="p_02452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02452"> • 1 multiplied by 2 to the exponent 4, which is 16</p>
        <p id="p_02453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02453"> • 1 multiplied by 2 to the exponent 5, which is 32</p>
        <p id="p_02454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02454"> • 1 multiplied by 2 to the exponent 6, which is 64</p>
        <p id="p_02455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02455"> • 0 multiplied by 2 to the exponent 7, which is 0</p>
        <p id="p_02456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02456"> Adding 2, 8,16, 32, and 64 gives you 124.</p>
        <p id="p_02457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02457"> Each of the 0s and Is in the binary number 01111100 is called a  bit  (which is short for binary digit). Each bit can have only two possible values: 0 or 1.</p>
        <p id="p_02458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02458"> In computers, integers are stored as a sequence of bits. This sequence of bits is normally 8,16, or 32 bits long, depending on the size and configuration of your computer. In the examples in today's lesson, 8-bit integers are assumed; to convert an 8-bit binary number to a 16-bit binary number, just add eight zeros to the left. For example, the following numbers are equivalent:</p>
        <p id="p_02459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02459"> 01111100 # 124 as an 8-bit integer</p>
        <p id="p_02460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02460"> 0000000001111100 # 124 as a 16-bit integer</p>
        <p id="p_02461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02461"> The examples in today's lesson use 8-bit integers. The Perl bitwise operators will work on integers of any size.</p>
        <p id="p_02462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02462"> The Bit-Manipulation Operators</p>
        <p id="p_02463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02463"> The following bit-manipulation operators are supported in Perl:</p>
        <p id="p_02464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02464"> • The &amp; (bitwise AND) operator</p>
        <p id="p_02465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02465"> • The | (bitwise OR) operator</p>
        <p id="p_02466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02466"> • The  A  (bitwise XOR or "exclusive or") operator</p>
        <p id="p_02467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02467"> • The ~ (bitwise NOT) operator</p>
        <p id="p_02468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02468"> • The « (left shift) and » (right shift) operators</p>
        <p id="p_02469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02469"> The Bitwise AND Operator</p>
        <p id="p_02470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02470"> In Perl, the &amp; operator represents the bitwise AND operation. This operation works as follows:</p>
        <p id="p_02471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02471"> • The value to the left side of the &amp; (also called the  left operand  of the &amp; operation) is converted to an integer, if necessary.</p>
        <p id="p_02472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02472"> • The value to the right side of the &amp; (the  right operand)  also is converted to an integer.</p>
        <p id="p_02473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02473"> • Each bit of the left operand is compared to the corresponding bit of the right operand.</p>
        <p id="p_02474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02474"> • If a pair of corresponding bits both have the value 1, the corresponding bit of the result is set to 1. Otherwise, the corresponding bit of the result is set to 0.</p>
        <p id="p_02475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02475"> This might sound complicated, but when you take a look at an example, you'll see that it's pretty easy to figure out. For instance, consider the following:</p>
        <p id="p_02476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02476"> $result = 124.3 &amp; 99;</p>
        <p id="p_02477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02477"> First, the left operand, 124.3, is converted to an integer, becoming 124. (The right operand, 99, does not need to be converted.) Next, take a look at the binary representations of 124 and 99:</p>
        <p id="p_02478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02478"> 01111100 # this is 124 in binary</p>
        <p id="p_02479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02479"> 01100011 # this is 99 in binary</p>
        <p id="p_02480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02480"> When you examine each pair of bits in turn, you can see that only the second and third pairs (from the left) are both 1. Thus, the &amp; operation yields the following binary result:</p>
        <p id="p_02481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02481"> 01100000</p>
        <p id="p_02482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02482"> This is 96 in standard notation. As a consequence, the statement</p>
        <p id="p_02483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02483"> $result = 124.3 &amp; 99;</p>
        <p id="p_02484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02484"> assigns 96 to $ result.</p>
        <p id="p_02485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02485"> DO  use the &amp; operator with strings, provided the strings can be converted to numbers, as follows:</p>
        <p id="p_02486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02486"> $result = "124.3" &amp; "99";</p>
        <p id="p_02487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02487"> Remember: Strings and integers are interchangeable in Perl.</p>
        <p id="p_02488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02488"> DON'T  confuse the &amp; operator with the &amp; &amp; operator. The &amp; &amp; operator performs a logical AND operation, not a bitwise AND operation. For example, the statement</p>
        <p id="p_02489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02489"> $result = 124.3 &amp;&amp; 99;</p>
        <p id="p_02490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02490"> assigns a nonzero value to  $result  (because 124.3 and 99 are both nonzero). This nonzero value is not likely to be the result you want.</p>
        <p id="p_02491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02491"> DON'T  use the &amp; operator with negative integers, because Perl will convert them to unsigned integers, and you won't get the result you want.</p>
        <p id="p_02492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02492"> The Bitwise OR Operator</p>
        <p id="p_02493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02493"> The bitwise OR operator, |, also compares two integers one bit at a time. However, in the bitwise OR operation, a result bit is 1 if either of the corresponding bits in the operands is 1.</p>
        <p id="p_02494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02494"> To see how this works, look at another example:</p>
        <p id="p_02495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02495"> $result = 124.3 | 99;</p>
        <p id="p_02496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02496"> Here's how this operation is performed:</p>
        <p id="p_02497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02497"> • As before, the two operands are converted to integers if necessary. The operands become 124 and 99; in binary representation, these are, as before,</p>
        <p id="p_02498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02498"> 01111100</p>
        <p id="p_02499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02499"> 01100011</p>
        <p id="p_02500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02500"> • Each bit of the left operand is compared with the corresponding bit in the right operand. If either of the corresponding bits is 1, the corresponding result bit is 1.</p>
        <p id="p_02501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02501"> In this example, every bit becomes 1 except the first one, because at least one of each of the other pairs</p>
        <p id="p_02502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02502"> is a 1. Therefore, the result is</p>
        <p id="p_02503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02503"> 01111111</p>
        <p id="p_02504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02504"> which translates to 127. This means that the following statement assigns 127 to $result:</p>
        <p id="p_02505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02505"> $result =  124.3 | 99;</p>
        <p id="p_02506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02506"> Don't</p>
        <p id="p_02507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02507"> DO  make sure you are using the proper bitwise operator. It's easy to slip and assume you want bitwise OR when you really want bitwise AND. (Trust me.)</p>
        <p id="p_02508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02508"> DON'T  confuse the | operator (bitwise OR) with the | | operator (logical OR).</p>
        <p id="p_02509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02509"> The Bitwise XOR Operator</p>
        <p id="p_02510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02510"> The bitwise XOR ("exclusive or") operator,  A , is similar to the bitwise OR operator, but it's a little more demanding. In the bitwise OR operation, a result bit is 1 if either of the corresponding bits in the operands is 1. In the bitwise XOR operation, a result bit is 1 if  exactly one  of the corresponding bits in the operands is 1.</p>
        <p id="p_02511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02511"> Here is an example of the bitwise XOR operation:</p>
        <p id="p_02512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02512"> $result =  124.3  A  99;</p>
        <p id="p_02513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02513"> This works as follows:</p>
        <p id="p_02514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02514"> • As before, 124.3 is converted to 124, and the binary representations of the two operands are as follows:</p>
        <p id="p_02515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02515"> • Each bit of the left operand is compared with the corresponding bit of the right operand. The corresponding result bit is set to 1 if exactly one of the bits in the operands is 1.</p>
        <p id="p_02516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02516"> In this case, the result is</p>
        <p id="p_02517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02517"> 00011111</p>
        <p id="p_02518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02518"> which is 31. To work through how you get this result, consider the following:</p>
        <p id="p_02519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02519"> • The first bit of the left operand and the first bit of the right operand are both 0. This means the first bit of the result is 0.</p>
        <p id="p_02520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02520"> • The second bit of the left operand and the second bit of the right operand both are 1.</p>
        <p id="p_02521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02521"> Therefore, the second bit of the result is 0, not 1.</p>
        <p id="p_02522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02522"> • The same applies for the third bits: Both are 1, so the result bit is 0.</p>
        <p id="p_02523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02523"> • The fourth bit of the left operand is 1, and the fourth bit of the right operand is 0. Here, exactly one of the bits is 1, so the result bit becomes 1.</p>
        <p id="p_02524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02524"> • Same for the fifth and sixth pairs: The first bit is 1 and the second is 0, so the result is 1.</p>
        <p id="p_02525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02525"> • The seventh bit of the left operand is 0, and the seventh bit of the right operand is 1. Again, exactly one of the bits is 1, and the result bit is also 1.</p>
        <p id="p_02526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02526"> • Same for the eighth pair: The first bit is 0, the second is 1, so the result is 1.</p>
        <p id="p_02527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02527"> From this, you can determine that the following statement assigns 31 to $result:</p>
        <p id="p_02528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02528"> $result = 124.3  A  99;</p>
        <p id="p_02529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02529"> The Bitwise NOT Operator</p>
        <p id="p_02530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02530"> Unlike the other bitwise operators you've seen so far, the bitwise NOT operator, ~, is a  unary operator, meaning it works on only one operand.</p>
        <p id="p_02531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02531"> The way it works is straightforward, as follows:</p>
        <p id="p_02532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02532"> • The operand is converted to an integer, if necessary.</p>
        <p id="p_02533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02533"> • Each bit of the operand is examined. If a bit is 0, the corresponding result bit is set to 1, and vice versa.</p>
        <p id="p_02534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02534"> For example, consider the following:</p>
        <p id="p_02535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02535"> $result = ~99;</p>
        <p id="p_02536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02536"> The binary representation of 99 is</p>
        <p id="p_02537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02537"> 01100011</p>
        <p id="p_02538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02538"> Applying the bitwise NOT operation to this number produces</p>
        <p id="p_02539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02539"> 10011100</p>
        <p id="p_02540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02540"> This number, in standard notation, is 156. Therefore, the following statement assigns 156 to $result:</p>
        <p id="p_02541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02541"> $result =  ~99;</p>
        <p id="p_02542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02542"> Note that the number of bits used to store an integer affects the results produced by the ~ operator. For example, if integers are stored in 16 bits on your computer, the number 99 is represented as</p>
        <p id="p_02543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02543"> 0000000001100011</p>
        <p id="p_02544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02544"> This means that applying ~ to this number yields</p>
        <p id="p_02545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02545"> 1111111110011100</p>
        <p id="p_02546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02546"> which is 65436 in standard notation. As a consequence, the statement</p>
        <p id="p_02547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02547"> $result =  ~99;</p>
        <p id="p_02548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02548"> assigns 65436, not 156, to $result. (On a computer with 32-bit integers, the value assigned is 4294967196.)</p>
        <p id="p_02549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02549"> The Shift Operators</p>
        <p id="p_02550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02550"> Perl enables you to shift the bits of an integer using the « (shift left) and » (shift right) operators. For example, in the statement</p>
        <p id="p_02551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02551"> $result = $x &gt;&gt; 1;</p>
        <p id="p_02552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02552"> every bit of the value stored in $x is shifted one place to the right, and the result is assigned to $result ($x itself is not changed).</p>
        <p id="p_02553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02553"> To see how this works, consider the following example:</p>
        <p id="p_02554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02554"> $result = 99 &gt;&gt; 1;</p>
        <p id="p_02555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02555"> As you saw earlier, the binary representation of 99 is</p>
        <p id="p_02556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02556"> 01100011</p>
        <p id="p_02557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02557"> Shifting every bit right one place yields</p>
        <p id="p_02558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02558"> 00110001</p>
        <p id="p_02559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02559"> Note that a 0 is added at the far left, and the bit at the far right disappears.</p>
        <p id="p_02560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02560"> Because 00110001 in binary notation is the same as 49 in standard notation, the following statement assigns 49 to $ result:</p>
        <p id="p_02561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02561"> $result = 99 &gt;&gt; 1;</p>
        <p id="p_02562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02562"> The &lt;&lt;, or shift-left, operator works in the same way:</p>
        <p id="p_02563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02563"> $result = 99 « 1;</p>
        <p id="p_02564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02564"> The shift-left operator works as follows:</p>
        <p id="p_02565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02565"> 01100011 # the binary representation of 99</p>
        <p id="p_02566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02566"> 11000110 # after shifting left 1 bit</p>
        <p id="p_02567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02567"> Don't</p>
        <p id="p_02568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02568"> The result of the shift is 198, which is assigned to $result.</p>
        <p id="p_02569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02569"> DO  remember that when you use the &gt;&gt; operator, the bits on the right are lost. For example:</p>
        <p id="p_02570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02570"> $resultl = 17 &gt;&gt; 1;</p>
        <p id="p_02571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02571"> $result2 = 16 &gt;&gt; 1;</p>
        <p id="p_02572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02572"> In this case, $  re suit 1  and $  re suit 2  are the same value,  8.  This is because the rightmost bit is shifted out in both cases.</p>
        <p id="p_02573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02573"> DON'T  shift left too far, or you might not get the result you want. For example, if you are using 16-bit integers, the statement</p>
        <p id="p_02574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02574"> $result =  35000  &lt;&lt; 1;</p>
        <p id="p_02575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02575"> does not assign 70000 to $  re suit  as you might think it would because the largest value that can be stored in a 16-bit integer is 65536.</p>
        <p id="p_02576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02576"> Shifting and Powers of 2</p>
        <p id="p_02577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02577"> In the following statement, the variable $result is assigned the value 49:</p>
        <p id="p_02578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02578"> $result  =99/2;</p>
        <p id="p_02579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02579"> Take a look at the binary representations of 99 and 49:</p>
        <p id="p_02580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02580"> As you can see, dividing by 2 is identical to shifting right one bit-in each case, every bit is moved one place to the right. Similarly, shifting right two bits is equivalent to dividing by 4:</p>
        <p id="p_02581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02581"> Multiplying by 4 is similar to shifting left two bits:</p>
        <p id="p_02582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02582"> $result = 17 * 4;</p>
        <p id="p_02583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02583"> # $result is assigned 68</p>
        <p id="p_02584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02584"> 00010001</p>
        <p id="p_02585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02585"> # 17 in binary</p>
        <p id="p_02586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02586"> 01000100</p>
        <p id="p_02587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02587"> # 68 in binary</p>
        <p id="p_02588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02588"> The general rules are as follows:</p>
        <p id="p_02589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02589"> • Shifting left n bits, where n is some number greater than 0, is equivalent to multiplying by 2**n.</p>
        <p id="p_02590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02590"> • Shifting right n bits, where n is some number greater than 0, is equivalent to dividing by 2**n.</p>
        <p id="p_02591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02591"> In the early days of programming, many programmers used shift operators in place of multiplication and division wherever possible, because the shift operations were usually more efficient. (In fact, some compilers would optimize their code by converting multiplication and division to shifts.) Today, it's usually best to use the shift operators when you are manipulating bits, and to use the multiplication and division operators when you're actually doing arithmetic. This will make your programs easier to understand.</p>
        <p id="p_02592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02592"> Using the Assignment Operators</p>
        <p id="p_02593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02593"> As you saw on  Day 2 ,  the assignment operator = associates, or assigns, a value to a variable. For example, the statement</p>
        <p id="p_02594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02594"> $result = 42;</p>
        <p id="p_02595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02595"> assigns the value 42 to the variable $result.</p>
        <p id="p_02596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02596"> The = operator can appear more than once in a single statement. For example, in the statement</p>
        <p id="p_02597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02597"> $valuel = $value2 = "a string";</p>
        <p id="p_02598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02598"> the character string a string is assigned to both $valuel and $value2.</p>
        <p id="p_02599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02599"> Perl also supports other assignment operators, each of which combines an assignment with another operation. For example, suppose that you want to add a value to a scalar variable and assign the result to the following variable:</p>
        <p id="p_02600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02600"> $var = $var + 1;</p>
        <p id="p_02601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02601"> Another way to write this is with the += assignment operator:</p>
        <p id="p_02602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02602"> $var += 1;</p>
        <p id="p_02603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02603"> This statement adds the value 1 to the existing value of $var.</p>
        <p id="p_02604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02604"> An assignment operator exists for just about every bitwise operator and arithmetic operator that Perl supports. Table 4.3 lists the assignment operators supported in Perl.</p>
        <p id="p_02605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02605"> Table 4.3. The assignment operators.</p>
        <p id="p_02606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02606"> Table 4.4 shows examples of the assignment operators, along with equivalent statements that use operators you've seen earlier.</p>
        <p id="p_02607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02607"> Table 4.4. Examples of assignment operators.</p>
        <p id="p_02608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02608"> Assignment Operators as Subexpressions</p>
        <p id="p_02609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02609"> Any expression that contains an assignment operator can appear on the left side of another assignment operator. The following is an example:</p>
        <p id="p_02610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02610"> ($a = $b) += 3;</p>
        <p id="p_02611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02611"> In cases such as this, the assignment enclosed in parentheses is performed first. This assignment is then treated as a separate subexpression whose value is the variable to which it is being assigned. For example, $a = $b has the value $a.</p>
        <p id="p_02612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02612"> This means that the statement shown previously is equivalent to the following two statements:</p>
        <p id="p_02613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02613"> $a = $b; $a += 3;</p>
        <p id="p_02614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02614"> TIP</p>
        <p id="p_02615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02615"> Don't use assignments in this way unless you absolutely have to. At first glance, the statement ($a = $b) += 3;</p>
        <p id="p_02616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02616"> appears to add  3  to  $b  as well as to  $a.</p>
        <p id="p_02617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02617"> Using Autoincrement and Autodecrement</p>
        <p id="p_02618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02618"> So far, you've seen two ways to add 1 to a scalar variable:</p>
        <p id="p_02619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02619"> $a = $a + 1; $a += 1;</p>
        <p id="p_02620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02620"> The first method uses the standard assignment operator = and the addition operator +, and the second method uses the addition assignment operator +=.</p>
        <p id="p_02621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02621"> Perl also supports a third method of adding 1 to a scalar variable: the  autoincrement operator,  or ++. Here are some examples of the ++ operator in action:</p>
        <p id="p_02622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02622"> $a++;</p>
        <p id="p_02623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02623"> ++$a;</p>
        <p id="p_02624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02624"> $result = $a++;</p>
        <p id="p_02625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02625"> $result2 = ++$a;</p>
        <p id="p_02626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02626"> In each case, the ++ operator tells Perl to add 1 to the value stored in $a.</p>
        <p id="p_02627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02627"> In some of the examples, the ++ is in front of the variable it is affecting, whereas in others the ++ follows the variable. If the ++ is first, the operation is a  pre-increment  operation; if the ++ follows, the operation is a  post-increment  operation.</p>
        <p id="p_02628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02628"> The Autoincrement Operator Pre-Increment</p>
        <p id="p_02629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02629"> To understand how the pre-increment operation works, first recall that you can use a single statement to assign a value to more than one variable, as follows:</p>
        <p id="p_02630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02630"> $varl = 43;</p>
        <p id="p_02631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02631"> $var2 = $varl += 1;</p>
        <p id="p_02632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02632"> Here, the original value stored in $varl, 43, has 1 added to it. The result, 44, becomes the new value of $varl. This new value of 44 is then assigned to $var2.</p>
        <p id="p_02633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02633"> The pre-increment operation works in the same way:</p>
        <p id="p_02634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02634"> $varl = 43;</p>
        <p id="p_02635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02635"> $var2 = ++$varl;</p>
        <p id="p_02636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02636"> The following code fragment tells Perl to add 1 to $varl before doing anything else:</p>
        <p id="p_02637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02637"> ++$varl</p>
        <p id="p_02638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02638"> As a result, $varl becomes 44 before the value of $varl is assigned to $var2. Therefore, $var2 is assigned 44.</p>
        <p id="p_02639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02639"> The ++ operator is most frequently used in while statements. Listing 4.3 provides an example of a simple program that uses the ++ operator in a while statement.</p>
        <p id="p_02640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02640"> Type</p>
        <p id="p_02641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02641"> Listing 4.3. A program that uses the pre-increment operation.</p>
        <p id="p_02642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02642"> 1: #!/usr/local/bin/perl</p>
        <p id="p_02643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02643"> 2: $value = 0;</p>
        <p id="p_02644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02644"> 3: while (++$value &lt;= 5) {</p>
        <p id="p_02645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02645"> 4: print("value is now $value\n");</p>
        <p id="p_02646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02646"> 5: }</p>
        <p id="p_02647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02647"> 6: print("all done\n");</p>
        <p id="p_02648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02648"> Output</p>
        <p id="p_02649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02649"> $ program4_3 value is now 1 value is now 2 value is now 3 value is now 4 value is now 5 all done $</p>
        <p id="p_02650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02650"> Analysis</p>
        <p id="p_02651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02651"> ' Note that the pre-increment operation enables you to add 1 to $value and test it all at the same time. This means that you no longer have to remember to add the following:</p>
        <p id="p_02652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02652"> $value = $value + 1;</p>
        <p id="p_02653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02653"> at the bottom of the while statement, which means that you are less likely to write a while statement that goes on forever.</p>
        <p id="p_02654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02654"> Now see what happens when you change</p>
        <p id="p_02655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02655"> while (++$value &lt;= 5) {</p>
        <p id="p_02656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02656"> to this:</p>
        <p id="p_02657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02657"> while (++$value &lt;= 0) {</p>
        <p id="p_02658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02658"> and then run the program again. This time, you get the following:</p>
        <p id="p_02659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02659"> all done</p>
        <p id="p_02660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02660"> Because the ++ operator is in front of $value, 1 is added to $value before testing. This means that $value is not less than or equal to 0 when the while statement is executed for the first time; as a result, the code inside the while statement is never executed.</p>
        <p id="p_02661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02661"> The Autoincrement Operator Post-Increment</p>
        <p id="p_02662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02662"> The post-increment operator also adds 1 to the variable with which it is associated. However, its behavior is slightly different:</p>
        <p id="p_02663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02663"> $varl = 43;</p>
        <p id="p_02664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02664"> $var2 = $varl++;</p>
        <p id="p_02665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02665"> When the ++ operator appears after the variable, the ++ operator is performed  after  every- thing else is finished. This means that the original value of $varl, 43, is assigned to $var2. After this assignment is completed, 1 is added to $varl and the new value of $varl becomes 44.</p>
        <p id="p_02666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02666"> To see how this works in while statements, examine Listing 4.4. Although it is similar to Listing 4.3, it performs a post-increment operation instead of a pre-increment operation.</p>
        <p id="p_02667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02667"> Type</p>
        <p id="p_02668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02668"> Listing 4.4. A program that uses the post-increment operation.</p>
        <p id="p_02669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02669"> 1: #!/usr/local/bin/perl</p>
        <p id="p_02670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02670"> 2: $value = 0;</p>
        <p id="p_02671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02671"> 3: while ($value++ &lt;= 5) {</p>
        <p id="p_02672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02672"> 4: print("value is now $value\n");</p>
        <p id="p_02673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02673"> 5: }</p>
        <p id="p_02674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02674"> 6: print("all done\n");</p>
        <p id="p_02675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02675"> Output</p>
        <p id="p_02676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02676"> $</p>
        <p id="p_02677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02677"> Analysis</p>
        <p id="p_02678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02678"> You are probably wondering why the output of Listing 4.4 contained the following line:</p>
        <p id="p_02679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02679"> value is now 6</p>
        <p id="p_02680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02680"> To figure out what happened, examine the value stored in $value each time the condition in the while statement is tested. Table 4.5 lists the contents of $value when the condition is tested, the result of the test, and $value immediately after the condition is tested (after the ++ operator is applied).</p>
        <p id="p_02681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02681"> Table 4.5. Condition evaluation.</p>
        <p id="p_02682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02682"> As you know, when the condition at the top of a while statement is true, the code inside the statement is executed, which in this case is</p>
        <p id="p_02683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02683"> print("value is now $value\n");</p>
        <p id="p_02684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02684"> This is why the line</p>
        <p id="p_02685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02685"> value is now 6</p>
        <p id="p_02686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02686"> appears-$value is 5 at the time the condition is tested, so the result is true.</p>
        <p id="p_02687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02687"> To fix this problem, change the while condition to the following and run the program again:</p>
        <p id="p_02688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02688"> while ($value &lt; 5) {</p>
        <p id="p_02689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02689"> This is the output you get from the changed program:</p>
        <p id="p_02690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02690"> value is now 1 value is now 2 value is now 3 value is now 4 value is now 5</p>
        <p id="p_02691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02691"> all done</p>
        <p id="p_02692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02692"> Now, when $value is 5, the statement</p>
        <p id="p_02693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02693"> while ($value++ &lt; 5)</p>
        <p id="p_02694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02694"> is false, and the code inside the while is not executed.</p>
        <p id="p_02695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02695"> The Autodecrement Operator</p>
        <p id="p_02696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02696"> As you've seen, the ++ operator adds 1 to the value of the variable it is associated with and can appear either before or after the variable. The — operator, or  autodecrement operator , works in the same way, but it subtracts 1 from the value of the variable it is associated with, as follows:</p>
        <p id="p_02697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02697"> $a—;</p>
        <p id="p_02698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02698"> —$a;</p>
        <p id="p_02699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02699"> $result = $a—; $result2 = —$a;</p>
        <p id="p_02700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02700"> When the — operator is in front of the variable, the operation is a  pre-decrement  operation, which means that 1 is subtracted from the variable before anything else happens.</p>
        <p id="p_02701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02701"> $varl = 56;</p>
        <p id="p_02702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02702"> $var2 = —$varl;</p>
        <p id="p_02703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02703"> This subtracts 1 from $varl and assigns the result, 55, back to $varl. The value 55 is then assigned to $var2.</p>
        <p id="p_02704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02704"> When the — operator follows the variable, the operation is a  post-decrement  operation, which means that 1 is subtracted from the variable after everything else happens.</p>
        <p id="p_02705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02705"> $varl = 56;</p>
        <p id="p_02706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02706"> $var2 = $varl—;</p>
        <p id="p_02707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02707"> This assigns 56 to $var2 and then subtracts 1 from $varl, which means that $varl now has the value 55.</p>
        <p id="p_02708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02708"> Don't</p>
        <p id="p_02709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02709"> DO  be careful when you use the autoincrement and autodecrement operators. As you've seen, it's easy to get confused and tell your program to loop one too many times or one too few.</p>
        <p id="p_02710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02710"> I tend not to use these operators in  while  statements except in very simple cases, because they can get confusing. A better solution is to use the  for  statement, which you'll learn about on  Day  8 ,  "More Control Structures."</p>
        <p id="p_02711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02711"> DON'T  use ++ or — on both sides of a single variable, as in this statement, because it isn't allowed in Perl:</p>
        <p id="p_02712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02712"> ++$varl—;</p>
        <p id="p_02713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02713"> DON'T  use autoincrement or autodecrement on a variable and then use the variable again in the same statement.</p>
        <p id="p_02714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02714"> $varl = 10;</p>
        <p id="p_02715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02715"> $var2 = $varl + ++$varl;</p>
        <p id="p_02716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02716"> Is  $var2  now 20, 21, or 22? It's impossible to tell. Even different versions of Perl can produce different results!</p>
        <p id="p_02717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02717"> Using Autoincrement With Strings</p>
        <p id="p_02718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02718"> If a string value contains only alphabetic characters, the ++ operator can be used to "add one" to a string. In other words, the operator replaces the last character of the string with the next letter of the alphabet. The following is an example:</p>
        <p id="p_02719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02719"> $stringvar = "abc"; $stringvar++;</p>
        <p id="p_02720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02720"> Here, $stringvar now contains abd. Note that this works only with ++, not —:</p>
        <p id="p_02721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02721"> $stringvar = "abc"; $stringvar—;</p>
        <p id="p_02722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02722"> The — operator treats abc as a number, which means that it is equivalent to 0. The resulting value of $stringvar is, therefore, -1.</p>
        <p id="p_02723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02723"> Auto-incrementing strings using ++ also works on capital letters.</p>
        <p id="p_02724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02724"> $stringvar = "aBC";</p>
        <p id="p_02725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02725"> $stringvar++;</p>
        <p id="p_02726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02726"> The value stored  in $stringvar  is  now aBD.</p>
        <p id="p_02727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02727"> If the last letter of the string is  z  or  Z,  ++ converts this letter to  a  or  A,  and then "adds one" to the second-to-last character of the string:</p>
        <p id="p_02728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02728"> $stringvar = "abz";</p>
        <p id="p_02729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02729"> $stringvar++; # $stringvar now contains "aca"</p>
        <p id="p_02730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02730"> $stringvar = "AGZZZ";</p>
        <p id="p_02731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02731"> $stringvar++; # $stringvar now contains "AHAAA"</p>
        <p id="p_02732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02732"> This also works if the string contains one or more trailing digits.</p>
        <p id="p_02733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02733"> $stringvar = "ab4";</p>
        <p id="p_02734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02734"> $stringvar++; # $stringvar now contains "ab5"</p>
        <p id="p_02735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02735"> As in numeric operations, incrementing a string that ends in 9 carries over to the next character of the string. This works regardless of whether the next character is a digit or alphabetic character.</p>
        <p id="p_02736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02736"> $stringvar = "bc999";</p>
        <p id="p_02737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02737"> $stringvar++; # $stringvar now contains "bdOOO"</p>
        <p id="p_02738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02738"> Warning</p>
        <p id="p_02739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02739"> Incrementing string values using ++ works only if the variable has not already been converted to a number.</p>
        <p id="p_02740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02740"> $stringvar = "abc";</p>
        <p id="p_02741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02741"> $stringvar += 5;</p>
        <p id="p_02742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02742"> $stringvar++;</p>
        <p id="p_02743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02743"> Here, the value of  $stringvar  is 6 because  abc  is converted to 0 by the += operator in the second statement.</p>
        <p id="p_02744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02744"> Also note that this does not work if the string value contains any character other than a letter or digit, or if a digit is located in the middle of the string.</p>
        <p id="p_02745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02745"> $stringvar = "ab*c";</p>
        <p id="p_02746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02746"> $stringvar++;</p>
        <p id="p_02747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02747"> $stringvar = "ab5c";</p>
        <p id="p_02748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02748"> $stringvar++;</p>
        <p id="p_02749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02749"> In both of these cases, the value stored in  $stringvar  is converted to its numeric equivalent, 0, before the ++ operation is performed.</p>
        <p id="p_02750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02750"> This means that  $stringvar  is assigned the value 1.</p>
        <p id="p_02751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02751"> The String Concatenation and Repetition Operators</p>
        <p id="p_02752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02752"> So far, the Perl operators you've seen operate only on integers. (To be exact, they can also operate on strings, but they convert the strings to integers first.) Perl also supports the following special operators that manipulate strings:</p>
        <p id="p_02753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02753"> • The . operator, which  concatenates  (joins together) two strings</p>
        <p id="p_02754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02754"> • The x operator, which repeats a string</p>
        <p id="p_02755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02755"> • The . = operator, which combines concatenation and assignment</p>
        <p id="p_02756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02756"> The String-Concatenation Operator</p>
        <p id="p_02757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02757"> The string-concatenation operator, ., joins two strings together. For example, the following statement assigns the string potatohead to $newstring:</p>
        <p id="p_02758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02758"> $newstring = "potato" . "head";</p>
        <p id="p_02759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02759"> You can use the . operator with variables as in this example:</p>
        <p id="p_02760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02760"> $stringl = "potato";</p>
        <p id="p_02761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02761"> $string2 = "head";</p>
        <p id="p_02762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02762"> $newstring = $stringl . $string2;</p>
        <p id="p_02763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02763"> This also assigns potatohead to $newstring. Note that the values of $stringl and $string2 are not changed by the . operator: $stringl still has the value potato, and $string2 still has the value head.</p>
        <p id="p_02764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02764"> The String-Repetition Operator</p>
        <p id="p_02765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02765"> The string-repetition operator, x (literally the letter  x ), makes multiple copies of a string and joins the copies together, as shown in this example:</p>
        <p id="p_02766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02766"> $newstring = "t" x 5;</p>
        <p id="p_02767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02767"> This statement takes five copies of the string t and joins them together, producing the string ttttt. This string is then assigned to the variable $newstring.</p>
        <p id="p_02768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02768"> You can use variables as operands for the x operator, if you like, as follows:</p>
        <p id="p_02769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02769"> $copystring =  "t";</p>
        <p id="p_02770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02770"> $repeats = 5;</p>
        <p id="p_02771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02771"> $newstring = $copystring x $repeats;</p>
        <p id="p_02772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02772"> The only restriction is that the variable on the right of the x must contain an integer or a value that can be converted to an integer.</p>
        <p id="p_02773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02773"> DO make sure you leave a space between the  x  operator and the values or variables on either side:</p>
        <p id="p_02774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02774"> $newstring = $oldstring x 5; # this is correct</p>
        <p id="p_02775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02775"> $newstring = $oldstringx 5; # incorrect $newstring = $oldstring x5; # also incorrect</p>
        <p id="p_02776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02776"> Normally, you don't need to put spaces between an operator and its operands.</p>
        <p id="p_02777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02777"> $x = $x + 1; # this is OK $x=$x+l; # this is also OK</p>
        <p id="p_02778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02778"> You need spaces around the  x  because the letter  x  can appear in variable names. (For example,  $oldst ringx  is a perfectly valid variable name.)</p>
        <p id="p_02779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02779"> Concatenation and Assignment</p>
        <p id="p_02780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02780"> The . = operator combines the operations of string concatenation and assignment. For example, the following statements:</p>
        <p id="p_02781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02781"> $a = "be";</p>
        <p id="p_02782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02782"> $a .= "witched"; # $a is now "bewitched"</p>
        <p id="p_02783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02783"> are equivalent to these statements:</p>
        <p id="p_02784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02784"> $a = "be";</p>
        <p id="p_02785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02785"> $a = $a . "witched";</p>
        <p id="p_02786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02786"> You can use the . = operator to write a very simple program that reads multiple lines of input and joins them into a single string. This program is shown in Listing 4.5.</p>
        <p id="p_02787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02787"> Listing 4.5. A program that reads input lines and concatenates them.</p>
        <p id="p_02788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02788"> 1: #!/usr/local/bin/perl</p>
        <p id="p_02789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02789"> 2:</p>
        <p id="p_02790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02790"> $resultstring =</p>
        <p id="p_02791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02791"> 3: print("Enter your input - type an empty line to quit\n");</p>
        <p id="p_02792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02792"> 4: $input = &lt;STDIN&gt;;</p>
        <p id="p_02793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02793"> 5: chop ($input);</p>
        <p id="p_02794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02794"> 6: while ($input ne "") {</p>
        <p id="p_02795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02795"> 7: $resultstring .= $input;</p>
        <p id="p_02796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02796"> 8: $input = &lt;STDIN&gt;;</p>
        <p id="p_02797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02797"> 9: chop ($input);</p>
        <p id="p_02798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02798"> 10 : }</p>
        <p id="p_02799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02799"> 11: print ("Here is the final string:\n");</p>
        <p id="p_02800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02800"> 12: print ("$resultstring\n");</p>
        <p id="p_02801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02801"> Output</p>
        <p id="p_02802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02802"> $ program4_5</p>
        <p id="p_02803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02803"> Enter your input - type an empty line to quit</p>
        <p id="p_02804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02804"> this</p>
        <p id="p_02805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02805"> is</p>
        <p id="p_02806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02806"> a</p>
        <p id="p_02807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02807"> test</p>
        <p id="p_02808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02808"> Here is the final string: thisisatest $</p>
        <p id="p_02809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02809"> Analysis</p>
        <p id="p_02810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02810"> As you can see from the output of Listing 4.5, the four input lines are joined and have become a single string.</p>
        <p id="p_02811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02811"> Note that there is a much simpler way to do this in Perl: using the built-in function join (). You'll learn about join () on  Day  5,   "Lists and Array Variables."</p>
        <p id="p_02812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02812"> Other Perl Operators</p>
        <p id="p_02813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02813"> Perl also supports two other operators that do not fit into any of the preceding categories:</p>
        <p id="p_02814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02814"> • The comma operator</p>
        <p id="p_02815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02815"> • The conditional operator</p>
        <p id="p_02816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02816"> The Comma Operator</p>
        <p id="p_02817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02817"> The  comma operator  (,) is an operator borrowed from the C programming language. It guarantees that a particular part of an expression (the part before the ,) is evaluated first.</p>
        <p id="p_02818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02818"> Here is an example of a simple statement that uses the , operator:</p>
        <p id="p_02819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02819"> $varl += 1, $var2 = $varl;</p>
        <p id="p_02820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02820"> Because the , operator indicates that the left operand is to be performed first, 1 is added to $varl before $varl is assigned to $var2. In effect, the , operator breaks a statement into two separate statements, as follows:</p>
        <p id="p_02821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02821"> $varl += 1;</p>
        <p id="p_02822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02822"> $var2 = $varl;</p>
        <p id="p_02823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02823"> In fact, the only real reason to use the , operator is when two operations are so closely tied together that it is easier to understand the program if they appear as part of the same expression.</p>
        <p id="p_02824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02824"> The comma operator is often used in conjunction with the = operator, as follows:</p>
        <p id="p_02825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02825"> $val = 26;</p>
        <p id="p_02826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02826"> $result = (++$val, $val + 5);</p>
        <p id="p_02827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02827"> In this statement, the</p>
        <p id="p_02828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02828"> ++$val</p>
        <p id="p_02829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02829"> operation is performed first, because it appears before the , operator. This adds 1 to $val, which means that $val now has the value 27. Then this new value of $val has 5 added to it, and the result, 32, is assigned to $result.</p>
        <p id="p_02830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02830"> Note that the following expression is enclosed in parentheses:</p>
        <p id="p_02831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02831"> ++$val, $val + 5</p>
        <p id="p_02832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02832"> This indicates that this set of operations is to be performed first. Had the parentheses not been present, the statement would have been</p>
        <p id="p_02833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02833"> $result = ++$val, $val + 5;</p>
        <p id="p_02834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02834"> In this case, everything before the comma would be performed first:</p>
        <p id="p_02835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02835"> $result = ++$val</p>
        <p id="p_02836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02836"> This means that $result would be assigned 27, not 32.</p>
        <p id="p_02837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02837"> You'll learn more about parentheses and the order of operations later today, in the section titled "The Order of Operations."</p>
        <p id="p_02838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02838"> The Conditional Operator</p>
        <p id="p_02839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02839"> The conditional operator also is borrowed from the C programming language. Unlike the other operators you've seen, the conditional operator requires three operands, as follows:</p>
        <p id="p_02840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02840"> • A condition to test</p>
        <p id="p_02841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02841"> • A value that is to be used when the test condition is true (evaluates to a nonzero value)</p>
        <p id="p_02842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02842"> • A value that is to be used when the test condition is false (evaluates to zero)</p>
        <p id="p_02843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02843"> The first two operands are separated by the character ?, and the second and third operands are separated by the character :.</p>
        <p id="p_02844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02844"> Here is a simple example of an expression that uses the conditional operator:</p>
        <p id="p_02845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02845"> $result = $var == 0 ? 14 : 7;</p>
        <p id="p_02846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02846"> Here, the test condition is the expression</p>
        <p id="p_02847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02847"> $var == 0</p>
        <p id="p_02848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02848"> If this expression is true, the value 14 is assigned to $result. If it is false, the value 7 is assigned to $result.</p>
        <p id="p_02849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02849"> As you can see, the conditional operator behaves just like the if and else statements. The expression</p>
        <p id="p_02850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02850"> $result = $var == 0 ? 14 : 7;</p>
        <p id="p_02851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02851"> is identical to the following:</p>
        <p id="p_02852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02852"> if ($var == 0) {</p>
        <p id="p_02853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02853"> $result = 14;</p>
        <p id="p_02854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02854"> } else {</p>
        <p id="p_02855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02855"> $result = 7;</p>
        <p id="p_02856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02856"> }</p>
        <p id="p_02857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02857"> The difference between the conditional operator and the if-else construct is that the conditional operator can appear in the middle of expressions. For example, the conditional operator can be used as another way to prevent division by 0, as follows:</p>
        <p id="p_02858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02858"> $result = 43 + ($divisor == 0 ? 0 : $dividend / $divisor);</p>
        <p id="p_02859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02859"> Here, $result is assigned the value 43 plus the result of $dividend divided by $divisor, unless $divisor is 0. If $divisor is 0, the result of the division is assumed to be 0, and $result is assigned 43.</p>
        <p id="p_02860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02860"> Listing 4.6 is a simple program that reads from the standard input file and compares the input line with a predetermined password.</p>
        <p id="p_02861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02861"> Type</p>
        <p id="p_02862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02862"> Listing 4.6. A very simple password checker.</p>
        <p id="p_02863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02863"> Output</p>
        <p id="p_02864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02864"> $ program4_6</p>
        <p id="p_02865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02865"> Enter the secret password: orioles</p>
        <p id="p_02866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02866"> No, that is not the correct password.</p>
        <p id="p_02867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02867"> $</p>
        <p id="p_02868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02868"> Analysis</p>
        <p id="p_02869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02869"> I When you run program4_ the Input-Output example.</p>
        <p id="p_02870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02870"> .6 and type in a random password, you get the results shown in</p>
        <p id="p_02871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02871"> The advantage of using the conditional operator here is that the assignment to $outputline occurs in only one place, and the statement is much more concise. If you use if and else, you need two assignments to $ output line and five lines, as follows:</p>
        <p id="p_02872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02872"> if ($inputline eq $password) {</p>
        <p id="p_02873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02873"> $outputline = "Yes, that is the correct password!\n";</p>
        <p id="p_02874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02874"> } else {</p>
        <p id="p_02875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02875"> $outputline = "No, that is not the correct password.\n");</p>
        <p id="p_02876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02876"> }</p>
        <p id="p_02877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02877"> Of course, the if and else statements are easier to use when things get more complex. Consider the following example:</p>
        <p id="p_02878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02878"> if ($varl == 47) {</p>
        <p id="p_02879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02879"> print("varl is already  47\n");</p>
        <p id="p_02880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02880"> $is_fortyseven = 1;</p>
        <p id="p_02881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02881"> } else {</p>
        <p id="p_02882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02882"> $varl = 47;</p>
        <p id="p_02883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02883"> print("varl set to  47\n");</p>
        <p id="p_02884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02884"> $is_fortyseven = 0;</p>
        <p id="p_02885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02885"> }</p>
        <p id="p_02886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02886"> You can write this using the conditional operator if you use the comma operator, as follows:</p>
        <p id="p_02887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02887"> $varl == 47 ? (print("varl is already  47\n"),  $is_fortyseven = 1) :</p>
        <p id="p_02888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02888"> ($varl = 47, print("varl set to  47\n"),  $is_fortyseven =</p>
        <p id="p_02889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02889"> 0 );</p>
        <p id="p_02890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02890"> As you can see, this is difficult to understand. The basic rules are as follows:</p>
        <p id="p_02891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02891"> • Use the conditional operator for very simple conditional statements.</p>
        <p id="p_02892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02892"> • Use if and else for everything else.</p>
        <p id="p_02893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02893"> Conditional Operators on the Left Side of Assignments</p>
        <p id="p_02894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02894"> In Perl 5, you can use the conditional operator on the left side of an assignment. This enables you to assign a value to either of two variables, depending on the result of a conditional expression.</p>
        <p id="p_02895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02895"> $condvar == 43 ? $varl : $var2 = 14;</p>
        <p id="p_02896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02896"> This statement checks whether $condvar has the value 43. If it does, $varl is assigned 14. If it</p>
        <p id="p_02897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02897"> doesn't, $var2 is assigned 14.</p>
        <p id="p_02898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02898"> Normally, you won't want to use conditional operators in this way because your code will become difficult to follow. Although the following code is a little less efficient, it performs the same task in a way that is easier to understand:</p>
        <p id="p_02899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02899"> $condvar == 43 ? $varl = 14 : $var2 = 14;</p>
        <p id="p_02900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02900"> The Order of Operations</p>
        <p id="p_02901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02901"> Perl, like all programming languages, has a clearly defined set of rules that determine which operations are to be performed first in a particular expression. The following three concepts help explain these rules:</p>
        <p id="p_02902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02902"> • The concept of  precedence</p>
        <p id="p_02903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02903"> • The concept of  associativity</p>
        <p id="p_02904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02904"> • The ability to override precedence and associativity using  parentheses</p>
        <p id="p_02905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02905"> Precedence</p>
        <p id="p_02906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02906"> In grade school, you learned that certain arithmetic operations always are performed before other ones. For example, multiplication and division always are performed before addition and subtraction.</p>
        <p id="p_02907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02907"> 4 + 5*3</p>
        <p id="p_02908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02908"> Here, the multiplication is performed first, even though the addition is encountered first when the statement is read from left to right. Because multiplication always is performed first, it has higher precedence  than addition.</p>
        <p id="p_02909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02909"> Table 4.6 defines the precedence of the operators in Perl. The items at the top of the table have the highest precedence, and the items at the bottom have the lowest.</p>
        <p id="p_02910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02910"> Table 4.6. Operator precedence.</p>
        <p id="p_02911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02911"> Using this table, you can determine the order of operations in complicated expressions. For example:</p>
        <p id="p_02912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02912"> $result = 11 * 2 + 6 ** 2 « 2;</p>
        <p id="p_02913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02913"> To determine the order of operations in this expression, start at the top of Table 4.6 and work down. The first operator you see is **, which means that it is performed first, leaving</p>
        <p id="p_02914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02914"> $result = 11 * 2 + 36 &lt;&lt; 2;</p>
        <p id="p_02915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02915"> The next operation you find in the table is the * operator. Performing the multiplication leaves the following:</p>
        <p id="p_02916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02916"> $result = 22 + 36 « 2;</p>
        <p id="p_02917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02917"> The + operator is next:</p>
        <p id="p_02918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02918"> $result = 58 « 2;</p>
        <p id="p_02919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02919"> Next up is the « operator:</p>
        <p id="p_02920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02920"> $result = 232;</p>
        <p id="p_02921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02921"> The = operator is last on the list and assigns 232 to $result.</p>
        <p id="p_02922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02922"> You might have noticed that Table 4.6 contains some operators that you've not yet seen and which you'll learn about later:</p>
        <p id="p_02923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02923"> • The list-range operator, defined on  Day 5</p>
        <p id="p_02924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02924"> • The file-status operators, defined on  Day  6 ,  "Reading from and Writing to Files"</p>
        <p id="p_02925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02925"> • The pattern-matching operators, =~ and ! ~, defined on  Day  7,   "Pattern Matching"</p>
        <p id="p_02926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02926"> Associativity</p>
        <p id="p_02927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02927"> The rules of operator precedence enable you to determine which operation to perform first when an expression contains different operators. But what should you do when an expression contains two or more operators that have the same precedence?</p>
        <p id="p_02928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02928"> In some cases, it doesn't matter what order you perform the operations in. For example:</p>
        <p id="p_02929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02929"> $result  =4+5+3;</p>
        <p id="p_02930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02930"> Here, $result gets 12 no matter which addition is performed first. However, for some operations the order of evaluation matters.</p>
        <p id="p_02931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02931"> $result = 2 ** 3 ** 2;</p>
        <p id="p_02932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02932"> If you perform the leftmost exponentiation first, $result is assigned 8 ** 2, or 64. If you perform the rightmost exponentiation first, $result is assigned 2 ** 9, or 512.</p>
        <p id="p_02933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02933"> Because the order of operations is sometimes important, Perl defines the order in which operations of the same precedence are to be performed. Operations that are performed right-to-left (with the rightmost operation performed first) are said to be  right associative.  Operations that are performed left-to-right (with the leftmost operation performed first) are  left associative.</p>
        <p id="p_02934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02934"> Table 4.7 lists the associativity for each of the Perl operators. The operators are sorted according to precedence (in the same order as Table 4.6).</p>
        <p id="p_02935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02935"> Table 4.7. Operator associativity.</p>
        <p id="p_02936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02936"> From Table 4.7, you see that the exponentiation operator is right associative. This means that in the following:</p>
        <p id="p_02937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02937"> $result = 2 ** 3 ** 2;</p>
        <p id="p_02938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02938"> $result is assigned 512, because the rightmost ** operation is performed first.</p>
        <p id="p_02939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02939"> Forcing Precedence Using Parentheses</p>
        <p id="p_02940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02940"> Perl enables you to force the order of evaluation of operations in expressions. To do this, use parentheses as follows:</p>
        <p id="p_02941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02941"> $result = 4 * (5 + 3) ;</p>
        <p id="p_02942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02942"> In this statement, 5 is added to 3 and then multiplied by 4, yielding 32. You can use as many sets of parentheses as you like:</p>
        <p id="p_02943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02943"> $result = 4 ** (5 % (8 -  6));</p>
        <p id="p_02944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02944"> Here, the result is 4:</p>
        <p id="p_02945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02945"> • 8 - 6 is performed, leaving 4 * * (5 % 2)</p>
        <p id="p_02946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02946"> • 5 % 2 is performed, leaving  4**1 . 4 **  lis4</p>
        <p id="p_02947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02947"> Don't</p>
        <p id="p_02948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02948"> DO  use parentheses whenever you aren't sure whether a particular operation is to be evaluated first. For example, I don't know many programmers who remember that addition operators are evaluated before shifts:</p>
        <p id="p_02949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02949"> $result = 4 &lt;&lt; 2 + 3;</p>
        <p id="p_02950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02950"> And virtually no one remembers that &amp; &amp; has higher precedence than | | :</p>
        <p id="p_02951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02951"> if ($value == 0 || $value == 2 &amp;&amp; $value2 == "hello") {</p>
        <p id="p_02952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02952"> print("my condition is true\n");</p>
        <p id="p_02953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02953"> }</p>
        <p id="p_02954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02954"> You can make life a lot easier for people who read your code if you use parentheses when the order of evaluation is not obvious. For example:</p>
        <p id="p_02955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02955"> $result = 4 &lt;&lt;  (2+3);</p>
        <p id="p_02956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02956"> if ($value == 0 J1 ($value == 2 &amp;&amp; $value2 == "hello")) { print("my condition is true\n");</p>
        <p id="p_02957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02957"> }</p>
        <p id="p_02958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02958"> DO  use multiple lines, extra spaces, and indentation to make complicated expressions easier to read. For example:</p>
        <p id="p_02959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02959"> if ($value == 0 ||</p>
        <p id="p_02960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02960"> ($value == 2 &amp;&amp; $value2 == "hello")) {</p>
        <p id="p_02961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02961"> Here, it's obvious that there are two main conditions to be tested and that one of them contains a pair of subconditions.</p>
        <p id="p_02962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02962"> DON'T  leave out closing parentheses by mistake.</p>
        <p id="p_02963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02963"> $result = 4 + (2 &lt;&lt; ($value / 2) ; # error</p>
        <p id="p_02964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02964"> This statement will be flagged as erroneous because you are missing a closing parenthesis.</p>
        <p id="p_02965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02965"> A handy way of checking whether you have enough parentheses in complicated expressions is to use this simple trick:</p>
        <p id="p_02966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02966"> • Start at the left end of your expression.</p>
        <p id="p_02967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02967"> • Starting from 0, add 1 for every left parenthesis you see.</p>
        <p id="p_02968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02968"> • Subtract 1 for every closing parenthesis you see.</p>
        <p id="p_02969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02969"> If your final result is 0, you've got enough opening and closing parentheses. (This doesn't guarantee that you've put the parentheses in the right places, but at least you now know that you have enough of them.)</p>
        <p id="p_02970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02970"> Summary</p>
        <p id="p_02971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02971"> Today you learned about the operators that Perl supports. Each operator requires one or more operands, which are the values on which the operator operates. A collection of operands and operators is known as an expression.</p>
        <p id="p_02972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02972"> The operators you learned how to use are as follows:</p>
        <p id="p_02973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02973"> • The arithmetic operators and unary negation</p>
        <p id="p_02974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02974"> • The integer-comparison operators ==, ! =, &lt;, &gt;, &lt;=, &gt;=, and &lt;=&gt;</p>
        <p id="p_02975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02975"> • The string-comparison operators eq, ne, It, gt, le, ge, and cmp</p>
        <p id="p_02976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02976"> • The logical operators | |,&amp;&amp;, and !</p>
        <p id="p_02977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02977"> • The bit-manipulation operators |, &amp;,  A , ~, «, and &gt;&gt;</p>
        <p id="p_02978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02978"> • The assignment operators =, +=, -=, *=,/=,%=,**=, !=,&amp;=,  A =, and . =</p>
        <p id="p_02979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02979"> • The autoincrement operator ++</p>
        <p id="p_02980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02980"> • The autodecrement operator —</p>
        <p id="p_02981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02981"> • The string-concatenation operator .</p>
        <p id="p_02982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02982"> • The string-repetition operator x</p>
        <p id="p_02983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02983"> • The comma operator ,</p>
        <p id="p_02984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02984"> • The conditional operator (? and : together)</p>
        <p id="p_02985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02985"> You also learned about operator precedence and associativity, two concepts that tell you which operators in an expression usually are performed first. Operator precedence and associativity can be controlled by putting parentheses around the operations you want to perform first.</p>
        <p id="p_02986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02986"> Q&amp;A</p>
        <p id="p_02987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02987"> Q: Is there a limit on how large my expressions can be?</p>
        <p id="p_02988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02988"> A: Effectively, no. There is a limit, but it's so large that no one would possibly want to create an</p>
        <p id="p_02989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02989"> expression that long, because it would be impossible to read or understand.***It's easier to understand expressions if they are shorter.</p>
        <p id="p_02990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02990"> Q: Is it better to use += or ++ when adding 1 to a variable?</p>
        <p id="p_02991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02991"> A: It's best to use ++ when using a variable as a counter in a  while  statement (or in other loops,</p>
        <p id="p_02992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02992"> which you learn about on  Day  8 ,  "More Control Structures"). For other addition operations, you should use +=.</p>
        <p id="p_02993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02993"> Q: Why are some operators left associative and others right associative?</p>
        <p id="p_02994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02994"> A: Most operators are left associative, because we normally read from left to right.</p>
        <p id="p_02995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02995"> Assignment is right associative because it's easier to read. For instance:</p>
        <p id="p_02996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02996"> $varl = $var2 = 5;</p>
        <p id="p_02997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02997"> If assignment happened to be left associative,  $var 1  would be assigned the old value of  $var2, not 5. This would not be obvious to a casual reader of the program.Exponentiation is right associative because that's how exponentiation is performed in mathematics.Other operators that are right associative are easier to read from right to left.</p>
        <p id="p_02998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02998"> Workshop</p>
        <p id="p_02999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_02999"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_03000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03000"> Quiz</p>
        <p id="p_03001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03001"> 1. Define the following terms:</p>
        <p id="p_03002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03002"> a. operator</p>
        <p id="p_03003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03003"> b. operand</p>
        <p id="p_03004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03004"> c. expression</p>
        <p id="p_03005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03005"> d. precedence</p>
        <p id="p_03006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03006"> e. associativity</p>
        <p id="p_03007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03007"> 2.  What operations are performed by the following operators?</p>
        <p id="p_03008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03008"> a. &amp; &amp;</p>
        <p id="p_03009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03009"> b. &amp;</p>
        <p id="p_03010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03010"> c.  A</p>
        <p id="p_03011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03011"> d. ne</p>
        <p id="p_03012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03012"> e. .</p>
        <p id="p_03013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03013"> 3.  What operators perform the following operations?</p>
        <p id="p_03014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03014"> a. string-equality comparison</p>
        <p id="p_03015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03015"> b. remainder</p>
        <p id="p_03016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03016"> c. string duplication</p>
        <p id="p_03017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03017"> d. bitwise OR</p>
        <p id="p_03018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03018"> e. numeric greater-than-or-equal-to</p>
        <p id="p_03019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03019"> 4. What is the binary representation of the following numbers?</p>
        <p id="p_03020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03020"> a. 171</p>
        <p id="p_03021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03021"> b. 1105</p>
        <p id="p_03022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03022"> c. 0</p>
        <p id="p_03023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03023"> 5.  What is the standard (base-10) representation of the following numbers?</p>
        <p id="p_03024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03024"> a. 01100100</p>
        <p id="p_03025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03025"> b. 00001111</p>
        <p id="p_03026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03026"> c. 01000001</p>
        <p id="p_03027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03027"> 6. What is the value of the following expressions?</p>
        <p id="p_03028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03028"> a . 17 * 2 ** 3 / 9 % 2 « 2</p>
        <p id="p_03029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03029"> b. 0 &amp;&amp; (171567 * 98275 / 1174.5 ** 4)</p>
        <p id="p_03030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03030"> c. 1171  A  904</p>
        <p id="p_03031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03031"> d. "abc" . "de" x 2</p>
        <p id="p_03032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03032"> Exercises</p>
        <p id="p_03033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03033"> 1. Write a program that uses the &lt;&lt; operator to print out the first 16 powers of 2.</p>
        <p id="p_03034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03034"> 2. Rewrite the following statement using the conditional operator:</p>
        <p id="p_03035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03035"> if ($varl == 5 || $var2 == 7) {</p>
        <p id="p_03036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03036"> $result = $varl * $var2 +  16.5;</p>
        <p id="p_03037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03037"> } else {</p>
        <p id="p_03038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03038"> print("condition is  false\n");</p>
        <p id="p_03039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03039"> $result = 0;</p>
        <p id="p_03040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03040"> }</p>
        <p id="p_03041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03041"> 3. Rewrite the following expression using the if and else statements:</p>
        <p id="p_03042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03042"> $result = $varl &lt;= 26 ? ++$var2 : 0;</p>
        <p id="p_03043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03043"> 4. Write a program that reads two integers from standard input (one at a time), divides the first one by the second one, and prints out the quotient (the result) and the remainder.</p>
        <p id="p_03044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03044"> 5. Why might the following statement not assign the value 5.1 to $result?</p>
        <p id="p_03045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03045"> $result = 5.1 +  100005.2 - 100005.2;</p>
        <p id="p_03046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03046"> 6. Determine the order of operations in the following statement, and add parentheses to the statement to indicate this order:</p>
        <p id="p_03047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03047"> $result = $varl * 2 « 5 + 3 || $var2 ** 3, $var3;</p>
        <p id="p_03048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03048"> 7. What value is assigned to $result by the following code?</p>
        <p id="p_03049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03049"> $varl = 43;</p>
        <p id="p_03050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03050"> $var2 = 16;</p>
        <p id="p_03051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03051"> $result = ++$var2 == 17 ? $varl++ * 2 - 5 : ++$varl * 3 -11;</p>
        <p id="p_03052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03052"> 8. BUG BUSTER: Find and fix the bugs in the following program:</p>
        <p id="p_03053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03053"> #!/usr/local/bin/perl</p>
        <p id="p_03054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03054"> $num = &lt;STDIN&gt;; chop ($num);</p>
        <p id="p_03055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03055"> $x = "";</p>
        <p id="p_03056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03056"> $x += "hello";</p>
        <p id="p_03057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03057"> if ($x != "goodbye" | $x == "farewell") {</p>
        <p id="p_03058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03058"> $result = $num eq 0 ? 43;</p>
        <p id="p_03059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03059"> } else {</p>
        <p id="p_03060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03060"> $result = ++$num++;</p>
        <p id="p_03061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03061"> }</p>
        <p id="p_03062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03062"> print  ("the  result is  $result\n");</p>
        <p id="p_03063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03063"> Chapter 5</p>
        <p id="p_03064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03064"> Lists and Array Variables</p>
        <p id="p_03065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03065"> CONTENTS</p>
        <p id="p_03066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03066"> •  Introducing Lists</p>
        <p id="p_03067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03067"> •  Scalar Variables and Lists</p>
        <p id="p_03068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03068"> o  Lists and String Substitution</p>
        <p id="p_03069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03069"> •  Storing Lists in Array Variables</p>
        <p id="p_03070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03070"> •  Accessing an Element of an Array Variable</p>
        <p id="p_03071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03071"> o  More Details on Array Element Names</p>
        <p id="p_03072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03072"> •  Using Lists and Arrays in Perl Programs</p>
        <p id="p_03073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03073"> o  Using Brackets and Substituting for Variables</p>
        <p id="p_03074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03074"> •  Using List Ranges</p>
        <p id="p_03075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03075"> o  Expressions and List Ranges</p>
        <p id="p_03076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03076"> •  More on Assignment and Array Variables</p>
        <p id="p_03077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03077"> o  Copying from One Array Variable to Another</p>
        <p id="p_03078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03078"> o  Using Array Variables in Lists o  Substituting for Array Variables in Strings o  Assigning to Scalar Variables from Array Variables</p>
        <p id="p_03079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03079"> •  Retrieving the Length of a List</p>
        <p id="p_03080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03080"> •  Using Array Slices</p>
        <p id="p_03081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03081"> o  Using List Ranges in Array-Slice Subscripts</p>
        <p id="p_03082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03082"> o  Using Variables in Array-Slice Subscripts</p>
        <p id="p_03083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03083"> o  Assigning to Array Slices</p>
        <p id="p_03084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03084"> o  Overlapping Array Slices o  Using the Array-Slice Notation as a Shorthand</p>
        <p id="p_03085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03085"> •  Reading an Array from the Standard Input File</p>
        <p id="p_03086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03086"> •  Array Library Functions</p>
        <p id="p_03087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03087"> o  Sorting a List or Array Variable o  Reversing a List or Array Variable</p>
        <p id="p_03088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03088"> o  Using  chop  on Array Variables o  Creating a Single String from a List</p>
        <p id="p_03089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03089"> o  Splitting a String into a List o  Other List-Manipulation Functions</p>
        <p id="p_03090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03090"> •  Summary</p>
        <p id="p_03091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03091"> Q&amp;A</p>
        <p id="p_03092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03092"> •  Workshop o  Quiz o  Exercises</p>
        <p id="p_03093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03093"> The Perl programs you have seen so far deal with  scalar values,  which are single units of data, and scalar variables,  which can store one piece of information.</p>
        <p id="p_03094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03094"> Perl also enables you to define an ordered collection of values, known as a  list,  this collection of values can be stored in variables known as  array variables.</p>
        <p id="p_03095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03095"> Today's lesson describes lists and array variables, and it shows you what you can do with them. Today, you learn about the following:</p>
        <p id="p_03096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03096"> • What lists are</p>
        <p id="p_03097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03097"> • The relationship between scalar variables and lists</p>
        <p id="p_03098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03098"> • Storing lists in array variables</p>
        <p id="p_03099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03099"> • Accessing an element of an array variable or list</p>
        <p id="p_03100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03100"> • How to use list ranges</p>
        <p id="p_03101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03101"> • Assigning to array variables</p>
        <p id="p_03102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03102"> • Assigning to scalar variables from array variables</p>
        <p id="p_03103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03103"> • Retrieving the length of a list</p>
        <p id="p_03104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03104"> • Using array slices</p>
        <p id="p_03105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03105"> • Using an array to store input</p>
        <p id="p_03106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03106"> • Sorting a list or array variable</p>
        <p id="p_03107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03107"> • Reversing a list or array variable</p>
        <p id="p_03108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03108"> • Creating a string from a list</p>
        <p id="p_03109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03109"> • Creating a list from a string</p>
        <p id="p_03110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03110"> Introducing Lists</p>
        <p id="p_03111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03111"> A  list  is a sequence of scalar values enclosed in parentheses. The following is a simple example of a list:</p>
        <p id="p_03112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03112"> (1,  5.3,  "hello", 2)</p>
        <p id="p_03113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03113"> This list contains four elements, each of which is a scalar value: the numbers 1 and  5.3,  the string hello,  and the number  2.</p>
        <p id="p_03114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03114"> Lists can be as long as needed, and they can contain any scalar value. A list can have no elements at all, as follows:</p>
        <p id="p_03115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03115"> 0</p>
        <p id="p_03116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03116"> This list also is called an  empty list.</p>
        <p id="p_03117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03117"> NOTE</p>
        <p id="p_03118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03118"> A list with one element and a scalar value are different entities. For example, the list</p>
        <p id="p_03119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03119"> (43.2)</p>
        <p id="p_03120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03120"> and the scalar value</p>
        <p id="p_03121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03121"> 43.2</p>
        <p id="p_03122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03122"> are not the same thing. This is not a severe limitation because one can be converted to or assigned to the other. See the section titled "Assigning to Scalar Variables from Array Variables" later today.</p>
        <p id="p_03123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03123"> Scalar Variables and Lists</p>
        <p id="p_03124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03124"> A scalar variable name can always be included as part of a list. In this case, the current value of the scalar variable becomes the list element value. For example:</p>
        <p id="p_03125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03125"> (17, $var, "a string")</p>
        <p id="p_03126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03126"> If  $var  has been assigned the value  2 6,  the second element of the list becomes  2 6.  (It remains  2 6 even if a different value is assigned to  $var.)</p>
        <p id="p_03127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03127"> Similarly, you can use the value of an expression as an element of a list. For example:</p>
        <p id="p_03128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03128"> (17, 26 &lt;&lt; 2)</p>
        <p id="p_03129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03129"> This list contains two elements: 17 and 104 (which is 26 left-shifted two places). Expressions in lists, like other expressions, can contain scalar variables.</p>
        <p id="p_03130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03130"> (17, $varl + $var2)</p>
        <p id="p_03131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03131"> Here, the expression  $varl + $var2  is evaluated and its value becomes the second element of the list.</p>
        <p id="p_03132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03132"> Lists and String Substitution</p>
        <p id="p_03133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03133"> Because character strings are scalar values, they can be used in lists, as follows:</p>
        <p id="p_03134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03134"> ("my string", 24.3, "another string")</p>
        <p id="p_03135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03135"> You can substitute for scalar variable names in character strings in lists, as follows:</p>
        <p id="p_03136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03136"> ($value, "The answer is $value")</p>
        <p id="p_03137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03137"> This list contains two elements: the value of the scalar variable $  value,  and a string containing the name of $  value.  If the current value of $  value  is  2 6,  the two elements of the list are 26 and  The</p>
        <p id="p_03138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03138"> answer is 26.</p>
        <p id="p_03139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03139"> Storing Lists in Array Variables</p>
        <p id="p_03140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03140"> Perl enables you to store lists in special variables designed for that purpose. These variables are called array variables  (or  arrays  for short).</p>
        <p id="p_03141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03141"> The following is an example of a list being assigned to an array variable:</p>
        <p id="p_03142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03142"> @array = (1, 2, 3) ;</p>
        <p id="p_03143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03143"> Here, the list ( 1 ,  2 ,  3 ) is assigned to the array variable @ array.</p>
        <p id="p_03144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03144"> Note that the name of the array variable starts with the character @. This enables Perl to distinguish array variables from other kinds of variables-for example, scalar variables, which start with the character $. As with scalar variables, the second character of the variable name must be a letter, while subsequent characters of the name can be letters, numbers, or underscores. Array variable names can be as long as you want.</p>
        <p id="p_03145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03145"> The following are legal array-variable names:</p>
        <p id="p_03146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03146"> @my_array</p>
        <p id="p_03147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03147"> @list2</p>
        <p id="p_03148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03148"> @a_very_long_array_name_with_lots_of_underscores</p>
        <p id="p_03149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03149"> The following are not legal array-variable names:</p>
        <p id="p_03150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03150"> @larray @_array @a.new.array</p>
        <p id="p_03151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03151"> # can't start with a number</p>
        <p id="p_03152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03152"> # can't start with an underscore</p>
        <p id="p_03153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03153"> # . is not a legal variable-name character</p>
        <p id="p_03154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03154"> When an array variable is first created (that is, seen for the first time), it is assumed to contain the empty list () unless it is assigned to.</p>
        <p id="p_03155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03155"> NOTE</p>
        <p id="p_03156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03156"> Because Perl uses @ and $ to distinguish array variables from scalar variables, the same name can be used in an array variable and in a scalar variable. For example:</p>
        <p id="p_03157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03157"> $var = 1;</p>
        <p id="p_03158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03158"> @var = (11, 27.1, "a string");</p>
        <p id="p_03159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03159"> Here, the name  var  is used in both the scalar variable  $var  and the array variable  @var.  These are two completely separate variables.</p>
        <p id="p_03160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03160"> Normally, you won't want to use the same name in both an array and a scalar variable, because this is confusing.</p>
        <p id="p_03161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03161"> Accessing an Element of an Array Variable</p>
        <p id="p_03162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03162"> After you have assigned a list to an array variable, you can refer to any element of the array variable as if it is a scalar variable.</p>
        <p id="p_03163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03163"> For example, to assign the first element of the array variable @  array  to the scalar variable $ scalar,  use the following statement:</p>
        <p id="p_03164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03164"> $scalar = $array[0];</p>
        <p id="p_03165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03165"> The character sequence [ 0 ] is an example of a  subscript.  A subscript indicates a particular element of an array. In this case, 0 refers to the first element of the array. Similarly, the subscript 1 refers to the second element of the array, as follows:</p>
        <p id="p_03166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03166"> $scalar = $array[1];</p>
        <p id="p_03167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03167"> Here, the second element of the array  @array  is assigned to $  scalar.  The general rule is this:</p>
        <p id="p_03168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03168"> An array subscript  n,  where  n  is any non-negative integer, always refers to array element  n+1.</p>
        <p id="p_03169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03169"> This notation is employed to ensure compatibility with the C programming language, which also starts its array subscripting with 0.</p>
        <p id="p_03170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03170"> You can assign a scalar value to an individual array element in the same way:</p>
        <p id="p_03171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03171"> @array = (1, 2, 3, 4) ;</p>
        <p id="p_03172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03172"> $array[3] = 5;</p>
        <p id="p_03173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03173"> After the second assignment, the value of @ array  becomes</p>
        <p id="p_03174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03174"> (1, 2, 3, 5)</p>
        <p id="p_03175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03175"> This is because the fourth element of the array has been replaced.</p>
        <p id="p_03176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03176"> |note</p>
        <p id="p_03177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03177"> If you try to access an array element that does not exist, the Perl interpreter uses the null string (which is equivalent to zero).</p>
        <p id="p_03178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03178"> @array = (1, 2, 3, 4);</p>
        <p id="p_03179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03179"> $scalar = $array[4];</p>
        <p id="p_03180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03180"> Here, $  array [ 4  ] refers to the fifth element of @  array,  which does not exist. In this case, $  scalar  is assigned the null string.</p>
        <p id="p_03181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03181"> NOTE</p>
        <p id="p_03182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03182"> The same thing happens when the subscript is a negative number, as follows:</p>
        <p id="p_03183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03183"> $scalar = $array[-1];</p>
        <p id="p_03184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03184"> Once again, the null string is assigned to  $scalar.</p>
        <p id="p_03185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03185"> Note also that arrays automatically grow when a previously unreferenced element is assigned to for the first time:</p>
        <p id="p_03186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03186"> @array = (1, 2, 3, 4) ;</p>
        <p id="p_03187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03187"> $array[6] = 17;</p>
        <p id="p_03188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03188"> Because the seventh element of @ array  is assigned 17, the value of @ array  is now</p>
        <p id="p_03189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03189"> (1, 2, 3, 4, 17)</p>
        <p id="p_03190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03190"> The missing fifth and sixth elements now contain the null string.</p>
        <p id="p_03191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03191"> You can use the value of a scalar variable as a subscript, as follows:</p>
        <p id="p_03192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03192"> $index = 1;</p>
        <p id="p_03193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03193"> $scalar = $array[$index];</p>
        <p id="p_03194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03194"> Here, the value of $  index,   1 ,  becomes the subscript. This means that the second element of @array  is assigned to  $scalar.</p>
        <p id="p_03195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03195"> When you use a scalar variable as a subscript, make sure that the value stored in the scalar variable corresponds to an array element that exists. For example:</p>
        <p id="p_03196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03196"> @array = (1, 2, 3, 4) ;</p>
        <p id="p_03197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03197"> $index = 4;</p>
        <p id="p_03198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03198"> $scalar = $array[$index];</p>
        <p id="p_03199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03199"> Here, the third statement tries to access the fifth element of @array,  which does not exist. In this case, $  scalar  is assigned the null string, and the Perl interpreter doesn't tell you that anything went wrong.</p>
        <p id="p_03200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03200"> More Details on Array Element Names</p>
        <p id="p_03201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03201"> Note that the first character of an array-element variable name is the $ character, not the @ character. For example, to refer to the first element of the array  ©potato,  use</p>
        <p id="p_03202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03202"> $potato [ 0]</p>
        <p id="p_03203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03203"> and not</p>
        <p id="p_03204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03204"> ©potato [ 0]</p>
        <p id="p_03205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03205"> The basic rule is as follows:</p>
        <p id="p_03206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03206"> Things that reference one value-such as scalar variables and array elements-must start with a $.</p>
        <p id="p_03207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03207"> NOTE</p>
        <p id="p_03208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03208"> Even though references to elements of array variables start with a $, the Perl interpreter still has no trouble distinguishing scalar variables from array-variable elements. For example, if you have defined a scalar variable $  potato  and an array variable @potato,  the Perl interpreter uses the subscript to distinguish between the scalar variable and the array-variable element.</p>
        <p id="p_03209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03209"> $result = $potato; # the scalar variable $potato</p>
        <p id="p_03210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03210"> $result = $potato[0]; # the first element of @potato</p>
        <p id="p_03211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03211"> Using Lists and Arrays in Perl Programs</p>
        <p id="p_03212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03212"> Now that you have seen how lists and array variables work, it's time to take a look at a simple program that uses them. Listing 5.1 is a simple program that prints the elements of a list.</p>
        <p id="p_03213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03213"> Listing 5.1. A program that prints the elements of a list.</p>
        <p id="p_03214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03214"> }</p>
        <p id="p_03215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03215"> $ program5_l element 1 is element 2 is element 3 is element 4 is element 5 is</p>
        <p id="p_03216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03216"> 1</p>
        <p id="p_03217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03217"> chicken</p>
        <p id="p_03218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03218"> 1.23</p>
        <p id="p_03219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03219"> "Having fun?" 9.3300000000000005+e23</p>
        <p id="p_03220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03220"> $</p>
        <p id="p_03221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03221"> Analysis</p>
        <p id="p_03222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03222"> Line  3  assigns a list containing five elements to the array variable @ array.</p>
        <p id="p_03223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03223"> Line 5 tests whether $  count  is less than or equal to 5. This conditional expression ensures that the while  statement loops five times.</p>
        <p id="p_03224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03224"> Line  6  prints the current value of $  count  and the corresponding element of @  array.  Note that the expression used in the subscript is  $count-l,  not  $count,  because subscripting starts from 0. For example, when  count  is 3, the subscript is 2, which means that the third element of @ array  is printed.</p>
        <p id="p_03225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03225"> When you examine line 6, you see that Perl lets you substitute for array elements in character strings. When the Perl interpreter sees  $array[$count-l]  in the character string, it replaces this array element name with its corresponding value.</p>
        <p id="p_03226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03226"> Listing 5.2 is another example of a program that uses arrays. This one is a little more interesting; it uses the built-in functions  rand  and  int  to generate random integers between 1 and 10.</p>
        <p id="p_03227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03227"> Type</p>
        <p id="p_03228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03228"> Listing 5.2. A program that generates random integers between 1 and 10.</p>
        <p id="p_03229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03229"> 1:  #!/usr/local/bin/perl</p>
        <p id="p_03230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03230"> 3: # collect the random numbers</p>
        <p id="p_03231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03231"> 4: $count = 1;</p>
        <p id="p_03232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03232"> 5: while ($count &lt;= 100) {</p>
        <p id="p_03233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03233"> 6: $randnum = int( rand(10) ) + 1;</p>
        <p id="p_03234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03234"> 7: $randtotal[$randnum] += 1;</p>
        <p id="p_03235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03235"> 8: $count++;</p>
        <p id="p_03236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03236"> 9: }</p>
        <p id="p_03237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03237"> 10  : 11 : 12 :</p>
        <p id="p_03238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03238"> 13 :</p>
        <p id="p_03239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03239"> 14 :</p>
        <p id="p_03240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03240"> 15 : 16: 17 :</p>
        <p id="p_03241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03241"> # print the total of each number $count = 1;</p>
        <p id="p_03242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03242"> print ("Total for each number:\n"); while ($count &lt;= 10) {</p>
        <p id="p_03243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03243"> print ("\tnumber $count: $randtotal[$count]\n"); $count++;</p>
        <p id="p_03244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03244"> Output</p>
        <p id="p_03245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03245"> $ program5_2</p>
        <p id="p_03246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03246"> Total for each number:</p>
        <p id="p_03247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03247"> number 1: 11</p>
        <p id="p_03248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03248"> number 2: 8</p>
        <p id="p_03249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03249"> number 3: 13</p>
        <p id="p_03250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03250"> number 4: 6</p>
        <p id="p_03251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03251"> Analysis</p>
        <p id="p_03252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03252"> This program is divided into two parts: second part prints them.</p>
        <p id="p_03253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03253"> the first part collects the random numbers, and the</p>
        <p id="p_03254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03254"> Line 5 ensures that the loop  iterates  (is performed) 100 times. You can just as easily have the program generate any other quantity of random numbers just by changing the value in this conditional expression.</p>
        <p id="p_03255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03255"> Line 6 generates a random number between 1 and 10 and assigns it to the scalar variable  $randnum. To see how it does this, first note that the code fragment</p>
        <p id="p_03256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03256"> int ( rand (10) )</p>
        <p id="p_03257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03257"> actually is two function calls, one inside another. When the Perl interpreter sees this, it first calls the inner one, which is  rand.  The value returned by  rand  becomes the argument to the library function int.</p>
        <p id="p_03258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03258"> Here's how line 6 generates a random number:</p>
        <p id="p_03259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03259"> 1. First, it calls the Perl library function  rand.  This function generates a floating-point random number between 0 and 1 and then multiplies it by the argument it is passed. In this program, rand  is passed 10, which means that the random number is multiplied by 10 and is now a floating-point number that is greater than 0 and less than 10.</p>
        <p id="p_03260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03260"> 2. The value returned by  rand  is then passed to the library function  int,  which takes a floating¬ point number and gets rid of the non-integer part. This operation is known as  truncation.  The integer produced by this truncation operation becomes the return value of the function. For example, the following returns 5:</p>
        <p id="p_03261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03261"> int  (5.7)</p>
        <p id="p_03262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03262"> In this program,  int  truncates the random number returned by  rand  and returns the resulting</p>
        <p id="p_03263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03263"> integer, which is now a random number between 0 and 9.</p>
        <p id="p_03264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03264"> 3.  The value  1  is added to the number returned by  int,  resulting in a random number between  1 and 10.</p>
        <p id="p_03265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03265"> 4. This number is assigned to the scalar variable  $randnum.</p>
        <p id="p_03266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03266"> Line 7 now adds 1 to the element of the array  @randtotal  corresponding to the number generated. For example, if the random number is 7, the array element  $randtotal  [  7  ] has 1 added to it.</p>
        <p id="p_03267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03267"> NOTE</p>
        <p id="p_03268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03268"> As you can see, line 7 works even though  @randtotal  is not initialized. When the program refers to an array element for the first time, the Perl interpreter assumes that the element has an initial value of the null string " ". This null string is converted to 0, which means that adding 1 for the first time produces the result 1, which is what you want.</p>
        <p id="p_03269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03269"> The second part of the program, which prints the total of each random number, starts with lines 12 and 13. These lines get things started by resetting the counter variable $  count  to 1 and printing an introductory message.</p>
        <p id="p_03270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03270"> The conditional expression in line 14 ensures that the loop iterates 10 times-once for each possible random number.</p>
        <p id="p_03271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03271"> Line 15 prints the total for a particular random number.</p>
        <p id="p_03272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03272"> Using Brackets and Substituting for Variables</p>
        <p id="p_03273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03273"> As you have just seen, Perl lets you substitute for array-element variable names in strings, as follows:</p>
        <p id="p_03274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03274"> print ("element $count is $array[ $count-l]\n");</p>
        <p id="p_03275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03275"> This might lead to problems if you want to include the characters [ and ] in character strings. For example, suppose that you have defined the scalar variable  $var  and the array variable  @var.  The character string</p>
        <p id="p_03276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03276"> "$var[0]"</p>
        <p id="p_03277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03277"> substitutes the value of the first element of  @var  in the string. To substitute the value of  $var  and keep the [  0   ] as it is, you must use one of the following:</p>
        <p id="p_03278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03278"> "${var}[0]"</p>
        <p id="p_03279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03279"> "$var\[0]" "$var"  . "[0]"</p>
        <p id="p_03280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03280"> The character string</p>
        <p id="p_03281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03281"> "${var}[0]"</p>
        <p id="p_03282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03282"> uses the brace characters { and } to keep  var  and [ separate; this tells the Perl interpreter to substitute for the variable  $var,  not  $var [ 0  ]. After the substitution, the brace characters are not included in the string.</p>
        <p id="p_03283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03283"> NOTE</p>
        <p id="p_03284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03284"> To include a brace character after a $, use a backslash, as follows:</p>
        <p id="p_03285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03285"> "$\{var}"</p>
        <p id="p_03286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03286"> This character string contains the text $ { var}.</p>
        <p id="p_03287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03287"> The character string</p>
        <p id="p_03288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03288"> "$var\[0]"</p>
        <p id="p_03289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03289"> uses \ to indicate that the [ character is to be given a different meaning than normal; in this case, this means that [ is to be treated as a printable character and not as part of the variable name to be substituted.</p>
        <p id="p_03290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03290"> The expression</p>
        <p id="p_03291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03291"> "  $var"  . "[0]"</p>
        <p id="p_03292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03292"> consists of two character strings joined together by the . operator. Here, the Perl interpreter replaces</p>
        <p id="p_03293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03293"> the first character string with the current value of  $var.</p>
        <p id="p_03294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03294"> Using List Ranges</p>
        <p id="p_03295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03295"> Suppose that you want to define a list consisting of the numbers 1 through 10, inclusive. You can do this by typing each of the numbers in turn.</p>
        <p id="p_03296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03296"> (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)</p>
        <p id="p_03297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03297"> However, there is a simpler way to do it: Use the  list-range operator,  which is . . (two consecutive period characters). The following is an example of a list created using the list-range operator:</p>
        <p id="p_03298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03298"> ( 1 .. 10 )</p>
        <p id="p_03299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03299"> This tells Perl to define a list that has a first value of 1, a second value of 2, and so on up to 10. The list-range operator can be used to define part of a list.</p>
        <p id="p_03300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03300"> (2, 5..7, 11)</p>
        <p id="p_03301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03301"> This list consists of five elements: the numbers  2, 5, 6, 7,  and 11. List-range operators can be used with floating-point values. For example:</p>
        <p id="p_03302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03302"> (2 . 1 . .  5 . 3 )</p>
        <p id="p_03303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03303"> This list consists of four elements:  2.1, 3.1, 4.1,  and  5.1.  Each element of the list is one greater than the previous element, and the last element of the list is the largest possible number less than or equal to the number to the right of the . . operator. Here, 5.1 is less than  5.3,  so it is included in the list; however, 6.1 is greater than  5.3,  so it is not included.</p>
        <p id="p_03304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03304"> NOTE</p>
        <p id="p_03305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03305"> If the value to the left of the . . operator is greater than the value to the right, an empty list is created.</p>
        <p id="p_03306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03306"> (4.5 . . 1.6)</p>
        <p id="p_03307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03307"> Because 4.5 is greater than  1.6,  this list is empty.</p>
        <p id="p_03308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03308"> If the two values are equal, a one-element list is created.</p>
        <p id="p_03309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03309"> (3 . . 3)</p>
        <p id="p_03310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03310"> This is equivalent to the list  (3).</p>
        <p id="p_03311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03311"> List-range operators can specify ranges of strings. For example, the list (  " aaa " , " aab " ,</p>
        <p id="p_03312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03312"> "aac", "aad")  can be expressed as (  "aaa" .. "aad"  ). Similarly, the list  ("BCY", "BCZ", "BDA"  ,  "BDB"  ) is equivalent to (  "BCY" . . "BDB"  ), and the statement  ©alphabet  =</p>
        <p id="p_03313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03313"> ( "  a  " . . " z ") ; creates a list consisting of the 26 lowercase letters of the alphabet and assigns this list to the array variable  ©alphabet.</p>
        <p id="p_03314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03314"> List ranges also enable you to use strings to specify numbers that contain leading zeros.</p>
        <p id="p_03315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03315"> @day_of_month = ("01". ."31" ) ;</p>
        <p id="p_03316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03316"> This statement creates a list consisting of the strings  01,  02 ,  0 3  and so on, up to  31,  and then assigns this list to  @day_of_month.  Because each string contains two characters, this array is suitable for use when you are printing a date in a format such as  08-June-1960.</p>
        <p id="p_03317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03317"> Expressions and List Ranges</p>
        <p id="p_03318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03318"> The values that define the range of a list-range operator can be expressions, and these expressions can contain scalar variables. For example:</p>
        <p id="p_03319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03319"> ($varl..$var2+5)</p>
        <p id="p_03320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03320"> This list consists of all values between the current value of  $var  1 and the current value of the expression  $var2 + 5.</p>
        <p id="p_03321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03321"> Listing 5.3 is an example of a program that uses list ranges. This program asks for a start number and an end number, and it prints all the numbers between them.</p>
        <p id="p_03322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03322"> Type</p>
        <p id="p_03323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03323"> Listing 5.3. A program that uses list ranges to print a list of numbers.</p>
        <p id="p_03324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03324"> Output</p>
        <p id="p_03325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03325"> $ program5_3</p>
        <p id="p_03326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03326"> Enter the start number:</p>
        <p id="p_03327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03327"> -2</p>
        <p id="p_03328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03328"> Enter the end number: 2</p>
        <p id="p_03329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03329"> Here is the list:</p>
        <p id="p_03330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03330"> -2</p>
        <p id="p_03331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03331"> -1</p>
        <p id="p_03332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03332"> 0</p>
        <p id="p_03333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03333"> 1</p>
        <p id="p_03334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03334"> 2</p>
        <p id="p_03335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03335"> $</p>
        <p id="p_03336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03336"> Analysis</p>
        <p id="p_03337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03337"> Lines 3 through 5 retrieve the start of the range to be printed. Line 3 retrieves the number from the standard input file. Line 4 assigns the resulting number to the scalar variable $  start . Line 5 chops the trailing newline character.</p>
        <p id="p_03338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03338"> Lines 6 through 8 repeat the same process for the end of the range, assigning the end of the range to the scalar variable  $end.</p>
        <p id="p_03339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03339"> Line 9 creates a list that consists of the numbers between $  start  and  $end,  and stores the list in the array variable @  1 i s t.</p>
        <p id="p_03340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03340"> Line 10 initializes the counter variable $  count  to  0.</p>
        <p id="p_03341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03341"> Line 11  isaprint  statement that indicates that the list is about to be printed.</p>
        <p id="p_03342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03342"> Lines 12 and 13 are the start of the loop that prints the range. The conditional expression to be evaluated consists of three subexpressions that are operands for the logical or operator | |. If any of these subexpressions are true, the loop continues.</p>
        <p id="p_03343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03343"> The first subexpression tests for the end of the range. To do this, it takes advantage of the fact that an unidentified list element is equal to the null string and that the null string is equivalent to 0. When the list element  $list[$count]  is undefined, the following subexpression is false:</p>
        <p id="p_03344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03344"> $list[$count] != 0</p>
        <p id="p_03345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03345"> The second and third subexpressions cover the cases in which 0 is actually a part of the list. If the list to be printed contains 0, one or both of the following conditions must be true:</p>
        <p id="p_03346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03346"> • The number 1 must be the next element in the list.</p>
        <p id="p_03347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03347"> • The number -1 must be the previous element in the list.</p>
        <p id="p_03348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03348"> The second and third subexpressions test for these conditions. If either or both of these conditions is true, at least one of the following subexpressions also must be true:</p>
        <p id="p_03349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03349"> $list[$count-l] == -1 $list[$count+l] == 1</p>
        <p id="p_03350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03350"> This ensures that the loop continues. Of course, this doesn't cover the case in which the list consists of just 0; however, that’s not a meaningful case. (If you want to be finicky, you can add a special chunk of code that prints 0 if $  start  and  $end  are both 0, but that’s not really worth bothering with.)</p>
        <p id="p_03351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03351"> After this, the rest of the program is straightforward. Line 14 prints a number in the range, line 15 adds one to the counter variable $  count,  and line 16 ends the  while  statement.</p>
        <p id="p_03352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03352"> TIP</p>
        <p id="p_03353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03353"> One of the problems with Perl is that it is sometimes difficult to distinguish the following scalar variable or array-element values:</p>
        <p id="p_03354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03354"> • The null string " ", which is converted to 0 in numeric expressions</p>
        <p id="p_03355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03355"> • An undefined variable or element, which defaults to the null string, which in turn is converted to 0 in numeric expressions</p>
        <p id="p_03356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03356"> • The string 0, which is converted to the number 0 in numeric expressions</p>
        <p id="p_03357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03357"> • A  non-numeric string such as  string,  which is converted to 0 in numeric expressions</p>
        <p id="p_03358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03358"> There are several ways of dealing with this confusion:</p>
        <p id="p_03359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03359"> 1. Retrieve the length of the list stored in an array variable before processing it. This ensures that you don’t go past the end of the list. See the section titled "Retrieving the</p>
        <p id="p_03360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03360"> Length of a List" later in today's lesson for more details on how to do this.</p>
        <p id="p_03361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03361"> 2.  Compare the value with the string  0  rather than the number 0, as follows:</p>
        <p id="p_03362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03362"> if ($value eq  "0")   ...</p>
        <p id="p_03363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03363"> This handles the strings that convert to 0 in numeric expressions that are not  0  itself. (It doesn't handle strings such as  0   0   0   0   or  0 . 0 , which you might want your program to consider equivalent to 0; to deal with these, see the discussion of the  split  function later in today's lesson.)</p>
        <p id="p_03364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03364"> 3. Initialize the scalar variable or array element to a value other than 0 that you know is not going to appear naturally in your program, such as -99999.</p>
        <p id="p_03365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03365"> Which particular method is best depends on the program you want to write, the input it expects, and how "bulletproof" the program needs to be.</p>
        <p id="p_03366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03366"> More on Assignment and Array Variables</p>
        <p id="p_03367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03367"> So far, you've seen that you can assign lists to array variables.</p>
        <p id="p_03368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03368"> @array = (1, 2, 3, 4, 5);</p>
        <p id="p_03369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03369"> You've also seen that you can assign an element of an array to a scalar variable.</p>
        <p id="p_03370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03370"> $scalar = $array[3];</p>
        <p id="p_03371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03371"> The following sections describe the other ways you can use assignment with lists and array variables.</p>
        <p id="p_03372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03372"> Copying from One Array Variable to Another</p>
        <p id="p_03373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03373"> You also can assign one array variable to another.</p>
        <p id="p_03374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03374"> @result = ©original;</p>
        <p id="p_03375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03375"> Here, the list currently stored in the array variable  @original  is copied to the array variable</p>
        <p id="p_03376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03376"> @ result.  Each element of the new array  @ result  is the same as the corresponding element of the array  @original.  Listing 5.4 shows that this is true.</p>
        <p id="p_03377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03377"> Type</p>
        <p id="p_03378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03378"> Listing 5.4. A program that copies an array and compares the elements of the two arrays.</p>
        <p id="p_03379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03379"> 1:</p>
        <p id="p_03380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03380"> 2 :</p>
        <p id="p_03381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03381"> 3 :</p>
        <p id="p_03382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03382"> 4 :</p>
        <p id="p_03383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03383"> 5 : 6 :</p>
        <p id="p_03384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03384"> 7 :</p>
        <p id="p_03385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03385"> 8  : 9: 10  :</p>
        <p id="p_03386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03386"> #!/usr/local/bin/perl</p>
        <p id="p_03387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03387"> @arrayl = (14, "cheeseburger", 1.23, -7, "toad") @array2 = @arrayl;</p>
        <p id="p_03388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03388"> $count = 1;</p>
        <p id="p_03389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03389"> while ($count &lt;= 5) {</p>
        <p id="p_03390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03390"> print("element $count: $arrayl[$count-l] print ("$array2[$count-l]\n");</p>
        <p id="p_03391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03391"> $count++;</p>
        <p id="p_03392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03392"> }</p>
        <p id="p_03393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03393"> H</p>
        <p id="p_03394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03394"> r</p>
        <p id="p_03395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03395"> Output</p>
        <p id="p_03396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03396"> $ program5_4 element 1: 14 14</p>
        <p id="p_03397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03397"> element 2: cheeseburger cheeseburger element 3: 1.23 1.23</p>
        <p id="p_03398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03398"> element 4: -7 -7</p>
        <p id="p_03399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03399"> element 5: toad toad</p>
        <p id="p_03400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03400"> $</p>
        <p id="p_03401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03401"> Analysis</p>
        <p id="p_03402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03402"> Line 3 assigns the list</p>
        <p id="p_03403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03403"> (14, "cheeseburger", 1.23,</p>
        <p id="p_03404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03404"> -7, "toad")</p>
        <p id="p_03405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03405"> to the array variable  @arrayl.  Line 4 then copies this array into a second array variable,  @array2. The rest of the program prints the elements of each array, as follows:</p>
        <p id="p_03406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03406"> • Line 5 initializes the counter variable $  count  to 1.</p>
        <p id="p_03407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03407"> • The conditional expression in line 6 ensures that the loop is performed five times.</p>
        <p id="p_03408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03408"> • Lines 7 and  8  print the matching element of each array. (Note that the subscript is $  count-1, not $  count,  because the subscript 0 is the first element of the array.)</p>
        <p id="p_03409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03409"> • Line 9 adds one to the counter variable $  count.</p>
        <p id="p_03410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03410"> |NOTE</p>
        <p id="p_03411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03411"> You can assign to multiple arrays in one statement. For example:</p>
        <p id="p_03412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03412"> @arrayl = @array2 = (1, 2, 3);</p>
        <p id="p_03413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03413"> This assigns a copy of the list (1,  2, 3)  to both  @arrayl and  @array2.</p>
        <p id="p_03414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03414"> Using Array Variables in Lists</p>
        <p id="p_03415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03415"> As you've already seen, lists can contain scalar variables. For example:</p>
        <p id="p_03416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03416"> @list = (1, $scalar, 3);</p>
        <p id="p_03417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03417"> Here, the value of the scalar variable $  scalar  becomes the second element of the list assigned to @list.</p>
        <p id="p_03418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03418"> You also can specify that the value of an array variable is to appear in a list, as follows:</p>
        <p id="p_03419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03419"> @list1</p>
        <p id="p_03420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03420"> (2, 3, 4);</p>
        <p id="p_03421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03421"> @list2 = (1, @listl, 5);</p>
        <p id="p_03422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03422"> Here, the value of the array variable @  list  1-the list ( 2 ,  3 ,  4 ) -is substituted for the name @listl,  and the resulting list  (1, 2, 3, 4, 5)  is assigned to @ list2.</p>
        <p id="p_03423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03423"> Listing 5.5 shows an example of a list being contained in another list.</p>
        <p id="p_03424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03424"> Type</p>
        <p id="p_03425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03425"> Listing  5.5. A  program that assigns a list as part of another list.</p>
        <p id="p_03426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03426"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03427"> 2 :</p>
        <p id="p_03428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03428"> 3: @innerlist = " never</p>
        <p id="p_03429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03429"> 4: @outerlist = ("I", @innerlist, "fail!\n"); 5: print @outerlist;</p>
        <p id="p_03430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03430"> $ program5_5 I never fail! $</p>
        <p id="p_03431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03431"> Analysis</p>
        <p id="p_03432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03432"> Although this program is quite simple, it contains a couple of new tricks. The first of these is in line 3. Here, a scalar value, "  never  " (note the surrounding spaces), is assigned to the array variable @  inner list.  This works because the Perl interpreter automatically converts the scalar value into a one-element list before assigning it to the array variable.</p>
        <p id="p_03433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03433"> Line 4 assigns a list to the array variable  ©outerlist.  This list is assembled by taking the following list:</p>
        <p id="p_03434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03434"> ("I",  ©innerlist,  "fail!\n")</p>
        <p id="p_03435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03435"> and substituting in the current value of the array variable  ©inner list.  As a result, the list assigned to  ©outerlist  is</p>
        <p id="p_03436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03436"> ("I",  " never ",  "fail!\n")</p>
        <p id="p_03437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03437"> Line  5  prints the list. To do this, it calls the library function  print  and passes it the array variable ©outerlist.  When  print  is given an array variable or a list to print, it prints each element in turn. This means that the following is written to the standard output file:</p>
        <p id="p_03438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03438"> I never fail!</p>
        <p id="p_03439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03439"> Note that  print  doesn't leave any spaces between the elements of the list when it prints them. The only reason the output is readable is because the character string contains spaces around  never.  This means that  print  isn't usually used to print a list of numbers in this way:</p>
        <p id="p_03440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03440"> ©list = (1, 2, 3); print ©list;</p>
        <p id="p_03441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03441"> This prints the following, which isn't quite what you want:</p>
        <p id="p_03442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03442"> 123</p>
        <p id="p_03443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03443"> In Listing 5.5, the argument passed to the  print  function is not enclosed in parentheses. This is perfectly acceptable. In Perl, the parentheses enclosing arguments to functions are optional. For example, when you call the library function  chop,  instead of writing</p>
        <p id="p_03444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03444"> chop ($number) ; you can write chop $number;</p>
        <p id="p_03445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03445"> Although this saves a few extra keystrokes, it makes things a little less readable (in this author's opinion)</p>
        <p id="p_03446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03446"> Besides, eliminating the parentheses can lead to problems. Consider the following example</p>
        <p id="p_03447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03447"> $fred = "Fred";</p>
        <p id="p_03448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03448"> print  (("Hello, " . $fred .  "!\n")  x  2);</p>
        <p id="p_03449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03449"> This code prints</p>
        <p id="p_03450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03450"> Hello, Fred!</p>
        <p id="p_03451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03451"> Hello, Fred!</p>
        <p id="p_03452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03452"> In this case, the parentheses enclosing the arguments to  print are absolutely necessary. Without them, you have</p>
        <p id="p_03453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03453"> print ("Hello, " .  $fred . "!\n")  x  2;</p>
        <p id="p_03454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03454"> When the Perl interpreter sees this statement, it assumes that print  is being called with the following argument, which is not what you want:</p>
        <p id="p_03455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03455"> "Hello, " .  $fred . "!\n"</p>
        <p id="p_03456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03456"> As always in programming, the basic rule to follow is this: Do whatever makes your program easier to work with, and use your best judgment.</p>
        <p id="p_03457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03457"> Substituting for Array Variables in Strings</p>
        <p id="p_03458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03458"> As you have seen, Perl does not leave spaces if you pass an array variable to  print:</p>
        <p id="p_03459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03459"> @array = (1, 2, 3);</p>
        <p id="p_03460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03460"> print (@array, "\n");</p>
        <p id="p_03461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03461"> This prints the following on your screen:</p>
        <p id="p_03462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03462"> 123</p>
        <p id="p_03463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03463"> To get around this problem, put the array you want to print into a string:</p>
        <p id="p_03464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03464"> print ( "@array\n");</p>
        <p id="p_03465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03465"> When the Perl interpreter sees the array variable inside the string, it substitutes the values of the list assigned to the array variables, and leaves a space between each pair of elements. For example:</p>
        <p id="p_03466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03466"> @array = (1,  2 ,  3);</p>
        <p id="p_03467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03467"> print ( "@array\n");</p>
        <p id="p_03468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03468"> This prints the following on your screen:</p>
        <p id="p_03469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03469"> 12 3</p>
        <p id="p_03470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03470"> Assigning to Scalar Variables from Array Variables</p>
        <p id="p_03471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03471"> Consider the following assignment, which you've already seen:</p>
        <p id="p_03472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03472"> @array = ($varl, $var2);</p>
        <p id="p_03473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03473"> Here, the values of the scalar variables  $var  1 and  $var2  are used to form a two-element list that is assigned to the array variable @ array.</p>
        <p id="p_03474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03474"> Perl also enables you to take the current value of an array variable and assign its components to a group of scalar variables. For example:</p>
        <p id="p_03475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03475"> @array = (5, 7) ;</p>
        <p id="p_03476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03476"> ($varl, $var2) = @array;</p>
        <p id="p_03477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03477"> Here, the first element of the list currently stored in @  array, 5,  is assigned to  $var  1. The second element,  7,  is assigned to  $var2.</p>
        <p id="p_03478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03478"> Additional elements in an array, if they exist, are ignored. For example:</p>
        <p id="p_03479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03479"> @array = (5 ,  7, 11);</p>
        <p id="p_03480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03480"> ($varl, $var2) = ©array;</p>
        <p id="p_03481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03481"> Here,  5  is assigned to  $varl, 7  is assigned to  $var2,  and  11  is not assigned to anything.</p>
        <p id="p_03482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03482"> If there are more scalar variables than elements in an array variable, the excess scalar variables are assigned the null string, as follows:</p>
        <p id="p_03483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03483"> ©array = (5, 7 ) ;</p>
        <p id="p_03484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03484"> ($varl, $var2, $var3) = ©array;</p>
        <p id="p_03485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03485"> This assigns  5  to  $var  1 and  7  to  $var2.  Because there are not enough elements in  ©array  to assign anything to  $var3, $var3  is assigned the null string " ".</p>
        <p id="p_03486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03486"> NOTE</p>
        <p id="p_03487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03487"> You also can assign to several scalar variables using a list. For example:</p>
        <p id="p_03488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03488"> ($varl, $var2, $var3) = ("one", "two",</p>
        <p id="p_03489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03489"> "three");</p>
        <p id="p_03490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03490"> This assigns  one  to  $varl, two  to  $var2,  and  three  to $var3.</p>
        <p id="p_03491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03491"> As with array variables, extra values in the list are ignored and extra scalar variables are assigned the null string, as follows:</p>
        <p id="p_03492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03492"> ($varl, $var2) = (1, 2, 3); # 3 is ignored</p>
        <p id="p_03493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03493"> ($varl, $var2, $var3) = (1, 2); # $var3 is</p>
        <p id="p_03494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03494"> Retrieving the Length of a List</p>
        <p id="p_03495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03495"> As you've seen, lists and array variables can be any length you want. As a consequence, Perl provides a way of determining the length of the list assigned to an array variable.</p>
        <p id="p_03496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03496"> Here's how it works: If an array variable (or list) appears anywhere that a scalar value is expected, the Perl interpreter obtains a scalar value by calculating the length of the list assigned to the array variable.</p>
        <p id="p_03497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03497"> Consider the following example:</p>
        <p id="p_03498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03498"> @array = (1, 2, 3) ;</p>
        <p id="p_03499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03499"> $scalar = @array;</p>
        <p id="p_03500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03500"> In the assignment to $  scalar,  the Perl interpreter replaces  @array  with the length of the list currently assigned to @ array,  which is  3. $scalar,  therefore, is assigned the value  3.</p>
        <p id="p_03501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03501"> NOTE</p>
        <p id="p_03502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03502"> Note that the following two statements are not equivalent:</p>
        <p id="p_03503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03503"> $scalar = ©array;</p>
        <p id="p_03504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03504"> ($scalar) = ©array;</p>
        <p id="p_03505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03505"> In the first statement, the length of the list in  ©array  is assigned to $  scalar.  In the second statement, the first element of ©array  is assigned to $  scalar.</p>
        <p id="p_03506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03506"> It is always important to remember that $  scalar  and ($ scalar)  are not the same thing. $  scalar  is a scalar variable, and  ($scalar)  is a one-element list containing</p>
        <p id="p_03507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03507"> $scalar.</p>
        <p id="p_03508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03508"> Being able to access the length of an array is useful if you want to write a loop that performs an operation on every element of an array. Listing 5.6 is an example of a program that does just that.</p>
        <p id="p_03509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03509"> Type</p>
        <p id="p_03510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03510"> Listing 5.6. A program that prints every element of an array.</p>
        <p id="p_03511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03511"> }</p>
        <p id="p_03512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03512"> $ program5, element 1: element 2: element 3: element 4: element 5: $</p>
        <p id="p_03513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03513"> .6</p>
        <p id="p_03514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03514"> 14</p>
        <p id="p_03515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03515"> cheeseburger</p>
        <p id="p_03516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03516"> 1.23</p>
        <p id="p_03517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03517"> -7</p>
        <p id="p_03518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03518"> toad</p>
        <p id="p_03519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03519"> Analysis</p>
        <p id="p_03520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03520"> The only new feature of this program is line 5, which compares the counter variable $ count  to the length of the array @  array.  Because the list assigned to @  array  contains five elements, the conditional expression</p>
        <p id="p_03521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03521"> $count &lt;= @array</p>
        <p id="p_03522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03522"> ensures that the loop iterates five times.</p>
        <p id="p_03523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03523"> Once again, note that the subscript in line 6 is  $count-l,  not $  count.  This caution bears repeating: It is very easy to forget to subtract 1 when you use a value as a subscript.</p>
        <p id="p_03524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03524"> If you like, you can write your loop in a different way and use $  count  as a subscript. For example:</p>
        <p id="p_03525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03525"> $count = 0; while ($count print</p>
        <p id="p_03526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03526"> &lt; @array)</p>
        <p id="p_03527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03527"> ("element</p>
        <p id="p_03528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03528"> {</p>
        <p id="p_03529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03529"> $count+l:</p>
        <p id="p_03530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03530"> $array[  $count]\n");</p>
        <p id="p_03531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03531"> }</p>
        <p id="p_03532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03532"> As you can see, this isn't any easier to follow because you now have to remember these two things:</p>
        <p id="p_03533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03533"> 1. The conditional expression now must use the &lt; operator, not the &lt;= operator. If you use &lt;=</p>
        <p id="p_03534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03534"> here, the loop iterates six times, not five.</p>
        <p id="p_03535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03535"> 2.  The value of $  count  is now not the same as the element you are referring to. For example, if you are printing the third element of the array, $  count  has the value  2.  This means that references to $  count,  such as element $count+l:</p>
        <p id="p_03536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03536"> must add one to the value of $  count  to get the result you want.</p>
        <p id="p_03537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03537"> As you can see, there is no intuitive or obvious way of writing programs that loop through arrays. Generally, it's best to pick the way that is easiest for you to remember.</p>
        <p id="p_03538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03538"> Warning</p>
        <p id="p_03539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03539"> You cannot retrieve the length of a list without first assigning the list to an array variable. For example:</p>
        <p id="p_03540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03540"> @array = (10, 20,  30);</p>
        <p id="p_03541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03541"> $scalar = @array;</p>
        <p id="p_03542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03542"> This assigns  3  to $  scalar.  Compare this with the following statement:</p>
        <p id="p_03543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03543"> $scalar = (10, 20,  30);</p>
        <p id="p_03544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03544"> This statement actually assigns  30   to  $scalar,  not  3.  In this statement, the subexpression</p>
        <p id="p_03545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03545"> (10, 20, 30)</p>
        <p id="p_03546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03546"> is treated as three scalar values separated by comma operators.</p>
        <p id="p_03547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03547"> For more details on the comma operator, refer to "The Comma Operator" in  Day  4 .</p>
        <p id="p_03548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03548"> Using Array Slices</p>
        <p id="p_03549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03549"> As you've seen, array subscripting enables you to change or access one element of an array. For example:</p>
        <p id="p_03550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03550"> $var =  $array[2];</p>
        <p id="p_03551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03551"> $array[2]  = $var;</p>
        <p id="p_03552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03552"> Perl enables you to access more than one element of an array at a time in much the same way. Following is a simple example:</p>
        <p id="p_03553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03553"> @subarray  = @array[0,l];</p>
        <p id="p_03554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03554"> Here, the code fragment</p>
        <p id="p_03555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03555"> @array[0,1]</p>
        <p id="p_03556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03556"> refers to the first two elements of the list stored in the array variable. This portion of the array is known as an  array slice.  An array slice is treated just like any other list. In the statement</p>
        <p id="p_03557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03557"> @subarray  = @array[0,l];</p>
        <p id="p_03558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03558"> the list consisting of the first two elements of  @array  is assigned to the array variable @  subarray. Here is another example:</p>
        <p id="p_03559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03559"> @slice  = @array[1,2,3];</p>
        <p id="p_03560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03560"> This statement assigns the array slice consisting of the second, third, and fourth elements of @  array to the array variable @ slice.</p>
        <p id="p_03561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03561"> Although single elements of an array are referenced using the $ character, array slices are referenced using @:</p>
        <p id="p_03562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03562"> $var = $array[0];</p>
        <p id="p_03563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03563"> @subarray = @array[0,l];</p>
        <p id="p_03564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03564"> The basic rules are as follows:</p>
        <p id="p_03565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03565"> • References to single items, such as scalar variables or single array elements, start with a $.</p>
        <p id="p_03566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03566"> • References to array variables or array slices, which refer to lists, start with a @.</p>
        <p id="p_03567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03567"> Listing 5.7 shows a simple example of an array slice.</p>
        <p id="p_03568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03568"> Type</p>
        <p id="p_03569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03569"> Listing 5.7. A program that demonstrates the use of an array slice.</p>
        <p id="p_03570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03570"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03571"> 2 :</p>
        <p id="p_03572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03572"> 3: @array = (1, 2, 3, 4);</p>
        <p id="p_03573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03573"> 4: @subarray = @array[l,2];</p>
        <p id="p_03574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03574"> 5:  print  ("The first  element of subarray  is $subarray[0]\n");</p>
        <p id="p_03575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03575"> 6:  print  ("The  second element of subarray  is $subarray[1]\n");</p>
        <p id="p_03576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03576"> Output</p>
        <p id="p_03577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03577"> $  program5_7</p>
        <p id="p_03578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03578"> The  first  element of subarray is 2</p>
        <p id="p_03579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03579"> The second element of subarray is 3</p>
        <p id="p_03580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03580"> $</p>
        <p id="p_03581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03581"> Analysis</p>
        <p id="p_03582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03582"> Line</p>
        <p id="p_03583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03583"> 3 of this program assigns the following list to the array variable @  array:</p>
        <p id="p_03584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03584"> (1, 2, 3, 4)</p>
        <p id="p_03585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03585"> Line 4 assigns a slice of the array variable @ array  to the array variable @  subarray.  The array slice</p>
        <p id="p_03586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03586"> @array[1,2]</p>
        <p id="p_03587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03587"> specifies that the second and third elements of the array are to be treated as a list and assigned to</p>
        <p id="p_03588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03588"> @subarray.</p>
        <p id="p_03589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03589"> NOTE</p>
        <p id="p_03590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03590"> In array slices, as in references to single elements of an array, subscripts start from zero. For example, the array slice</p>
        <p id="p_03591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03591"> @array[1,2]</p>
        <p id="p_03592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03592"> refers to the second and third elements of an array.</p>
        <p id="p_03593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03593"> The final two lines of the program print the two elements of the array variable @  sub array.  As you can see, these elements are identical to the second and third elements of @ array.</p>
        <p id="p_03594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03594"> Using List Ranges in Array-Slice Subscripts</p>
        <p id="p_03595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03595"> Perl provides a convenient way to refer to large array slices. Instead of writing</p>
        <p id="p_03596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03596"> @array[0,1,2,3,4]</p>
        <p id="p_03597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03597"> to refer to the first five elements of array @ array,  you can use the list range operator, as follows:</p>
        <p id="p_03598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03598"> ©array [ 0. . 4 ]</p>
        <p id="p_03599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03599"> This enables you to assign large array slices easily:</p>
        <p id="p_03600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03600"> ©subarray = ©array[0. .19] ;</p>
        <p id="p_03601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03601"> This assigns the first 20 elements of  ©array  to  ©subarray.</p>
        <p id="p_03602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03602"> Using Variables in Array-Slice Subscripts</p>
        <p id="p_03603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03603"> You can use the value of a scalar variable in a list range in an array slice subscript. The following is an example:</p>
        <p id="p_03604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03604"> $endrange = 19;</p>
        <p id="p_03605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03605"> ©subarray = ©array[0..$endrange];</p>
        <p id="p_03606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03606"> Here, the scalar variable  $endrange  contains the upper limit of the array slice, which in this case is 19. This means that the array slice to assign is</p>
        <p id="p_03607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03607"> ©array[0..19]</p>
        <p id="p_03608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03608"> which assigns the first 20 elements of  ©array  to  ©subarray.</p>
        <p id="p_03609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03609"> You can also use the list stored in an array variable to define an array slice. Listing 5.8 shows how this works.</p>
        <p id="p_03610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03610"> Listing 5.8. A program that uses an array variable as an array-slice subscript.</p>
        <p id="p_03611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03611"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03612"> 2 :</p>
        <p id="p_03613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03613"> 3: @array = ("one", "two", "three", "four", "five");</p>
        <p id="p_03614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03614"> 4: @range = (1, 2, 3);</p>
        <p id="p_03615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03615"> 5: @subarray = @array[@range];</p>
        <p id="p_03616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03616"> 6: print ("The array slice is: @subarray\n");</p>
        <p id="p_03617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03617"> Output</p>
        <p id="p_03618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03618"> $ program5_8</p>
        <p id="p_03619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03619"> The array slice is: two three four $</p>
        <p id="p_03620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03620"> Analysis</p>
        <p id="p_03621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03621"> Line 3 of this program assigns the following list to the array variable @  array:</p>
        <p id="p_03622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03622"> ("one", "two", "three", "four", "five")</p>
        <p id="p_03623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03623"> Line 4 assigns the list (1, 2, 3) to the array variable @  range,  which is to serve as the list range.</p>
        <p id="p_03624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03624"> Line 5 uses the value of @  range  as the array subscript for an array slice. Because @  range  contains (1, 2, 3), the slice of @  array  that is selected consists of the second, third, and fourth elements. These elements are then assigned to the array variable @  subarray.</p>
        <p id="p_03625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03625"> Line 6 prints the selected array slice. When the Perl interpreter sees the variable name @  subarray  in the character string to be printed, it substitutes the value of @  subarray  for its name. Because @subarray  is inside a character string, the Perl interpreter leaves a space between each pair of elements when printing.</p>
        <p id="p_03626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03626"> Compare line 6 with the following:</p>
        <p id="p_03627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03627"> print (@subarray,  "\n");</p>
        <p id="p_03628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03628"> Here,  print  leaves no spaces between the elements of @  subarray,  which means that it prints</p>
        <p id="p_03629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03629"> twothreefour</p>
        <p id="p_03630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03630"> Which outcome you want depends, of course, on what you want your program to do.</p>
        <p id="p_03631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03631"> Assigning to Array Slices</p>
        <p id="p_03632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03632"> You can assign to array slices using the notation you have just seen. The following is an example:</p>
        <p id="p_03633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03633"> ©array[0,1] = ("string", 46);</p>
        <p id="p_03634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03634"> Here, the first two elements of the array @  array  become  string  and  4 6,  respectively.</p>
        <p id="p_03635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03635"> You can use list-range operators and variables when you assign to array slices as well. The following is an example:</p>
        <p id="p_03636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03636"> @array[0..3] = (1, 2, 3, 4);</p>
        <p id="p_03637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03637"> @array[0..$endrange] = (1, 2, 3, 4);</p>
        <p id="p_03638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03638"> If there are more items in the array slice than in the list, the extra items in the array slice are assigned the null string, as follows:</p>
        <p id="p_03639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03639"> @array[0..2] = ("stringl", "string2");</p>
        <p id="p_03640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03640"> The third element of  ©array  now holds the null string.</p>
        <p id="p_03641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03641"> If there are fewer items in the array slice than in the list, the extra items in the list are ignored, as in the following:</p>
        <p id="p_03642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03642"> @array[0..2] = (1, 2, 3, 4);</p>
        <p id="p_03643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03643"> In this assignment, the fourth element in the list,  4,  is not assigned to anything.</p>
        <p id="p_03644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03644"> When an array slice is assigned to, the remainder of the array is not changed. Listing 5.9 shows how</p>
        <p id="p_03645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03645"> this works.</p>
        <p id="p_03646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03646"> Type</p>
        <p id="p_03647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03647"> Listing 5.9. A program that assigns to an array slice.</p>
        <p id="p_03648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03648"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03649"> 2 :</p>
        <p id="p_03650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03650"> 3: @array = ("oldl", "old2", "old3", "old4");</p>
        <p id="p_03651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03651"> 4: @array[l,2] = ("new2", "new3");</p>
        <p id="p_03652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03652"> 5: print ( " @array\n");</p>
        <p id="p_03653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03653"> $ program5_9 oldl new2 new3 old4 $</p>
        <p id="p_03654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03654"> Analysis</p>
        <p id="p_03655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03655"> In the preceding program, the only statement that did not appear in previous programs is line 4, which assigns the list  ("new2", "new3  " ) to the array slice of @  array  consisting of the second and third elements. This assignment changes the value of @ array  from</p>
        <p id="p_03656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03656"> ("oldl", "old2", "old3", "old4")</p>
        <p id="p_03657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03657"> to</p>
        <p id="p_03658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03658"> ("oldl",</p>
        <p id="p_03659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03659"> "new2", "new3",</p>
        <p id="p_03660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03660"> "old4")</p>
        <p id="p_03661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03661"> Line 5 then prints the changed array.</p>
        <p id="p_03662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03662"> Overlapping Array Slices</p>
        <p id="p_03663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03663"> As you've seen, Perl enables you to use array slices on either side of an assignment statement. The following is an example:</p>
        <p id="p_03664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03664"> @newarray = @array[2,3,4];</p>
        <p id="p_03665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03665"> @array[2,3,4] = @newarray;</p>
        <p id="p_03666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03666"> This means that you can assign from one array slice to another, even if the two slices overlap, as in the following:</p>
        <p id="p_03667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03667"> @array[1,2,3] = @array[2,3,4];</p>
        <p id="p_03668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03668"> The Perl interpreter has no problem with this statement because it copies the list stored in  ©array [2,3,4]  into a temporary location (invisible to you) before assigning it to ©array[1,2,3].</p>
        <p id="p_03669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03669"> Listing 5.10 provides an example of overlapping array slices in use.</p>
        <p id="p_03670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03670"> Type</p>
        <p id="p_03671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03671"> Listing 5.10. A program containing overlapping array slices.</p>
        <p id="p_03672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03672"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03673"> 2 :</p>
        <p id="p_03674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03674"> 3: ©array = ("one", "two", "three", "four", "five");</p>
        <p id="p_03675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03675"> 4: ©array[1,2,3] = ©array[2,3,4];</p>
        <p id="p_03676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03676"> 5:  print  ("©array\n");</p>
        <p id="p_03677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03677"> Output</p>
        <p id="p_03678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03678"> $ program5_10 one three four five five $</p>
        <p id="p_03679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03679"> Analysis</p>
        <p id="p_03680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03680"> Line 4 is an example of an assignment with overlapping array slices. At the time of assignment, the array slice @ array [2,3,4]  contains the list</p>
        <p id="p_03681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03681"> ("three", "four", "five")</p>
        <p id="p_03682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03682"> This list consists of the last three elements of @  array.  Assigning this list to @  array [1,2,3] means that the list stored in @ array  changes from</p>
        <p id="p_03683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03683"> ("one", "two", "three", "four", "five")</p>
        <p id="p_03684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03684"> to</p>
        <p id="p_03685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03685"> ("one", "three", "four", "five", "five")</p>
        <p id="p_03686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03686"> NOTE</p>
        <p id="p_03687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03687"> Overlapping array slices of varying lengths are dealt with in the same way as other array slice assignments of non-matching lengths. For example:</p>
        <p id="p_03688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03688"> @array = (1, 2, 3, 4, 5);</p>
        <p id="p_03689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03689"> @array[0..2] = @array[3,4];</p>
        <p id="p_03690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03690"> This assignment assigns the array slice  ©array [3,4],  which is the list ( 4 ,  5 ), to the array slice  ©array [ 0 . . 2  ]. After this assignment, the value of  ©array  is the list</p>
        <p id="p_03691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03691"> (4, 5, 4, 5)</p>
        <p id="p_03692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03692"> The third element of  ©array  is now the null string because there are only two elements in the array slice being assigned.</p>
        <p id="p_03693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03693"> Using the Array-Slice Notation as a Shorthand</p>
        <p id="p_03694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03694"> So far, I've been using the following array-slice notation to refer to consecutive elements of an array:</p>
        <p id="p_03695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03695"> ©array[0,1]</p>
        <p id="p_03696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03696"> In Perl, however, there is no real difference between an array slice and a list containing consecutive elements of the same array. For example, the following statements are equivalent:</p>
        <p id="p_03697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03697"> ©subarray = @array[0,l];</p>
        <p id="p_03698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03698"> ©subarray = ($array[0], $array[1]);</p>
        <p id="p_03699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03699"> Because of this, you can use the array-slice notation to refer to any elements of an array, regardless of whether they are in order. For example, the following two statements are equivalent:</p>
        <p id="p_03700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03700"> ©subarray = ($array[4], $array[l], $array[3]);</p>
        <p id="p_03701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03701"> ©subarray = ©array[4,1,3];</p>
        <p id="p_03702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03702"> In both cases, the array variable  ©subarray  is assigned a list consisting of three elements: the fifth, second, and fourth elements of  ©array.</p>
        <p id="p_03703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03703"> You can use this array-slice notation in a variety of ways. For example, you can assign one element of an array multiple times:</p>
        <p id="p_03704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03704"> @subarray = @array[0,0,0];</p>
        <p id="p_03705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03705"> This creates a list consisting of three copies of the first element of @  array,  and then assigns this list</p>
        <p id="p_03706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03706"> to  @subarray.</p>
        <p id="p_03707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03707"> The array-slice notation provides an easy way to swap elements in a list. The following is an example:</p>
        <p id="p_03708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03708"> @array[l,2] = @array[2,l];</p>
        <p id="p_03709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03709"> This statement swaps the second and third elements of @ array.  As with the overlapping array slices you saw earlier, the Perl interpreter copies @ array [2,1]  into a temporary location before assigning it, which ensures that the assignment takes place properly.</p>
        <p id="p_03710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03710"> For an example of a program that swaps array elements, look at Listing 5.11, which sorts the elements in an array using a simple sort algorithm.</p>
        <p id="p_03711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03711"> Type</p>
        <p id="p_03712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03712"> Listing 5.11. A program that sorts an array.</p>
        <p id="p_03713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03713"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03714"> 2 :</p>
        <p id="p_03715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03715"> 3: # read the array from standard input one item at a time</p>
        <p id="p_03716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03716"> 4: print ("Enter the array to sort, one item at a time.\n"); 5: print ("Enter an empty line to quit.\n");</p>
        <p id="p_03717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03717"> 6: $count = 1;</p>
        <p id="p_03718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03718"> 7: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_03719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03719"> chop ($inputline);</p>
        <p id="p_03720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03720"> 9:</p>
        <p id="p_03721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03721"> while ($inputline ne "") {</p>
        <p id="p_03722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03722"> 15 :</p>
        <p id="p_03723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03723"> 28 :</p>
        <p id="p_03724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03724"> $ program5_ll</p>
        <p id="p_03725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03725"> Enter the array to sort, one item at a time.</p>
        <p id="p_03726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03726"> Enter an empty line to quit.</p>
        <p id="p_03727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03727"> too</p>
        <p id="p_03728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03728"> baz</p>
        <p id="p_03729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03729"> dip</p>
        <p id="p_03730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03730"> bar</p>
        <p id="p_03731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03731"> bar baz dip too $</p>
        <p id="p_03732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03732"> Analysis</p>
        <p id="p_03733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03733"> This program is divided into three parts:</p>
        <p id="p_03734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03734"> • Reading the array</p>
        <p id="p_03735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03735"> • Sorting the array</p>
        <p id="p_03736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03736"> • Printing the array</p>
        <p id="p_03737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03737"> Lines 3-14 read the array into the variable @ array.  The conditional expression in line 9,</p>
        <p id="p_03738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03738"> $ input line ne  " is true as long as the line is not empty. (Recall that an empty line consists of just the newline character, which the library function  chop  removes.) In this example, the list  too baz dip bar  is read into the array variable @ array.</p>
        <p id="p_03739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03739"> Lines 17-27 perform the sort. The sort consists of two loops, one inside the other. The inner loop works like this:</p>
        <p id="p_03740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03740"> • Line 21 compares the first item in the list with the item next to it. If the first item is greater, line 22 swaps the two items. Otherwise, the two items are left where they are. In this example, f oo  is greater than  baz,  so  f oo  becomes the second element in the list. At this point, the list is</p>
        <p id="p_03741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03741"> baz foo dip bar</p>
        <p id="p_03742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03742"> • The program then loops back to line 21, which now compares the second pair in the list (the second and third elements). The new second element,  foo,  is compared to  dip. foo  is greater, so  foo  becomes the new third element, and  dip  becomes the second element:</p>
        <p id="p_03743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03743"> baz dip foo bar</p>
        <p id="p_03744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03744"> • Line 20 terminates the loop when the last pair is compared. (Note that the conditional</p>
        <p id="p_03745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03745"> expression compares the inner counting variable  $x  with the length of the array variable @array.  When  $x  becomes equal to  @array,  every pair of elements in the list has been compared.)</p>
        <p id="p_03746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03746"> At this point, the largest element in the list is at the far end of the list:</p>
        <p id="p_03747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03747"> baz dip bar foo</p>
        <p id="p_03748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03748"> The largest value in the list,  foo,  has been moved to the far right end of the list, where it belongs. The other elements have been displaced to make room.</p>
        <p id="p_03749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03749"> Lines 17-19 and 26-27 contain the outer loop. This outer loop just makes sure that the inner loop is repeated  n-1  times, where  n  is the number of elements in the list. When the inner loop is repeated a second time, the second-largest element moves up to the second position from the right:</p>
        <p id="p_03750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03750"> baz bar dip foo</p>
        <p id="p_03751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03751"> The final pass through the inner loop sorts the final two elements:</p>
        <p id="p_03752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03752"> bar baz dip foo</p>
        <p id="p_03753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03753"> Line 30 then prints the sorted list.</p>
        <p id="p_03754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03754"> NOTE</p>
        <p id="p_03755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03755"> You'll never need to write a program that sorts values in a list because Perl has a library function,  sort,  that does it for you. See the section "Array Library Functions" later today for more details.</p>
        <p id="p_03756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03756"> Reading an Array from the Standard Input File</p>
        <p id="p_03757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03757"> In the programs you have seen so far, single lines of input are read from the standard input file and stored in scalar variables. For example:</p>
        <p id="p_03758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03758"> $var</p>
        <p id="p_03759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03759"> &lt;STDIN&gt;;</p>
        <p id="p_03760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03760"> In this case, every appearance of  &lt;STDIN&gt;  means that another line of input is obtained from the standard input file.</p>
        <p id="p_03761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03761"> Perl also provides a quicker approach: If you assign  &lt;STDIN&gt;  to an array variable instead of a scalar variable, the Perl interpreter reads in all of the data from the standard input file at once and assigns it. For example, the statement</p>
        <p id="p_03762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03762"> @array = &lt;STDIN&gt;;</p>
        <p id="p_03763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03763"> reads everything typed in and assigns it all to the array variable @ array.  The variable @ array  now contains a list; each element of the list is a line of input.</p>
        <p id="p_03764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03764"> Listing 5.12 is an example of a simple program that reads its input data into an array.</p>
        <p id="p_03765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03765"> Type</p>
        <p id="p_03766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03766"> Listing 5.12. A program that reads data into an array and writes the array.</p>
        <p id="p_03767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03767"> Output</p>
        <p id="p_03768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03768"> $ program5_12</p>
        <p id="p_03769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03769"> Here is my first line of data. Here is another line.</p>
        <p id="p_03770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03770"> Here is the last line.</p>
        <p id="p_03771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03771"> A D</p>
        <p id="p_03772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03772"> Here</p>
        <p id="p_03773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03773"> Here</p>
        <p id="p_03774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03774"> Here</p>
        <p id="p_03775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03775"> is my first line of data, is another line, is the last line.</p>
        <p id="p_03776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03776"> $</p>
        <p id="p_03777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03777"> Analysis</p>
        <p id="p_03778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03778"> As you can see, this program is very short. Line 3 reads the input from the standard input file. In this example, the input that is entered consists of the three lines</p>
        <p id="p_03779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03779"> followed by the Ctrl+D key combination. Ctrl+D produces a special character that indicates end of file; when the Perl interpreter sees this, it knows that there is no more input.</p>
        <p id="p_03780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03780"> NOTE</p>
        <p id="p_03781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03781"> A blank line is perfectly acceptable input and does not terminate the reading of input from the standard input file. Only the Ctrl+D character can do that.</p>
        <p id="p_03782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03782"> Also note that the Ctrl+D character is a non-printing character. When you type it, nothing appears on the screen. In the examples in this book, control characters that are part of the input, such as Ctrl+D, are represented by the  A  character followed by the letter typed. For example, Ctrl+D is represented as</p>
        <p id="p_03783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03783"> A D</p>
        <p id="p_03784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03784"> This representation is the standard one used in the computing world.</p>
        <p id="p_03785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03785"> After line 3 is executed, the array variable @ array  contains a list comprising three elements: the three lines of input you just entered. The last character of each input line is the newline character (because you didn't call  chop  to get rid of it).</p>
        <p id="p_03786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03786"> Line 4 prints the lines of input you just read. Note that you do not need to separate the lines with spaces or newline characters because each line in @ array  is terminated by a newline character.</p>
        <p id="p_03787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03787"> Warning</p>
        <p id="p_03788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03788"> When you use the following statement:</p>
        <p id="p_03789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03789"> @array = &lt;STDIN&gt;;</p>
        <p id="p_03790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03790"> every line of input you enter is stored in @  array  all at once. If you enter a lot of input, @  array  can get very large.</p>
        <p id="p_03791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03791"> Use this statement only when you really need to work with the entire input file at once.</p>
        <p id="p_03792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03792"> Array Library Functions</p>
        <p id="p_03793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03793"> Perl provides a number of library functions that work on lists and array variables. You can use them to do the following:</p>
        <p id="p_03794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03794"> • Sort array elements in alphabetical order</p>
        <p id="p_03795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03795"> • Reverse the elements of an array</p>
        <p id="p_03796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03796"> • Remove the last character from all elements of an array</p>
        <p id="p_03797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03797"> • Merge the elements of an array into a single string</p>
        <p id="p_03798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03798"> • Split a string into array elements</p>
        <p id="p_03799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03799"> The following sections describe these array library functions.</p>
        <p id="p_03800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03800"> Sorting a List or Array Variable</p>
        <p id="p_03801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03801"> The library function  sort  sorts the elements of an array in alphabetical order and returns the sorted list.</p>
        <p id="p_03802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03802"> The syntax for the  sort  library function is</p>
        <p id="p_03803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03803"> retlist = sort (array);</p>
        <p id="p_03804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03804"> In this syntax,  array  is the list to sort, and  retlist  is the sorted list.</p>
        <p id="p_03805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03805"> Here are some examples:</p>
        <p id="p_03806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03806"> @array = ("this", "is", "a", "test");</p>
        <p id="p_03807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03807"> @array2 = sort (@array);</p>
        <p id="p_03808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03808"> After  sort  is called, the value of @ array2  is the list</p>
        <p id="p_03809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03809"> ("a", "is", "test", "this")</p>
        <p id="p_03810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03810"> Note that  sort  does not modify the original list. The statement</p>
        <p id="p_03811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03811"> @array2 = sort (@array);</p>
        <p id="p_03812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03812"> does not change the value of @ array.  To replace the contents of an array variable with the sorted list, put the array variable on both sides of the assignment, as follows:</p>
        <p id="p_03813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03813"> @array = sort (@array);</p>
        <p id="p_03814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03814"> Here, the sorted list is put back in @ array.</p>
        <p id="p_03815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03815"> The sorted list must be assigned to an array variable in order to be used. The statement</p>
        <p id="p_03816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03816"> sort (@array);</p>
        <p id="p_03817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03817"> doesn't do anything useful because the sorted list is not assigned to anything.</p>
        <p id="p_03818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03818"> Note that  sort  treats its items as strings, not integers; items are sorted in alphabetical, not numeric, order. For example:</p>
        <p id="p_03819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03819"> In this case,  sort  produces</p>
        <p id="p_03820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03820"> (100, 70, 8)</p>
        <p id="p_03821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03821"> not</p>
        <p id="p_03822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03822"> (8, 70, 100)</p>
        <p id="p_03823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03823"> Because  sort  is treating the elements of the list as strings, the strings to be sorted are  7 0, 10 0,  and 8.  When sorting characters that are not alphabetic,  sort  looks at the internal representation of the characters to be sorted. If you are not familiar with ASCII (which will be described shortly), this might sound complicated, but it's not too difficult to understand.</p>
        <p id="p_03824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03824"> Here's how it works: When Perl (or any other programming language) stores a character such as  r  or 1, what it actually does is store a unique eight-bit number that corresponds to this character. For example, the letter  r  is represented by the number  114,   and  1  is represented by the number  4  9.   Every possible character has its own unique number.</p>
        <p id="p_03825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03825"> The  s ort  function uses these unique numbers to determine how to sort character strings. When sorting  7  0,   10   0,   and  8,  sort  looks at the unique numbers corresponding to  7, 1,  and  8,  which are the first characters in each of the strings. As it happens, the unique number for 1 is less than that for 7,  which is less than that for  8  (which makes sense when you think of it). This means that  10 0  is "less than"  7 0,  and  7 0  is "less than"  8.</p>
        <p id="p_03826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03826"> Of course, if two strings have identical first characters,  sort  then compares the second characters.</p>
        <p id="p_03827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03827"> For example, when  sort  sorts  7 2  and  7$,  the first characters are identical;  sort  then compares the unique number representing 2 with the number representing $. As it happens, the number for $ is smaller, so  7$  is "less than"  7 2.</p>
        <p id="p_03828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03828"> NOTE</p>
        <p id="p_03829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03829"> The set of unique numbers that correspond to the characters understood by the computer is known as the  ASCII character set.</p>
        <p id="p_03830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03830"> Most computers today use the ASCII character set, with a couple of exceptions as follows:</p>
        <p id="p_03831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03831"> • Some IBM computers use an IBM-developed character set called  EBCDIC.  EBCDIC works the same way as ASCII. In both cases, a character such as  r  or  1  is translated into a number that represents it. The only difference between EBCDIC and ASCII is that the translated numbers are different.</p>
        <p id="p_03832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03832"> • Computers that print a variety of spoken languages, or which deal with languages such as Japanese or Chinese, use a more complicated 16-bit code to represent the wide variety of characters they understand.</p>
        <p id="p_03833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03833"> You don't really need to worry about what character set your machine uses, except to take note of the sorting order. A complete listing of the ASCII characters can be found in Appendix B, "ASCII Character Set."</p>
        <p id="p_03834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03834"> Using Other Sort Keys</p>
        <p id="p_03835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03835"> Normally,  sort  sorts in alphabetical order. You can tell the Perl interpreter to sort using any criterion you like. To learn more about sort keys, refer to  Day  9 ,  "Using Subroutines."</p>
        <p id="p_03836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03836"> Reversing a List or Array Variable</p>
        <p id="p_03837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03837"> The library function  reverse  reverses the order of the elements of a list or array variable, and returns the reversed list.</p>
        <p id="p_03838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03838"> The syntax for the  reverse  library function is</p>
        <p id="p_03839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03839"> retlist = reverse (array);</p>
        <p id="p_03840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03840"> array  is the list to reverse, and  retlist  is the reversed list.</p>
        <p id="p_03841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03841"> Here is an example:</p>
        <p id="p_03842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03842"> @array = ("backwards", "is", "array", "this");</p>
        <p id="p_03843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03843"> @array2 = reverse(@array);</p>
        <p id="p_03844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03844"> The value assigned to @ array2  is the list</p>
        <p id="p_03845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03845"> ("this", "array", "is", "backwards")</p>
        <p id="p_03846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03846"> As with  sort, reverse  does not change the original array.</p>
        <p id="p_03847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03847"> If you like, you can sort and reverse the same list by passing the list returned by  sort  to  reverse. Listing 5.13 shows an example of this. It reads lines of data from the standard input file and sorts them in reverse order.</p>
        <p id="p_03848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03848"> Type</p>
        <p id="p_03849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03849"> Listing 5.13. A program that sorts input lines in reverse order.</p>
        <p id="p_03850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03850"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03851"> 2 :</p>
        <p id="p_03852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03852"> 3: @input = &lt;STDIN&gt;;</p>
        <p id="p_03853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03853"> 4: @input = reverse (sort (@input));</p>
        <p id="p_03854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03854"> 5: print (@input);</p>
        <p id="p_03855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03855"> Output</p>
        <p id="p_03856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03856"> $ program5_13 too</p>
        <p id="p_03857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03857"> bar</p>
        <p id="p_03858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03858"> dip baz A D f oo dip baz bar $</p>
        <p id="p_03859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03859"> Analysis</p>
        <p id="p_03860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03860"> Line 3 reads all the input lines from the standard input file into the array variable @ input . Each element of input consists of a single line of input terminated with a newline character.</p>
        <p id="p_03861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03861"> Line 4 sorts and reverses the input line. First,  sort  is called to sort the input lines in alphabetical order. (Recall that when one library function appears inside another, the innermost one is called first.) The list returned by  sort  is then passed to  reverse,  which reverses the order of the elements of the list. The result is a list sorted in reverse order, which is then assigned to @ input.</p>
        <p id="p_03862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03862"> Line 5 prints the sorted lines. Because each line is terminated by a newline character, no extra spaces or newline characters need to be added to make the output readable.</p>
        <p id="p_03863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03863"> NOTE</p>
        <p id="p_03864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03864"> If you like, you can omit the parentheses to the call to  reverse. This gives you the following statement:</p>
        <p id="p_03865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03865"> @input = reverse sort (@input);</p>
        <p id="p_03866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03866"> Here is a case where eliminating a set of parentheses actually makes the code more readable; it is obvious that the statement sorts @  input  in reverse order.</p>
        <p id="p_03867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03867"> Using chop on Array Variables</p>
        <p id="p_03868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03868"> As you've seen, the  chop  library function removes the last character from a character string. The following is an example:</p>
        <p id="p_03869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03869"> $var = "bathe";</p>
        <p id="p_03870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03870"> chop ($var); # $var now contains "bath"</p>
        <p id="p_03871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03871"> The  chop  function also can work on lists in array variables. If you pass an array variable to  chop,  it removes the last character from every element in the list stored in the array variable. For example:</p>
        <p id="p_03872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03872"> @list = ("rabbit", "12345", "quartz"); chop (@list);</p>
        <p id="p_03873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03873"> After  chop  is called, the list stored in @ list  is</p>
        <p id="p_03874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03874"> ("rabbi", "1234", "quart")</p>
        <p id="p_03875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03875"> The  chop  function often is used on arrays read from the standard input file, as shown in the following:</p>
        <p id="p_03876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03876"> @array = &lt;STDIN&gt;; chop (@array);</p>
        <p id="p_03877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03877"> This call to  chop  removes the newline character from each input line. In the following section, you will see programs in which this is helpful.</p>
        <p id="p_03878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03878"> Creating a Single String from a List</p>
        <p id="p_03879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03879"> The library function  join  creates a single string from a list of strings, which then can be assigned to a scalar variable.</p>
        <p id="p_03880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03880"> The syntax for the  join  library function is</p>
        <p id="p_03881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03881"> string = join (array);</p>
        <p id="p_03882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03882"> array  is the list to join together, and  string  is the resulting character string.</p>
        <p id="p_03883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03883"> The following is an example using  join:</p>
        <p id="p_03884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03884"> $string = join(" ", "this", "is", "a", "string");</p>
        <p id="p_03885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03885"> The first element of the list supplied to  join  contains the characters that are to be used to join the parts of the created string together. In this example, $  string  becomes  this is a string.</p>
        <p id="p_03886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03886"> join  can specify other join strings besides " ". For example, the following statement uses a pair of colons to join the strings:</p>
        <p id="p_03887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03887"> $string =  join"words",  "and", "colons");</p>
        <p id="p_03888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03888"> In this statement, $  string  becomes  words: :and: : colons.</p>
        <p id="p_03889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03889"> You can use any list or array variable as part or all of the argument to  join.  For example:</p>
        <p id="p_03890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03890"> @list = ("here", "is",  "a");</p>
        <p id="p_03891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03891"> $string = join(" ", @list, "string");</p>
        <p id="p_03892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03892"> This assigns  here is a string  to  $string.</p>
        <p id="p_03893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03893"> Listing 5.14 is a simple program that uses  join.  It joins together all the input lines from the standard input file.</p>
        <p id="p_03894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03894"> Type</p>
        <p id="p_03895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03895"> Listing 5.14. A program that takes its input and joins it into a single string.</p>
        <p id="p_03896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03896"> 1: #!/usr/local/bin/perl</p>
        <p id="p_03897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03897"> 2 :</p>
        <p id="p_03898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03898"> 3: @input = &lt;STDIN&gt;;</p>
        <p id="p_03899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03899"> 4 :</p>
        <p id="p_03900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03900"> chop (@input);</p>
        <p id="p_03901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03901"> 5: $string = join(" ", @input);</p>
        <p id="p_03902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03902"> 6: print ( "$string\n");</p>
        <p id="p_03903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03903"> $ program5_14</p>
        <p id="p_03904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03904"> This</p>
        <p id="p_03905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03905"> is</p>
        <p id="p_03906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03906"> my</p>
        <p id="p_03907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03907"> input</p>
        <p id="p_03908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03908"> A D</p>
        <p id="p_03909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03909"> This is my input $</p>
        <p id="p_03910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03910"> Analysis</p>
        <p id="p_03911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03911"> Line  3  reads all of the input lines into the array variable @  input . Each element of @ input  is a single line of input terminated by a newline character.</p>
        <p id="p_03912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03912"> Line 4 passes the array variable @  input  to the library function  chop,  which removes the last character from each element of the list stored in @  input . This removes all of the trailing newline characters.</p>
        <p id="p_03913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03913"> Line 5 calls  join,  which joins all the input lines into a single string. The first argument passed to join  is " ", which tells  join  to put one space between each pair of lines. This turns the list</p>
        <p id="p_03914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03914"> ("This", "is", "my", "input")</p>
        <p id="p_03915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03915"> into the string</p>
        <p id="p_03916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03916"> This is my input</p>
        <p id="p_03917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03917"> Line  6  prints the string produced by  join.  Note that the call to  print  has to specify a newline character because all the newline characters in the input lines have been removed by the call to  chop.</p>
        <p id="p_03918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03918"> Splitting a String into a List</p>
        <p id="p_03919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03919"> As you've seen, the library function join creates a character string from a list. To undo the effects of join-to split a character string into separate items-call the function  split.</p>
        <p id="p_03920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03920"> The syntax for the library function  split  is</p>
        <p id="p_03921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03921"> array = split (string);</p>
        <p id="p_03922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03922"> string  is the character string to split, and  array  is the resulting array.</p>
        <p id="p_03923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03923"> The following is a simple example of the use of  split:</p>
        <p id="p_03924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03924"> $string = "words::separated::by::colons";</p>
        <p id="p_03925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03925"> @array = split(/::/, $string);</p>
        <p id="p_03926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03926"> The first argument passed to  split  tells it where to break the string into separate parts. In this example, the first argument is : : (two colons); because there are three pairs of colons in the string, split  breaks the string into four separate parts. The result is the list</p>
        <p id="p_03927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03927"> ("words", "separated", "by", "colons")</p>
        <p id="p_03928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03928"> which is assigned to the array variable @ array.</p>
        <p id="p_03929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03929"> NOTE</p>
        <p id="p_03930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03930"> The / characters surrounding the : : in the call to  split indicate that the : : is a  pattern  to be matched. Perl supports a wide variety of special pattern-matching sequences, which you will learn about on  Day  7 ,  "Pattern Matching."</p>
        <p id="p_03931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03931"> The  split  function is used in a variety of applications. Listing 5.15 uses  split  to count the number of words in the standard input file.</p>
        <p id="p_03932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03932"> Type</p>
        <p id="p_03933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03933"> Listing 5.15. A simple word-count program.</p>
        <p id="p_03934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03934"> 1: 2 :</p>
        <p id="p_03935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03935"> 3 :</p>
        <p id="p_03936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03936"> 4 :</p>
        <p id="p_03937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03937"> 5 : 6 :</p>
        <p id="p_03938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03938"> 7 :</p>
        <p id="p_03939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03939"> 8  : 9:</p>
        <p id="p_03940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03940"> #!/usr/local/bin/perl</p>
        <p id="p_03941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03941"> $wordcount = 0;</p>
        <p id="p_03942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03942"> $line = &lt;STDIN&gt;; while ($line ne "") {</p>
        <p id="p_03943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03943"> chop ($line);</p>
        <p id="p_03944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03944"> @array = split (/ /, $line); $wordcount += @array;</p>
        <p id="p_03945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03945"> $line = &lt;STDIN&gt;;</p>
        <p id="p_03946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03946"> 10 : }</p>
        <p id="p_03947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03947"> 11: print ("Total number of words: $wordcount\n");</p>
        <p id="p_03948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03948"> Output</p>
        <p id="p_03949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03949"> $ program5_15 Here is some input.</p>
        <p id="p_03950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03950"> Here are some more words. Here is my last line.</p>
        <p id="p_03951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03951"> A D</p>
        <p id="p_03952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03952"> Total number of words: 14</p>
        <p id="p_03953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03953"> $</p>
        <p id="p_03954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03954"> When you enter a Ctrl+D (End-of-File) character and read it using  &lt;STDIN&gt;,  the resulting line is the null string. Line 5 of this program tests for this null string.</p>
        <p id="p_03955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03955"> Note that line 5 has no problem distinguishing the end of file from a blank input line because a blank input line contains the newline character, and  chop  has not yet been called. Once the Perl interpreter knows that the program is not at the end of file, line 6 can be called; it chops the newline character off the end of the input line.</p>
        <p id="p_03956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03956"> Line 7 splits the input line into words. The first argument to split, / /, indicates that the line is to be broken whenever the Perl interpreter sees a space. The resulting list is stored in @ array.</p>
        <p id="p_03957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03957"> Because each element of the list in @  array  is one word in the input line, the total number of words in the line is equivalent to the number of elements in the array. Line 8 takes advantage of this to count the number of words in the input line. Here's how line 8 works:</p>
        <p id="p_03958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03958"> • When an array variable appears in a place where the Perl interpreter normally expects a scalar value, the number of elements in the list stored in the array variable is substituted for the variable name. In this program, when the Perl interpreter sees @ array,  it replaces it with the number of elements in @ array.</p>
        <p id="p_03959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03959"> • Because the number of elements in the array is the same as the number of words in the input line, the statement</p>
        <p id="p_03960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03960"> $wordcount += @array;</p>
        <p id="p_03961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03961"> actually adds the number of words in the line to  $wordcount.</p>
        <p id="p_03962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03962"> Analysis</p>
        <p id="p_03963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03963"> NOTE</p>
        <p id="p_03964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03964"> Listing 5.15 does not work properly if an input line contains more than one space between words. The following is an example:</p>
        <p id="p_03965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03965"> This is a line</p>
        <p id="p_03966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03966"> Because there are two spaces between  This  and  is,  the  split function breaks</p>
        <p id="p_03967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03967"> This is</p>
        <p id="p_03968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03968"> into three words:  This,  an empty word " ", and  is.  Because of this, the line</p>
        <p id="p_03969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03969"> This is a line</p>
        <p id="p_03970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03970"> appears to contain five words when it really contains only four.</p>
        <p id="p_03971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03971"> To get around this problem, what you need is a pattern that matches one or more spaces. To learn about special patterns such as this, see  Day  7 .</p>
        <p id="p_03972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03972"> Listing 5.16 is an example of a program that uses  split, join,  and  reverse  to reverse the word order of the input read from the standard input file.</p>
        <p id="p_03973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03973"> Type</p>
        <p id="p_03974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03974"> Listing 5.16. A program that reverses the word order of the input file.</p>
        <p id="p_03975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03975"> 15: # now, reverse the order of the input lines and print them</p>
        <p id="p_03976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03976"> 16: @input = reverse(@input); 17: print (@input);</p>
        <p id="p_03977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03977"> $ program5_16 This sentence is in</p>
        <p id="p_03978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03978"> reverse order. A D</p>
        <p id="p_03979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03979"> order, reverse in is</p>
        <p id="p_03980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03980"> sentence This $</p>
        <p id="p_03981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03981"> Analysis</p>
        <p id="p_03982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03982"> Line 3 reads all of the standard input file into the array @  input . Line 4 then removes the trailing newline characters from the input lines.</p>
        <p id="p_03983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03983"> Lines 7-13 reverse each individual line. Line 7 compares the current line number, stored in $ cur r line,  with the number of lines of input. (Recall that the number of elements in the list is used whenever an array variable appears where a scalar value is expected.)</p>
        <p id="p_03984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03984"> Line  9  splits a line of input into words. The first argument to  split,  / /, indicates that a split is to occur every time a space is seen. The list of words is stored in the array variable  @words.</p>
        <p id="p_03985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03985"> Line 10 reverses the order of the list of words stored in  @words.  After the list has been reversed, line 11 joins the input line back together again. Note that line 11 appends a newline character to the input line.</p>
        <p id="p_03986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03986"> Now that the words in each individual line have been reversed, all that the program needs to do is reverse the order of the lines themselves. Line 16 accomplishes this.</p>
        <p id="p_03987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03987"> Line 17 prints the reversed input file. Note that the period character (.) appears at the end of the first word; this is because the reversing program isn't smart enough to detect and get rid of it. (You can use split  to get rid of this, too, if you want.)</p>
        <p id="p_03988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03988"> Other List-Manipulation Functions</p>
        <p id="p_03989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03989"> Perl provides several other list-manipulation functions also. To learn about these, refer to  Day 14 , "Scalar-Conversion and List-Manipulation Functions."</p>
        <p id="p_03990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03990"> Summary</p>
        <p id="p_03991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03991"> In today's lesson, you learned about lists and array variables. A list is an ordered collection of scalar values. A list can consist of any number of scalar values.</p>
        <p id="p_03992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03992"> Lists can be stored in array variables, which are variables whose names begin with the character @.</p>
        <p id="p_03993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03993"> Individual elements of array variables can be accessed using subscripts. The subscript  0  refers to the first element of the list stored in the array variable, the subscript 1 refers to the second element, and so on. If an array element is not defined, it is assumed to hold the null string " ". If a previously undefined array element is assigned to, the array grows appropriately.</p>
        <p id="p_03994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03994"> The list-range operator provides a convenient way to create a list containing consecutive numbers.</p>
        <p id="p_03995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03995"> You can copy lists from one array variable to another. In addition, you can include an array variable in a list, which means that the list stored in the array variable is copied into the list containing the array-variable name.</p>
        <p id="p_03996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03996"> Array-variable names can appear in character strings; in this case, the elements of the list are included in place of the variable name, with a space separating each pair of elements.</p>
        <p id="p_03997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03997"> You can assign values to scalar variables from array variables, and vice versa.</p>
        <p id="p_03998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03998"> If an array variable appears in a place where a scalar variable is expected, the length of the list stored in the array variable is used.</p>
        <p id="p_03999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_03999"> You can access any part of a list stored in an array variable by using the array-slice notation. You can assign values to array slices, and they can be used anywhere a list is expected.</p>
        <p id="p_04000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04000"> The entire contents of the standard input file can be stored in a single array variable.</p>
        <p id="p_04001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04001"> The library functions  sort  and  reverse  sort and reverse lists, respectively. The function  chop removes the last character from each element of a list. The function  split  breaks a single string into</p>
        <p id="p_04002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04002"> a collection of list elements. The function  join  takes a collection of list elements and joins them into a single string.</p>
        <p id="p_04003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04003"> Q&amp;A</p>
        <p id="p_04004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04004"> Q: How can I tell whether a reference to an array variable such as @  array  refers to the</p>
        <p id="p_04005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04005"> stored list or to the length of the list?</p>
        <p id="p_04006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04006"> A: It's usually pretty easy to tell. In a lot of places, using a list makes no sense:</p>
        <p id="p_04007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04007"> $result = $number + @array;</p>
        <p id="p_04008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04008"> For example, it makes no sense here to add a list to $  number,  so the length of the list stored in  @array  is used.</p>
        <p id="p_04009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04009"> Q: Why do array elements use $ for the first character of the element name, and not @?</p>
        <p id="p_04010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04010"> Wouldn't it make more sense to refer to an array element as @array[2]</p>
        <p id="p_04011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04011"> because we all know that the @ indicates an array variable?</p>
        <p id="p_04012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04012"> A: This relates to the first question. The Perl interpreter needs to know as soon as possible</p>
        <p id="p_04013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04013"> whether a variable reference is a scalar value or a list. The $ indicates right away that the upcoming item is a scalar value.</p>
        <p id="p_04014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04014"> Eventually, you'll get used to this notation.</p>
        <p id="p_04015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04015"> Q: Is there a difference between an undefined array variable and an array variable</p>
        <p id="p_04016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04016"> containing the empty list?</p>
        <p id="p_04017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04017"> A: No. By default, all array variables contain the empty list. Note, however, that the empty list is</p>
        <p id="p_04018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04018"> not the same as a list containing the null string:</p>
        <p id="p_04019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04019"> @array =  ("");</p>
        <p id="p_04020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04020"> This list contains one element, which happens to be a null string.</p>
        <p id="p_04021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04021"> Q: How large an input file can I read in using the following statement?</p>
        <p id="p_04022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04022"> @array = &lt;STDIN&gt;;</p>
        <p id="p_04023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04023"> A: Perl imposes no limit on the size of arrays. Your computer, however, has a finite amount of</p>
        <p id="p_04024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04024"> memory, which limits how large your arrays can be.</p>
        <p id="p_04025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04025"> Q: Why does Perl add spaces when you substitute for an array variable in a string?</p>
        <p id="p_04026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04026"> A: The most common use of string substitution is in the  print  statement. Normally, when you</p>
        <p id="p_04027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04027"> print a list you don't want to have the elements of the list running together, because you want to see where one element stops and the next one starts.</p>
        <p id="p_04028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04028"> To print the elements of a string without spaces between them, pass the list to  print  without enclosing it in a string, as follows:</p>
        <p id="p_04029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04029"> print ("Here is my list", @list,  "\n");</p>
        <p id="p_04030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04030"> Q: Why does $ appear before 1 in the ASCII character set?</p>
        <p id="p_04031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04031"> A: The short answer is: Just because. (This reasoning occurs more often in computing than you</p>
        <p id="p_04032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04032"> might think.)</p>
        <p id="p_04033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04033"> Here's a more detailed explanation: On early machines that used the ASCII character set, performance was more efficient if there was a relationship between, for instance, the location of the uppercase alphabetic characters and the lowercase alphabetic characters. (In fact, if you add  0x2 0,  or 20 hexadecimal, to the ASCII representation of an uppercase letter, you get the corresponding lowercase letter.)</p>
        <p id="p_04034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04034"> Establishing relationships such as these meant that gaps existed between, for example, the representation of  Z  (which is 90) and the representation of  a  (which is 97). These gaps are filled by printable non-alphanumeric characters; for example, the representation of [ is 91.</p>
        <p id="p_04035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04035"> As for why $ appears before 1, as opposed to ?, which appears after 1, the explanation is: Just because.</p>
        <p id="p_04036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04036"> Workshop</p>
        <p id="p_04037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04037"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_04038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04038"> Quiz</p>
        <p id="p_04039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04039"> 1. Define the following terms:</p>
        <p id="p_04040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04040"> a. list</p>
        <p id="p_04041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04041"> b. empty list</p>
        <p id="p_04042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04042"> c. array variable</p>
        <p id="p_04043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04043"> d. subscript</p>
        <p id="p_04044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04044"> e. array slice</p>
        <p id="p_04045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04045"> 2. Assume the following assignments have been performed:</p>
        <p id="p_04046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04046"> @list = (1, 2, 3);</p>
        <p id="p_04047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04047"> $scalarl = "hello";</p>
        <p id="p_04048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04048"> $scalar2 = "there";</p>
        <p id="p_04049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04049"> What is assigned to the array variable  @newlist  in each of the following cases?</p>
        <p id="p_04050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04050"> a.  @newlist = @list;</p>
        <p id="p_04051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04051"> b.  @newlist = reverse(@list[1,2]);</p>
        <p id="p_04052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04052"> c. @newlist = ($scalarl, @list[l,l]);</p>
        <p id="p_04053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04053"> d.  ($dummy, @newlist) = @list;</p>
        <p id="p_04054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04054"> e.  @newlist [2 ,  1, 3] =  @list  [  1,2,1   ];</p>
        <p id="p_04055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04055"> f.  @newlist  =  &lt;STDIN&gt;;</p>
        <p id="p_04056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04056"> 3. Assume that the following assignments have been performed:</p>
        <p id="p_04057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04057"> @list1 = (1, 2, 3, 4);</p>
        <p id="p_04058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04058"> @list2 = ("one", "two", "three");</p>
        <p id="p_04059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04059"> What is the value of  $result  in each of the following cases?</p>
        <p id="p_04060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04060"> ($dummy, $result) = @listl;</p>
        <p id="p_04061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04061"> $result = @listl;</p>
        <p id="p_04062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04062"> ($result) = @list2;</p>
        <p id="p_04063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04063"> ($result) = @listl [ 1. . 2];</p>
        <p id="p_04064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04064"> $result = $list2[$list1 [ $list1 [  0]]];</p>
        <p id="p_04065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04065"> $result = $list2[3];</p>
        <p id="p_04066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04066"> 4.  What is the difference between a list and an array variable?</p>
        <p id="p_04067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04067"> 5. How does the Perl interpreter distinguish between an array element and a scalar variable?</p>
        <p id="p_04068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04068"> 6. How can you ensure that the @, $, and [ characters are not substituted for in strings?</p>
        <p id="p_04069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04069"> 7. How can you obtain the length of a list stored in an array variable?</p>
        <p id="p_04070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04070"> 8. What happens when you refer to an array element that has not yet been defined?</p>
        <p id="p_04071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04071"> 9. What happens when you assign to an array element that is larger than the current length of the array?</p>
        <p id="p_04072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04072"> Exercises</p>
        <p id="p_04073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04073"> 1. Write a program that counts all occurrences of the word the in the standard input file.</p>
        <p id="p_04074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04074"> 2. Write a program that reads lines of input containing numbers, each of which is separated by exactly one space, and prints out the following:</p>
        <p id="p_04075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04075"> a. The total for each line</p>
        <p id="p_04076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04076"> b. The grand total</p>
        <p id="p_04077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04077"> 3. Write a program that reads all input from the standard input file and sorts all the words in reverse order, printing out one word per line with duplicates omitted.</p>
        <p id="p_04078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04078"> 4. BUG BUSTER: What is wrong with the following statement?</p>
        <p id="p_04079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04079"> $result = @array[4];</p>
        <p id="p_04080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04080"> 5. BUG BUSTER: What is wrong with the following program? (See if you can figure out what's wrong without checking the listings in today's lesson.)</p>
        <p id="p_04081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04081"> #!/usr/local/bin/perl</p>
        <p id="p_04082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04082"> @input =  &lt;STDIN&gt; ;</p>
        <p id="p_04083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04083"> $currline  = 1;</p>
        <p id="p_04084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04084"> while  ($currline &lt; @input) {</p>
        <p id="p_04085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04085"> @words = split(/   /,  $input [  $currline]);</p>
        <p id="p_04086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04086"> @words = sort(@words);</p>
        <p id="p_04087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04087"> $input[$currline] = join(" ", @words);</p>
        <p id="p_04088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04088"> $currline++;</p>
        <p id="p_04089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04089"> }</p>
        <p id="p_04090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04090"> print (@input);</p>
        <p id="p_04091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04091"> Chapter 6</p>
        <p id="p_04092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04092"> Reading from and Writing to Files</p>
        <p id="p_04093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04093"> CONTENTS</p>
        <p id="p_04094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04094"> •  Opening a File</p>
        <p id="p_04095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04095"> o  The File Variable o  The Filename o  The File Mode</p>
        <p id="p_04096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04096"> o  Checking Whether the Open Succeeded</p>
        <p id="p_04097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04097"> •  Reading from a File</p>
        <p id="p_04098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04098"> o  File Variables and the Standard Input File</p>
        <p id="p_04099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04099"> o  Terminating a Program Using  die</p>
        <p id="p_04100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04100"> o  Reading into Array Variables</p>
        <p id="p_04101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04101"> •  Writing to a File</p>
        <p id="p_04102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04102"> o  The Standard Output File Variable</p>
        <p id="p_04103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04103"> o  Merging Two Files into One</p>
        <p id="p_04104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04104"> •  Redirecting Standard Input and Standard Output</p>
        <p id="p_04105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04105"> •  The Standard Error File</p>
        <p id="p_04106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04106"> •  Closing a File</p>
        <p id="p_04107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04107"> •  Determining the Status of a File</p>
        <p id="p_04108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04108"> o  File-Test Operator Syntax o  Available File-Test Operators</p>
        <p id="p_04109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04109"> o  More on the  -e  Operator o  Testing for Read Permission-the -r Operator</p>
        <p id="p_04110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04110"> o  Checking for Other Permissions</p>
        <p id="p_04111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04111"> o  Checking for Empty Files o  Using File-Test Operators with File Variables</p>
        <p id="p_04112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04112"> •  Reading from a Sequence of Files</p>
        <p id="p_04113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04113"> o  Reading into an Array Variable</p>
        <p id="p_04114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04114"> •  Using Command-Line Arguments as Values</p>
        <p id="p_04115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04115"> o  ARGV  and the  &lt;&gt;  Operator</p>
        <p id="p_04116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04116"> •  Opening Pipes</p>
        <p id="p_04117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04117"> •  Summary</p>
        <p id="p_04118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04118"> •  O&amp;A</p>
        <p id="p_04119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04119"> •  Workshop</p>
        <p id="p_04120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04120"> o  Quiz o  Exercises</p>
        <p id="p_04121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04121"> So far, you've learned to read input from the standard input file, which stores data that is entered from the keyboard. You've also learned how to write to the standard output file, which sends data to your screen. In today's lesson, you'll learn the following:</p>
        <p id="p_04122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04122"> • How to open a file</p>
        <p id="p_04123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04123"> • How to read from and write to an opened file</p>
        <p id="p_04124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04124"> • How to redirect standard input and standard output and how to use the standard error file</p>
        <p id="p_04125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04125"> • How to close a file</p>
        <p id="p_04126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04126"> • About file-test operators, which determine the status of a file</p>
        <p id="p_04127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04127"> • How to read from multiple files</p>
        <p id="p_04128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04128"> • How to use command-line arguments</p>
        <p id="p_04129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04129"> • How to open pipes</p>
        <p id="p_04130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04130"> Opening a File</p>
        <p id="p_04131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04131"> Before you can read from or write to a file, you must first open the file. This operation tells the operating system that you are currently accessing the file and that no one else can change it while you are working with it. To open a file, call the library function  open.</p>
        <p id="p_04132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04132"> The syntax for the  open  library function is</p>
        <p id="p_04133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04133"> open (filevar, filename);</p>
        <p id="p_04134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04134"> When you call  open,  you must supply two arguments:</p>
        <p id="p_04135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04135"> • f ilevar  represents the name you want to use in your Perl program to refer to the file.</p>
        <p id="p_04136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04136"> • filename  represents the location of the file on your machine.</p>
        <p id="p_04137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04137"> The File Variable</p>
        <p id="p_04138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04138"> The first argument passed to  open  is the name that the Perl interpreter uses to refer to the file. This name is also known as th efile variable  (or  ihe,  file handle).</p>
        <p id="p_04139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04139"> A file-variable name can be any sequence of letters, digits, and underscores, as long as the first character is a letter.</p>
        <p id="p_04140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04140"> The following are legal file-variable names:</p>
        <p id="p_04141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04141"> filename MY NAME</p>
        <p id="p_04142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04142"> NAME 2</p>
        <p id="p_04143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04143"> A_REALL Y_L ONG_FILE_VARIAB LE_NAME</p>
        <p id="p_04144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04144"> The following are not legal file-variable names:</p>
        <p id="p_04145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04145"> 1NAME</p>
        <p id="p_04146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04146"> A.FILE.NAME</p>
        <p id="p_04147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04147"> _ANOTHERNAME</p>
        <p id="p_04148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04148"> if</p>
        <p id="p_04149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04149"> if is not a valid file-variable name because it has another meaning: as you've seen, it indicates the start of an  if  statement. Words such as  if  that have special meanings in Perl are known as  resented words  and cannot be used as names.</p>
        <p id="p_04150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04150"> Tip</p>
        <p id="p_04151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04151"> It's a good idea to use all uppercase letters for your file-variable names. This makes it easier to distinguish file-variable names from other variable names and from reserved words.</p>
        <p id="p_04152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04152"> The Filename</p>
        <p id="p_04153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04153"> The second item passed to  open  is the name of the file you want to open. For example, if you are running Perl on a UNIX file system, and your current working directory contains a file named  f ilel  that you would like to open, you can open it as follows:</p>
        <p id="p_04154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04154"> open(FILEl, "filel");</p>
        <p id="p_04155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04155"> This statement tells Perl that you want to open the file  filel  and associate it with the file variable  FI LEI. If you want to open a file in a different directory, you can specify the complete pathname, as follows:</p>
        <p id="p_04156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04156"> open(FILE1, "/u/jqpublic/filel") ;</p>
        <p id="p_04157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04157"> This opens the file  /u/ jqpublic/ filel  and associates it with the file variable  FI LEI.</p>
        <p id="p_04158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04158"> NOTE</p>
        <p id="p_04159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04159"> If you are running Perl on a file system other than UNIX, use the filename and directory syntax that is appropriate for your system. The Perl interpreter running on that system will be able to figure out where your file is located.</p>
        <p id="p_04160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04160"> The File Mode</p>
        <p id="p_04161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04161"> When you open a file, you must decide how you want to access the file. There are three different  file-access modes  (or, simply,  file modes)  available in Perl:</p>
        <p id="p_04162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04162"> By default,  open  assumes that a file is to be opened in read mode. To specify write mode, put a &gt; character in front of the filename that you pass to  open,  as follows:</p>
        <p id="p_04163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04163"> open (OUTFILE, "&gt;/u/jqpublic/outfile");</p>
        <p id="p_04164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04164"> This opens the file  /u/ jqpublic/out file  for writing and associates it with the file variable OUTFILE.</p>
        <p id="p_04165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04165"> To specify append mode, put two &gt; characters in front of the filename, as follows:</p>
        <p id="p_04166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04166"> open (APPENDFILE, "&gt;&gt;/u/jqpublic/appendfile");</p>
        <p id="p_04167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04167"> This opens the file  /u/ jqpublic/appendf ile  in append mode and associates it with the file variable APPENDFILE.</p>
        <p id="p_04168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04168"> NOTE</p>
        <p id="p_04169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04169"> Here are a few things to remember when opening files:</p>
        <p id="p_04170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04170"> • When you open a file for writing, any existing contents are destroyed.</p>
        <p id="p_04171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04171"> • You cannot read from and write to the same file at the same time.</p>
        <p id="p_04172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04172"> • When you open a file in append mode, the existing contents are not destroyed, but you cannot read the file while writing to it.</p>
        <p id="p_04173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04173"> Checking Whether the Open Succeeded</p>
        <p id="p_04174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04174"> Before you can use a file opened by the  open  function, you should first check whether the  open  function actually is giving you access to the file. The  open  function enables you to do this by returning a value indicating whether the file-opening operation succeeded:</p>
        <p id="p_04175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04175"> • If  open  returns a nonzero value, the file has been opened successfully.</p>
        <p id="p_04176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04176"> • If  open  returns  0,  an error has occurred.</p>
        <p id="p_04177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04177"> As you can see, the values returned by  open  correspond to the values for true and false in conditional expressions. This means that you can use  open  in  if  and  unless  statements. The following is an example:</p>
        <p id="p_04178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04178"> if (open(MYFILE, "/u/jqpublic/myfile")) {</p>
        <p id="p_04179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04179"> # here's what to do if the file opened</p>
        <p id="p_04180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04180"> }</p>
        <p id="p_04181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04181"> The code inside the if statement is executed only if the file has been successfully opened. This ensures that your programs read or write only to files that you can access.</p>
        <p id="p_04182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04182"> NOTE</p>
        <p id="p_04183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04183"> If  open  returns false, you can find out what went wrong by using the file-test operators, which you'll learn about later today.</p>
        <p id="p_04184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04184"> Reading from a File</p>
        <p id="p_04185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04185"> Once you have opened a file and determined that the file is available for use, you can read information from it.</p>
        <p id="p_04186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04186"> To read from a file, enclose the file variable associated with the file in angle brackets (&lt; and &gt;), as follows:</p>
        <p id="p_04187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04187"> $line</p>
        <p id="p_04188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04188"> &lt;MYFILE&gt;;</p>
        <p id="p_04189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04189"> This statement reads a line of input from the file specified by the file variable  MYFILE  and stores the line of input in the scalar variable  $line.</p>
        <p id="p_04190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04190"> Listing 6.1 is a simple program that reads input from a file and writes it to the standard output file.</p>
        <p id="p_04191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04191"> Type</p>
        <p id="p_04192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04192"> Listing  6.1. A  program that reads lines from a file and prints them.</p>
        <p id="p_04193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04193"> Output</p>
        <p id="p_04194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04194"> $ program6_l</p>
        <p id="p_04195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04195"> Here is a line of input.</p>
        <p id="p_04196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04196"> Here is another line of input. Here is the last line of input.</p>
        <p id="p_04197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04197"> $</p>
        <p id="p_04198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04198"> Analysis</p>
        <p id="p_04199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04199"> Line 3 opens the file  f ilel  in read mode, which means that the file is to be made available for reading,  f ilel  is assumed to be in the current working directory. The file variable  MYFILE  is associated with the file  f i 1 e 1.</p>
        <p id="p_04200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04200"> If the call to  open  returns a nonzero value, the conditional expression</p>
        <p id="p_04201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04201"> open(MYFILE, "filel")</p>
        <p id="p_04202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04202"> is assumed to be true, and the code inside the  i f  statement is executed.</p>
        <p id="p_04203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04203"> Lines 4-8 print the contents of filel. The sample output shown here assumes that filel contains the following three lines:</p>
        <p id="p_04204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04204"> Line 4 reads the first line of input from the file specified by the file variable  MYFILE,  which is  filel.  This line of input is stored in the scalar variable  $line.</p>
        <p id="p_04205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04205"> Line 5 tests whether the end of the file specified by  MYFILE  has been reached. If there are no more lines left in  MYFILE, $line  is assigned the empty string.</p>
        <p id="p_04206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04206"> Line  6  prints the text stored in  $line,  which is the line of input read from  MYFILE.</p>
        <p id="p_04207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04207"> Line  7  reads the next line of  MYFILE,  preparing for the loop to start again.</p>
        <p id="p_04208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04208"> File Variables and the Standard Input File</p>
        <p id="p_04209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04209"> Now that you have seen how Perl programs read input from files in read mode, take another look at a statement that reads a line of input from the standard input file.</p>
        <p id="p_04210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04210"> $line = &lt;STDIN&gt;;</p>
        <p id="p_04211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04211"> Here's what is actually happening: The Perl program is referencing the file variable  STD IN,  which represents the standard input file. The &lt; and &gt; on either side of  STD IN  tell the Perl interpreter to read a line of input from the standard input file, just as the &lt; and &gt; on either side of  MYFILE  in</p>
        <p id="p_04212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04212"> $line</p>
        <p id="p_04213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04213"> &lt;MYFILE&gt;;</p>
        <p id="p_04214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04214"> tell the Perl interpreter to read a line of input from  MYFILE.</p>
        <p id="p_04215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04215"> STD IN  is a file variable that behaves like any other file variable representing a file in read mode. The only difference is that  STD IN  does not need to be opened by the  open  function because the Perl interpreter does that for you.</p>
        <p id="p_04216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04216"> Terminating a Program Using die</p>
        <p id="p_04217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04217"> In Listing 6.1, you saw that the return value from  open  can be tested to see whether the program actually has access to the file. The code that operates on the opened file is contained in an  if  statement.</p>
        <p id="p_04218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04218"> If you are writing a large program, you might not want to put all of the code that affects a file inside an  i f statement, because the distance between the beginning of the  i f  statement and the closing brace (}) could get very large. For example:</p>
        <p id="p_04219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04219"> if (open(MYFILE, "filel")) {</p>
        <p id="p_04220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04220"> # this could be many pages of statements!</p>
        <p id="p_04221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04221"> }</p>
        <p id="p_04222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04222"> Besides, after a while, you'll probably get tired of typing the spaces or tabs you use to indent the code inside the  if  statement. Perl provides a way around this using the library function  die.</p>
        <p id="p_04223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04223"> The syntax for the  die  library function is</p>
        <p id="p_04224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04224"> die (message);</p>
        <p id="p_04225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04225"> When the Perl interpreter executes the  die  function, the program terminates immediately and prints the message passed to  die.</p>
        <p id="p_04226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04226"> For example, the statement</p>
        <p id="p_04227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04227"> die ("Stop this now!\n") ;</p>
        <p id="p_04228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04228"> prints the following on your screen and terminates the program:</p>
        <p id="p_04229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04229"> Stop this now!</p>
        <p id="p_04230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04230"> Listing  6.2  shows how you can use  die  to smoothly test whether a file has been opened correctly.</p>
        <p id="p_04231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04231"> Type</p>
        <p id="p_04232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04232"> Listing 6.2. A program that uses die when testing for a successful file open operation.</p>
        <p id="p_04233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04233"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04234"> 2  :</p>
        <p id="p_04235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04235"> 3: unless (open(MYFILE, "filel")) {</p>
        <p id="p_04236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04236"> 4: die ("cannot open input file filel\n");</p>
        <p id="p_04237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04237"> 5: }</p>
        <p id="p_04238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04238"> 6 :</p>
        <p id="p_04239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04239"> 7: # if the program gets this far, the file was</p>
        <p id="p_04240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04240"> 8: # opened successfully</p>
        <p id="p_04241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04241"> 9: $line = &lt;MYFILE&gt;;</p>
        <p id="p_04242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04242"> 10: while ($line ne "") {</p>
        <p id="p_04243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04243"> 11: print ($line);</p>
        <p id="p_04244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04244"> 12: $line = &lt;MYFILE&gt;;</p>
        <p id="p_04245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04245"> 13: }</p>
        <p id="p_04246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04246"> Output</p>
        <p id="p_04247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04247"> $ program6_2</p>
        <p id="p_04248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04248"> Here is a line of input.</p>
        <p id="p_04249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04249"> Here is another line of input.</p>
        <p id="p_04250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04250"> Here is the last line of input.</p>
        <p id="p_04251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04251"> $</p>
        <p id="p_04252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04252"> Analysis</p>
        <p id="p_04253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04253"> This program behaves the same way as the one in Listing 6.1, except that it prints out an error message when it can't open the file.</p>
        <p id="p_04254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04254"> Line 3 opens the file and tests whether the file opened successfully. Because this is an  unless  statement, the code inside the braces ({ and }) is executed unless the file opened successfully.</p>
        <p id="p_04255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04255"> Line 4 is the call to  die  that is executed if the file does not open successfully. This statement prints the following message on the screen and exits:</p>
        <p id="p_04256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04256"> cannot open input file filel</p>
        <p id="p_04257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04257"> Because line 4 terminates program execution when the file is not open, the program can make it past line 5 only if the file has been opened successfully.</p>
        <p id="p_04258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04258"> The loop in lines 9-13 is identical to the loop you saw in Listing 6.1. The only difference is that this loop is no longer inside an  i f  statement.</p>
        <p id="p_04259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04259"> NOTE</p>
        <p id="p_04260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04260"> Here is another way to write lines 3-5:</p>
        <p id="p_04261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04261"> open (MYFILE, "filel") || die ("Could not open file");</p>
        <p id="p_04262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04262"> Recall that the logical OR operator only evaluates the expression on its right if the expression on its left is false. This means that  die  is called only if  open  returns false (if the open operation fails).</p>
        <p id="p_04263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04263"> Printing Error Information Using die</p>
        <p id="p_04264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04264"> If you like, you can have  die  print the name of the Perl program and the line number of the statement containing the call to  die.  To do this, leave off the trailing newline character in the character string, as follows:</p>
        <p id="p_04265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04265"> die ("Missing input file");</p>
        <p id="p_04266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04266"> If the Perl program containing this statement is called  myprog,  and this statement is line 14 of  myprog.</p>
        <p id="p_04267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04267"> this call to  die  prints the following and exits:</p>
        <p id="p_04268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04268"> Missing input file at myprog line 14.</p>
        <p id="p_04269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04269"> Compare this with</p>
        <p id="p_04270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04270"> die ("Missing input file\n");</p>
        <p id="p_04271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04271"> which simply prints the following before exiting:</p>
        <p id="p_04272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04272"> Missing input file</p>
        <p id="p_04273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04273"> Specifying the program name and line number is useful in two cases:</p>
        <p id="p_04274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04274"> • If the program contains many similar error messages, you can use  die  to specify the line number of the message that actually appeared.</p>
        <p id="p_04275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04275"> • If the program is called from within another program, you can use  die  to indicate that this program generated the error.</p>
        <p id="p_04276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04276"> Reading into Array Variables</p>
        <p id="p_04277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04277"> Perl enables you to read an entire file into a single array variable. To do this, assign the file variable to the array variable, as follows:</p>
        <p id="p_04278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04278"> @array = &lt;MYFILE&gt;;</p>
        <p id="p_04279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04279"> This reads the entire file represented by  MYFILE  into the array variable @  array.  Each line of the file becomes an element of the list that is stored in @  array.</p>
        <p id="p_04280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04280"> Listing 6.3 is a simple program that reads an entire file into an array.</p>
        <p id="p_04281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04281"> Type</p>
        <p id="p_04282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04282"> Listing 6.3. A program that reads an entire input file into an array.</p>
        <p id="p_04283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04283"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04284"> 3: unless (open(MYFILE, "filel")) {</p>
        <p id="p_04285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04285"> 4: die ("cannot open input file filel\n");</p>
        <p id="p_04286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04286"> 5: }</p>
        <p id="p_04287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04287"> 6: @input = &lt;MYFILE&gt;;</p>
        <p id="p_04288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04288"> 7: print (@input);</p>
        <p id="p_04289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04289"> Output</p>
        <p id="p_04290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04290"> $ program6_3</p>
        <p id="p_04291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04291"> Here is a line of input.</p>
        <p id="p_04292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04292"> Here is another line of input. Here is the last line of input. $</p>
        <p id="p_04293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04293"> Lines 3-5 open the file, test whether the file has been opened successfully, and terminate the program if the file cannot be opened.</p>
        <p id="p_04294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04294"> Line  6  reads the entire contents of the file represented by  MYFILE  into the array variable @  input.</p>
        <p id="p_04295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04295"> @ input  now contains a list consisting of the following three elements:</p>
        <p id="p_04296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04296"> ("Here is a line of input.\n",</p>
        <p id="p_04297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04297"> "Here is another line of input.\n",</p>
        <p id="p_04298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04298"> "Here is the last line of input.\n")</p>
        <p id="p_04299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04299"> Note that a newline character is included as the last character of each line.</p>
        <p id="p_04300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04300"> Analysis</p>
        <p id="p_04301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04301"> Line  7  uses the  print  function to print the entire file.</p>
        <p id="p_04302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04302"> Writing to a File</p>
        <p id="p_04303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04303"> After you have opened a file in write or append mode, you can write to the file you have opened by specifying the file variable with the  print  function. For example, if you have opened a file for writing using the statement</p>
        <p id="p_04304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04304"> open(OUTFILE, "&gt;outfile");</p>
        <p id="p_04305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04305"> the following statement:</p>
        <p id="p_04306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04306"> print OUTFILE ("Here is an output line.\n");</p>
        <p id="p_04307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04307"> writes the following line to the file specified by OUTFILE, which is the file called  out file:</p>
        <p id="p_04308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04308"> Here is an output line.</p>
        <p id="p_04309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04309"> Listing 6.4 is a simple program that reads from one file and writes to another.</p>
        <p id="p_04310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04310"> Type</p>
        <p id="p_04311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04311"> Listing 6.4. A program that opens two files and copies one into another.</p>
        <p id="p_04312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04312"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04313"> 2  :</p>
        <p id="p_04314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04314"> 3: unless (open(INFILE, "filel")) {</p>
        <p id="p_04315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04315"> 4: die ("cannot open input file filel\n");</p>
        <p id="p_04316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04316"> 5: }</p>
        <p id="p_04317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04317"> 6: unless (open(OUTFILE, "&gt;outfile")) {</p>
        <p id="p_04318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04318"> 7: die ("cannot open output file outfile\n");</p>
        <p id="p_04319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04319"> 8: }</p>
        <p id="p_04320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04320"> 9: $line = &lt;INFILE&gt;;</p>
        <p id="p_04321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04321"> 10: while ($line ne "") {</p>
        <p id="p_04322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04322"> 11 :</p>
        <p id="p_04323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04323"> print OUTFILE ($line);</p>
        <p id="p_04324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04324"> $line</p>
        <p id="p_04325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04325"> &lt;INFILE&gt;;</p>
        <p id="p_04326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04326"> 12 : 13: }</p>
        <p id="p_04327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04327"> This program writes nothing to the screen because all output is directed to the file called  out file.</p>
        <p id="p_04328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04328"> 1  |</p>
        <p id="p_04329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04329"> I Lines 3-5 open f ilel for reading. If the file cannot be opened, line 4 is executed, which prints the following message on the screen and terminates the program:</p>
        <p id="p_04330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04330"> cannot open input file filel</p>
        <p id="p_04331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04331"> Lines 6-8 open  out file  for writing; the &gt; in  &gt;out f ile  indicates that the file is to be opened in write mode. If  out file  cannot be opened, line 7 prints the message</p>
        <p id="p_04332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04332"> cannot open output file outfile</p>
        <p id="p_04333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04333"> on the screen and terminates the program.</p>
        <p id="p_04334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04334"> The only other line in the program that you have not seen in other listings in this lesson is line 11, which writes the contents of the scalar variable  $line  on the file specified by  OUTFILE.</p>
        <p id="p_04335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04335"> Once this program has completed, the contents of  filel  are copied into  outfile.</p>
        <p id="p_04336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04336"> Make sure that files you open in write mode contain nothing valuable. When the  open  function opens a file in write mode, any existing contents are destroyed.</p>
        <p id="p_04337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04337"> The Standard Output File Variable</p>
        <p id="p_04338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04338"> If you want, your program can reference the standard output file by referring to the file variable associated with the output file. This file variable is named  STDOUT.</p>
        <p id="p_04339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04339"> By default, the  print  statement sends output to the standard output file, which means that it sends the output to the file associated with  STDOUT.  As a consequence, the following statements are equivalent:</p>
        <p id="p_04340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04340"> print ("Here is a line of output.\n"); print STDOUT ("Here is a line of output.\n");</p>
        <p id="p_04341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04341"> Merging Two Files into One</p>
        <p id="p_04342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04342"> In Perl, you can open as many files as you like, provided you define a different file variable for each one. (Actually, there is an upper limit on the number of files you can open, but it's fairly large and also system-dependent.) For an example of a program that has multiple files open at one time, take a look at Listing 6.5. This program merges two files by creating an output file consisting of one line from the first file, one line from the second file, another line from the first file, and so on. For example, if an input file named  mergel contains the lines</p>
        <p id="p_04343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04343"> al</p>
        <p id="p_04344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04344"> a2</p>
        <p id="p_04345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04345"> a3</p>
        <p id="p_04346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04346"> and another file,  merge 2 ,  contains the lines</p>
        <p id="p_04347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04347"> bl</p>
        <p id="p_04348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04348"> b2</p>
        <p id="p_04349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04349"> b3</p>
        <p id="p_04350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04350"> then the resulting output file consists of</p>
        <p id="p_04351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04351"> al</p>
        <p id="p_04352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04352"> bl</p>
        <p id="p_04353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04353"> a2</p>
        <p id="p_04354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04354"> b2</p>
        <p id="p_04355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04355"> a3</p>
        <p id="p_04356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04356"> b3</p>
        <p id="p_04357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04357"> Type</p>
        <p id="p_04358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04358"> Listing 6.5. A program that merges two files.</p>
        <p id="p_04359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04359"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04360"> 2  :</p>
        <p id="p_04361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04361"> 3: open (INFILEl, "mergel") | |</p>
        <p id="p_04362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04362"> 4: die ("Cannot open input file mergel\n");</p>
        <p id="p_04363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04363"> 5:  open (INFILE2, "merge2") ||</p>
        <p id="p_04364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04364"> 6: die ("Cannot open input file merge2\n");</p>
        <p id="p_04365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04365"> 7: $linel = &lt;INFILE1&gt;;</p>
        <p id="p_04366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04366"> 8: $line2 = &lt;INFILE2&gt;;</p>
        <p id="p_04367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04367"> 9: while ($linel ne "" || $line2 ne "") {</p>
        <p id="p_04368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04368"> 10: if ($linel ne "") {</p>
        <p id="p_04369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04369"> 11: print ($linel);</p>
        <p id="p_04370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04370"> 12 :</p>
        <p id="p_04371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04371"> $linel = &lt;INFILE1&gt;;</p>
        <p id="p_04372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04372"> Output</p>
        <p id="p_04373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04373"> $ program6_5 al bl a2 b2 a3 b3 $</p>
        <p id="p_04374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04374"> Analysis</p>
        <p id="p_04375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04375"> I Lines 3 and 4 show another way to write a statement that either opens a file or calls  die  if the open fails. Recall that the | | operator first evaluates its left operand; if the left operand evaluates to true (a nonzero value), the right operand is not evaluated because the result of the expression is true.</p>
        <p id="p_04376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04376"> Because of this, the right operand, the call to  die,  is evaluated only when the left operand is false-which happens only when the call to  open  fails and the file  mergel  cannot be opened.</p>
        <p id="p_04377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04377"> Lines 5 and 6 repeat the preceding process for the file  merge 2 .  Again, either the file is opened successfully or the program aborts by calling  die.</p>
        <p id="p_04378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04378"> The program then loops repeatedly, reading a line of input from each file each time. The loop terminates only when both files have been exhausted. If one file is empty but the other is not, the program just copies</p>
        <p id="p_04379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04379"> the line from the non-empty file to the standard output file.</p>
        <p id="p_04380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04380"> Note that the output from this program is printed on the screen. If you decide that you want to send this output to a file, you can do one of two things:</p>
        <p id="p_04381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04381"> • You can modify the program to write its output to a different file. To do this, open the file in write mode and associate it with a file variable. Then, change the  print  statements to refer to this file variable.</p>
        <p id="p_04382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04382"> • You can redirect the standard output file on the command line.</p>
        <p id="p_04383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04383"> For a discussion of the second method, see the following section.</p>
        <p id="p_04384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04384"> Redirecting Standard Input and Standard Output</p>
        <p id="p_04385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04385"> When you run programs on UNIX, you can redirect input and output using &lt; and &gt;, respectively, as follows</p>
        <p id="p_04386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04386"> myprog cinput &gt;output</p>
        <p id="p_04387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04387"> Here, when you run the program called  myprog,  the input for the program is taken from the file specified by  input  instead of from the keyboard, and the output for the program is sent to the file specified by output  instead of to the screen.</p>
        <p id="p_04388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04388"> When you run a Perl program and redirect input using &lt;, the standard input file variable  STD IN  now represents the file specified with &lt;. For example, consider the following simple program:</p>
        <p id="p_04389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04389"> #!/usr/local/bin/perl $line = &lt;STDIN&gt;; print ($line);</p>
        <p id="p_04390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04390"> Suppose this program is named  myperlprog  and is called with the command</p>
        <p id="p_04391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04391"> myperlprog &lt;filel</p>
        <p id="p_04392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04392"> In this case, the statement</p>
        <p id="p_04393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04393"> $line = &lt;STDIN&gt;;</p>
        <p id="p_04394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04394"> reads a line of input from  f ilel  because the file variable  STD IN  represents  f ilel.</p>
        <p id="p_04395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04395"> Similarly, specifying &gt; on the command file redirects the standard output file from the screen to the specified file. For example, consider this command:</p>
        <p id="p_04396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04396"> myperlprog &lt;filel &gt;outfile</p>
        <p id="p_04397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04397"> It redirects output from the standard output file to the file called  out file.  Now, the following statement writes a line of data to  out file:</p>
        <p id="p_04398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04398"> print ($line);</p>
        <p id="p_04399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04399"> The Standard Error File</p>
        <p id="p_04400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04400"> Besides the standard input file and the standard output file, Perl also defines a third built-in file variable, STDERR,  which represents the standard error file. By default, text sent to this file is written to the screen. This enables the program to send messages to the screen even when the standard output file has been redirected to write to a file. As with  STD IN  and  STDOUT,  you do not need to open  STDERR  because it automatically is opened for you.</p>
        <p id="p_04401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04401"> Listing  6.6  provides a simple example of the use of  STDERR.  The output shown in the input-output example assumes that the standard input file and standard output file have been redirected to files using &lt; and &gt;, as in</p>
        <p id="p_04402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04402"> myprog cinfile &gt;outfile</p>
        <p id="p_04403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04403"> Therefore, the only output you see is what is written to  STDERR.</p>
        <p id="p_04404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04404"> Type</p>
        <p id="p_04405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04405"> Listing 6,6.  A  program that writes to the standard error file.</p>
        <p id="p_04406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04406"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04407"> 2  :</p>
        <p id="p_04408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04408"> 3: open(MYFILE, "filel") ||</p>
        <p id="p_04409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04409"> 4: die ("Unable to open input file filel\n");</p>
        <p id="p_04410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04410"> 5: print STDERR ("File filel opened successfully.\n");</p>
        <p id="p_04411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04411"> Output</p>
        <p id="p_04412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04412"> $ program6_6</p>
        <p id="p_04413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04413"> File filel opened successfully. $</p>
        <p id="p_04414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04414"> Analysis</p>
        <p id="p_04415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04415"> This program converts the contents of a file into uppercase and sends the converted contents to the standard output file.</p>
        <p id="p_04416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04416"> Line 3 tries to open  filel.  If the file cannot be opened, line 4 is executed. This calls  die,  which prints the following message and terminates:</p>
        <p id="p_04417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04417"> Unable to open input file filel</p>
        <p id="p_04418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04418"> NOTE</p>
        <p id="p_04419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04419"> The function  die  sends its messages to the standard error file, not the standard output file. This means that when a program terminates, the message printed by  die  always appears on your screen, even when you have redirected output to a file.</p>
        <p id="p_04420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04420"> If the file is opened successfully, line 5 writes a message to the standard error file, which indicates that the file has been opened. As you can see, the standard error file is not reserved solely for errors. You can write anything you want to  STDERR  at any time.</p>
        <p id="p_04421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04421"> Lines 6-11 read one line of  f ilel  at a time and write it out in uppercase (using the escape characters  \U and  \E,  which you learned about on  Day  3 ,  "Understanding Scalar Values").</p>
        <p id="p_04422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04422"> Closing a File</p>
        <p id="p_04423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04423"> When you are finished reading from or writing to a file, you can tell the Perl interpreter that you are finished by calling the library function  close.</p>
        <p id="p_04424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04424"> The syntax for the  close  library function is</p>
        <p id="p_04425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04425"> close (filevar);</p>
        <p id="p_04426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04426"> close  requires one argument: the file variable representing the file you want to close. Once you have closed the file, you cannot read from it or write to it without invoking  open  again.</p>
        <p id="p_04427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04427"> Note that you do not have to call  close  when you are finished with a file: Perl automatically closes the file when the program terminates or when you open another file using a previously defined file variable. For example, consider the following statements:</p>
        <p id="p_04428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04428"> open (MYFILE, print MYFILE open (MYFILE, print MYFILE</p>
        <p id="p_04429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04429"> "&gt;filel");</p>
        <p id="p_04430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04430"> ("Here is a line of output.\n"); "&gt;file2");</p>
        <p id="p_04431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04431"> ("Here is another line of output.\n");</p>
        <p id="p_04432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04432"> Here, when  file 2  is opened for writing,  f ilel  automatically is closed. The file variable  MYFILE  is now associated with  file 2.  This means that the second  print  statement sends the following to  file 2:</p>
        <p id="p_04433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04433"> Here is another line of output.</p>
        <p id="p_04434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04434"> DO use the &lt;&gt; operator, which is an easy way to read input from several files in succession. See the section titled "Reading from a Sequence of Files," later in this lesson, for more information on the &lt;&gt; operator.</p>
        <p id="p_04435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04435"> DON'T use the same file variable to represent multiple files unless it is absolutely necessary. It is too easy to lose track of which file variable belongs to which file, especially if your program is large or has many nested conditional statements.</p>
        <p id="p_04436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04436"> Determining the Status of a File</p>
        <p id="p_04437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04437"> Many of the example programs in today's lesson call  open  and test the returned result to see whether the file has been opened successfully. If  open  fails, it might be useful to find out exactly why the file could not be opened. To do this, use one of  the file-test operators.</p>
        <p id="p_04438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04438"> Listing 6.7 provides an example of the use of a file-test operator. This program is a slight modification of Listing 6.6, which is an uppercase conversion program.</p>
        <p id="p_04439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04439"> Type</p>
        <p id="p_04440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04440"> Listing 6.7. A program that checks whether an unopened file actually exists.</p>
        <p id="p_04441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04441"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04442"> 2  :</p>
        <p id="p_04443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04443"> 3: unless (open(MYFILE, "filel")) {</p>
        <p id="p_04444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04444"> 4: if (-e "filel") {</p>
        <p id="p_04445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04445"> 5: die ("File filel exists, but cannot be</p>
        <p id="p_04446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04446"> opened.\n");</p>
        <p id="p_04447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04447"> 6: } else {</p>
        <p id="p_04448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04448"> 7: die ("File filel does not exist.\n");</p>
        <p id="p_04449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04449"> 8: }</p>
        <p id="p_04450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04450"> 9: }</p>
        <p id="p_04451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04451"> 10: $line = &lt;MYFILE&gt;;</p>
        <p id="p_04452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04452"> 11: while ($line ne "") {</p>
        <p id="p_04453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04453"> Output</p>
        <p id="p_04454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04454"> $ program6_7</p>
        <p id="p_04455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04455"> File filel does not exist. $</p>
        <p id="p_04456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04456"> Analysis</p>
        <p id="p_04457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04457"> I Line 3 attempts to open the file filel for reading. If filel cannot be opened, the program executes the if statement starting in line 4.</p>
        <p id="p_04458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04458"> Line 4 is an example of a file-test operator. This file-test operator,  -e,  tests whether its operand, a file, actually exists. If the file  filel  exists, the expression  -e "filel " returns true, the message  File filel exists, but cannot be opened . is displayed, and the program exits. If  filel  does not exist,  -e "filel " is false, and the library function  die  prints the following message before exiting:</p>
        <p id="p_04459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04459"> File filel does not exist.</p>
        <p id="p_04460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04460"> File-Test Operator Syntax</p>
        <p id="p_04461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04461"> All file-test operators have the same syntax as the  -e  operator used in Listing 6.7. The syntax for the file-test operators is</p>
        <p id="p_04462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04462"> -x expr</p>
        <p id="p_04463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04463"> Here,  x  is an alphabetic character and  expr  is any expression. The value of  expr  is assumed to be a string that contains the name of the file to be tested.</p>
        <p id="p_04464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04464"> Because the operand for a file-test operator can be any expression, you can use scalar variables and string</p>
        <p id="p_04465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04465"> operators in the expression if you like. For example:</p>
        <p id="p_04466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04466"> $var = "filel"; if (~e $var) {</p>
        <p id="p_04467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04467"> print STDERR ("File filel exists.\n");</p>
        <p id="p_04468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04468"> }</p>
        <p id="p_04469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04469"> if (-e $var . "a") {</p>
        <p id="p_04470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04470"> print STDERR ("File filela exists.\n");</p>
        <p id="p_04471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04471"> }</p>
        <p id="p_04472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04472"> In the first use of  -e,  the contents of  $var, filel,  are assumed to be the name of a file, and this file tested for existence. In the second case,  a  is appended to the contents of  filel,  producing the string filela.  The  -e  operator then tests whether a file named  filela  exists.</p>
        <p id="p_04473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04473"> NOTE</p>
        <p id="p_04474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04474"> The Perl interpreter does not get confused by the expression</p>
        <p id="p_04475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04475"> -e $var . "a"</p>
        <p id="p_04476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04476"> because the . operator has higher precedence than the  -e  operator. This means that the string concatenation is performed first.</p>
        <p id="p_04477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04477"> The file-test operators have higher precedence than the comparison operators but lower precedence than the shift operators. To see a complete list of the Perl operators and their precedences, refer to  Day 4,  "More Operators."</p>
        <p id="p_04478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04478"> The string can be a complete path name, if you like. The following is an example:</p>
        <p id="p_04479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04479"> if (-e "/u/jqpublic/filel" ) {</p>
        <p id="p_04480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04480"> print ("The file exists.\n");</p>
        <p id="p_04481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04481"> }</p>
        <p id="p_04482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04482"> This  i f  statement tests for the existence of the file  /u/jqpublic/filel.</p>
        <p id="p_04483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04483"> Available File-Test Operators</p>
        <p id="p_04484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04484"> Table 6.1 provides a complete list of the file-test operators available in Perl. In this table,  name  is placeholder for the name of the operand being tested.</p>
        <p id="p_04485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04485"> Table 6.1. The file-test operators.</p>
        <p id="p_04486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04486"> * In this case, the "real user" is the  userid  specified at login, as opposed to the effective user ID, which is the  userid  under which you currently are working. (On some systems, a command such as  /user/local/etc/suid  enables you to change your effective user ID.)</p>
        <p id="p_04487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04487"> The following sections describe some of the more common file-test operators and show you how they can be useful. (You'll also learn about more of these operators on  Day 12 ,  "Working with the File System.")</p>
        <p id="p_04488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04488"> More on the -e Operator</p>
        <p id="p_04489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04489"> When a Perl program opens a file for writing, it destroys anything that already exists in the file. This might not be what you want. Therefore, you might want to make sure that your program opens a file only if the file does not already exist.</p>
        <p id="p_04490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04490"> You can use the  -e  file-test operator to test whether or not to open a file for writing. Listing 6.8 is an example of a program that does this.</p>
        <p id="p_04491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04491"> Type</p>
        <p id="p_04492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04492"> Listing 6.8. A program that tests whether a file exists before opening it for writing.</p>
        <p id="p_04493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04493"> 9: unless (open(OUTFILE, "&gt;outfile")) {</p>
        <p id="p_04494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04494"> 10: die ("Output file outfile cannot be opened.\n");</p>
        <p id="p_04495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04495"> 11 : }</p>
        <p id="p_04496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04496"> 12: $line = &lt;INFILE&gt;;</p>
        <p id="p_04497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04497"> 13: while ($line ne "") {</p>
        <p id="p_04498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04498"> Output</p>
        <p id="p_04499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04499"> $ program6_8</p>
        <p id="p_04500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04500"> Output file outfile already exists. $</p>
        <p id="p_04501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04501"> Analysis</p>
        <p id="p_04502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04502"> This program is the uppercase conversion program again; most of it should be familiar to you.</p>
        <p id="p_04503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04503"> The only difference is lines 6-8, which use the  -e  file-test operator to check whether the output file outfile  exists. If  outfile  exists, the program aborts, which ensures that the existing contents of outfile  are not lost.</p>
        <p id="p_04504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04504"> If  outfile  does not exist, the following expression fails:</p>
        <p id="p_04505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04505"> -e "outfile"</p>
        <p id="p_04506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04506"> and the program knows that it is safe to open  outfile  because it does not already exist.</p>
        <p id="p_04507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04507"> Using File-Test Operators in Expressions</p>
        <p id="p_04508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04508"> If you don't need to know exactly why your program is failing, you can combine all of the tests in Listing 6.8 into a single statement, as follows:</p>
        <p id="p_04509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04509"> open(INFILE, "infile") &amp;&amp;  !(-e  "outfile") &amp;&amp;</p>
        <p id="p_04510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04510"> open(OUTFILE, "&gt;outfile") || die("Cannot open files\n");</p>
        <p id="p_04511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04511"> Can you see how this works? Here's what is happening: The &amp; &amp; operator, logical AND, is true only if both of its operands are true. In this case, the two &amp; &amp; operators indicate that the subexpression up to, but not including, the | | is true only if all three of the following are true:</p>
        <p id="p_04512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04512"> open(INFILE, "infile")</p>
        <p id="p_04513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04513"> !(-e  "outfile")</p>
        <p id="p_04514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04514"> open(OUTFILE, "&gt;outfile")</p>
        <p id="p_04515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04515"> All three are true only when the following conditions are met:</p>
        <p id="p_04516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04516"> • The input file  inf ile  can be opened.</p>
        <p id="p_04517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04517"> • The output file  out file  does not already exist.</p>
        <p id="p_04518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04518"> • The output file  out file  can be opened.</p>
        <p id="p_04519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04519"> If any of these subexpressions is false, the entire expression up to the | | is false. This means that the subexpression after the | | (the call to  die)  is executed, and the program aborts.</p>
        <p id="p_04520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04520"> Note that each of the three subexpressions associated with the &amp; &amp; operators is evaluated in turn. This means that the subexpression</p>
        <p id="p_04521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04521"> !(-e  "outfile")</p>
        <p id="p_04522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04522"> is evaluated only if</p>
        <p id="p_04523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04523"> open(INFILE, "infile")</p>
        <p id="p_04524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04524"> is true, and that the subexpression</p>
        <p id="p_04525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04525"> open(OUTFILE, "&gt;outfile")</p>
        <p id="p_04526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04526"> is evaluated only if</p>
        <p id="p_04527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04527"> !(-e  "outfile")</p>
        <p id="p_04528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04528"> is true. This is exactly the same logic that Listing 6.8 uses.</p>
        <p id="p_04529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04529"> If any of the subexpressions is false, the Perl interpreter doesn't evaluate the rest of them because it knows</p>
        <p id="p_04530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04530"> that the final result of</p>
        <p id="p_04531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04531"> open(INFILE, "infile") &amp;&amp;  !(-e  "outfile") &amp;&amp;</p>
        <p id="p_04532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04532"> open(OUTFILE, "&gt;outfile")</p>
        <p id="p_04533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04533"> is going to be false. Instead, it goes on to evaluate the subexpression to the right of the | |, which is the call to  die.</p>
        <p id="p_04534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04534"> This program logic is somewhat complicated, and you shouldn't use it unless you feel really comfortable with it. The if statements in Listing 6.8 do the same thing and are easier to understand; however, it's useful to know how complicated statements such as the following one work because many Perl programmers like to write code that works in this way:</p>
        <p id="p_04535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04535"> open(INFILE, "infile") &amp;&amp;  !(-e  "outfile") &amp;&amp;</p>
        <p id="p_04536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04536"> open(OUTFILE, "&gt;outfile") || die("Cannot open files\n");</p>
        <p id="p_04537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04537"> In the next few days, you'll see several more examples of code that exploits how expressions work in Perl. "Perl hackers "-experienced Perl programmers-often enjoy compressing multiple statements into shorter ones, and they delight in complexity. Be warned.</p>
        <p id="p_04538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04538"> Testing for Read Permission-the -r Operator</p>
        <p id="p_04539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04539"> Before you can open a file for reading, you must have permission to read the file. The -r file-test operator tests whether you have permission to read a file.</p>
        <p id="p_04540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04540"> Listing 6.9 checks whether the person running the program has permission to access a particular file.</p>
        <p id="p_04541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04541"> Type</p>
        <p id="p_04542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04542"> Listing 6.9. A program that tests for read permission on a file.</p>
        <p id="p_04543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04543"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04544"> 2  :</p>
        <p id="p_04545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04545"> 3: unless (open(MYFILE,  "filel")) {</p>
        <p id="p_04546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04546"> 4 :</p>
        <p id="p_04547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04547"> if (!(-e "filel")) {</p>
        <p id="p_04548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04548"> die ("File filel does not  exist.\n");</p>
        <p id="p_04549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04549"> 5 :</p>
        <p id="p_04550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04550"> 6: } elsif ( ! (-r "filel")) {</p>
        <p id="p_04551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04551"> 7: die ("You are not allowed to read filel.\n");</p>
        <p id="p_04552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04552"> 8: } else {</p>
        <p id="p_04553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04553"> 9: die ("Filel cannot be opened\n");</p>
        <p id="p_04554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04554"> 10 : }</p>
        <p id="p_04555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04555"> 11 : }</p>
        <p id="p_04556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04556"> Output</p>
        <p id="p_04557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04557"> $ program6_9</p>
        <p id="p_04558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04558"> You are not allowed to read filel. $</p>
        <p id="p_04559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04559"> Analysis</p>
        <p id="p_04560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04560"> Line 3 of this program tries to open  filel.  If the call to  open  fails, the program tries to find</p>
        <p id="p_04561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04561"> out why.</p>
        <p id="p_04562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04562"> First, line 4 tests whether the file actually exists. If the file exists, the Perl interpreter executes line 6, which tests whether the file has the proper read permission. If it does not,  die  is called; it then prints the following message and exits:</p>
        <p id="p_04563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04563"> You are not allowed to read filel.</p>
        <p id="p_04564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04564"> NOTE</p>
        <p id="p_04565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04565"> You do not need to use the  -e  file-test operator before using the  -r file-test operator. If the file does not exist, -  r  returns false because you can't read a file that isn't there.</p>
        <p id="p_04566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04566"> The only reason to use both  -e  and  -r  is to enable your program to determine exactly what is wrong.</p>
        <p id="p_04567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04567"> Checking for Other Permissions</p>
        <p id="p_04568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04568"> You can use file-test operators to test for other permissions as well. To check whether you have write permission on a file, use the  -w  file-test operator.</p>
        <p id="p_04569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04569"> if (—w "filel") {</p>
        <p id="p_04570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04570"> print STDERR ("I can write to filel.\n");</p>
        <p id="p_04571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04571"> } else {</p>
        <p id="p_04572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04572"> print STDERR ("I can't write to filel.\n");</p>
        <p id="p_04573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04573"> }</p>
        <p id="p_04574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04574"> The  -x  file-test operator checks whether you have execute permission on the file (in other words, whether the system thinks this is an executable program, and whether you have permission to run it if it is), as illustrated here:</p>
        <p id="p_04575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04575"> if (-x "filel") {</p>
        <p id="p_04576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04576"> print STDERR ("I can run filel.\n");</p>
        <p id="p_04577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04577"> } else {</p>
        <p id="p_04578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04578"> print STDERR ("I can't run filel.\n");</p>
        <p id="p_04579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04579"> NOTE</p>
        <p id="p_04580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04580"> If you are the system administrator (for example, you are running as user ID  root)  and have permission to access any file, the  -r  and  -w file-test operators always return true if the file exists. Also, the  -x  test operator always returns true if the file is an executable program.</p>
        <p id="p_04581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04581"> Checking for Empty Files</p>
        <p id="p_04582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04582"> The -  z  file-test operator tests whether a file is empty. This provides a more refined test for whether or not to open a file for writing: if the file exists but is empty, no information is lost if you overwrite the existing file.</p>
        <p id="p_04583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04583"> Listing 6.10 shows how to use -  z.</p>
        <p id="p_04584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04584"> Type</p>
        <p id="p_04585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04585"> Listing 6.10.  A  program that tests whether the file is empty before opening it for writing.</p>
        <p id="p_04586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04586"> 1: 2  : 3:</p>
        <p id="p_04587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04587"> 4 :</p>
        <p id="p_04588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04588"> 5 : 6 :</p>
        <p id="p_04589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04589"> 7 :</p>
        <p id="p_04590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04590"> 8  : 9:</p>
        <p id="p_04591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04591"> #!/usr/local/bin/perl</p>
        <p id="p_04592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04592"> if (-e "outfile") {</p>
        <p id="p_04593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04593"> if  (!(-w  "outfile")) {</p>
        <p id="p_04594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04594"> die ("Missing write permission for outfile.\n");</p>
        <p id="p_04595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04595"> }</p>
        <p id="p_04596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04596"> if  (!(-z  "outfile")) {</p>
        <p id="p_04597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04597"> die ("File outfile is non-empty.\n");</p>
        <p id="p_04598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04598"> }</p>
        <p id="p_04599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04599"> 10 : }</p>
        <p id="p_04600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04600"> 11: # at this point, the file is either empty or doesn't exist, 12: # and we have permission to write to it if it exists</p>
        <p id="p_04601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04601"> Output</p>
        <p id="p_04602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04602"> $ program6_10</p>
        <p id="p_04603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04603"> File outfile is non-empty. $</p>
        <p id="p_04604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04604"> Analysis</p>
        <p id="p_04605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04605"> Line 3 checks whether the file  outfile  exists using  -e.  If it exists, it can only be opened if the program has permission to write to the file; line 4 checks for this using  -w.</p>
        <p id="p_04606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04606"> Line  7  uses  -z  to test whether the file is empty. If it is not, line  7  calls  die  to terminate program execution. The opposite of  -z  is the  -s  file-test operator, which returns a nonzero value if the file is not empty.</p>
        <p id="p_04607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04607"> $size = -s "outfile"; if ($size == 0) {</p>
        <p id="p_04608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04608"> print ("The file is empty.\n");</p>
        <p id="p_04609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04609"> } else {</p>
        <p id="p_04610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04610"> print ("The file is $size bytes long.\n");</p>
        <p id="p_04611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04611"> }</p>
        <p id="p_04612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04612"> The  -s  file-test operator actually returns the size of the file in bytes. It can still be used in conditional expressions, though, because any nonzero value (indicating that the file is not empty) is treated as true.</p>
        <p id="p_04613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04613"> Listing 6.11 uses  -s  to return the size of a file that has a name which is supplied via the standard input file.</p>
        <p id="p_04614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04614"> Listing 6.11.  A  program that prints the size of a file in bytes.</p>
        <p id="p_04615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04615"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04616"> 2  :</p>
        <p id="p_04617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04617"> 3: print ("Enter the name of the file:\n");</p>
        <p id="p_04618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04618"> 4: $filename = &lt;STDIN&gt;;</p>
        <p id="p_04619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04619"> 5: chop ($filename);</p>
        <p id="p_04620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04620"> 6: if  (!(-e  $filename)) {</p>
        <p id="p_04621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04621"> 7: print ("File $filename does not  exist.\n");</p>
        <p id="p_04622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04622"> 8:  } else {</p>
        <p id="p_04623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04623"> 9: $size = -s $filename;</p>
        <p id="p_04624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04624"> 10 :</p>
        <p id="p_04625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04625"> print ("File $filename contains $size  bytes.\n");</p>
        <p id="p_04626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04626"> 11: }</p>
        <p id="p_04627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04627"> I Output</p>
        <p id="p_04628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04628"> $ program6_ll</p>
        <p id="p_04629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04629"> Enter the name of the file: filel</p>
        <p id="p_04630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04630"> File filel contains 128 bytes. $</p>
        <p id="p_04631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04631"> kNALYSI</p>
        <p id="p_04632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04632"> ! Lines 3-5 obtain the name of the file and remove the trailing newline character.</p>
        <p id="p_04633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04633"> Line 6 tests whether the file exists. If the file doesn't exist, the program indicates this.</p>
        <p id="p_04634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04634"> Line 9 stores the size of the file in the scalar variable  $size.  The size is measured in bytes (one byte is equivalent to one character in a character string).</p>
        <p id="p_04635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04635"> Line 10 prints out the number of bytes in the file.</p>
        <p id="p_04636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04636"> Using File-Test Operators with File Variables</p>
        <p id="p_04637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04637"> You can use file-test operators on file variables as well as character strings. In the following example the file-test operator  -z  tests the file represented by the file variable  MYFILE:</p>
        <p id="p_04638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04638"> if (-z MYFILE) {</p>
        <p id="p_04639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04639"> print ("This file is empty!\n");</p>
        <p id="p_04640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04640"> }</p>
        <p id="p_04641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04641"> As before, this file-test operator returns true if the file is empty and false if it is not.</p>
        <p id="p_04642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04642"> Warning</p>
        <p id="p_04643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04643"> Remember that file variables can be used only after you open the file. If you need to test a particular condition before opening the file (such as whether the file is nonzero), test it using the name of the file.</p>
        <p id="p_04644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04644"> Reading from a Sequence of Files</p>
        <p id="p_04645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04645"> Many UNIX utility programs are invoked using the following command syntax:</p>
        <p id="p_04646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04646"> programname filel file2 file3 . . .</p>
        <p id="p_04647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04647"> A program that uses this command syntax operates on all of the files specified on the command line in order, starting with  filel.  When  filel  has been processed, the program then proceeds on to  f ile2,  and so on until all of the files have been exhausted.</p>
        <p id="p_04648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04648"> In Perl, it's easy to write programs that process an arbitrary number of files because there is a special operator, the &lt;&gt; operator, that does all of the file-handling work for you.</p>
        <p id="p_04649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04649"> To understand how the &lt;&gt; operator works, recall what happens when you put &lt; and &gt; around a file variable:</p>
        <p id="p_04650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04650"> $list = &lt;MYFILE&gt;;</p>
        <p id="p_04651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04651"> This statement reads a line of input from the file represented by the file variable  MYFILE  and stores it in the scalar variable  $list.  Similarly, the statement</p>
        <p id="p_04652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04652"> $list = &lt;&gt;;</p>
        <p id="p_04653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04653"> reads a line of input and stores it in the scalar variable $ 1  i s t ; however, the file from which it reads is contained on the command line. Suppose, for example, a program containing a statement using the &lt;&gt; operator, such as the statement</p>
        <p id="p_04654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04654"> $list = &lt;&gt;;</p>
        <p id="p_04655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04655"> is called  myprog  and is called using the command</p>
        <p id="p_04656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04656"> $ myprog filel file2 file3</p>
        <p id="p_04657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04657"> In this case, the first occurrence of the &lt;&gt; operator reads the first line of input from filel. Successive</p>
        <p id="p_04658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04658"> occurrences of &lt;&gt; read more lines from  f ilel.  When  f ilel  is exhausted, &lt;&gt; reads the first line from f ile2,  and so on. When the last file,  file 3,  is exhausted, &lt;&gt; returns an empty string, which indicates that all the input has been read.</p>
        <p id="p_04659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04659"> NOTE</p>
        <p id="p_04660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04660"> If a program containing a &lt;&gt; operator is called with no command-line arguments, the &lt;&gt; operator reads input from the standard input file. In this case, the &lt;&gt; operator is equivalent to  &lt;STDIN&gt;.</p>
        <p id="p_04661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04661"> If a file named in a command-line argument does not exist, the Perl interpreter writes the following message to the standard error file:</p>
        <p id="p_04662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04662"> Can't open name: No such file or directory</p>
        <p id="p_04663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04663"> Here,  name  is a placeholder for the name of the file that the Perl interpreter cannot find. In this case, the Perl interpreter ignores  name and continues on with the next file in the command line.</p>
        <p id="p_04664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04664"> To see how the &lt;&gt; operator works, look at Listing 6.12, which displays the contents of the files specified on the command line. (If you are familiar with UNIX, you will recognize this as the behavior of the UNIX utility  cat.)  The output from Listing 6.12 assumes that files  f ilel  and  f ile2  are specified on the command line and that each file contains one line.</p>
        <p id="p_04665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04665"> Type</p>
        <p id="p_04666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04666"> Listing 6.12. A program that displays the contents of one or more files.</p>
        <p id="p_04667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04667"> Output</p>
        <p id="p_04668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04668"> $</p>
        <p id="p_04669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04669"> Analysis</p>
        <p id="p_04670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04670"> Once again, you can see how powerful and useful Perl is. five lines, including the header comment and a blank line.</p>
        <p id="p_04671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04671"> This entire program consists of only</p>
        <p id="p_04672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04672"> Line 3 both reads a line from a file and tests to see whether the line is the empty string. Because the assignment operator = returns the value assigned, the expression</p>
        <p id="p_04673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04673"> $inputline = &lt;&gt;</p>
        <p id="p_04674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04674"> has the value " " (the null string) if and only if &lt;&gt; returns the null string, which happens only when there are no more lines to read from any of the input files. This is exactly the point at which the program wants to stop looping. (Recall that a "blank line" in a file is not the same as the null string because the blank line contains the newline character.) Because the null string is equivalent to false in a conditional expression, there is no need to use a conditional operator such as  ne.</p>
        <p id="p_04675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04675"> When line  3  is executed for the first time, the first line in the first input file,  f ilel,  is read and stored in the scalar variable $  input line.  Because  f ilel  contains only one line, the second pass through the loop, and the second execution of line  3,  reads the first line of the second input file,  f ile2.</p>
        <p id="p_04676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04676"> After this, there are no more lines in either  filelorfile2,so  line  3  assigns the null string to $ input line,  which terminates the loop.</p>
        <p id="p_04677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04677"> When it reaches the end of the last file on the command line, the &lt;&gt; operator returns the empty string. However, if you use the &lt;&gt; operator after it has returned the empty string, the Perl interpreter assumes that you want to start reading input from the standard input file. (Recall that &lt;&gt; reads from the standard input file if there are no files on the command line.)</p>
        <p id="p_04678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04678"> This means that you have to be a little more careful when you use &lt;&gt; than when you are reading using  &lt;MYFILE&gt;  (where  MYFILE  is a file variable). If  MYFILE  has been exhausted, repeated attempts to read using  &lt;MYFILE&gt;  continue to return the null string because there isn't anything left to read.</p>
        <p id="p_04679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04679"> Reading into an Array Variable</p>
        <p id="p_04680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04680"> As you have seen, if you read from a file using  &lt;STDIN&gt;  or  &lt;MYFILE&gt;  in an assignment to an array variable, the Perl interpreter reads the entire contents of the file into the array, as follows:</p>
        <p id="p_04681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04681"> @array = &lt;MYFILE&gt;;</p>
        <p id="p_04682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04682"> This works also with &lt;&gt;. For example, the statement</p>
        <p id="p_04683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04683"> @array = &lt;&gt;;</p>
        <p id="p_04684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04684"> reads all the contents all of the files on the command line into the array variable @  array.</p>
        <p id="p_04685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04685"> As always, be careful when you use this because you might end up with a very large array.</p>
        <p id="p_04686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04686"> Using Command-Line Arguments as Values</p>
        <p id="p_04687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04687"> As you've seen, the &lt;&gt; operator assumes that its command-line arguments are files. For example, if you start up the program shown in Listing 6.12 with the command</p>
        <p id="p_04688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04688"> $ program6_12 myfilel myfile2</p>
        <p id="p_04689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04689"> the Perl interpreter assumes that the command-line arguments  myfilel  and  myf ile2  are files and displays their contents.</p>
        <p id="p_04690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04690"> Perl enables you to use the command-line arguments any way you want by defining a special array variable called  @ARGV.  When a Perl program starts up, this variable contains a list consisting of the command-line</p>
        <p id="p_04691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04691"> arguments. For example, the command</p>
        <p id="p_04692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04692"> $ program6_12 myfilel myfile2</p>
        <p id="p_04693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04693"> sets  @ARGV  to the list</p>
        <p id="p_04694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04694"> ("myfilel", "myfile2")</p>
        <p id="p_04695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04695"> NOTE</p>
        <p id="p_04696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04696"> The shell you are running  (sh, csh,  or whatever you are using) is responsible for turning a command line such as</p>
        <p id="p_04697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04697"> program6_12 myfilel myfile2</p>
        <p id="p_04698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04698"> into arguments. Normally, any spaces or tab characters are assumed to be separators that indicate where one command-line argument stops and the next begins. For example, the following are identical:</p>
        <p id="p_04699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04699"> program6_12 myfilel myfile2 program6_12 myfilel myfile2</p>
        <p id="p_04700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04700"> In each case, the command-line arguments are  myfilel  and myfile2.</p>
        <p id="p_04701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04701"> See your shell documentation for details on how to put blank spaces or tab characters into your command-line arguments.</p>
        <p id="p_04702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04702"> As with all other array variables, you can access individual elements of  @ARGV.  For example, the statement</p>
        <p id="p_04703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04703"> $var = $ARGV[0];</p>
        <p id="p_04704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04704"> assigns the first element of  @ARGV  to the scalar variable  $var.</p>
        <p id="p_04705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04705"> You even can assign to some or all of  @ARGV  if you like. For example:</p>
        <p id="p_04706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04706"> $ARGV[0]</p>
        <p id="p_04707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04707"> 43;</p>
        <p id="p_04708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04708"> If you assign to any or all of  @ARGV,  you overwrite what was already there, which means that any command¬ line arguments overwritten are lost.</p>
        <p id="p_04709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04709"> To determine the number of command-line arguments, assign the array variable to a scalar variable, as follows:</p>
        <p id="p_04710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04710"> $numargs = @ARGV;</p>
        <p id="p_04711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04711"> As with all array variables, using an array variable in a place where the Perl interpreter expects a scalar variable means that the length of the array is used. In this case,  $numargs  is assigned the number of command-line arguments.</p>
        <p id="p_04712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04712"> Warning</p>
        <p id="p_04713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04713"> C programmers should take note that the first element of  @ARGV, unlike  argv  [  0  ] in C, does not contain the name of the program. In Perl, the first element of  @ARGV  is the first command-line argument.</p>
        <p id="p_04714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04714"> To get the name of the program, use the system variable $  0,  which is discussed on  Day  17 , "System Variables."</p>
        <p id="p_04715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04715"> To see how you can use @  ARGV  in a program, examine Listing 6.13. This program assumes that its first argument is a word to look for. The remaining arguments are assumed to be files in which to look for the word. The program prints out the searched-for word, the number of occurrences in each file, and the total number of occurrences.</p>
        <p id="p_04716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04716"> This example assumes that the files  f ilel  and  file 2  are defined and that each file contains the single line</p>
        <p id="p_04717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04717"> This file contains a single line of input.</p>
        <p id="p_04718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04718"> This example is then run with the command</p>
        <p id="p_04719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04719"> $ programname single filel file2</p>
        <p id="p_04720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04720"> where  programname  is a placeholder for the name of the program. (If you are running the program yourself, you can name the program anything you like.)</p>
        <p id="p_04721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04720"/>
        <p id="p_04722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04722"> Listing 6.13. A word-search and counting program.</p>
        <p id="p_04723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04723"> 1: #!/usr/local/bin/perl</p>
        <p id="p_04724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04724"> 2  :</p>
        <p id="p_04725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04725"> 3: print ("Word to search for: $ARGV[0]\n") ;</p>
        <p id="p_04726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04726"> 4: $filecount = 1;</p>
        <p id="p_04727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04727"> 5: $totalwordcount = 0;</p>
        <p id="p_04728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04728"> 6: while ($filecount &lt;= SARGV-1) {</p>
        <p id="p_04729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04729"> 7: unless (open (INFILE, $ARGV[$filecount])) {</p>
        <p id="p_04730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04730"> 8: die ("Can't open input file</p>
        <p id="p_04731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04731"> $ARGV[$filecount]\n");</p>
        <p id="p_04732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04732"> $filecount++;</p>
        <p id="p_04733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04733"> 24 :</p>
        <p id="p_04734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04734"> 25: $totalwordcount += $wordcount;</p>
        <p id="p_04735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04735"> 26: }</p>
        <p id="p_04736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04736"> 27: print ("total number of occurrences: $totalwordcount\n");</p>
        <p id="p_04737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04737"> Output</p>
        <p id="p_04738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04738"> $ program6_13 single filel file2 Word to search for: single occurrences in file filel: 1 occurrences in file file2: 1 total number of occurrences: 2 $</p>
        <p id="p_04739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04739"> Analysis</p>
        <p id="p_04740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04740"> I Line 3 prints the word to search for. The program assumes that this word is the first argument in the command line and, therefore, is the first element of the array  @ARGV.</p>
        <p id="p_04741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04741"> Lines 7-9 open a file named on the command line. The first time line 7 is executed, the variable $f ilecount  has the value 1, and the file whose name is in  $ARGV  [  1  ] is opened. The next time through, $ file count  is  2  and the file named in  $ARGV  [  2  ] is opened, and so on. If a file cannot be opened, the program terminates.</p>
        <p id="p_04742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04742"> Line 11 reads a line from a file. As before, the conditional expression</p>
        <p id="p_04743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04743"> $line = &lt;INFILE&gt;</p>
        <p id="p_04744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04744"> reads a line from the file represented by the file  INFILE  and assigns it to  $line.  If the file is empty, $line  is assigned the null string, the conditional expression is false, and the loop in lines  11-21  is terminated.</p>
        <p id="p_04745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04745"> Line 13 splits the line into words, and lines 15-20 compare each word with the search word. If the word matches, the word count for this file is incremented. This word count is reset when a new file is opened.</p>
        <p id="p_04746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04746"> ARGV and the &lt;&gt; Operator</p>
        <p id="p_04747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04747"> In Perl, the &lt;&gt; operator actually contains a hidden reference to the array  @ARGV.  Here's how it works:</p>
        <p id="p_04748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04748"> 1. When the Perl interpreter sees the &lt;&gt; for the first time, it opens the file whose name is stored in</p>
        <p id="p_04749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04749"> $ARGV[0] .</p>
        <p id="p_04750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04750"> 2. After opening the file, the Perl interpreter executes the following library function:</p>
        <p id="p_04751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04751"> shift(SARGV);</p>
        <p id="p_04752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04752"> This library function gets rid of the first element of  @ARGV  and moves every other element over one. This means that element  x  of  @ARGV  becomes element  x-1.</p>
        <p id="p_04753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04753"> 3. The &lt;&gt; operator then reads all of the lines of the file opened in step 1.</p>
        <p id="p_04754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04754"> 4. When the &lt;&gt; operator exhausts an input file, the Perl interpreter goes back to step 1 and repeats the cycle again.</p>
        <p id="p_04755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04755"> If you like, you can modify your program to retrieve a value from the command line and then fix  @ARGV  so that the &lt;&gt; operator can work properly. If you modify Listing 6.13 to do this, the result is Listing 6.14.</p>
        <p id="p_04756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04756"> Type</p>
        <p id="p_04757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04757"> Listing 6.14.  A  word-search and counting program that uses &lt;&gt;.</p>
        <p id="p_04758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04758"> $wordcount += 1;</p>
        <p id="p_04759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04759"> 14 :</p>
        <p id="p_04760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04760"> 15 :</p>
        <p id="p_04761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04761"> 16 :</p>
        <p id="p_04762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04762"> 17 :</p>
        <p id="p_04763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04763"> 18 :</p>
        <p id="p_04764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04764"> 19:</p>
        <p id="p_04765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04765"> 20  :</p>
        <p id="p_04766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04766"> 21 :</p>
        <p id="p_04767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04767"> 22  :</p>
        <p id="p_04768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04768"> 23 :</p>
        <p id="p_04769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04769"> 24 :</p>
        <p id="p_04770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04770"> 25: }</p>
        <p id="p_04771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04771"> 26: print</p>
        <p id="p_04772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04772"> }</p>
        <p id="p_04773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04773"> $w+ + ;</p>
        <p id="p_04774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04774"> }</p>
        <p id="p_04775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04775"> if (eof) {</p>
        <p id="p_04776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04776"> print ("occurrences in file $filename: "); print ( "$wordcount\n");</p>
        <p id="p_04777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04777"> $totalwordcount += $wordcount;</p>
        <p id="p_04778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04778"> $wordcount = 0;</p>
        <p id="p_04779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04779"> $filename = $ARGV[0];</p>
        <p id="p_04780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04780"> }</p>
        <p id="p_04781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04781"> ("total  number  of occurrences: $totalwordcount\n");</p>
        <p id="p_04782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04782"> Output</p>
        <p id="p_04783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04783"> $ program6_14 single filel file2 Word to search for: single occurrences in file filel: 1 occurrences in file file2: 1 total  number  of occurrences: 2 $</p>
        <p id="p_04784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04784"> Analysis</p>
        <p id="p_04785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04785"> I Line 3 assigns the first command-line argument, the search word, to the scalar variable $searchword.  This is necessary because the call to  shift  in line 5 destroys the initial value of</p>
        <p id="p_04786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04786"> $ARGV[0] .</p>
        <p id="p_04787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04787"> Line 5 adjusts the array  @ARGV  so that the &lt;&gt; operator can use it. To do this, it calls the library function shift.  This function "shifts" the elements of the list stored in  @ARGV.  The element in  $ARGV  [ 1 ] is moved to  $ARGV  [  0  ], the element in  $ARGV  [  2  ] is moved to  $ARGV  [ 1 ], and so on. After  shift  is called,  @ARGV  contains the files to be searched, which is exactly what the &lt;&gt; operator is looking for.</p>
        <p id="p_04788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04788"> Line  7  assigns the current value of  $ARGV [ 0  ] to the scalar variable  $f ilename.  Because the &lt;&gt; operator in line  8  calls  shift,  the value of  $ARGV  [  0  ] is lost unless the program does this.</p>
        <p id="p_04789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04789"> Line  8  uses the &lt;&gt; operator to open the file named in  $ARGV  [  0  ] and to read a line from the file. The array variable  @ARGV  is shifted at this point.</p>
        <p id="p_04790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04790"> Lines 9-16 behave as in Listing 6.13. The only difference is that the search word is now in  $searchword, not in  $ARGV [ 0 ].</p>
        <p id="p_04791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04791"> Line 18 introduces the library function  eof . This function indicates whether the program has reached the end of the file being read by &lt;&gt;. If  eof  returns true, the next use of &lt;&gt; opens a new file and shifts  @ARGV again.</p>
        <p id="p_04792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04792"> Lines 19-23 prepare for the opening of a new file. The number of occurrences of the search word is printed, the current word count is added to the total word count, and the word count is reset to 0. Because the new filename to be opened is in  $ARGV [ 0 ] , line 23 preserves this filename by assigning it to $  filename.</p>
        <p id="p_04793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04793"> NOTE</p>
        <p id="p_04794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04794"> You can use the &lt;&gt; operator to open and read any file you like by setting the value of  @ARGV  yourself. For example:</p>
        <p id="p_04795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04795"> @ARGV = ("myfilel", "myfile2"); while ($line = &lt;&gt;) {</p>
        <p id="p_04796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04796"> }</p>
        <p id="p_04797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04797"> Here, when the statement containing the &lt;&gt; is executed for the first time, the file  myfilel  is opened and its first line is read. Subsequent executions of &lt;&gt; each read another line of input from  myfilel. When  myfilel  is exhausted,  myf ile2  is opened and read one line at a time.</p>
        <p id="p_04798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04798"> Opening Pipes</p>
        <p id="p_04799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04799"> On machines running the UNIX operating system, two commands can be linked using a  pipe.  In this case, the standard output from the first command is linked, or piped, to the standard input to the second command.</p>
        <p id="p_04800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04800"> Perl enables you to establish a pipe that links a Perl output file to the standard input file of another command. To do this, associate the file with the command by calling  open,  as follows:</p>
        <p id="p_04801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04801"> open (MYPIPE, "| cat  &gt;hello");</p>
        <p id="p_04802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04802"> The | character tells the Perl interpreter to establish a pipe. When  MYPIPE  is opened, output sent to MYPIPE  becomes input to the command</p>
        <p id="p_04803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04803"> cat &gt;hello</p>
        <p id="p_04804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04804"> Because the  cat  command displays the contents of the standard input file when called with no arguments, and  &gt;hello  redirects the standard output file to the file  hello,  the  open  statement given here is identical to the statement</p>
        <p id="p_04805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04805"> open (MYPIPE, "&gt;hello");</p>
        <p id="p_04806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04806"> You can use a pipe to send mail from within a Perl program. For example:</p>
        <p id="p_04807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04807"> open (MESSAGE, "| mail dave");</p>
        <p id="p_04808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04808"> print MESSAGE ("Hi, Dave! Your Perl program sent  this!\n"); close (MESSAGE) ;</p>
        <p id="p_04809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04809"> The call to  open  establishes a pipe to the command  mail dave.  The file variable  MESSAGE  is now associated with this pipe. The call to  print  adds the line</p>
        <p id="p_04810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04810"> Hi, Dave! Your Perl program sent  this!</p>
        <p id="p_04811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04811"> to the message to be sent to user ID  dave.</p>
        <p id="p_04812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04812"> The call to  close  closes the pipe referenced by  MESSAGE,  which tells the system that the message is complete and can be sent. As you can see, the call to  close  is useful here because you can control exactly when the message is to be sent. (If you do not call  close, MESSAGE  is closed-and the message is sent-when the program terminates.)</p>
        <p id="p_04813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04813"> Summary</p>
        <p id="p_04814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04814"> Perl accesses files by means of file variables. File variables are associated with files by the  open  statement.</p>
        <p id="p_04815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04815"> Files can be opened in any of three modes: read mode, write mode, and append mode. A file opened in read mode cannot be written to; a file opened in either of the other modes cannot be read. Opening a file in write</p>
        <p id="p_04816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04816"> mode destroys the existing contents of the file.</p>
        <p id="p_04817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04817"> To read from an opened file, reference it using  &lt;name&gt;,  where  name  is a placeholder for the name of the file variable associated with the file. To write to a file, specify its file variable when calling  print.</p>
        <p id="p_04818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04818"> Perl defines three built-in file variables:</p>
        <p id="p_04819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04819"> • STD IN, which represents the standard input file</p>
        <p id="p_04820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04820"> • STDOUT,  which represents the standard output file</p>
        <p id="p_04821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04821"> • STDERR,  which represents the standard error file</p>
        <p id="p_04822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04822"> You can redirect  STDIN  and  STDOUT  by specifying &lt; and &gt;, respectively, on the command line. Messages sent to  STDERR  appear on the screen even if  STDOUT  is redirected to a file.</p>
        <p id="p_04823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04823"> The  close  function closes the file associated with a particular file variable,  close  never needs to be called unless you want to control exactly when a file is to be made inaccessible.</p>
        <p id="p_04824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04824"> The file-test operators provide a way of retrieving information on a particular file. The most common file-test operators are</p>
        <p id="p_04825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04825"> • -e,  which tests whether a file exists</p>
        <p id="p_04826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04826"> •  -r, -w,  and  -x,  which test whether a file has read, write, and execute permission, respectively</p>
        <p id="p_04827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04827"> •  -z,  which tests whether a file is empty</p>
        <p id="p_04828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04828"> • - s, which returns the size of a file</p>
        <p id="p_04829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04829"> You can use  -w  and  -z  to ensure that you do not overwrite a non-empty file.</p>
        <p id="p_04830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04830"> The &lt;&gt; operator enables you to read data from files specified on the command line. This operator uses the built-in array variable  @ARGV,  whose elements consist of the items specified on the command line.</p>
        <p id="p_04831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04831"> Perl enables you to open pipes. A pipe links the output from your Perl program to the input to another program.</p>
        <p id="p_04832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04832"> Q&amp;A</p>
        <p id="p_04833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04833"> Q: How many files can I have open at one time?</p>
        <p id="p_04834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04834"> A: Basically, as many as you like. The actual limit depends on the limitations of your operating</p>
        <p id="p_04835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04835"> system.</p>
        <p id="p_04836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04836"> Q: Why does adding a closing newline character to the text string affect how die behaves?</p>
        <p id="p_04837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04837"> A: Perl enables you to choose whether you want the filename and line number of the error message to</p>
        <p id="p_04838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04838"> appear. If you add a closing newline character to the string, the Perl interpreter assumes that you want to control how your error message is to appear.</p>
        <p id="p_04839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04839"> Q: Which is better: to use &lt;&gt;, or to use  @ARGV  and  shift  when appropriate?</p>
        <p id="p_04840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04840"> A: As is often the case, the answer is "It depends." If your program treats almost all of the command¬</p>
        <p id="p_04841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04841"> line arguments as files, it is better to use &lt;&gt; because the mechanics of opening and closing files are taken care of for you. If you are doing a lot of unusual things with  @ARGV,  it is better not to manipulate it to use &lt;&gt;, because things can get complicated and confusing.</p>
        <p id="p_04842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04842"> Q: Can I open more than one pipe at a time?</p>
        <p id="p_04843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04843"> A: Yes. Your operating system keeps all of the various commands and processes organized and keeps</p>
        <p id="p_04844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04844"> track of which output goes with which input.</p>
        <p id="p_04845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04845"> Q:  Can  I redirect STDERR?</p>
        <p id="p_04846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04846"> A: Yes, but there is (normally) no reason why you should.  STDERR's  job is to report extraordinary</p>
        <p id="p_04847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04847"> conditions, and you usually want to see these, not have them buried in a file somewhere.</p>
        <p id="p_04848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04848"> Q: How many command-line arguments can I specify?</p>
        <p id="p_04849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04849"> A: Basically, as many as your command-line shell can handle.</p>
        <p id="p_04850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04850"> Q: Can I write to a file and then read from it later?</p>
        <p id="p_04851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04851"> A: Yes, but you can't do both at the same time. To read from a file you have written to, close the file</p>
        <p id="p_04852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04852"> by calling  close  and then open the file in read mode.</p>
        <p id="p_04853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04853"> Workshop</p>
        <p id="p_04854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04854"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_04855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04855"> Quiz</p>
        <p id="p_04856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04856"> 1. Define the following terms:</p>
        <p id="p_04857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04857"> a. file variable</p>
        <p id="p_04858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04858"> b. reserved word</p>
        <p id="p_04859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04859"> c. file mode</p>
        <p id="p_04860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04860"> d. append mode</p>
        <p id="p_04861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04861"> e. pipe</p>
        <p id="p_04862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04862"> 2. From where does the &lt;&gt; operator read its data?</p>
        <p id="p_04863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04863"> 3. What do the following file-test operators do?</p>
        <p id="p_04864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04864"> a. -r</p>
        <p id="p_04865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04865"> b. -x</p>
        <p id="p_04866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04866"> c. -s</p>
        <p id="p_04867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04867"> 4. What are the contents of the array  @ARGV  when the following Perl program is executed?</p>
        <p id="p_04868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04868"> $ myprog filel file2 file3</p>
        <p id="p_04869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04869"> 5. How do you indicate that a file is to be opened:</p>
        <p id="p_04870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04870"> a. In write mode?</p>
        <p id="p_04871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04871"> b. In append mode?</p>
        <p id="p_04872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04872"> c. In read mode?</p>
        <p id="p_04873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04873"> d. As a pipe?</p>
        <p id="p_04874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04874"> 6. What is the relationship between  @ARGV  and the &lt;&gt; operator?</p>
        <p id="p_04875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04875"> Exercises</p>
        <p id="p_04876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04876"> 1. Write a program that takes the values on the command line, adds them together, and prints the result.</p>
        <p id="p_04877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04877"> 2. Write a program that takes a list of files from the command line and examines their size. If a file is bigger than 10,000 bytes, print</p>
        <p id="p_04878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04878"> File name is a big file!</p>
        <p id="p_04879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04879"> where  name  is a placeholder for the name of the big file.</p>
        <p id="p_04880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04880"> 3. Write a program that copies a file named  filel  to  file 2,  and then appends another copy of filel  to  file2.</p>
        <p id="p_04881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04881"> 4. Write a program that counts the total number of words in the files specified on the command line. When it has counted the words, it sends a message to user ID  dave  indicating the total number of words.</p>
        <p id="p_04882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04882"> 5. Write a program that takes a list of files and indicates, for each file, whether the user has read, write, or execute permission.</p>
        <p id="p_04883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04883"> 6. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_04884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04884"> #!/usr/local/bin/perl</p>
        <p id="p_04885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04885"> open (OUTFILE, "outfile");</p>
        <p id="p_04886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04886"> print OUTFILE ("This is my message\n");</p>
        <p id="p_04887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04887"> Chapter 7</p>
        <p id="p_04888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04888"> Pattern Matching</p>
        <p id="p_04889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04889"> CONTENTS</p>
        <p id="p_04890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04890"> •  Introduction</p>
        <p id="p_04891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04891"> •  The Match Operators</p>
        <p id="p_04892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04892"> o  Match-Operator Precedence</p>
        <p id="p_04893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04893"> •  Special Characters in Patterns</p>
        <p id="p_04894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04894"> o  The + Character o  The [ 1 Special Characters o  The * and ? Special Characters o  Escape Sequences for Special Characters</p>
        <p id="p_04895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04895"> o  Matching Any Letter or Number</p>
        <p id="p_04896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04896"> o  Anchoring Patterns o  Variable Substitution in Patterns</p>
        <p id="p_04897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04897"> o  Excluding Alternatives o  Character-Range Escape Sequences</p>
        <p id="p_04898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04898"> o  Matching Any Character o  Matching a Specified Number of Occurrences</p>
        <p id="p_04899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04899"> o  Specifying Choices o  Reusing Portions of Patterns o  Pattern-Sequence Scalar Variables</p>
        <p id="p_04900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04900"> o  Special-Character Precedence o  Specifying a Different Pattern Delimiter</p>
        <p id="p_04901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04901"> •  Pattern-Matching Options</p>
        <p id="p_04902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04902"> o  Matching All Possible Patterns</p>
        <p id="p_04903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04903"> o  Ignoring Case</p>
        <p id="p_04904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04904"> o  Treating the String as Multiple Lines</p>
        <p id="p_04905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04905"> o  Evaluating a Pattern Only Once o  Treating the String as a Single Line</p>
        <p id="p_04906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04906"> o  Using White Space in Patterns</p>
        <p id="p_04907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04907"> •  The Substitution Operator</p>
        <p id="p_04908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04908"> o  Using Pattern-Sequence Variables in Substitutions</p>
        <p id="p_04909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04909"> o  Options for the Substitution Operator</p>
        <p id="p_04910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04910"> o  Evaluating a Pattern Only Once</p>
        <p id="p_04911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04911"> Treating the String as Single or Multiple Lines</p>
        <p id="p_04912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04912"> o</p>
        <p id="p_04913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04913"> o</p>
        <p id="p_04914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04914"> Using White Space in Patterns o  Specifying a Different Delimiter</p>
        <p id="p_04915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04915"> •  The Translation Operator</p>
        <p id="p_04916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04916"> o  Options for the Translation Operator</p>
        <p id="p_04917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04917"> •  Extended Pattern-Matching</p>
        <p id="p_04918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04918"> o  Parenthesizing Without Saving in Memory</p>
        <p id="p_04919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04919"> o  Embedding Pattern Options o  Positive and Negative Look-Ahead</p>
        <p id="p_04920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04920"> o  Pattern Comments</p>
        <p id="p_04921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04921"> •  Summary</p>
        <p id="p_04922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04922"> •  Q&amp;A</p>
        <p id="p_04923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04923"> •  Workshop</p>
        <p id="p_04924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04924"> o  Quiz o  Exercises</p>
        <p id="p_04925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04925"> This lesson describes the pattern-matching features of Perl. Today, you learn about the following:</p>
        <p id="p_04926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04926"> • How pattern matching works</p>
        <p id="p_04927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04927"> • The pattern-matching operators</p>
        <p id="p_04928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04928"> • Special characters supported in pattern matching</p>
        <p id="p_04929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04929"> • Pattern-matching options</p>
        <p id="p_04930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04930"> • Pattern substitution</p>
        <p id="p_04931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04931"> • Translation</p>
        <p id="p_04932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04932"> • Extended pattern-matching features</p>
        <p id="p_04933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04933"> Introduction</p>
        <p id="p_04934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04934"> A  pattern  is a sequence of characters to be searched for in a character string. In Perl, patterns are normally enclosed in slash characters:</p>
        <p id="p_04935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04935"> /def /</p>
        <p id="p_04936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04936"> This represents the pattern  def.</p>
        <p id="p_04937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04937"> If the pattern is found, a match occurs. For example, if you search the string  redefine  for the pattern  /def /, the pattern matches the third, fourth, and fifth characters.</p>
        <p id="p_04938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04938"> redefine</p>
        <p id="p_04939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04939"> You already have seen a simple example of pattern matching in the library function  split.</p>
        <p id="p_04940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04940"> @array = split(/ /, $line);</p>
        <p id="p_04941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04941"> Here the pattern / / matches a single space, which splits a line into words.</p>
        <p id="p_04942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04942"> The Match Operators</p>
        <p id="p_04943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04943"> Perl defines special operators that test whether a particular pattern appears in a character string.</p>
        <p id="p_04944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04944"> The =~ operator tests whether a pattern is matched, as shown in the following:</p>
        <p id="p_04945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04945"> $result = $var =~ /abc/;</p>
        <p id="p_04946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04946"> The result of the =~ operation is one of the following:</p>
        <p id="p_04947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04947"> • A nonzero value, or true, if the pattern is found in the string</p>
        <p id="p_04948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04948"> • 0, or false, if the pattern is not matched</p>
        <p id="p_04949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04949"> In this example, the value stored in the scalar variable  $var  is searched for the pattern  abc.  If  abc  is found,  $result  is assigned a nonzero value; otherwise,  $result  is set to zero.</p>
        <p id="p_04950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04950"> The ! ~ operator is similar to =~, except that it checks whether a pattern is not matched.</p>
        <p id="p_04951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04951"> $result = $var !~ /abc/;</p>
        <p id="p_04952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04952"> Here,  $result  is set to  0  if  abc  appears in the string assigned to  $var,  and to a nonzero value if abc  is not found.</p>
        <p id="p_04953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04953"> Because =~ and ! ~ produce either true or false as their result, these operators are ideally suited for use in conditional expressions. Listing 7.1 is a simple program that uses the =~ operator to test whether a particular sequence of characters exists in a character string.</p>
        <p id="p_04954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04954"> Type</p>
        <p id="p_04955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04955"> Listing  7.1. A  program that illustrates the use of the matching operator.</p>
        <p id="p_04956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04956"> 8: print ("That was not very polite!\n");</p>
        <p id="p_04957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04957"> 9: }</p>
        <p id="p_04958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04958"> Output</p>
        <p id="p_04959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04959"> $ program7_l</p>
        <p id="p_04960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04960"> Ask me a question politely:</p>
        <p id="p_04961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04961"> May I have a glass of water, please? Thank you for being polite!</p>
        <p id="p_04962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04962"> $</p>
        <p id="p_04963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04963"> Line 5 is an example of the use of the match operator =~ in a conditional expression. The following expression is true if the value stored in  $question  contains the word  please,  and it is false if it does not:</p>
        <p id="p_04964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04964"> Analyse</p>
        <p id="p_04965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04965"> $question</p>
        <p id="p_04966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04966"> /please/</p>
        <p id="p_04967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04967"> Match-Operator Precedence</p>
        <p id="p_04968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04968"> Like all operators, the match operators have a defined precedence. By definition, the =~ and ! ~ operators have higher precedence than multiplication and division, and lower precedence than the exponentiation operator * *.</p>
        <p id="p_04969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04969"> For a complete list of Perl operators and their precedence, see  Day  4 ,  "More Operators."</p>
        <p id="p_04970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04970"> Special Characters in Patterns</p>
        <p id="p_04971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04971"> Perl supports a variety of special characters inside patterns, which enables you to match any of a number of character strings. These special characters are what make patterns useful.</p>
        <p id="p_04972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04972"> The + Character</p>
        <p id="p_04973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04973"> The special character + means "one or more of the preceding characters." For example, the pattern /de+f  / matches any of the following:</p>
        <p id="p_04974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04974"> def</p>
        <p id="p_04975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04975"> deef</p>
        <p id="p_04976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04976"> deeef</p>
        <p id="p_04977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04977"> deeeeeeef</p>
        <p id="p_04978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04978"> The + special character makes it possible to define a better way to split lines into words. So far, the</p>
        <p id="p_04979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04979"> sample programs you have seen have used</p>
        <p id="p_04980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04980"> ©words = split (/ /, $line);</p>
        <p id="p_04981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04981"> to break an input line into words. This works well if there is exactly one space between words. However, if an input line contains more than one space between words, as in</p>
        <p id="p_04982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04982"> Here's multiple spaces.</p>
        <p id="p_04983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04983"> the call to  split  produces the following list:</p>
        <p id="p_04984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04984"> ("Here's", "multiple", "", "spaces.")</p>
        <p id="p_04985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04985"> The pattern / / tells  split  to start a new word whenever it sees a space. Because there are two spaces between each word,  split  starts a word when it sees the first space, and then starts another word when it sees the second space. This means that there are now "empty words" in the line.</p>
        <p id="p_04986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04986"> The + special character gets around this problem. Suppose the call to  split  is changed to this:</p>
        <p id="p_04987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04987"> ©array = split (/ +/, $line);</p>
        <p id="p_04988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04988"> Because the pattern / +/ tries to match as many blank characters as possible, the line</p>
        <p id="p_04989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04989"> Here's multiple spaces.</p>
        <p id="p_04990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04990"> produces the following list:</p>
        <p id="p_04991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04991"> ("Here's", "multiple", "spaces")</p>
        <p id="p_04992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04992"> Listing 7.2 shows how you can use the / +/ pattern to produce a count of the number of words in a file.</p>
        <p id="p_04993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04993"> Type</p>
        <p id="p_04994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04994"> Listing 7.2. A word-count program that handles multiple spaces between words.</p>
        <p id="p_04995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04995"> Output</p>
        <p id="p_04996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04996"> A D</p>
        <p id="p_04997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04997"> Total number of words: 14</p>
        <p id="p_04998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04998"> $</p>
        <p id="p_04999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_04999"> This is the same word-count program you saw in Listing 5.15, with only one change: The pattern / +/ is being used to break the line into words. As you can see, this handles spaces between words properly.</p>
        <p id="p_05000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05000"> You might have noticed the following problems with this word-count program:</p>
        <p id="p_05001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05001"> • Spaces at the beginning of a line are counted as a word, because  split  always starts a new word when it sees a space.</p>
        <p id="p_05002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05002"> • Tab characters are counted as a word.</p>
        <p id="p_05003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05003"> For an example of the first problem, take a look at the following input line:</p>
        <p id="p_05004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05004"> Analysis</p>
        <p id="p_05005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05005"> This line contains leading spaces.</p>
        <p id="p_05006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05006"> The call to split in line 7 breaks the preceding into the following list:</p>
        <p id="p_05007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05007"> ("",  "This", "line", "contains", "leading", "spaces")</p>
        <p id="p_05008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05008"> This yields a word count of 6, not the expected 5.</p>
        <p id="p_05009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05009"> There can be at most one empty word produced from a line, no matter how many leading spaces there are, because the pattern / + / matches as many spaces as possible. Note also that the program can distinguish between lines containing words and lines that are blank or contain just spaces. If a line is blank or contains only spaces, the line</p>
        <p id="p_05010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05010"> @words = split (/ +/, $line);</p>
        <p id="p_05011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05011"> assigns the empty list to  @words.  Because of this, you can fix the problem of leading spaces in lines by modifying line 8 as follows:</p>
        <p id="p_05012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05012"> $wordcount +=</p>
        <p id="p_05013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05013"> (@words &gt; 0 &amp;&amp;</p>
        <p id="p_05014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05014"> $words[0]</p>
        <p id="p_05015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05015"> eg "" ?</p>
        <p id="p_05016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05016"> @words-l</p>
        <p id="p_05017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05017"> @words);</p>
        <p id="p_05018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05018"> This checks for lines containing leading spaces; if a line contains leading spaces, the first "word (which is the empty string) is not added to the word count.</p>
        <p id="p_05019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05019"> To find out how to modify the program to deal with tab characters as well as spaces, see the following section.</p>
        <p id="p_05020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05020"> The [ ] Special Characters</p>
        <p id="p_05021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05021"> The [ ] special characters enable you to define patterns that match one of a group of alternatives. For example, the following pattern matches  de f  or  dE f:</p>
        <p id="p_05022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05022"> /d[eE]f/</p>
        <p id="p_05023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05023"> You can specify as many alternatives as you like.</p>
        <p id="p_05024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05024"> /a[0123456789] c/</p>
        <p id="p_05025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05025"> This matches  a,  followed by any digit, followed by  c.</p>
        <p id="p_05026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05026"> You can combine [ ] with + to match a sequence of characters of any length.</p>
        <p id="p_05027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05027"> /d[eE]+f/</p>
        <p id="p_05028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05028"> This matches all of the following:</p>
        <p id="p_05029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05029"> def</p>
        <p id="p_05030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05030"> dEf</p>
        <p id="p_05031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05031"> deef</p>
        <p id="p_05032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05032"> dEef</p>
        <p id="p_05033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05033"> dEEEeeeEef</p>
        <p id="p_05034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05034"> Any combination of  E  and  e,  in any order, is matched by [  eE ] +.</p>
        <p id="p_05035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05035"> You can use [ ] and + together to modify the word-count program you’ve just seen to accept either tab characters or spaces. Listing 7.3 shows how you can do this.</p>
        <p id="p_05036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05036"> Type</p>
        <p id="p_05037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05037"> Listing 7.3. A word-count program that handles multiple spaces and tabs between</p>
        <p id="p_05038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05038"> Output</p>
        <p id="p_05039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05039"> $ program7_3</p>
        <p id="p_05040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05040"> Here is some input.</p>
        <p id="p_05041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05041"> Here are some more words. Here is my last line.</p>
        <p id="p_05042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05042"> A D</p>
        <p id="p_05043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05043"> Total number of words: 14</p>
        <p id="p_05044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05044"> $</p>
        <p id="p_05045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05045"> This program is identical to Listing 7.2, except that the pattern is now / [ \ t ] + /.</p>
        <p id="p_05046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05046"> The \ t special-character sequence represents the tab character, and this pattern matches any combination or quantity of spaces and tabs.</p>
        <p id="p_05047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05047"> NOTE</p>
        <p id="p_05048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05048"> Any escape sequence that is supported in double-quoted strings is supported in patterns. See  Day  3 , "Understanding Scalar Values," for a list of the escape sequences that are available.</p>
        <p id="p_05049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05049"> The * and ? Special Characters</p>
        <p id="p_05050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05050"> As you have seen, the + character matches one or more occurrences of a character. Perl also defines two other special characters that match a varying number of characters: * and ?.</p>
        <p id="p_05051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05051"> The * special character matches zero or more occurrences of the preceding character. For example, the pattern</p>
        <p id="p_05052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05052"> /de*f/</p>
        <p id="p_05053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05053"> matches  df, def, deef,  and so on.</p>
        <p id="p_05054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05054"> This character can also be used with the [ ] special character.</p>
        <p id="p_05055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05055"> /[eE]*/</p>
        <p id="p_05056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05056"> This matches the empty string as well as any combination of  E  or  e  in any order.</p>
        <p id="p_05057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05057"> Warning</p>
        <p id="p_05058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05058"> Be sure not to confuse the * special character with the + special character. If you use the wrong special character, you might not get the results that you want.</p>
        <p id="p_05059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05059"> For example, suppose that you modify Listing 7.3 to call  split as follows:</p>
        <p id="p_05060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05060"> @words = split  (/[\t ]*/,  $list);</p>
        <p id="p_05061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05061"> This matches zero or more occurrences of the space or tab character. When you run this with the input</p>
        <p id="p_05062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05062"> a line</p>
        <p id="p_05063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05063"> here's the list that is assigned to @ words:</p>
        <p id="p_05064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05064"> ("a",  "1",  "i",  "n",  "e")</p>
        <p id="p_05065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05065"> Because the pattern / [ \  t  ] * / matches on zero occurrences of the space or tab character, it matches after every character. This means that  split  starts a word after every character that is not a space or tab. (It skips spaces and tabs because / [ \  t ] * / matches them.)</p>
        <p id="p_05066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05066"> The best way to avoid problems such as this one is to use the * special character only when there is another character appearing in the pattern. Patterns such as</p>
        <p id="p_05067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05067"> /b* [c] /</p>
        <p id="p_05068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05068"> never match the null string, because the matched sequence has to contain at least the character  c.</p>
        <p id="p_05069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05069"> The ? character matches zero or one occurrence of the preceding character. For example, the pattern</p>
        <p id="p_05070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05070"> /de?f/</p>
        <p id="p_05071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05071"> matches either  df  or  def . Note that it does not match  deef , because the ? character does not match two occurrences of a character.</p>
        <p id="p_05072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05072"> Escape Sequences for Special Characters</p>
        <p id="p_05073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05073"> If you want your pattern to include a character that is normally treated as a special character, precede the character with a backslash \. For example, to check for one or more occurrences of * in a string, use the following pattern:</p>
        <p id="p_05074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05074"> /\* + /</p>
        <p id="p_05075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05075"> The backslash preceding the * tells the Perl interpreter to treat the * as an ordinary character, not as the special character meaning "zero or more occurrences."</p>
        <p id="p_05076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05076"> To include a backslash in a pattern, specify two backslashes:</p>
        <p id="p_05077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05077"> /\\ + /</p>
        <p id="p_05078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05078"> This pattern tests for one or more occurrences of \ in a string.</p>
        <p id="p_05079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05079"> If you are running Perl 5, another way to tell Perl that a special character is to be treated as a normal character is to precede it with the  \Q  escape sequence. When the Perl interpreter sees  \Q,  every character following the \  Q  is treated as a normal character until \  E  is seen. This means that the pattern</p>
        <p id="p_05080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05080"> / \ Q A ab*/</p>
        <p id="p_05081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05081"> matches any occurrence of the string  A   ab  *, and the pattern</p>
        <p id="p_05082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05082"> /\Q A ab\E*/</p>
        <p id="p_05083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05083"> matches  A a  followed by zero or more occurrences of  b.</p>
        <p id="p_05084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05084"> For a complete list of special characters in patterns that require \ to be given their natural meaning, see the section titled "Special-Character Precedence," which contains a table that lists them.</p>
        <p id="p_05085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05085"> TIP</p>
        <p id="p_05086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05086"> In Perl, any character that is not a letter or a digit can be preceded by a backslash. If the character isn't a special character in Perl, the backslash is ignored.</p>
        <p id="p_05087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05087"> If you are not sure whether a particular character is a special character, preceding it with a backslash will ensure that your pattern behaves the way you want it to.</p>
        <p id="p_05088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05088"> Matching Any Letter or Number</p>
        <p id="p_05089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05089"> As you have seen, the pattern</p>
        <p id="p_05090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05090"> /a[0123456789]c/</p>
        <p id="p_05091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05091"> matches  a,  followed by any digit, followed by  c.  Another way of writing this is as follows:</p>
        <p id="p_05092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05092"> /a[0-9]c/</p>
        <p id="p_05093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05093"> Here, the range [  0-9  ] represents any digit between 0 and 9. This pattern matches  aOc, ale, a2c, and so on up to  a 9c.</p>
        <p id="p_05094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05094"> Similarly, the range [  a-z  ] matches any lowercase letter, and the range [  A-Z  ] matches any uppercase letter. For example, the pattern</p>
        <p id="p_05095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05095"> /[A-Z][A-Z]/</p>
        <p id="p_05096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05096"> matches any two uppercase letters.</p>
        <p id="p_05097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05097"> To match any uppercase letter, lowercase letter, or digit, use the following range:</p>
        <p id="p_05098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05098"> / [ 0-9a-zA-Z]/</p>
        <p id="p_05099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05099"> Listing 7.4 provides an example of the use of ranges with the [ ] special characters. This program checks whether a given input line contains a legal Perl scalar, array, or file-variable name. (Note that this program handles only simple input lines. Later examples will solve this problem in a better way.)</p>
        <p id="p_05100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05100"> Type</p>
        <p id="p_05101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05101"> Listing 7.4. A simple variable-name validation program.</p>
        <p id="p_05102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05102"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05103"> 2 :</p>
        <p id="p_05104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05104"> 3: print ("Enter a variable name:\n");</p>
        <p id="p_05105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05105"> 4: $varname = &lt;STDIN&gt;;</p>
        <p id="p_05106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05106"> 5: chop ($varname);</p>
        <p id="p_05107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05107"> 6: if ($varname =~ /\$[A-Za-z][_0-9a-zA-Z]*/) {</p>
        <p id="p_05108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05108"> 7: print ("$varname is a legal scalar variable\n");</p>
        <p id="p_05109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05109"> 8: } elsif ($varname =~ /@[A-Za-z][_0-9a-zA-Z]*/) {</p>
        <p id="p_05110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05110"> 9: print ("$varname is a legal array variable\n");</p>
        <p id="p_05111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05111"> 10: } elsif ($varname =~ /[A-Za-z][_0-9a-zA-Z]*/) {</p>
        <p id="p_05112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05112"> 11: print ("$varname is a legal file variable\n");</p>
        <p id="p_05113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05113"> 12: } else {</p>
        <p id="p_05114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05114"> 13: print ("I don't understand what $varname is.\n");</p>
        <p id="p_05115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05115"> 14: }</p>
        <p id="p_05116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05116"> OuTPUt</p>
        <p id="p_05117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05117"> $ program7_4</p>
        <p id="p_05118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05118"> Enter a variable name:</p>
        <p id="p_05119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05119"> $result</p>
        <p id="p_05120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05120"> $result is a legal scalar variable</p>
        <p id="p_05121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05121"> $</p>
        <p id="p_05122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05122"> Analysis</p>
        <p id="p_05123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05123"> Line 6 checks whether the input line contains the name of a legal scalar variable. Recall that a legal scalar variable consists of the following:</p>
        <p id="p_05124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05124"> • A $ character</p>
        <p id="p_05125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05125"> • An uppercase or lowercase letter</p>
        <p id="p_05126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05126"> • Zero or more letters, digits, or underscore characters</p>
        <p id="p_05127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05127"> Each part of the pattern tested in line 6 corresponds to one of the aforementioned conditions given. The first part of the pattern, \ $, ensures that the pattern matches only if it begins with a $ character.</p>
        <p id="p_05128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05128"> NOTE</p>
        <p id="p_05129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05129"> The $ is preceded by a backslash, because $ is a special character in patterns. See the following section, "Anchoring Patterns," for more information on the $ special character.</p>
        <p id="p_05130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05130"> The second part of the pattern,</p>
        <p id="p_05131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05131"> [A-Za-z]</p>
        <p id="p_05132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05132"> matches exactly one uppercase or lowercase letter. The final part of the pattern,</p>
        <p id="p_05133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05133"> [_0-9a-zA-Z]*</p>
        <p id="p_05134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05134"> matches zero or more underscores, digits, or letters in any order.</p>
        <p id="p_05135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05135"> The patterns in line 8 and line 10 are very similar to the one in line 6. The only difference in line 8 is that the pattern there matches a string whose first character is @, not $. In line 10, this first character is omitted completely.</p>
        <p id="p_05136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05136"> The pattern in line 8 corresponds to the definition of a legal array-variable name, and the pattern in line 10 corresponds to the definition of a legal file-variable name.</p>
        <p id="p_05137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05137"> Anchoring Patterns</p>
        <p id="p_05138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05138"> Although Listing 7.4 can determine whether a line of input contains a legal Perl variable name, it cannot determine whether there is extraneous input on the line. For example, it can't tell the difference between the following three lines of input:</p>
        <p id="p_05139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05139"> $result junk$result $result# junk</p>
        <p id="p_05140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05140"> In all three cases, the pattern</p>
        <p id="p_05141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05141"> / \$[a-zA-Z] [_0-9a-zA-Z]*/</p>
        <p id="p_05142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05142"> finds the string $  result  and matches successfully; however, only the first line is a legal Perl variable name.</p>
        <p id="p_05143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05143"> To fix this problem, you can use  pattern anchors.  Table 7.1 lists the pattern anchors defined in Perl.</p>
        <p id="p_05144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05144"> Table 7.1. Pattern anchors in Perl.</p>
        <p id="p_05145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05145"> These pattern anchors are described in the following sections.</p>
        <p id="p_05146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05146"> The  A  and $ Pattern Anchors</p>
        <p id="p_05147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05147"> The pattern anchors  A  and $ ensure that the pattern is matched only at the beginning or the end of a string. For example, the pattern</p>
        <p id="p_05148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05148"> /  A def /</p>
        <p id="p_05149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05149"> matches  def  only if these are the first three characters in the string. Similarly, the pattern</p>
        <p id="p_05150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05150"> /def $/</p>
        <p id="p_05151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05151"> matches  def  only if these are the last three characters in the string.</p>
        <p id="p_05152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05152"> You can combine  A  and $ to force matching of the entire string, as follows:</p>
        <p id="p_05153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05153"> / A def$/</p>
        <p id="p_05154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05154"> This matches only if the string is  def.</p>
        <p id="p_05155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05155"> In most cases, the escape sequences  \A  and  \Z  (defined in Perl 5) are equivalent to  A  and $, respectively:</p>
        <p id="p_05156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05156"> /\Adef\Z/</p>
        <p id="p_05157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05157"> This also matches only if the string is  def.</p>
        <p id="p_05158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05158"> NOTE</p>
        <p id="p_05159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05159"> \A  and  \Z  behave differently from  A  and $ when the multiple¬ line pattern-matching option is specified. Pattern-matching options are described later today.</p>
        <p id="p_05160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05160"> Listing 7.5 shows how you can use pattern anchors to ensure that a line of input is, in fact, a legal Perl scalar-, array-, or file-variable name.</p>
        <p id="p_05161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05161"> Listing 7.5. A better variable-name validation program.</p>
        <p id="p_05162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05162"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05163"> 2 :</p>
        <p id="p_05164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05164"> 3: print ("Enter a variable name:\n");</p>
        <p id="p_05165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05165"> 4: $varname = &lt;STDIN&gt;;</p>
        <p id="p_05166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05166"> 5: chop ($varname);</p>
        <p id="p_05167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05167"> 6: if ($varname =~ / A \$[A-Za-z] [_0-9a-zA-Z]* $/) {</p>
        <p id="p_05168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05168"> 7: print ("$varname is a legal scalar variable\n");</p>
        <p id="p_05169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05169"> 8: } elsif ($varname =~ / A @[A-Za-z] [_0-9a-zA-Z]* $/) {</p>
        <p id="p_05170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05170"> 9: print ("$varname is a legal array variable\n");</p>
        <p id="p_05171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05171"> 10: } elsif ($varname =~ / A [A-Za-z] [_0-9a-zA-Z]* $/) {</p>
        <p id="p_05172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05172"> 11: print ("$varname is a legal file variable\n");</p>
        <p id="p_05173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05173"> 12: } else {</p>
        <p id="p_05174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05174"> 13: print ("I don't understand what $varname is.\n");</p>
        <p id="p_05175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05175"> 14: }</p>
        <p id="p_05176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05176"> Output</p>
        <p id="p_05177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05177"> $ program7_5</p>
        <p id="p_05178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05178"> Enter a variable name:</p>
        <p id="p_05179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05179"> x$result</p>
        <p id="p_05180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05180"> I don't understand what x$result is.</p>
        <p id="p_05181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05181"> $</p>
        <p id="p_05182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05182"> Analysis</p>
        <p id="p_05183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05183"> The only difference between this program and the one in Listing 7.4 is that this program uses the pattern anchors  A  and $ in the patterns in lines 6, 8, and 10. These anchors ensure that a valid pattern consists of only those characters that make up a legal Perl scalar, array, or file variable.</p>
        <p id="p_05184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05184"> In the sample output given here, the input</p>
        <p id="p_05185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05185"> x$result</p>
        <p id="p_05186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05186"> is rejected, because the pattern in line 6 is matched only when the $ character appears at the beginning of the line.</p>
        <p id="p_05187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05187"> Word-Boundary Pattern Anchors</p>
        <p id="p_05188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05188"> The word-boundary pattern anchors,  \b  and  \B,  specify whether a matched pattern must be on a word boundary or inside a word boundary. (A word boundary is the beginning or end of a word.)</p>
        <p id="p_05189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05189"> The  \b  pattern anchor specifies that the pattern must be on a word boundary. For example, the pattern</p>
        <p id="p_05190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05190"> /\bdef/</p>
        <p id="p_05191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05191"> matches only if  def  is the beginning of a word. This means that  def  and  def ghi  match but abodef  does not.</p>
        <p id="p_05192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05192"> You can also use  \b  to indicate the end of a word. For example,</p>
        <p id="p_05193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05193"> /def\b/</p>
        <p id="p_05194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05194"> matches  def  and  abode f,  but not  def ghi.  Finally, the pattern</p>
        <p id="p_05195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05195"> /\bdef\b/</p>
        <p id="p_05196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05196"> matches only the word  def,  not  abode f  or  def ghi.</p>
        <p id="p_05197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05197"> NOTE</p>
        <p id="p_05198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05198"> A word is assumed to contain letters, digits, and underscore characters, and nothing else. This means that</p>
        <p id="p_05199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05199"> /\bdef/</p>
        <p id="p_05200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05200"> matches  $def ghi:  because $ is not assumed to be part of a word,  def  is the beginning of the word  defghi,  and  /\bdef / matches it.</p>
        <p id="p_05201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05201"> The  \B  pattern anchor is the opposite of  \b. \B  matches only if the pattern is contained in a word. For example, the pattern</p>
        <p id="p_05202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05202"> /\Bdef/</p>
        <p id="p_05203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05203"> matches  abodef,  but not  def.  Similarly, the pattern</p>
        <p id="p_05204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05204"> /def\B/</p>
        <p id="p_05205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05205"> matches  defghi,  and</p>
        <p id="p_05206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05206"> /\Bdef\B/</p>
        <p id="p_05207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05207"> matches  cdefg  or  abcdefghi,  but not  def, defghi,  or  abcdef.</p>
        <p id="p_05208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05208"> The  \b  and  \B  pattern anchors enable you to search for words in an input line without having to break up the line using  split.  For example, Listing 7.6 uses  \b  to count the number of lines of an input file that contain the word  the.</p>
        <p id="p_05209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05209"> Listing 7.6. A program that counts the number of input lines containing the word the.</p>
        <p id="p_05210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05210"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05211"> $thecount = 0;</p>
        <p id="p_05212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05212"> 2 :</p>
        <p id="p_05213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05213"> 3 :</p>
        <p id="p_05214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05214"> 4 :</p>
        <p id="p_05215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05215"> 5 : 6 :</p>
        <p id="p_05216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05216"> 7 :</p>
        <p id="p_05217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05217"> 8  :</p>
        <p id="p_05218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05218"> print ("Enter the input  here:\n"); $line = &lt;STDIN&gt;; while ($line ne "") {</p>
        <p id="p_05219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05219"> if ($line =~  /\bthe\b/)  { $thecount += 1;</p>
        <p id="p_05220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05220"> 9: }</p>
        <p id="p_05221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05221"> 10: $line = &lt;STDIN&gt;;</p>
        <p id="p_05222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05222"> 11 : }</p>
        <p id="p_05223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05223"> 12: print ("Number of lines containing  'the'</p>
        <p id="p_05224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05224"> Output</p>
        <p id="p_05225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05225"> $ program7_6</p>
        <p id="p_05226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05226"> Enter the input here:</p>
        <p id="p_05227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05227"> Now is the time for all good men to come to the aid of the  party.</p>
        <p id="p_05228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05228"> A D</p>
        <p id="p_05229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05229"> Number of lines containing  'the': 3 $</p>
        <p id="p_05230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05230"> $thecount\n");</p>
        <p id="p_05231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05231"> Analysis</p>
        <p id="p_05232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05232"> This program checks each line in turn to see if it contains the word  the,  and then prints the total number of lines that contain the word.</p>
        <p id="p_05233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05233"> Line 7 performs the actual checking by trying to match the pattern</p>
        <p id="p_05234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05234"> /\bthe\b/</p>
        <p id="p_05235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05235"> If this pattern matches, the line contains the word  the,  because the pattern checks for word boundaries at either end.</p>
        <p id="p_05236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05236"> Note that this program doesn't check whether the word  the  appears on a line more than once. It is not difficult to modify the program to do this; in fact, you can do it in several different ways.</p>
        <p id="p_05237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05237"> The most obvious but most laborious way is to break up lines that you know contain  the  into words, and then check each word, as follows:</p>
        <p id="p_05238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05238"> if ($line =~ /\bthe\b/) {</p>
        <p id="p_05239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05239"> @words = split  (/[\t   ]+/,  $line);</p>
        <p id="p_05240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05240"> $count = 1;</p>
        <p id="p_05241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05241"> while ($count &lt;= @words) {</p>
        <p id="p_05242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05242"> if ($words[$count-l] eq "the") {</p>
        <p id="p_05243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05243"> $thecount += 1;</p>
        <p id="p_05244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05244"> }</p>
        <p id="p_05245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05245"> $count++;</p>
        <p id="p_05246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05246"> }</p>
        <p id="p_05247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05247"> }</p>
        <p id="p_05248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05248"> A cute way to accomplish the same thing is to use the pattern itself to break the line into words:</p>
        <p id="p_05249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05249"> if ($line =~ /\bthe\b/) {</p>
        <p id="p_05250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05250"> @words = split(/\bthe\b/, $line);</p>
        <p id="p_05251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05251"> $thecount += @words</p>
        <p id="p_05252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05252"> i;</p>
        <p id="p_05253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05253"> }</p>
        <p id="p_05254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05254"> In fact, you don't even need the  i f  statement.</p>
        <p id="p_05255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05255"> @words = split(/\bthe\b/, $line); $thecount += @words - 1;</p>
        <p id="p_05256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05256"> Here's why this works: Every time  split  sees the word  the,  it starts a new word. Therefore, the number of occurrences of  the  is equal to one less than the number of elements in  @words.  If there are no occurrences of  the, @words  has the length 1, and  $thecount  is not changed.</p>
        <p id="p_05257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05257"> Warning</p>
        <p id="p_05258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05258"> This trick works only if you know that there is at least one word on the line.</p>
        <p id="p_05259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05259"> Consider the following code, which tries to use the aforementioned trick on a line that has had its newline character removed using  chop:</p>
        <p id="p_05260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05260"> $line = &lt;STDIN&gt;; chop ($line);</p>
        <p id="p_05261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05261"> @words = split (/\bthe\b/, $line);</p>
        <p id="p_05262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05262"> $thecount += @words - 1;</p>
        <p id="p_05263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05263"> This code actually subtracts 1 from $  the count  if the line is blank or consists only of the word  the,  because in these cases @words  is the empty list and the length of  @words  is 0.</p>
        <p id="p_05264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05264"> Leaving off the call to  chop  protects against this problem, because there will always be at least one "word" in every line (consisting of the newline character).</p>
        <p id="p_05265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05265"> Variable Substitution in Patterns</p>
        <p id="p_05266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05266"> If you like, you can use the value of a scalar variable in a pattern. For example, the following code splits the line  $line  into words:</p>
        <p id="p_05267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05267"> $pattern =  "[\\t   ]+";</p>
        <p id="p_05268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05268"> @words = split (/$pattern/, $line);</p>
        <p id="p_05269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05269"> Because you can use a scalar variable in a pattern, there is nothing to stop you from reading the pattern from the standard input file. Listing 7.7 accepts a search pattern from a file and then searches for the pattern in the input files listed on the command line. If it finds the pattern, it prints the filename and line number of the match; at the end, it prints the total number of matches.</p>
        <p id="p_05270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05270"> This example assumes that two files exist,  f ilel  and  f ile2.  Each file contains the following:</p>
        <p id="p_05271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05271"> This is a line of input.</p>
        <p id="p_05272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05272"> This is another line of input.</p>
        <p id="p_05273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05273"> If you run this program with command-line arguments  f ilel  and  file 2  and search for the pattern another,  you get the output shown.</p>
        <p id="p_05274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05274"> Type</p>
        <p id="p_05275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05275"> Listing 7.7. A simple pattern-search program.</p>
        <p id="p_05276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05276"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05277"> 2 :</p>
        <p id="p_05278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05278"> 3: print ("Enter the search pattern:\n");</p>
        <p id="p_05279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05279"> 4: $pattern = &lt;STDIN&gt;;</p>
        <p id="p_05280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05280"> 5: chop ($pattern);</p>
        <p id="p_05281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05281"> 6: $filename = $ARGV[0];</p>
        <p id="p_05282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05282"> 7: $linenum = $matchcount = 0;</p>
        <p id="p_05283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05283"> 8: print ("Matches found:\n");</p>
        <p id="p_05284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05284"> 9: while ($line = &lt;&gt;) {</p>
        <p id="p_05285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05285"> OlJTPUt</p>
        <p id="p_05286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05286"> $ program7_7 filel file2 Enter the search pattern: another</p>
        <p id="p_05287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05287"> Matches found:</p>
        <p id="p_05288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05288"> filel, line 2</p>
        <p id="p_05289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05289"> file2, line 2</p>
        <p id="p_05290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05290"> Total number of matches: 2 $</p>
        <p id="p_05291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05291"> Analysis</p>
        <p id="p_05292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05292"> This program uses the following scalar</p>
        <p id="p_05293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05293"> variables to keep track of information:</p>
        <p id="p_05294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05294"> • $pattern  contains the search pattern read in from the standard input file</p>
        <p id="p_05295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05295"> • $ filename  contains the file currently being searched.</p>
        <p id="p_05296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05296"> • $linenum  contains the line number of the line currently being searched.</p>
        <p id="p_05297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05297"> • $matchcount  contains the total number of matches found to this point.</p>
        <p id="p_05298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05298"> Line 6 sets the current filename, which corresponds to the first element in the built-in array variable ©ARGV.  This array variable lists the arguments supplied on the command line. (To refresh your memory on how  0ARGV  works, refer back to  Day  6 ,  "Reading from and Writing to Files.") This current filename needs to be stored in a scalar variable, because the &lt; &gt; operator in line 9 shifts @ARGV  and destroys this name.</p>
        <p id="p_05299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05299"> Line 9 reads from each of the files on the command line in turn, one line at a time. The current input line is stored in the scalar variable  $line.  Once the line is read, line 10 adds 1 to the current line number.</p>
        <p id="p_05300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05300"> Lines 11-15 handle the matching process. Line 11 checks whether the pattern stored in  $pattern  is contained in the input line stored in $  line.  If a match is found, line 12 prints out the current filename and line number. Line 13 then splits the line into "words," using the trick described in the earlier section, "Word-Boundary Pattern Anchors." Because the number of elements of the list stored in  ©words  is one larger than the number of times the pattern is matched, the expression  ©words -1 is equivalent to the number of matches; its value is added to  $matchcount.</p>
        <p id="p_05301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05301"> Line 16 checks whether the &lt;&gt; operator has reached the end of the current input file. If it has, line 17 resets the current line number to 0. This ensures that the next pass through the loop will set the current line number to 1 (to indicate that the program is on the first line of the next file). Line 18 sets the filename to the next file mentioned on the command line, which is currently stored in  $ARGV [ 0 ].</p>
        <p id="p_05302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05302"> Lines 21-25 either print the total number of matches or indicate that no matches were found.</p>
        <p id="p_05303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05303"> NOTE</p>
        <p id="p_05304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05304"> Make sure that you remember to include the enclosing / characters when you use a scalar-variable name in a pattern. The Perl interpreter does not complain when it sees the following, for example, but the result might not be what you want:</p>
        <p id="p_05305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05305"> @words = split($pattern, $line);</p>
        <p id="p_05306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05306"> Excluding Alternatives</p>
        <p id="p_05307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05307"> As you have seen, when the special characters [ ] appear in a pattern, they specify a set of alternatives to choose from. For example, the pattern</p>
        <p id="p_05308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05308"> /d[eE]f/</p>
        <p id="p_05309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05309"> matches  def  or  dEf.</p>
        <p id="p_05310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05310"> When the  A  character appears as the first character after the [, it indicates that the pattern is to match any character  except  the ones displayed between the [ and ]. For example, the pattern</p>
        <p id="p_05311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05311"> /d[ A eE]f/</p>
        <p id="p_05312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05312"> matches any pattern that satisfies the following criteria:</p>
        <p id="p_05313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05313"> • The first character is  d.</p>
        <p id="p_05314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05314"> • The second character is anything other than  e  or  E.</p>
        <p id="p_05315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05315"> • The last character is f.</p>
        <p id="p_05316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05316"> NOTE</p>
        <p id="p_05317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05317"> To include a  A  character in a set of alternatives, precede it with a backslash, as follows:</p>
        <p id="p_05318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05318"> /d[\ A eE]f/</p>
        <p id="p_05319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05319"> This pattern matches  d A f,  def,  or  dEf.</p>
        <p id="p_05320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05320"> Character-Range Escape Sequences</p>
        <p id="p_05321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05321"> In the section titled "Matching Any Letter or Number" earlier in this chapter, you learned that you can represent consecutive letters or numbers inside the [ ] special characters by specifying ranges. For example, in the pattern</p>
        <p id="p_05322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05322"> /a[ 1-3 ]c/</p>
        <p id="p_05323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05323"> the [  1-3  ] matches any of 1, 2, or 3.</p>
        <p id="p_05324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05324"> Some ranges occur frequently enough that Perl defines special escape sequences for them. For example, instead of writing</p>
        <p id="p_05325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05325"> / [0-9] /</p>
        <p id="p_05326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05326"> to indicate that any digit is to be matched, you can write</p>
        <p id="p_05327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05327"> / \d/</p>
        <p id="p_05328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05328"> The  \d  escape sequence means "any digit."</p>
        <p id="p_05329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05329"> Table 7.2 lists the character-range escape sequences, what they match, and their equivalent character ranges.</p>
        <p id="p_05330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05330"> Table 7.2. Character-range escape sequences.</p>
        <p id="p_05331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05331"> These escape sequences can be used anywhere ordinary characters are used. For example, the following pattern matches any digit or lowercase letter:</p>
        <p id="p_05332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05332"> /[\da-z] /</p>
        <p id="p_05333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05333"> NOTE</p>
        <p id="p_05334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05334"> The definition of word boundary as used by the  \b  and  \B special characters corresponds to the definition of word character used by  \w  and  \W.</p>
        <p id="p_05335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05335"> If the pattern /  \w\W/  matches a particular pair of characters, the first character is part of a word and the second is not; this means that the first character is the end of a word, and that a word boundary exists between the first and second characters matched by the pattern.</p>
        <p id="p_05336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05336"> Similarly, if /  \W\w/  matches a pair of characters, the first character is not part of a word and the second character is. This means that the second character is the beginning of a word. Again, a word boundary exists between the first and second characters matched by the pattern.</p>
        <p id="p_05337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05337"> Matching Any Character</p>
        <p id="p_05338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05338"> Another special character supported in patterns is the period (.) character, which matches any character except the newline character. For example, the following pattern matches d, followed by any non-newline character, followed by f:</p>
        <p id="p_05339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05339"> /d.f/</p>
        <p id="p_05340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05340"> The . character is often used in conjunction with the * character. For example, the following pattern matches any string that contains the character  d  preceding the character  f :</p>
        <p id="p_05341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05341"> /d. *f /</p>
        <p id="p_05342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05342"> Normally, the . * special-character combination tries to match as much as possible. For example, if the string banana is searched using the following pattern, the pattern matches banana, not ba or bana:</p>
        <p id="p_05343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05343"> /b. *a/</p>
        <p id="p_05344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05344"> NOTE</p>
        <p id="p_05345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05345"> There is one exception to the preceding rule: The . * character only matches the longest possible string that enables the pattern match as a whole to succeed.</p>
        <p id="p_05346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05346"> For example, suppose the string  Mississippi  is searched using the pattern</p>
        <p id="p_05347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05347"> /M.*i.*pi/</p>
        <p id="p_05348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05348"> Here, the first .* in  /M.*i.*pi/  matches Mississippi</p>
        <p id="p_05349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05349"> If it tried to go further and match</p>
        <p id="p_05350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05350"> Mississippi</p>
        <p id="p_05351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05351"> or even</p>
        <p id="p_05352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05352"> Mississippi</p>
        <p id="p_05353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05353"> there would be nothing left for the rest of the pattern to match. When the first . * match is limited to</p>
        <p id="p_05354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05354"> Mississippi</p>
        <p id="p_05355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05355"> the rest of the pattern,  i . *pi,  matches  ippi,  and the pattern as a whole succeeds.</p>
        <p id="p_05356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05356"> Matching a Specified Number of Occurrences</p>
        <p id="p_05357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05357"> Several special characters in patterns that you have seen enable you to match a specified number of occurrences of a character. For example, + matches one or more occurrences of a character, and ? matches zero or one occurrences.</p>
        <p id="p_05358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05358"> Perl enables you to define how many occurrences of a character constitute a match. To do this, use the special characters { and }.</p>
        <p id="p_05359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05359"> For example, the pattern</p>
        <p id="p_05360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05360"> / de  {1,3   } f /</p>
        <p id="p_05361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05361"> matches d, followed by one, two, or three occurrences of e, followed by f. This means that def, deef, and deeef match, but df and deeeef do not.</p>
        <p id="p_05362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05362"> To specify an exact number of occurrences, include only one value between the { and the }.</p>
        <p id="p_05363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05363"> /de{3}f/</p>
        <p id="p_05364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05364"> This specifies exactly three occurrences of e, which means this pattern only matches deeef.</p>
        <p id="p_05365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05365"> To specify a minimum number of occurrences, leave off the upper bound.</p>
        <p id="p_05366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05366"> /de{3,   }f/</p>
        <p id="p_05367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05367"> This matches d, followed by at least three es, followed by f.</p>
        <p id="p_05368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05368"> Finally, to specify a maximum number of occurrences, use 0 as the lower bound.</p>
        <p id="p_05369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05369"> /de{0,3 } f /</p>
        <p id="p_05370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05370"> This matches d, followed by no more than three es, followed by f.</p>
        <p id="p_05371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05371"> NOTE</p>
        <p id="p_05372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05372"> You can use { and } with character ranges or any other special character, as follows:</p>
        <p id="p_05373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05373"> /[a-z]{1,3}/</p>
        <p id="p_05374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05374"> This matches one, two, or three lowercase letters.</p>
        <p id="p_05375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05375"> /•  {3}/</p>
        <p id="p_05376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05376"> This matches any three characters.</p>
        <p id="p_05377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05377"> Specifying Choices</p>
        <p id="p_05378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05378"> The special character | enables you to specify two or more alternatives to choose from when matching a pattern. For example, the pattern</p>
        <p id="p_05379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05379"> /def|ghi/</p>
        <p id="p_05380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05380"> matches either  def  or  ghi.  The pattern</p>
        <p id="p_05381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05381"> /[a-z]+|[0-9]+/</p>
        <p id="p_05382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05382"> matches one or more lowercase letters or one or more digits.</p>
        <p id="p_05383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05383"> Listing 7.8 is a simple example of a program that uses the | special character. It reads a number and checks whether it is a legitimate Perl integer.</p>
        <p id="p_05384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05384"> Listing 7.8. A simple integer-validation program.</p>
        <p id="p_05385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05385"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05386"> 2 :</p>
        <p id="p_05387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05387"> 3 :</p>
        <p id="p_05388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05388"> print ("Enter a number:\n");</p>
        <p id="p_05389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05389"> 4: $number = &lt;STDIN&gt;;</p>
        <p id="p_05390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05390"> 5: chop ($number);</p>
        <p id="p_05391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05391"> 6: if ($number =~  / A -?\d+$| A -?0[xX][\da-fa-F]+$/)  {</p>
        <p id="p_05392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05392"> 7:  print ("$number is a legal  integer.\n");</p>
        <p id="p_05393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05393"> 8:  } else {</p>
        <p id="p_05394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05394"> 9: print ("$number is not a legal  integer.\n");</p>
        <p id="p_05395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05395"> 10 : }</p>
        <p id="p_05396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05396"> OuTPUt</p>
        <p id="p_05397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05397"> $ program7_8 Enter a number:</p>
        <p id="p_05398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05398"> 0x3ff1</p>
        <p id="p_05399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05399"> 0x3ffl  is a legal integer.</p>
        <p id="p_05400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05400"> $</p>
        <p id="p_05401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05401"> Analysis</p>
        <p id="p_05402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05402"> Recall that Perl integers can be in any of three forms:</p>
        <p id="p_05403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05403"> • Standard base-10 notation, as in 12 3</p>
        <p id="p_05404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05404"> • Base-8 (octal) notation, indicated by a leading 0, as in 012 3</p>
        <p id="p_05405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05405"> • Base-16 (hexadecimal) notation, indicated by a leading  Ox  or  OX,  as in  0X1 f f</p>
        <p id="p_05406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05406"> Line 6 checks whether a number is a legal Perl integer. The first alternative in the pattern,</p>
        <p id="p_05407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05407"> A -?\d+$</p>
        <p id="p_05408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05408"> matches a string consisting of one or more digits, optionally preceded by a (The  A  and $ characters</p>
        <p id="p_05409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05409"> ensure that this is the only string that matches.) This takes care of integers in standard base-10 notation and integers in octal notation.</p>
        <p id="p_05410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05410"> The second alternative in the pattern,</p>
        <p id="p_05411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05411"> A -?0[xX][\da-fa-F]+$</p>
        <p id="p_05412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05412"> matches integers in hexadecimal notation. Take a look at this pattern one piece at a time:</p>
        <p id="p_05413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05413"> • The  A  matches the beginning of the line. This ensures that lines containing leading spaces or extraneous characters are not treated as valid hexadecimal integers.</p>
        <p id="p_05414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05414"> • The - ? matches a - if it is present. This ensures that negative numbers are matched.</p>
        <p id="p_05415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05415"> • The 0 matches the leading 0.</p>
        <p id="p_05416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05416"> • The [  xX  ] matches the  x  or  X  that follows the leading  0 .</p>
        <p id="p_05417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05417"> • The [  \da-f a-F  ] matches any digit, any letter between  a  and  f , or any letter between  A  and F.  Recall that these are precisely the characters which are allowed to appear in hexadecimal digits.</p>
        <p id="p_05418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05418"> • The + indicates that the pattern is to match one or more hexadecimal digits.</p>
        <p id="p_05419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05419"> • The closing $ indicates that the pattern is to match only if there are no extraneous characters following the hexadecimal integer.</p>
        <p id="p_05420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05420"> Warning</p>
        <p id="p_05421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05421"> Beware that the following pattern matches either  x  or  one or more of  y,  not one or more of  x  or  y:</p>
        <p id="p_05422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05422"> /x| y+/</p>
        <p id="p_05423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05423"> See the section called "Special-Character Precedence" later today for details on how to specify special-character precedence in patterns.</p>
        <p id="p_05424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05424"> Reusing Portions of Patterns</p>
        <p id="p_05425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05425"> Suppose that you want to write a pattern that matches the following:</p>
        <p id="p_05426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05426"> • One or more digits or lowercase letters</p>
        <p id="p_05427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05427"> • Followed by a colon or semicolon</p>
        <p id="p_05428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05428"> • Followed by another group of one or more digits or lowercase letters</p>
        <p id="p_05429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05429"> • Another colon or semicolon</p>
        <p id="p_05430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05430"> • Yet another group of one or more digits or lowercase letters One way to indicate this pattern is as follows:</p>
        <p id="p_05431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05431"> /[\da—z] + [:;] [\da— z] + [:;] [\da-z]+/</p>
        <p id="p_05432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05432"> This pattern is somewhat complicated and is quite repetitive.</p>
        <p id="p_05433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05433"> Perl provides an easier way to specify patterns that contain multiple repetitions of a particular sequence. When you enclose a portion of a pattern in parentheses, as in</p>
        <p id="p_05434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05434"> ([\da-z]+)</p>
        <p id="p_05435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05435"> Perl stores the matched sequence in memory. To retrieve a sequence from memory, use the special character \n, where n is an integer representing the nth pattern stored in memory.</p>
        <p id="p_05436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05436"> For example, the aforementioned pattern can be written as</p>
        <p id="p_05437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05437"> /([\da-z]+])[:;]\1[:;]\1/</p>
        <p id="p_05438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05438"> Here, the pattern matched by [  \da-z  ] + is stored in memory. When the Perl interpreter sees the escape sequence \ 1, it matches the matched pattern.</p>
        <p id="p_05439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05439"> You also can store the sequence [ :; ] in memory, and write this pattern as follows:</p>
        <p id="p_05440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05440"> /([\da-z]+)([:;])\1\2\1/</p>
        <p id="p_05441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05441"> Pattern sequences are stored in memory from left to right, so \ 1 represents the subpattern matched by [ \da-z  ] + and  \2  represents the subpattern matched by [ :; ].</p>
        <p id="p_05442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05442"> Pattern-sequence memory is often used when you want to match the same character in more than one place but don't care which character you match. For example, if you are looking for a date in  dcl-mm-yy  format, you might want to match</p>
        <p id="p_05443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05443"> /\d{2}([\W])\d{2}\l\d{2}/</p>
        <p id="p_05444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05444"> This matches two digits, a non-word character, two more digits, the same non-word character, and two more digits. This means that the following strings all match:</p>
        <p id="p_05445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05445"> 12-05-92 26.11.87 07 04 92</p>
        <p id="p_05446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05446"> However, the following string does not match:</p>
        <p id="p_05447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05447"> 21-05.91</p>
        <p id="p_05448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05448"> This is because the pattern is looking for a</p>
        <p id="p_05449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05449"> between the  0 5  and the  91,  not a period.</p>
        <p id="p_05450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05450"> VS?,</p>
        <p id="p_05451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05451"> Warning</p>
        <p id="p_05452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05452"> Beware that the pattern</p>
        <p id="p_05453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05453"> /\d{2}([\W])\d{2}\l\d{2}/</p>
        <p id="p_05454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05454"> is not the same as the pattern</p>
        <p id="p_05455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05455"> /(\d{2})([\W])\1\2\1/</p>
        <p id="p_05456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05456"> In the first pattern, any digit can appear anywhere. The second pattern matches any two digits as the first two characters, but then only matches the same two digits again. This means that</p>
        <p id="p_05457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05457"> 17-17-17</p>
        <p id="p_05458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05458"> matches, but the following does not:</p>
        <p id="p_05459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05459"> 17-05-91</p>
        <p id="p_05460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05460"> Pattern-Sequence Scalar Variables</p>
        <p id="p_05461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05461"> Note that pattern-sequence memory is preserved only for the length of the pattern. This means that if</p>
        <p id="p_05462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05462"> you define the following pattern (which, incidentally, matches any floating-point number that does not contain an exponent):</p>
        <p id="p_05463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05463"> /-?(\d+)\.?(\d+)/</p>
        <p id="p_05464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05464"> you cannot then define another pattern, such as the following:</p>
        <p id="p_05465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05465"> /\ 1 /</p>
        <p id="p_05466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05466"> and expect the Perl interpreter to remember that \  1  refers to the first \  d+  (the digits before the decimal point).</p>
        <p id="p_05467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05467"> To get around this problem, Perl defines special built-in variables that remember the value of patterns matched in parentheses. These special variables are named $n, where n is the nth set of parentheses in the pattern.</p>
        <p id="p_05468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05468"> For example, consider the following:</p>
        <p id="p_05469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05469"> $string = "This string contains the number 25.11.";</p>
        <p id="p_05470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05470"> $string =~  /-?(\d+)\.?(\d+)/;</p>
        <p id="p_05471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05471"> $integerpart = $1;</p>
        <p id="p_05472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05472"> $decimalpart = $2;</p>
        <p id="p_05473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05473"> In this case, the pattern</p>
        <p id="p_05474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05474"> /-?(\d+)\.?(\d+)/</p>
        <p id="p_05475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05475"> matches  2 5.11,  and the subpattern in the first set of parentheses matches  2 5 . This means that  2 5  is stored in $  1  and is later assigned to  $integerpart.  Similarly, the second set of parentheses matches 11, which is stored in  $2  and later assigned to  $decimalpart.</p>
        <p id="p_05476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05476"> Warning</p>
        <p id="p_05477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05477"> The values stored in $ 1, $ 2, and so on, are destroyed when another pattern match is performed. If you need these values, be sure to assign them to other scalar variables.</p>
        <p id="p_05478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05478"> There is also one other built-in scalar variable, $ &amp;, which contains the entire matched pattern, as follows:</p>
        <p id="p_05479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05479"> $string = "This string contains the number 25.11."; $string =~  /-?(\d+)\.?(\d+)/;</p>
        <p id="p_05480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05480"> $number = $&amp;;</p>
        <p id="p_05481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05481"> Here, the pattern matched is  2 5.11,  which is stored in $ &amp; and then assigned to $  number.</p>
        <p id="p_05482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05482"> Special-Character Precedence</p>
        <p id="p_05483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05483"> Perl defines rules of precedence to determine the order in which special characters in patterns are interpreted. For example, the pattern</p>
        <p id="p_05484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05484"> /x| y+/</p>
        <p id="p_05485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05485"> matches either  x  or one or more occurrences of  y,  because + has higher precedence than | and is therefore interpreted first.</p>
        <p id="p_05486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05486"> Table 7.3 lists the special characters that can appear in patterns in order of precedence (highest to lowest). Special characters with higher precedence are always interpreted before those of lower precedence.</p>
        <p id="p_05487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05487"> Table 7.3. The precedence of pattern-matching special characters.</p>
        <p id="p_05488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05488"> Because the pattern-memory special characters () have the highest precedence, you can use them to</p>
        <p id="p_05489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05489"> force other special characters to be evaluated first. For example, the pattern</p>
        <p id="p_05490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05490"> (ab|cd)+</p>
        <p id="p_05491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05491"> matches one or more occurrences of either  ab  or  cd.  This matches, for example,  abcdab.</p>
        <p id="p_05492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05492"> Warning</p>
        <p id="p_05493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05493"> Remember that when you use parentheses to force the order of precedence, you also are storing into pattern memory. For example, in the sequence</p>
        <p id="p_05494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05494"> /(ab|cd)+(.)(ef|gh)+\1/</p>
        <p id="p_05495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05495"> the \  1  refers to what  ab  |  cd  matched, not to what the . special character matched.</p>
        <p id="p_05496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05496"> Now that you know all of the special-pattern characters and their precedence, look at a program that does more complex pattern matching. Listing 7.9 uses the various special-pattern characters, including the parentheses, to check whether a given input string is a valid twentieth-century date.</p>
        <p id="p_05497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05497"> Type</p>
        <p id="p_05498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05498"> Listing 7.9. A date-validation program.</p>
        <p id="p_05499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05499"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05500"> 2 :</p>
        <p id="p_05501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05501"> 3: print ("Enter a date in the format YYYY-MM-DD:\n");</p>
        <p id="p_05502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05502"> 4: $date = &lt;STDIN&gt;;</p>
        <p id="p_05503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05503"> 5: chop ($date);</p>
        <p id="p_05504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05504"> 6 :</p>
        <p id="p_05505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05505"> 7: # Because this pattern is complicated, we split it</p>
        <p id="p_05506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05506"> 8: # into parts, assign the parts to scalar variables,</p>
        <p id="p_05507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05507"> 9: # then substitute them in later.</p>
        <p id="p_05508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05508"> 10  :</p>
        <p id="p_05509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05509"> 11: # handle 31-day months</p>
        <p id="p_05510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05510"> 12:  $mdl  = "(0[13578]|1[02])\\2(0[1-9]|[12]\\d|3[01])";</p>
        <p id="p_05511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05511"> 13: # handle 30-day months</p>
        <p id="p_05512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05512"> 14: $md2 =  "(0[469]|11)\\2(0[1-9]|[12]\\d|30)";</p>
        <p id="p_05513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05513"> 15: # handle February, without worrying about whether  it's 16: # supposed to be a leap year or not 17: $md3 =  "02\\2(0[1-9]|[12]\\d)";</p>
        <p id="p_05514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05514"> 18 :</p>
        <p id="p_05515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05515"> 19: # check for a twentieth-century date</p>
        <p id="p_05516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05516"> 20: $match = $date =~  / A (19)?\d\d(.)($mdl|$md2|$md3)$/;</p>
        <p id="p_05517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05517"> 21: # check for a valid but non-20th century date</p>
        <p id="p_05518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05518"> 22: $olddate = $date =~  / A (\d{l,4})(.)($mdl|$md2|$md3)$/;</p>
        <p id="p_05519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05519"> 23:   if  ($match) {</p>
        <p id="p_05520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05520"> 24: print ("$date is a valid  date\n");</p>
        <p id="p_05521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05521"> 25:  } elsif ($olddate) {</p>
        <p id="p_05522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05522"> 26: print ("$date is not in the 20th century\n");</p>
        <p id="p_05523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05523"> 27: } else {</p>
        <p id="p_05524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05524"> 28: print ("$date is not a valid  date\n");</p>
        <p id="p_05525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05525"> 29: }</p>
        <p id="p_05526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05526"> Output</p>
        <p id="p_05527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05527"> $ program7_9</p>
        <p id="p_05528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05528"> Enter a date in the format YYYY-MM-DD: 1991-04-31</p>
        <p id="p_05529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05529"> 1991-04-31 is not a valid date $</p>
        <p id="p_05530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05530"> Analysis</p>
        <p id="p_05531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05531"> Don't worry: this program is a lot less complicated than it looks! Basically, this program does the following:</p>
        <p id="p_05532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05532"> 1. It checks whether the date is in the format  YYYY-MM-DD.  (It allows  YY-MM-DD,  and also enables you to use a character other than a hyphen to separate the year, month, and date.)</p>
        <p id="p_05533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05533"> 2. It checks whether the year is in the twentieth century or not.</p>
        <p id="p_05534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05534"> 3. It checks whether the month is between 01 and 12.</p>
        <p id="p_05535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05535"> 4. Finally, it checks whether the date field is a legal date for that month. Legal date fields are between  01  and either  2 9, 3 0,  or  31,  depending on the number of days in that month.</p>
        <p id="p_05536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05536"> If the date is legal, the program tells you so. If the date is not a twentieth-century date but is legal, the program informs you of this also.</p>
        <p id="p_05537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05537"> Because the pattern to be matched is too long to fit on one line, this program breaks it into pieces and assigns the pieces to scalar variables. This is possible because scalar-variable substitution is supported in patterns.</p>
        <p id="p_05538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05538"> Line 12 is the pattern to match for months with 31 days. Note that the escape sequences (such as  \d) are preceded by another backslash (producing \  \d).  This is because the program actually wants to store a backslash in the scalar variable. (Recall that backslashes in double-quoted strings are treated as escape sequences.) The pattern</p>
        <p id="p_05539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05539"> (0[13578]|1[02])\2(0[1-9]I[12]\d|3[01])</p>
        <p id="p_05540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05540"> which is assigned to  $mdl,  consists of the following components:</p>
        <p id="p_05541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05541"> • The sequence  (0 [13578]  |  1  [02]  ), which matches the month values  01, 03, 05, 07, 08, 10, and 12 (the 31-day months)</p>
        <p id="p_05542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05542"> • \ 2, which matches the character that separates the day, month, and year</p>
        <p id="p_05543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05543"> • The sequence ( 0  [  1  —  9  ] |  [12]\d|3[01]),  which matches any two-digit number between</p>
        <p id="p_05544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05544"> 01 and 31</p>
        <p id="p_05545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05545"> Note that \  2  matches the separator character because the separator character will eventually be the second pattern sequence stored in memory (when the pattern is finally assembled).</p>
        <p id="p_05546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05546"> Line 14 is similar to line 12 and handles 30-day months. The only differences between this subpattern and the one in line 12 are as follows:</p>
        <p id="p_05547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05547"> • The month values accepted are  0 4, 0 6, 0 9,  and 11.</p>
        <p id="p_05548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05548"> • The valid date fields are  01  through  3 0 , not  01  through  31 .</p>
        <p id="p_05549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05549"> Line 17 is another similar pattern that checks whether the month is 02 (February) and the date field is between  01  and  2 9.</p>
        <p id="p_05550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05550"> Line 20 does the actual pattern match that checks whether the date is a valid twentieth-century date. This pattern is divided into three parts.</p>
        <p id="p_05551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05551"> •  A (19)?\d\d,  which matches any two-digit number at the beginning of a line, or any four¬ digit number starting with 1 9</p>
        <p id="p_05552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05552"> • The separator character, which is the second item in parentheses-the second item stored in memory-and thus can be retrieved using \ 2</p>
        <p id="p_05553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05553"> •  ($mdl | $md2 | $md3)  $, which matches any of the valid month-day combinations defined in lines 12, 14, and 17, provided it appears at the end of the line</p>
        <p id="p_05554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05554"> The result of the pattern match, either true or false, is stored in the scalar variable  $match.</p>
        <p id="p_05555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05555"> Line 22 checks whether the date is a valid date in any century. The only difference between this pattern and the one in line 20 is that the year can be any one-to-four-digit number. The result of the pattern match is stored in  $olddate.</p>
        <p id="p_05556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05556"> Lines 23-29 check whether either  $match  or  $olddate  is true and print the appropriate message.</p>
        <p id="p_05557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05557"> As you can see, the pattern-matching facility in Perl is quite powerful. This program is less than 30 lines long, including comments; the equivalent program in almost any other programming language would be substantially longer and much more difficult to write.</p>
        <p id="p_05558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05558"> Specifying a Different Pattern Delimiter</p>
        <p id="p_05559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05559"> So far, all the patterns you have seen have been enclosed by / characters.</p>
        <p id="p_05560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05560"> /de*f/</p>
        <p id="p_05561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05561"> These / characters are known as  pattern delimiters.</p>
        <p id="p_05562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05562"> Because / is the pattern-delimiter character, you must use \ / to include a / character in a pattern. This can become awkward if you are searching for a directory such as, for example,</p>
        <p id="p_05563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05563"> /u/jqpublic/perl/progl.</p>
        <p id="p_05564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05564"> /\/u\/jqpublic\/perl\/progl/</p>
        <p id="p_05565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05565"> To make it easier to write patterns that include / characters, Perl enables you to use any pattern-delimiter character you like. The following pattern also matches the directory</p>
        <p id="p_05566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05566"> /u/jqpublic/perl/progl:</p>
        <p id="p_05567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05567"> m!/u/jqpublic/perl/progl!</p>
        <p id="p_05568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05568"> Here, the m indicates the pattern-matching operation. If you are using a pattern delimiter other than /, you must include the m.</p>
        <p id="p_05569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05569"> Warning</p>
        <p id="p_05570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05570"> There are two things you should watch out for when you use other pattern delimiters.</p>
        <p id="p_05571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05571"> First, if you use the ' character as a pattern delimiter, the Perl interpreter does not substitute for scalar-variable names.</p>
        <p id="p_05572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05572"> m'$var'</p>
        <p id="p_05573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05573"> This matches the string  $var,  not the current value of the scalar variable  $var.</p>
        <p id="p_05574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05574"> Second, if you use a pattern delimiter that is normally a special-pattern character, you will not be able to use that special character in your pattern. For example, if you want to match the pattern  ab?c  (which matches  a,  optionally followed by  b, followed by  c)  you cannot use the ? character as a pattern delimiter. The pattern</p>
        <p id="p_05575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05575"> m?ab?c?</p>
        <p id="p_05576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05576"> produces a syntax error, because the Perl interpreter assumes that</p>
        <p id="p_05577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05577"> the ? after the  b  is a pattern delimiter. You can still use</p>
        <p id="p_05578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05578"> m?ab\?c?</p>
        <p id="p_05579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05579"> but this pattern won't match what you want. Because the ? inside the pattern is escaped, the Perl interpreter assumes that you want to match the actual ? character, and the pattern matches the sequence  ab?c.</p>
        <p id="p_05580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05580"> Pattern-Matching Options</p>
        <p id="p_05581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05581"> When you specify a pattern, you also can supply options that control how the pattern is to be matched. Table 7.4 lists these pattern-matching options.</p>
        <p id="p_05582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05582"> Table 7.4. Pattern-matching options.</p>
        <p id="p_05583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05583"> All pattern options are included immediately after the pattern. For example, the following pattern uses the  i  option to ignore case:</p>
        <p id="p_05584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05584"> /ab*c/i</p>
        <p id="p_05585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05585"> You can specify as many of the options as you like, and the options can be in any order.</p>
        <p id="p_05586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05586"> Matching All Possible Patterns</p>
        <p id="p_05587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05587"> The  g  operator tells the Perl interpreter to match all the possible patterns in a string. For example, if you search the string  balata  using the pattern</p>
        <p id="p_05588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05588"> / . a/g</p>
        <p id="p_05589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05589"> which matches any character followed by  a,  the pattern matches  ba, la,  and  ta.</p>
        <p id="p_05590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05590"> If a pattern with the  g  option specified appears as an assignment to an array variable, the array variable is assigned a list consisting of all the patterns matched. For example,</p>
        <p id="p_05591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05591"> ©matches = "balata" =~ /.a/g;</p>
        <p id="p_05592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05592"> assigns the following list to  ©matches:</p>
        <p id="p_05593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05593"> ("ba", "la", "ta")</p>
        <p id="p_05594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05594"> Now, consider the following statement:</p>
        <p id="p_05595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05595"> $match = "balata" =~ /.a/g;</p>
        <p id="p_05596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05596"> The first time this statement is executed,  $match  is assigned the first pattern matched, which in this case is  ba.  If this assignment is performed again,  $match  is assigned the second pattern matched in the string, which is la, and so on until the pattern runs out of matches.</p>
        <p id="p_05597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05597"> This means that you can use patterns with the  g  option in loops. Listing 7.10 shows how this works.</p>
        <p id="p_05598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05598"> Type</p>
        <p id="p_05599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05599"> Listing 7.10. A program that loops using a pattern.</p>
        <p id="p_05600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05600"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05601"> 2 :</p>
        <p id="p_05602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05602"> 3: while ("balata" =~ /.a/g) {</p>
        <p id="p_05603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05603"> 4: $match = $&amp;;</p>
        <p id="p_05604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05604"> 5 :</p>
        <p id="p_05605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05605"> print ("$match\n");</p>
        <p id="p_05606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05606"> 6: }</p>
        <p id="p_05607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05607"> Output</p>
        <p id="p_05608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05608"> $ program7_10 ba la ta $</p>
        <p id="p_05609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05609"> Analysis</p>
        <p id="p_05610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05610"> The first time through the loop, $  match  has the value of the first pattern matched, which is  ba.  (The system variable $ &amp; always contains the last pattern matched; this pattern is assigned to $match  in line 4.) When the loop is executed for a second time,  $match  has the value  la.  The third time through,  $match  has the value  ta.  After this, the loop terminates; because the pattern doesn't match anything else, the conditional expression is now false.</p>
        <p id="p_05611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05611"> Determining the Match Location</p>
        <p id="p_05612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05612"> If you need to know how much of a string has been searched by the pattern matcher when the  g operator is specified, use the  pos  function.</p>
        <p id="p_05613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05613"> $offset = pos($string);</p>
        <p id="p_05614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05614"> This returns the position at which the next pattern match will be started.</p>
        <p id="p_05615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05615"> You can reposition the pattern matcher by putting  pos  () on the left side of an assignment.</p>
        <p id="p_05616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05616"> pos($string) = $newoffset;</p>
        <p id="p_05617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05617"> This tells the Perl interpreter to start the next pattern match at the position specified by</p>
        <p id="p_05618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05618"> $newoffset.</p>
        <p id="p_05619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05619"> Warning</p>
        <p id="p_05620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05620"> If you change the string being searched, the match position is reset to the beginning of the string.</p>
        <p id="p_05621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05621"> NOTE</p>
        <p id="p_05622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05622"> The  pos  function is not available in Perl version 4.</p>
        <p id="p_05623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05623"> Ignoring Case</p>
        <p id="p_05624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05624"> The  i  option enables you to specify that a matched letter can either be uppercase or lowercase. For example, the following pattern matches  de, dE, De,  or  DE:</p>
        <p id="p_05625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05625"> /de/i</p>
        <p id="p_05626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05626"> Patterns that match either uppercase or lowercase letters are said to be  case-insensitive.</p>
        <p id="p_05627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05627"> Treating the String as Multiple Lines</p>
        <p id="p_05628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05628"> The m option tells the Perl interpreter that the string to be matched contains multiple lines of text. When the m option is specified, the  A  special character matches either the start of the string or the start of any new line. For example, the pattern</p>
        <p id="p_05629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05629"> /  A The/m</p>
        <p id="p_05630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05630"> matches the word  The  in</p>
        <p id="p_05631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05631"> This pattern matches\nThe first word on the second line</p>
        <p id="p_05632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05632"> The m option also specifies that the $ special character is to match the end of any line. This means that the pattern</p>
        <p id="p_05633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05633"> /line.$/m</p>
        <p id="p_05634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05634"> is matched in the following string:</p>
        <p id="p_05635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05635"> This is the end of the first line.\nHere's another line.</p>
        <p id="p_05636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05636"> NOTE</p>
        <p id="p_05637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05637"> The m option is defined only in Perl 5. To treat a string as multiple lines when you run Perl 4, set the $ * system variable, described on  Day  17 , "System Variables."</p>
        <p id="p_05638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05638"> Evaluating a Pattern Only Once</p>
        <p id="p_05639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05639"> The  o  option enables you to tell the Perl interpreter that a pattern is to be evaluated only once. For example, consider the following:</p>
        <p id="p_05640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05640"> $var = 1;</p>
        <p id="p_05641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05641"> $line = &lt;STDIN&gt;; while ($var &lt; 10) {</p>
        <p id="p_05642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05642"> $result = $line =~ /$var/o;</p>
        <p id="p_05643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05643"> $line = &lt;STDIN&gt;;</p>
        <p id="p_05644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05644"> $var++;</p>
        <p id="p_05645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05645"> }</p>
        <p id="p_05646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05646"> The first time the Perl interpreter sees the pattern /  $var  /, it replaces the name  $var  with the current value of  $var,  which is 1; this means that the pattern to be matched is /1 / .</p>
        <p id="p_05647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05647"> Because the  o  option is specified, the pattern to be matched remains /1 / even when the value of $var  changes. If the  o  option had not been specified, the pattern would have been /  2  / the next time through the loop.</p>
        <p id="p_05648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05648"> TIP</p>
        <p id="p_05649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05649"> There's no real reason to use the  o  option for patterns unless you are keen on efficiency. Here's an easier way to do the same thing:</p>
        <p id="p_05650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05650"> $var = &lt;STDIN&gt;;</p>
        <p id="p_05651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05651"> $matchval = $var;</p>
        <p id="p_05652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05652"> $line = &lt;STDIN&gt;; while ($var &lt; 10) {</p>
        <p id="p_05653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05653"> $result = $line =~ /$matchval/;</p>
        <p id="p_05654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05654"> $line = &lt;STDIN&gt;;</p>
        <p id="p_05655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05655"> $var++;</p>
        <p id="p_05656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05656"> }</p>
        <p id="p_05657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05657"> The value of  $matchval  never changes, so the  o  option is not necessary.</p>
        <p id="p_05658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05658"> Treating the String as a Single Line</p>
        <p id="p_05659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05659"> The  s  option specifies that the string to be matched is to be treated as a single line of text. In this case, the . special character matches every character in a string, including the newline character. For example, the pattern /  a . *bc/s  is matched successfully in the following string:</p>
        <p id="p_05660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05660"> axxxxx \nxxxxbc</p>
        <p id="p_05661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05661"> If the  s  option is not specified, this pattern does not match, because the . character does not match the newline.</p>
        <p id="p_05662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05662"> NOTE</p>
        <p id="p_05663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05663"> The  s  option is defined only in Perl 5.</p>
        <p id="p_05664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05664"> Using White Space in Patterns</p>
        <p id="p_05665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05665"> One problem with patterns in Perl is that they can become difficult to follow. For example, consider this pattern, which you saw earlier:</p>
        <p id="p_05666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05666"> /\d{2}([\W])\d{2}\l\d{2}/</p>
        <p id="p_05667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05667"> Patterns such as this are difficult to follow, because there are a lot of backslashes, braces, and brackets</p>
        <p id="p_05668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05668"> to sort out.</p>
        <p id="p_05669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05669"> Perl 5 makes life a little easier by supplying the  x  option. This tells the Perl interpreter to ignore white space in a pattern unless it is preceded by a backslash. This means that the preceding pattern can be rewritten as the following, which is much easier to follow:</p>
        <p id="p_05670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05670"> /\d{2} ([\W]) \d{2} \1 \d{2}/x</p>
        <p id="p_05671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05671"> Here is an example of a pattern containing an actual blank space:</p>
        <p id="p_05672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05672"> /[A—Z] [a-z]+ \ [A-Z] [a-z]+ /x</p>
        <p id="p_05673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05673"> This matches a name in the standard first-name/last-name format (such as  John Smith).  Normally, you won't want to use the  x  option if you're actually trying to match white space, because you wind up with the backslash problem all over again.</p>
        <p id="p_05674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05674"> NOTE</p>
        <p id="p_05675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05675"> The x option is defined only in Perl 5.</p>
        <p id="p_05676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05676"> The Substitution Operator</p>
        <p id="p_05677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05677"> Perl enables you to replace part of a string using the substitution operator, which has the following syntax:</p>
        <p id="p_05678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05678"> s/pattern/replacement/</p>
        <p id="p_05679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05679"> The Perl interpreter searches for the pattern specified by the placeholder  pattern.  If it finds pattern,  it replaces it with the string represented by the placeholder  replacement.  For example:</p>
        <p id="p_05680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05680"> $string = "abcl23def";</p>
        <p id="p_05681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05681"> $string =~  s/123/456/;</p>
        <p id="p_05682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05682"> Here,  12 3  is replaced by  4 5 6,  which means that the value stored in  $stringis  now  abc4 5 6def. You can use any of the pattern special characters in the substitution operator. For example,</p>
        <p id="p_05683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05683"> s/[abc]+/0/</p>
        <p id="p_05684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05684"> searches for a sequence consisting of one or more occurrences of the letters  a, b,  and  c  (in any order) and replaces the sequence with 0.</p>
        <p id="p_05685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05685"> If you just want to delete a sequence of characters rather than replace it, leave out the replacement string as in the following example, which deletes the first occurrence of the pattern  abc:</p>
        <p id="p_05686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05686"> s/abc//</p>
        <p id="p_05687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05687"> Using Pattern-Sequence Variables in Substitutions</p>
        <p id="p_05688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05688"> You can use pattern-sequence variables to include a matched pattern in the replacement string. The following is an example:</p>
        <p id="p_05689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05689"> s/(\d+)/[$!]/</p>
        <p id="p_05690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05690"> This matches a sequence of one or more digits. Because this sequence is enclosed in parentheses, it is stored in the scalar variable $ 1. In the replacement string, [ $ 1 ], the scalar variable name $ 1 is replaced by its value, which is the matched pattern.</p>
        <p id="p_05691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05691"> NOTE</p>
        <p id="p_05692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05692"> Because the replacement string in the substitution operator is a string, not a pattern, the pattern special characters, such as [ ], *, and +, do not have a special meaning. For example, in the substitution</p>
        <p id="p_05693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05693"> s/abc/[def]/</p>
        <p id="p_05694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05694"> the replacement string is  [def  ] (including the square brackets).</p>
        <p id="p_05695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05695"> Options for the Substitution Operator</p>
        <p id="p_05696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05696"> The substitution operator supports several options, which are listed in Table 7.5.</p>
        <p id="p_05697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05697"> Table 7.5. Options for the substitution operator.</p>
        <p id="p_05698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05698"> As with pattern matching, options are appended to the end of the operator. For example, to change all occurrences of  abc  to  def,  use the following:</p>
        <p id="p_05699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05699"> s/abc/def/g</p>
        <p id="p_05700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05700"> Global Substitution</p>
        <p id="p_05701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05701"> The  g  option changes all occurrences of a pattern in a particular string. For example, the following substitution puts parentheses around any number in the string:</p>
        <p id="p_05702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05702"> s/(\d+)/($1 )/ g</p>
        <p id="p_05703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05703"> Listing 7.11 is an example of a program that uses global substitution. It examines each line of its input, removes all extraneous leading spaces and tabs, and replaces multiple spaces and tabs between words with a single space.</p>
        <p id="p_05704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05704"> Listing 7.11. A simple white space cleanup program.</p>
        <p id="p_05705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05705"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05706"> 2  :</p>
        <p id="p_05707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05707"> Output</p>
        <p id="p_05708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05708"> $ program7_ll</p>
        <p id="p_05709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05709"> This is a line of input.</p>
        <p id="p_05710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05710"> Here is another line.</p>
        <p id="p_05711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05711"> This is my last line of input.</p>
        <p id="p_05712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05712"> A D</p>
        <p id="p_05713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05713"> Formatted  text:</p>
        <p id="p_05714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05714"> This is a line of input.</p>
        <p id="p_05715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05715"> Here is another line.</p>
        <p id="p_05716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05716"> This is my last line of input.</p>
        <p id="p_05717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05717"> $</p>
        <p id="p_05718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05718"> Analysis</p>
        <p id="p_05719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05719"> This program performs three substitutions on each line of its input. The first substitution, in line 6, checks whether there are any spaces or tabs at the beginning of the line. If any exist, they are removed.</p>
        <p id="p_05720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05720"> Similarly, line 7 checks whether there are any spaces or tabs at the end of the line (before the trailing newline character). If any exist, they are removed. To do this, line 7 replaces the following pattern (one or more spaces and tabs, followed by a newline character, followed by the end of the line) with a newline character:</p>
        <p id="p_05721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05721"> /[ \t]+\n$/</p>
        <p id="p_05722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05722"> Line 8 uses a global substitution to remove extra spaces and tabs between words. The following pattern matches one or more spaces or tabs, in any order; these spaces and tabs are replaced by a single space:</p>
        <p id="p_05723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05723"> /[ \t]+/</p>
        <p id="p_05724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05724"> Ignoring Case</p>
        <p id="p_05725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05725"> The  i  option ignores case when substituting. For example, the following substitution replaces all occurrences of the words  no, No, NO,  and  nO  with  NO.  (Recall that the  \b  escape character specifies a word boundary.)</p>
        <p id="p_05726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05726"> s/\bno\b/NO/gi</p>
        <p id="p_05727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05727"> Replacement Using an Expression</p>
        <p id="p_05728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05728"> The  e  option treats the replacement string as an expression, which it evaluates before replacing. For example, consider the following:</p>
        <p id="p_05729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05729"> $string = "Oabcl";</p>
        <p id="p_05730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05730"> $string =~  s/[a-zA-Z]+/$&amp;  x  2/e</p>
        <p id="p_05731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05731"> The substitution shown here is a quick way to duplicate part of a string. Here's how it works:</p>
        <p id="p_05732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05732"> 1. The pattern /  [a-zA-Z  ] +/ matches  abc,  which is stored in the built-in variable $&amp;.</p>
        <p id="p_05733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05733"> 2. The  e  option indicates that the replacement string, $ &amp;  x 2,  is to be treated as an expression. This expression is evaluated, producing the result  abcabc.</p>
        <p id="p_05734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05734"> 3. abcabc  is substituted for  abc  in the string stored in $  string.  This means that the new value of  $string  is  Oabcabcl.</p>
        <p id="p_05735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05735"> Listing 7.12 is another example that uses the  e  option in a substitution. This program takes every integer in a list of input files and multiplies them by 2, leaving the rest of the contents unchanged. (For the sake of simplicity, the program assumes that there are no floating-point numbers in the file.)</p>
        <p id="p_05736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05736"> Type</p>
        <p id="p_05737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05737"> Listing 7.12. A program that multiplies every integer in a file by 2.</p>
        <p id="p_05738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05738"> 1: #!/usr/local/bin/perl</p>
        <p id="p_05739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05739"> 2 :</p>
        <p id="p_05740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05740"> 3: $count = 0;</p>
        <p id="p_05741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05741"> 4: while ($ARGV[$count] ne "") {</p>
        <p id="p_05742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05742"> 5: open (FILE, "$ARGV[$count]");</p>
        <p id="p_05743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05743"> 6: @file = &lt;FILE&gt;;</p>
        <p id="p_05744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05744"> 7: $linenum = 0;</p>
        <p id="p_05745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05745"> 8: while ($file[$linenum] ne "") {</p>
        <p id="p_05746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05746"> 9: $file[$linenum] =~ s/\d+/$&amp; * 2/eg;</p>
        <p id="p_05747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05747"> 10: $linenum++;</p>
        <p id="p_05748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05748"> 11 : }</p>
        <p id="p_05749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05749"> 12: close (FILE);</p>
        <p id="p_05750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05750"> 13: open (FILE, "&gt;$ARGV[$count]");</p>
        <p id="p_05751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05751"> 14: print FILE (@file);</p>
        <p id="p_05752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05752"> 15: close (FILE);</p>
        <p id="p_05753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05753"> 16:</p>
        <p id="p_05754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05754"> $count++;</p>
        <p id="p_05755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05755"> 17 : }</p>
        <p id="p_05756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05756"> Output</p>
        <p id="p_05757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05757"> If a file named foo contains the text</p>
        <p id="p_05758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05758"> and the name  foo  is passed as a command-line argument to this program, the file  foo becomes</p>
        <p id="p_05759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05759"> This program uses the built-in variable  0ARGV  to retrieve filenames from the command line. Note that the program cannot use &lt;&gt;, because the following statement reads the entire contents of all the files into a single array:</p>
        <p id="p_05760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05760"> @file = &lt;&gt;;</p>
        <p id="p_05761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05761"> Analyse</p>
        <p id="p_05762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05762"> Lines 8-11 read and substitute one line of a file at a time. Line 9 performs the actual substitution as follows:</p>
        <p id="p_05763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05763"> 1. The pattern  \d+  matches a sequence of one or more digits, which is automatically assigned to</p>
        <p id="p_05764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05764"> $ &amp; .</p>
        <p id="p_05765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05765"> 2. The value of $ &amp; is substituted into the replacement string.</p>
        <p id="p_05766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05766"> 3.  The  e  option indicates that this replacement string is to be treated as an expression. This expression multiplies the matched integer by 2.</p>
        <p id="p_05767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05767"> 4. The result of the multiplication is then substituted into the file in place of the original integer.</p>
        <p id="p_05768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05768"> 5. The  g  option indicates that every integer on the line is to be substituted for.</p>
        <p id="p_05769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05769"> After all the lines in the file have been read, the file is closed and reopened for writing. The call to</p>
        <p id="p_05770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05770"> print  in line 14 takes the list stored in  @f  ile-the contents of the current file-and writes them back out to the file, overwriting the original contents.</p>
        <p id="p_05771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05771"> Evaluating a Pattern Only Once</p>
        <p id="p_05772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05772"> As with the match operator, the  o  option to the substitution operator tells the Perl interpreter to replace a scalar variable name with its value only once. For example, the following statement substitutes the current value of  $var  for its name, producing a replacement string:</p>
        <p id="p_05773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05773"> $string =~ /abc/$var/o;</p>
        <p id="p_05774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05774"> This replacement string then never changes, even if the value of  $var  changes. For example:</p>
        <p id="p_05775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05775"> $var = 17;</p>
        <p id="p_05776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05776"> while ($var &gt; 0) {</p>
        <p id="p_05777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05777"> $ string = &lt;STDIN&gt;;</p>
        <p id="p_05778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05778"> $string =~ /abc/$var/o; print ($string);</p>
        <p id="p_05779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05779"> $var—; # the replacement string is still "17"</p>
        <p id="p_05780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05780"> }</p>
        <p id="p_05781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05781"> Again, as with the match operator, there is no real reason to use the  o  option.</p>
        <p id="p_05782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05782"> Treating the String as Single or Multiple Lines</p>
        <p id="p_05783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05783"> As in the pattern-matching operator, the  s  and  m  options specify that the string to be matched is to be treated as a single line or as multiple lines, respectively.</p>
        <p id="p_05784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05784"> The  s  option ensures that the newline character \  n  is matched by the . special character.</p>
        <p id="p_05785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05785"> $string = "This is a\ntwo-line string.";</p>
        <p id="p_05786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05786"> $string =~ s/a.*o/one/s;</p>
        <p id="p_05787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05787"> # $string now contains "This is a one-line string."</p>
        <p id="p_05788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05788"> If the m option is specified,  A  and $ match the beginning and end of any line.</p>
        <p id="p_05789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05789"> $string = "The The first line\nThe The second line"; $string =~ s/ A The//gm;</p>
        <p id="p_05790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05790"> # $string now contains "The first line\nThe second line" $string =~ s/e$/k/gm;</p>
        <p id="p_05791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05791"> # $string now contains "The first link\nThe second link"</p>
        <p id="p_05792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05792"> Warning</p>
        <p id="p_05793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05793"> The  \A  and  \Z  escape sequences (defined in Perl 5) always match only the beginning and end of the string, respectively.</p>
        <p id="p_05794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05794"> (This is the only case where \  A  and \  Z  behave differently from  A and $.)</p>
        <p id="p_05795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05795"> NOTE</p>
        <p id="p_05796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05796"> The  m  and  s  options are defined only in Perl 5. To treat a string as multiple lines when you run Perl 4, set the $ * system variable, described on  Day  17 .</p>
        <p id="p_05797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05797"> Using White Space in Patterns</p>
        <p id="p_05798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05798"> The  x  option tells the Perl interpreter to ignore all white space unless preceded by a backslash. As with the pattern-matching operator, ignoring white space makes complicated string patterns easier to read.</p>
        <p id="p_05799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05799"> $string =~  s/\d{2} ([\W]) \d{2} \1 \d{2}/$l-$2-$3/x</p>
        <p id="p_05800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05800"> This converts a day-month-year string to the  dd-mm-yy  format.</p>
        <p id="p_05801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05801"> NOTE</p>
        <p id="p_05802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05802"> Even if the  x  option is specified, spaces in the replacement string are not ignored. For example, the following replaces  14/04/95 with  14 - 04 - 95,  not  14-04-95:</p>
        <p id="p_05803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05803"> $ string =~  s/\d{2} ([\W]) \d{2} \1 \d{2}/$l -</p>
        <p id="p_05804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05804"> $2 - $3/x</p>
        <p id="p_05805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05805"> Also note that the  x  option is defined only in Perl 5.</p>
        <p id="p_05806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05806"> Specifying a Different Delimiter</p>
        <p id="p_05807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05807"> You can specify a different delimiter to separate the pattern and replacement string in the substitution operator. For example, the following substitution operator replaces  /u/bin  with /usr/local/bin:</p>
        <p id="p_05808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05808"> s#/u/bin#/usr/local/bin#</p>
        <p id="p_05809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05809"> The search and replacement strings can be enclosed in parentheses or angle brackets.</p>
        <p id="p_05810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05810"> s(/u/bin)(/usr/local/bin) s&lt;/u/bin&gt;/\/usr\/local\/bin/</p>
        <p id="p_05811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05811"> NOTE</p>
        <p id="p_05812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05812"> As with the match operator, you cannot use a special character both as a delimiter and in a pattern.</p>
        <p id="p_05813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05813"> s . a.c.def.</p>
        <p id="p_05814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05814"> This substitution will be flagged as containing an error because the . character is being used as the delimiter. The substitution</p>
        <p id="p_05815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05815"> s.a\.c.def .</p>
        <p id="p_05816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05816"> does work, but it substitutes  def  for  a . c,  where . is an actual period and not the pattern special character.</p>
        <p id="p_05817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05817"> The Translation Operator</p>
        <p id="p_05818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05818"> Perl also provides another way to substitute one group of characters for another: the  t r  translation operator. This operator uses the following syntax:</p>
        <p id="p_05819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05819"> tr/stringl/string2/</p>
        <p id="p_05820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05820"> Here,  stringl  contains a list of characters to be replaced, and  string2  contains the characters that replace them. The first character in  stringl  is replaced by the first character in  string2,  the second character in  stringl  is replaced by the second character in  string2,  and so on.</p>
        <p id="p_05821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05821"> Here is a simple example:</p>
        <p id="p_05822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05822"> $string = "abcdefghicba"; $string =~ tr/abc/def/;</p>
        <p id="p_05823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05823"> Here, the characters  a, b,  and  c  are to be replaced as follows:</p>
        <p id="p_05824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05824"> • All occurrences of the character  a  are to be replaced by the character  d.</p>
        <p id="p_05825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05825"> • All occurrences of the character  b  are to be replaced by the character  e.</p>
        <p id="p_05826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05826"> • All occurrences of the character  c  are to be replaced by the character  f .</p>
        <p id="p_05827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05827"> After the translation, the scalar variable $  string  contains the value  def def ghif ed.</p>
        <p id="p_05828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05828"> NOTE</p>
        <p id="p_05829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05829"> If the string listing the characters to be replaced is longer than the string containing the replacement characters, the last character of the replacement string is repeated. For example:</p>
        <p id="p_05830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05830"> $string = "abcdefgh";</p>
        <p id="p_05831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05831"> $string =~ tr/efgh/abc/;</p>
        <p id="p_05832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05832"> Here, there is no character corresponding to  d  in the replacement list, so  c,  the last character in the replacement list, replaces  h. This translation sets the value of  $stringto abcdabcc.</p>
        <p id="p_05833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05833"> Also note that if the same character appears more than once in the list of characters to be replaced, the first replacement is used:</p>
        <p id="p_05834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05834"> $ string</p>
        <p id="p_05835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05835"> tr/AAA/XYZ/; replaces A with X</p>
        <p id="p_05836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05836"> The most common use of the translation operator is to convert alphabetic characters from uppercase to lowercase or vice versa. Listing 7.13 provides an example of a program that converts a file to all lowercase characters.</p>
        <p id="p_05837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05837"> Type</p>
        <p id="p_05838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05838"> Listing 7.13. An uppercase-to-lowercase conversion program.</p>
        <p id="p_05839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05839"> Output</p>
        <p id="p_05840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05840"> $ program7_13</p>
        <p id="p_05841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05841"> THIS LINE IS IN UPPER CASE, this line is in upper  case. This LiNE Is iN mlxED  cASe. this line is in mixed  case. A D</p>
        <p id="p_05842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05842"> $</p>
        <p id="p_05843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05843"> Analysis</p>
        <p id="p_05844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05844"> This program reads a line at a time from the standard input file, terminating when it sees a line containing the Ctrl+D (end-of-file) character.</p>
        <p id="p_05845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05845"> Line 4 performs the translation operation. As in the other pattern-matching operations, the range character (-) indicates a range of characters to be included. Here, the range  a-z  refers to all the lowercase characters, and the range  A-Z  refers to all the uppercase characters.</p>
        <p id="p_05846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05846"> NOTE</p>
        <p id="p_05847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05847"> There are two things you should note about the translation operator:</p>
        <p id="p_05848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05848"> The pattern special characters are not supported by the translation operator.</p>
        <p id="p_05849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05849"> You can use  y  in place of  tr  if you want.</p>
        <p id="p_05850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05850"> $string =~ y/a-z/A-Z/;</p>
        <p id="p_05851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05851"> Options for the Translation Operator</p>
        <p id="p_05852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05852"> The translation operator supports three options, which are listed in Table 7.6.</p>
        <p id="p_05853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05853"> The  c  option  (c  is for "complement") translates all characters that are not specified. For example, the statement</p>
        <p id="p_05854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05854"> $string =~ tr/\d/ /c;</p>
        <p id="p_05855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05855"> replaces everything that is not a digit with a space.</p>
        <p id="p_05856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05856"> Table 7.6. Options for the translation operator.</p>
        <p id="p_05857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05857"> s</p>
        <p id="p_05858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05858"> Replace multiple identical output characters with a single character</p>
        <p id="p_05859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05859"> The d option deletes every specified character.</p>
        <p id="p_05860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05860"> $string =~  tr/\t  //d;</p>
        <p id="p_05861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05861"> This deletes all the tabs and spaces from $  string.</p>
        <p id="p_05862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05862"> The  s  option (for "squeeze") checks the output from the translation. If two or more consecutive characters translate to the same output character, only one output character is actually used. For example, the following replaces everything that is not a digit and outputs only one space between digits:</p>
        <p id="p_05863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05863"> $string =~  tr/0-9/ /cs;</p>
        <p id="p_05864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05864"> Listing 7.14 is a simple example of a program that uses some of these translation options. It reads a number from the standard input file, and it gets rid of every input character that is not actually a digit.</p>
        <p id="p_05865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05865"> Type</p>
        <p id="p_05866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05866"> Listing 7.14. A program that ensures that a string consists of nothing but digits.</p>
        <p id="p_05867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05867"> Output</p>
        <p id="p_05868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05868"> $ program7_14</p>
        <p id="p_05869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05869"> The number 45 appears in this string. 45 $</p>
        <p id="p_05870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05870"> Analysis</p>
        <p id="p_05871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05871"> Line 4 of this program performs the translation. The d option indicates that the translated characters are to be deleted, and the  c  option indicates that every character not in the list is to be deleted. Therefore, this translation deletes every character in the string that is not a digit. Note that the trailing newline character is not a digit, so it is one of the characters deleted.</p>
        <p id="p_05872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05872"> Extended Pattern-Matching</p>
        <p id="p_05873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05873"> Perl 5 provides some additional pattern-matching capabilities not found in Perl 4 or in standard UNIX pattern-matching operations.</p>
        <p id="p_05874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05874"> Extended pattern-matching capabilities employ the following syntax:</p>
        <p id="p_05875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05875"> (?&lt;c&gt;pattern)</p>
        <p id="p_05876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05876"> &lt;c&gt;  is a single character representing the extended pattern-matching capability being used, and pattern  is the pattern or subpattern to be affected.</p>
        <p id="p_05877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05877"> The following extended pattern-matching capabilities are supported by Perl 5:</p>
        <p id="p_05878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05878"> • Parenthesizing subpatterns without saving them in memory</p>
        <p id="p_05879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05879"> • Embedding options in patterns</p>
        <p id="p_05880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05880"> • Positive and negative look-ahead conditions</p>
        <p id="p_05881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05881"> • Comments</p>
        <p id="p_05882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05882"> Parenthesizing Without Saving in Memory</p>
        <p id="p_05883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05883"> In Perl, when a subpattern is enclosed in parentheses, the subpattern is also stored in memory. If you want to enclose a subpattern in parentheses without storing it in memory, use the ? : extended pattern¬ matching feature. For example, consider this pattern:</p>
        <p id="p_05884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05884"> This matches the following:</p>
        <p id="p_05885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05885"> • One of  a, b,  or  c</p>
        <p id="p_05886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05886"> • One of d or  e</p>
        <p id="p_05887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05887"> • f</p>
        <p id="p_05888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05888"> • Whichever of  d  or  e  was matched earlier</p>
        <p id="p_05889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05889"> Here, \ 1 matches either  d  or  e,  because the subpattern  a  |  b | c  was not stored in memory. Compare this with the following:</p>
        <p id="p_05890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05890"> /(a|b|c)(d|e)f\l/</p>
        <p id="p_05891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05891"> Here, the subpattern  a  |  b  |  c  is stored in memory, and one of  a, b,  or  c  is matched by \ 1.</p>
        <p id="p_05892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05892"> Embedding Pattern Options</p>
        <p id="p_05893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05893"> Perl 5 provides a way of specifying a pattern-matching option within the pattern itself. For example, the following patterns are equivalent:</p>
        <p id="p_05894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05894"> /[a-z]+/i /(?i)[a-z]+/</p>
        <p id="p_05895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05895"> In both cases, the pattern matches one or more alphabetic characters; the  i  option indicates that case is to be ignored when matching.</p>
        <p id="p_05896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05896"> The syntax for embedded pattern options is</p>
        <p id="p_05897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05897"> (?option)</p>
        <p id="p_05898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05898"> where  option  is one of the options shown in Table 7.7.</p>
        <p id="p_05899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05899"> Table 7.7. Options for embedded patterns.</p>
        <p id="p_05900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05900"> The  g  and  o  options are not supported as embedded pattern options.</p>
        <p id="p_05901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05901"> Embedded pattern options give you more flexibility when you are matching patterns. For example:</p>
        <p id="p_05902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05902"> $patternl = "[a-zO-9]+";</p>
        <p id="p_05903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05903"> $pattern2 = "(?i) [a-z]+";</p>
        <p id="p_05904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05904"> if ($string =~ /$patternl|$pattern2/) {</p>
        <p id="p_05905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05905"> }</p>
        <p id="p_05906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05906"> Here, the  i  option is specified for some, but not all, of a pattern. (This pattern matches either any collection of lowercase letters mixed with digits, or any collection of letters.)</p>
        <p id="p_05907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05907"> Positive and Negative Look-Ahead</p>
        <p id="p_05908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05908"> Perl 5 enables you to use the ? = feature to define a boundary condition that must be matched in order for the pattern to match. For example, the following pattern matches  abc  only if it is followed by</p>
        <p id="p_05909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05909"> def:</p>
        <p id="p_05910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05910"> /abc(?=def)/</p>
        <p id="p_05911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05911"> This is known as  a positive look-ahead condition.</p>
        <p id="p_05912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05912"> (note</p>
        <p id="p_05913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05913"> The positive look-ahead condition is not part of the pattern matched. For example, consider these statements:</p>
        <p id="p_05914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05914"> $string = "25abc8";</p>
        <p id="p_05915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05915"> $string =~ /abc (  ?=[0-9])/;</p>
        <p id="p_05916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05916"> $matched = $&amp;;</p>
        <p id="p_05917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05917"> Here, as always, $ &amp; contains the matched pattern, which in this case is  abc,  not  abc8.</p>
        <p id="p_05918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05918"> Similarly, the ? ! feature defines a  negative look-ahead condition,  which is a boundary condition that must not be present if the pattern is to match. For example, the pattern  /abc (? ! def ) / matches any occurrence of  abc  unless it is followed by  def.</p>
        <p id="p_05919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05919"> Pattern Comments</p>
        <p id="p_05920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05920"> Perl 5 enables you to add comments to a pattern using the ? # feature. For example:</p>
        <p id="p_05921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05921"> if ($string =~  /(?i)[a-z]{2,3}(?#  match two or three alphabetic characters)/ {</p>
        <p id="p_05922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05922"> }</p>
        <p id="p_05923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05923"> Adding comments makes it easier to follow complicated patterns.</p>
        <p id="p_05924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05924"> Summary</p>
        <p id="p_05925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05925"> Perl enables you to search for sequences of characters using  patterns.  If a pattern is found in a string, the pattern is said to be  matched.</p>
        <p id="p_05926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05926"> Patterns often are used in conjunction with the pattern-match operators, =~ and ! The =~ operator returns true if the pattern matches, and the ! ~ operator returns true if the pattern does not match.</p>
        <p id="p_05927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05927"> Special-pattern characters enable you to search for a string that meets one of a variety of conditions.</p>
        <p id="p_05928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05928"> • The + character matches one or more occurrences of a character.</p>
        <p id="p_05929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05929"> • The * character matches zero or more occurrences of a character.</p>
        <p id="p_05930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05930"> • The [ ] characters enclose a set of characters, any one of which matches.</p>
        <p id="p_05931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05931"> • The ? character matches zero or one occurrences of a character.</p>
        <p id="p_05932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05932"> • The  A  and $ characters match the beginning and end of a line, respectively. The  \b  and  \B characters match a word boundary or somewhere other than a word boundary, respectively.</p>
        <p id="p_05933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05933"> • The { } characters specify the number of occurrences of a character.</p>
        <p id="p_05934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05934"> • The | character specifies alternatives, either of which match.</p>
        <p id="p_05935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05935"> To give a special character its natural meaning in a pattern, precede it with a backslash \.</p>
        <p id="p_05936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05936"> Enclosing a part of a pattern in parentheses stores the matched subpattern in memory; this stored subpattern can be recalled using the character sequence  \n,  and stored in a scalar variable using the built-in scalar variable  $n.  The built-in scalar variable $&amp; stores the entire matched pattern.</p>
        <p id="p_05937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05937"> You can substitute for scalar-variable names in patterns, specify different pattern delimiters, or supply options that match every possible pattern, ignore case, or perform scalar-variable substitution only once.</p>
        <p id="p_05938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05938"> The substitution operator, s, enables you to replace a matched pattern with a specified string. Options to the substitution operator enable you to replace every matched pattern, ignore case, treat the replacing string as an expression, or perform scalar-variable substitution only once.</p>
        <p id="p_05939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05939"> The translation operator, tr, enables you to translate one set of characters into another set. Options exist that enable you to perform translation on everything not in the list, to delete characters in the list, or to ignore multiple identical output characters.</p>
        <p id="p_05940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05940"> Perl 5 provides extended pattern-matching capabilities not provided in Perl 4. To use one of these extended pattern features on a subpattern, put (? at the beginning of the subpattern and ) at the end of the subpattern.</p>
        <p id="p_05941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05941"> Q&amp;A</p>
        <p id="p_05942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05942"> Q: How many subpatterns can be stored in memory using \1, \2, and so on?</p>
        <p id="p_05943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05943"> A: Basically, as many as you like. After you store more than nine patterns, you can retrieve the</p>
        <p id="p_05944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05944"> later patterns using two-digit numbers preceded by a backslash, such as \ 10.</p>
        <p id="p_05945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05945"> Q: Why does pattern-memory variable numbering start with 1, whereas subscript</p>
        <p id="p_05946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05946"> numbering starts with 0?</p>
        <p id="p_05947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05947"> A: Subscript numbering starts with 0 to remain compatible with the C programming language.</p>
        <p id="p_05948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05948"> There is no such thing as pattern memory in C, so there is no need to be compatible with it.</p>
        <p id="p_05949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05949"> Q: What happens when the replacement string in the translate command is left out, as in</p>
        <p id="p_05950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05950"> tr/abc//?</p>
        <p id="p_05951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05951"> A: If the replacement string is omitted, a copy of the first string is used. This means that</p>
        <p id="p_05952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05952"> : t:r/abc//</p>
        <p id="p_05953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05953"> does not do anything, because it is the same as</p>
        <p id="p_05954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05954"> tr/abc/abc/</p>
        <p id="p_05955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05955"> If the replacement string is omitted in the substitute command, as in</p>
        <p id="p_05956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05956"> s/abc//</p>
        <p id="p_05957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05957"> the pattern matched-in this case, abc-is deleted.</p>
        <p id="p_05958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05958"> Q: Why does Perl use characters such as +, *, and ? as pattern special characters?</p>
        <p id="p_05959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05959"> A: These special characters usually correspond to special characters used in other UNIX</p>
        <p id="p_05960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05960"> applications, such as  vi  and  csh.  Some of the special characters, such as +, are used in formal syntax description languages.</p>
        <p id="p_05961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05961"> Q: Why does Perl use both \1 and $1 to store pattern memory?</p>
        <p id="p_05962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05962"> A: To enable you to distinguish between a subpattern matched in the current pattern (which is</p>
        <p id="p_05963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05963"> stored in \ 1) and a subpattern matched in the previous statement (which is stored in $ 1).</p>
        <p id="p_05964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05964"> Workshop</p>
        <p id="p_05965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05965"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_05966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05966"> Quiz</p>
        <p id="p_05967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05967"> 1. What do the following patterns match?</p>
        <p id="p_05968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05968"> a.  /a | be* /</p>
        <p id="p_05969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05969"> b. /  [\d] {1,3}/</p>
        <p id="p_05970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05970"> c. Abc [aou] t\b/</p>
        <p id="p_05971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05971"> d. /  (xy+z) \ .\1/</p>
        <p id="p_05972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05972"> e.  / A $/</p>
        <p id="p_05973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05973"> 2. Write patterns that match the following:</p>
        <p id="p_05974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05974"> a. Five or more lowercase letters (a-z).</p>
        <p id="p_05975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05975"> b. Either the number 1 or the string  one.</p>
        <p id="p_05976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05976"> c. string of digits optionally containing a decimal point.</p>
        <p id="p_05977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05977"> d. Any letter, followed by any vowel, followed by the same letter again.</p>
        <p id="p_05978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05978"> e. One or more + characters.</p>
        <p id="p_05979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05979"> 3. Suppose the variable  $var  has the value  abc 12 3.  Indicate whether the following conditional expressions return true or false.</p>
        <p id="p_05980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05980"> 4. Suppose the variable  $var  has the value  abcl2 3abc.  What is the value of  $var  after the</p>
        <p id="p_05981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05981"> following substitutions?</p>
        <p id="p_05982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05982"> 5. Suppose the variable  $var  has the value  abcl2 3abc.  What is the value of  $var  after the following translations?</p>
        <p id="p_05983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05983"> a.  $var =~ tr/a-z/A-Z/;</p>
        <p id="p_05984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05984"> b.  $var =~ tr/123/456/;</p>
        <p id="p_05985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05985"> c. $var =~ tr/231/564/;</p>
        <p id="p_05986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05986"> d.  $var =~ tr/123/ /s;</p>
        <p id="p_05987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05987"> e.  $var =~ tr/123//cd;</p>
        <p id="p_05988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05988"> Exercises</p>
        <p id="p_05989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05989"> 1. Write a program that reads all the input from the standard input file, converts all the vowels (except  y)  to uppercase, and prints the result on the standard output file.</p>
        <p id="p_05990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05990"> 2. Write a program that counts the number of times each digit appears in the standard input file. Print the total for each digit and the sum of all the totals.</p>
        <p id="p_05991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05991"> 3. Write a program that reverses the order of the first three words of each input line (from the standard input file) using the substitution operator. Leave the spacing unchanged, and print each resulting line.</p>
        <p id="p_05992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05992"> 4. Write a program that adds 1 to every number in the standard input file. Print the results.</p>
        <p id="p_05993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05993"> 5. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_05994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05994"> #!/usr/local/bin/perl</p>
        <p id="p_05995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05995"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_05996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05996"> # put quotes around each line of input $line =~  / A .*$/"\l"/; print  ($line);</p>
        <p id="p_05997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05997"> }</p>
        <p id="p_05998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05998"> 6. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_05999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_05999"> #!/usr/local/bin/perl</p>
        <p id="p_06000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06000"> while ($line = &lt;STDIN&gt;) { if ($line =~  /[\d]*/)  {</p>
        <p id="p_06001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06001"> print  ("This  line contains the digits  '$&amp;'\n");</p>
        <p id="p_06002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06002"> }</p>
        <p id="p_06003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06003"> }</p>
        <p id="p_06004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06004"> Week 1 in Review</p>
        <p id="p_06005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06005"> By now, you know enough about programming in Perl to write programs that perform many useful tasks. The program in Listing Rl.l, which takes a number and prints out its English equivalent, illustrates some of the concepts you've learned during your first week.</p>
        <p id="p_06006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06006"> Listing Rl.l. Printing the English equivalent of numeric input.</p>
        <p id="p_06007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06007"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06008"> 2  :</p>
        <p id="p_06009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06009"> 3: # define the strings used in printing</p>
        <p id="p_06010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06010"> 4: @digitword = ("", "one", "two", "three", "four", "five",</p>
        <p id="p_06011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06011"> 5: "six", "seven", "eight", "nine");</p>
        <p id="p_06012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06012"> 6: @digitlOword = ("", "ten", "twenty", "thirty", "forty",</p>
        <p id="p_06013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06013"> 7: "fifty", "sixty", "seventy", "eighty", "ninety");</p>
        <p id="p_06014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06014"> 8: @teenword = ("ten", "eleven", "twelve", "thirteen", "fourteen", 9: "fifteen", "sixteen", "seventeen", "eighteen", "nineteen");</p>
        <p id="p_06015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06015"> 10: Ogroupword =  ("",  "thousand", "million", "billion", "trillion", 11: "quadrillion", "quintillion", "sextillion", "septillion",</p>
        <p id="p_06016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06016"> 12: "octillion", "novillion", "decillion");</p>
        <p id="p_06017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06017"> 13 :</p>
        <p id="p_06018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06018"> 14: # read a line of input and remove all blanks, commas and tabs; 15: # complain about anything else 16: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_06019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06019"> 17: chop ($inputline);</p>
        <p id="p_06020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06020"> 18: $inputline =~  s/[, \t]+//g;</p>
        <p id="p_06021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06021"> 19:  if  ($inputline =~  /[ A \d]/)  {</p>
        <p id="p_06022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06022"> 20: die ("Input must be a number.\n");</p>
        <p id="p_06023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06023"> 21 : }</p>
        <p id="p_06024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06024"> 22  :</p>
        <p id="p_06025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06025"> 23: # remove leading zeroes 24: $inputline =~  s/ A 0+//;</p>
        <p id="p_06026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06026"> 25:  $inputline =~  s/ A $/0/;  # put one back if they're all zero</p>
        <p id="p_06027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06027"> 26:</p>
        <p id="p_06028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06028"> 27: # split into digits: $grouping contains the number of groups 28: # of digits, and $oddlot contains the number of digits in the 29: # first group, which may be only 1 or 2  (e.g.,  the 1 in 1,000) 30: @digits =  split(//,  $inputline);</p>
        <p id="p_06029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06029"> 31: if (@digits &gt; 36) {</p>
        <p id="p_06030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06030"> 32: die ("Number too large for program to handle.\n");</p>
        <p id="p_06031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06031"> 33: }</p>
        <p id="p_06032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06032"> 34: $oddlot = @digits % 3;</p>
        <p id="p_06033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06033"> 35:  $grouping  =  (@digits-l)  / 3;</p>
        <p id="p_06034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06034"> 36:</p>
        <p id="p_06035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06035"> 37: #  this loop iterates once for each grouping 38:  Scount =  0;</p>
        <p id="p_06036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06036"> 39: while ($grouping &gt;= 0) {</p>
        <p id="p_06037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06037"> 40: if ($oddlot == 2) {</p>
        <p id="p_06038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06038"> $digitl = 0;</p>
        <p id="p_06039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06039"> 41:</p>
        <p id="p_06040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06040"> 42 :</p>
        <p id="p_06041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06041"> 43 :</p>
        <p id="p_06042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06042"> 44 :</p>
        <p id="p_06043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06043"> 45 : 46:</p>
        <p id="p_06044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06044"> 47 :</p>
        <p id="p_06045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06045"> 48 :</p>
        <p id="p_06046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06046"> 49 :</p>
        <p id="p_06047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06047"> 50 : 51:</p>
        <p id="p_06048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06048"> 52 :</p>
        <p id="p_06049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06049"> 53 :</p>
        <p id="p_06050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06050"> 54 :</p>
        <p id="p_06051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06051"> 55 : 56:</p>
        <p id="p_06052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06052"> 57 :</p>
        <p id="p_06053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06053"> 58 :</p>
        <p id="p_06054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06054"> 59 :</p>
        <p id="p_06055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06055"> 60 : 61: 62 :</p>
        <p id="p_06056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06056"> 63 :</p>
        <p id="p_06057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06057"> 64 :</p>
        <p id="p_06058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06058"> 65 : 6   6  :</p>
        <p id="p_06059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06059"> $digit2 = $digits[0];</p>
        <p id="p_06060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06060"> $digit3 = $digits[l];</p>
        <p id="p_06061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06061"> $count += 2;</p>
        <p id="p_06062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06062"> } elsif ($oddlot == 1) {</p>
        <p id="p_06063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06063"> $digitl = 0;</p>
        <p id="p_06064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06064"> $digit2 = 0;</p>
        <p id="p_06065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06065"> $digits = $digits[0];</p>
        <p id="p_06066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06066"> Scount += 1;</p>
        <p id="p_06067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06067"> } else { # regular group of three digits</p>
        <p id="p_06068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06068"> $digitl = $digits[Scount];</p>
        <p id="p_06069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06069"> $digit2 = $digits[$count+l];</p>
        <p id="p_06070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06070"> $digit3 = $digits[$count+2];</p>
        <p id="p_06071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06071"> $count += 3;</p>
        <p id="p_06072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06072"> }</p>
        <p id="p_06073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06073"> $oddlot = 0;</p>
        <p id="p_06074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06074"> if ($digitl != 0) {</p>
        <p id="p_06075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06075"> print ("$digitword[$digit1] hundred ");</p>
        <p id="p_06076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06076"> }</p>
        <p id="p_06077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06077"> if (($digitl !=  0  || ($grouping == 0 &amp;&amp; Scount &gt; 3)) &amp;&amp; ($digit2 != 0 || $digit3 != 0)) { print ("and ");</p>
        <p id="p_06078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06078"> }</p>
        <p id="p_06079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06079"> if ($digit2 == 1) {</p>
        <p id="p_06080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06080"> print ( "Steenword[$digit3] ");</p>
        <p id="p_06081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06081"> } elsif ($digit2 != 0 &amp;&amp; $digit3 != 0) {</p>
        <p id="p_06082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06082"> 67: print ( "$digit1Oword[$digit2]-</p>
        <p id="p_06083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06083"> $digitword[$digit3] ");</p>
        <p id="p_06084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06084"> $ programRl_l 11,683</p>
        <p id="p_06085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06085"> eleven thousand</p>
        <p id="p_06086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06086"> six hundred and eighty-three</p>
        <p id="p_06087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06087"> $</p>
        <p id="p_06088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06088"> Analysis</p>
        <p id="p_06089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06089"> This program reads in a number up to 36 digits long and prints out its English equivalent, using one line for each group of three digits.</p>
        <p id="p_06090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06090"> Lines 4-12 define array variables whose lists are the possible words that can be in a number. The variable @digitword  lists the digits;  @digitlOword  lists the words that indicate multiples of ten;</p>
        <p id="p_06091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06091"> @teenword  lists the words that represent the values from 11 to 19; and  @groupword  lists the names for each group of digits. Note that some of these lists have an empty first element; this ensures that the array subscripts refer to the correct value. (For example, without the empty word at the beginning of</p>
        <p id="p_06092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06092"> @digitword, $digitword  [5] would refer to  four,  not  five.)</p>
        <p id="p_06093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06093"> Lines 14-21 read the input and check whether it is valid. Valid numbers consist of digits optionally separated by spaces, tabs, or commas. The substitution operator in line 18 removes these valid separators; the conditional expression in line 19 checks whether any invalid separators exist.</p>
        <p id="p_06094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06094"> If the program reaches line 24, the input number is valid. Line 24 gets rid of any leading zeros (to ensure that, for example, 0 0 0 0 71 is converted to 71). If a number consists entirely of zeros, line 24 converts $inputline  to the empty string; line 25 tests for this empty string and adds a zero if necessary.</p>
        <p id="p_06095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06095"> Lines 30-35 split the number into individual digits and create a list consisting of these digits. This list is assigned to the array variable @ digits.  Line 34 determines whether the first group of digits contains fewer than three digits; an example of this is the number  45,771,  whose first group of digits consists of only two digits. The scalar variable  $oddlot  is assigned the number of digits in the first group if the group is an odd lot of one or two; it is assigned 0 if the first group of digits contains all three digits.</p>
        <p id="p_06096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06096"> Line 35 calculates the number of groups of digits (including the initial odd lot). This determines the number of times that the upcoming printing loop is to be iterated.</p>
        <p id="p_06097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06097"> Lines 38-79 actually print the English value for this number. Each group of three digits is printed on its own line. The scalar variable $  count  contains the number of digits printed so far and is used as a subscript for the array variable  @digit s.</p>
        <p id="p_06098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06098"> To actually print the English value corresponding to a group of three digits, this loop first executes lines 40-57, which assign the values of the digits in the group to three scalar variables: $  digit 1, $digit2,  and $digit3.  If the group being handled is the first group, lines 40 and 46 check whether the group is an odd lot. For example, if the first group contains only two digits, the condition in line 40 becomes true, and the variable  $digitl,  which represents the first digit of the group, is assigned  0.  Using $  digit 1,</p>
        <p id="p_06099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06099"> $digit2,  and  $digit3  reduces the complexity of the program because no code following line 57 has to check for the value of  $oddlot.</p>
        <p id="p_06100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06100"> The number of digits actually handled is added to the scalar variable $  count  at this point.</p>
        <p id="p_06101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06101"> Line 58 assigns 0 to  $oddlot.  Subsequent groups of digits always contain three digits.</p>
        <p id="p_06102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06102"> Lines 59-77 print the English value associated with this particular group of digits as follows:</p>
        <p id="p_06103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06103"> 1. Lines 59-61 print the value of the hundreds place in this group (the first of the three digits).</p>
        <p id="p_06104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06104"> 2. Lines 62-64 check whether the word and needs to appeal' here. The word and is required in the following cases:</p>
        <p id="p_06105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06105"> o $digitl  is nonzero and one of the other digits is nonzero (as in  three hundred and four)</p>
        <p id="p_06106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06106"> o $digitlis  zero, one of the other digits is nonzero, and this is the last group to be handled (as in the  and four  part of the number  11, 0 0 4)</p>
        <p id="p_06107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06107"> 3. If the second digit is a  1  (as in  317),  one of the "teen words" (such as  eleven, twelve,  and thirteen)  must be used. Line 66 checks for this condition, and line 67 prints the appropriate word.</p>
        <p id="p_06108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06108"> 4. If both of the last two digits are defined, they both must be printed, and a dash must separate them (as in  forty-two).  Line 69 prints this pair of words and the dash.</p>
        <p id="p_06109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06109"> 5. If only one of the last two digits is defined, it is printed using line 71. (Note that line 71 actually specifies that both digits are printed; however, because only one is actually nonzero, it is the only one that appears. The digit that is zero appears in the output as the empty string because zero is equivalent to the empty string in Perl.)</p>
        <p id="p_06110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06110"> 6. Lines 73-74 print the word associated with this group of digits. For example, if this group is the second-last group of digits, the word  thousand  is printed.</p>
        <p id="p_06111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06111"> 7. Line 75 handles the special case of the number  0.  In this case, the word  zero  is printed.</p>
        <p id="p_06112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06112"> Once the English value for a particular group of digits is printed, the scalar variable  $grouping  has its value decreased by one, and the program continues with the next group of digits. If there are no more digits to print, the program terminates.</p>
        <p id="p_06113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06113"> Week 2 at a Glance</p>
        <p id="p_06114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06114"> CONTENTS</p>
        <p id="p_06115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06115"> •  Where You’re Going</p>
        <p id="p_06116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06116"> By now, you know enough about Perl to write many useful programs. You've discovered that Perl is powerful enough to enable you to perform complicated tasks, and simple enough to accomplish them quickly.</p>
        <p id="p_06117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06117"> Where You're Going</p>
        <p id="p_06118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06118"> The second week covers most of the features of the language not covered in the first week and describes some of the many library functions supplied with Perl. Here's a summary of what you'll learn.</p>
        <p id="p_06119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06119"> Day  8 ,  "More Control Structures," discusses the control flow statements not previously covered.</p>
        <p id="p_06120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06120"> Day  9 ,  "Using Subroutines," shows how you can break down your program into more manageable chunks.</p>
        <p id="p_06121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06121"> Day  10 ,  "Associative Arrays," introduces one of the most powerful and useful constructs in Perl, associative arrays, and it shows how you can use these arrays to simulate other data structures.</p>
        <p id="p_06122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06122"> Day  11 ,  "Formatting Your Output," shows how you can use Perl to produce tidy reports.</p>
        <p id="p_06123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06123"> Day  12 ,  "Working with the File System," shows how you can interact with your system's directory structure.</p>
        <p id="p_06124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06124"> Day  13 ,  "Process, String, and Mathematical Functions," describes the library functions that interact with processes running on the system, operate on text strings, and perform mathematical operations.</p>
        <p id="p_06125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06125"> Day  14 ,  "Scalar-Conversion and List-Manipulation Functions," describes the library functions that convert values from one form to another and work with lists and array variables.</p>
        <p id="p_06126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06126"> By the end of the second week, you'll have mastered almost all of the features of Perl and you'll have learned about many of the library functions supplied with the language.</p>
        <p id="p_06127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06127"> Chapter 8</p>
        <p id="p_06128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06128"> More Control Structures</p>
        <p id="p_06129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06129"> CONTENTS</p>
        <p id="p_06130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06130"> •  Using Single-Line Conditional Statements</p>
        <p id="p_06131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06131"> o  Problems with Single-Line Conditional Statements</p>
        <p id="p_06132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06132"> •  Looping Using the  for  Statement</p>
        <p id="p_06133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06133"> o  Using the Comma Operator in a  for  Statement</p>
        <p id="p_06134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06134"> •  Looping Through a List: The  f preach  Statement</p>
        <p id="p_06135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06135"> o  The  f preach  Local Variable o  Changing the Value of the Local Variable o  Using Returned Lists in the  f preach  Statement</p>
        <p id="p_06136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06136"> •  The  do  Statement</p>
        <p id="p_06137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06137"> •  Exiting a Loop Using the  last  Statement</p>
        <p id="p_06138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06138"> •  Using  next  to Start the Next Iteration of a Loop</p>
        <p id="p_06139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06139"> •  The  redo  Statement</p>
        <p id="p_06140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06140"> •  Using Labeled Blocks for Multilevel Jumps</p>
        <p id="p_06141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06141"> o  Using  next  and  redo  with Labels</p>
        <p id="p_06142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06142"> •  The  continue  Block</p>
        <p id="p_06143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06143"> •  The  goto  Statement</p>
        <p id="p_06144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06144"> •  Summary</p>
        <p id="p_06145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06145"> •  Q&amp;A</p>
        <p id="p_06146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06146"> •  Workshop</p>
        <p id="p_06147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06147"> o  Quiz o  Exercises</p>
        <p id="p_06148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06148"> On  Day 2 ,  "Basic Operators and Control Flow," you learned about some of the simpler conditional statements in Perl, including the following:</p>
        <p id="p_06149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06149"> • The  i f  statement, which defines statements that are executed only when a certain condition is true</p>
        <p id="p_06150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06150"> • The  if-else  statement, which chooses between two alternatives</p>
        <p id="p_06151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06151"> • The  if-elsif-else  statement, which chooses between multiple alternatives</p>
        <p id="p_06152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06152"> • The  unless  statement, which defines statements that are executed unless a specified condition is true</p>
        <p id="p_06153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06153"> • The  while  statement, which executes a group of statements while a specified condition is true</p>
        <p id="p_06154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06154"> • The  until  statement, which executes a group of statements until a specified condition is true</p>
        <p id="p_06155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06155"> Today's lesson talks about the other control structures in Perl; these control structures give you a great deal</p>
        <p id="p_06156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06156"> of flexibility when you are determining the order of execution of your program statement.</p>
        <p id="p_06157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06157"> Today you learn the following control structures:</p>
        <p id="p_06158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06158"> • Single-line conditional statements</p>
        <p id="p_06159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06159"> • The for statement</p>
        <p id="p_06160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06160"> • The  for each  statement</p>
        <p id="p_06161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06161"> • The  do  statement</p>
        <p id="p_06162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06162"> • The  last  statement</p>
        <p id="p_06163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06163"> • The  next  statement</p>
        <p id="p_06164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06164"> • The  redo  statement</p>
        <p id="p_06165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06165"> • The  continue  statement</p>
        <p id="p_06166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06166"> • Labeled blocks</p>
        <p id="p_06167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06167"> • The  goto  statement</p>
        <p id="p_06168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06168"> Using Single-Line Conditional Statements</p>
        <p id="p_06169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06169"> On  Day 2   you saw the if statement, which works as follows:</p>
        <p id="p_06170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06170"> if ($var == 0) {</p>
        <p id="p_06171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06171"> print ("This is zero.\n");</p>
        <p id="p_06172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06172"> }</p>
        <p id="p_06173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06173"> If the statement block inside the  i f  statement consists of only one statement, Perl enables you to write this in a more convenient way using a  single-line conditional statement.  This is a conditional statement whose statement block contains only one line of code.</p>
        <p id="p_06174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06174"> The following single-line conditional statement is identical to the if statement defined previously:</p>
        <p id="p_06175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06175"> print ("This is zero.\n") if ($var == 0);</p>
        <p id="p_06176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06176"> Single-line conditional statements also work with  unless, while,  and  until:</p>
        <p id="p_06177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06177"> In all four cases, the syntax of the single-line conditional statement is the same.</p>
        <p id="p_06178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06178"> The syntax for the single-line conditional statement is</p>
        <p id="p_06179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06179"> statement keyword condexpr</p>
        <p id="p_06180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06180"> Here,  statement  is any Perl statement,  keyword  is either  if, unless, while,  or  until, condexpr</p>
        <p id="p_06181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06181"> is the conditional expression that is evaluated.</p>
        <p id="p_06182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06182"> statement  is executed in the following cases:</p>
        <p id="p_06183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06183"> • If  keyword  is  if,  statement is executed if  condexpr  is true.</p>
        <p id="p_06184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06184"> • If  keyword  is  unless,  statement is executed unless  condexpr  is true.</p>
        <p id="p_06185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06185"> • If  keyword  is  while,  statement is executed while  condexpr  is true.</p>
        <p id="p_06186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06186"> • If  keyword  is  until,  statement is executed until  condexpr  is true.</p>
        <p id="p_06187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06187"> To see how single-line conditional expressions can be useful, look at the following examples, starting with Listing 8.1. This is a simple program that copies one file to another. Single-line conditional statements are used to check whether the files opened successfully, and another single-line conditional statement actually copies the file.</p>
        <p id="p_06188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06188"> Type</p>
        <p id="p_06189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06189"> Listing 8.1. A program that uses single-line conditional statements to copy one file to</p>
        <p id="p_06190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06190"> Output</p>
        <p id="p_06191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06191"> There is no output; this program writes to a file.</p>
        <p id="p_06192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06192"> Analysis</p>
        <p id="p_06193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06193"> As you can see, this program is clear and concise. Instead of using three lines to open a file and check it, as in</p>
        <p id="p_06194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06194"> unless (open (INFILE, "infile")) { die ("Can't open input\n");</p>
        <p id="p_06195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06195"> }</p>
        <p id="p_06196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06196"> you can now use just one:</p>
        <p id="p_06197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06197"> die ("Can't open input\n") unless (open(INFILE, "infile"));</p>
        <p id="p_06198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06198"> Line 3 opens the input file. If the open is not successful, the program terminates by calling  die.</p>
        <p id="p_06199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06199"> Line 4 is similar to line 3. It opens the output file and checks whether the file actually is open; if the file is not open, the program terminates.</p>
        <p id="p_06200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06200"> Line 5 actually copies the file. The conditional expression</p>
        <p id="p_06201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06201"> $line = &lt;INFILE&gt;</p>
        <p id="p_06202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06202"> reads a line from the file represented by the file variable  INFILE  and assigns it to  $line.  If the line is empty, the conditional expression is false, and the  while  statement stops executing. If the line is not empty, it is written to  OUTFILE.</p>
        <p id="p_06203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06203"> NOTE</p>
        <p id="p_06204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06204"> The conditional expression in a single-line conditional statement is always executed first, even though it appears at the end of the statement. For example:</p>
        <p id="p_06205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06205"> print OUTFILE ($line) while ($line = &lt;INFILE&gt;);</p>
        <p id="p_06206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06206"> Here, the conditional expression that reads a line of input and assigns it to $  line  is always executed first. This means that  print  is not called until  $line  contains something to print. This also means that the call to  print  is never executed if  INFILE  is an empty file (which is what you want).</p>
        <p id="p_06207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06207"> Because single-line conditional expressions are "backward," be careful when you use them with anything more complicated than what you see here.</p>
        <p id="p_06208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06208"> You can use the single-line conditional statement in conjunction with the autoincrement operator ++ to write a loop in a single line. For example, examine Listing 8.2, which prints the numbers from 1 to 5 using a single-line conditional statement.</p>
        <p id="p_06209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06209"> Type</p>
        <p id="p_06210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06210"> Listing 8.2. A program that loops using a single-line conditional statement.</p>
        <p id="p_06211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06211"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06212"> 2  :</p>
        <p id="p_06213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06213"> 3: $count = 0;</p>
        <p id="p_06214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06214"> 4: print ("$count\n") while ($count++ &lt; 5);</p>
        <p id="p_06215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06215"> Output</p>
        <p id="p_06216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06216"> $ program8_2 1 2</p>
        <p id="p_06217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06217"> 3</p>
        <p id="p_06218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06218"> 4</p>
        <p id="p_06219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06219"> 5 $</p>
        <p id="p_06220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06220"> Analysis</p>
        <p id="p_06221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06221"> When the Perl interpreter executes line 3, it first evaluates the conditional expression</p>
        <p id="p_06222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06222"> $count++ &lt; 5</p>
        <p id="p_06223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06223"> Because the ++ appears after $  count,  1 is added to the value of $  count  after the conditional expression is evaluated. This means that $  count  has the value 0, not 1, the first time the expression is evaluated. Similarly, $  count  has the value 1 the second time, 2 the third time, 3 the fourth time, and 4 the fifth time. In each of these five cases, the conditional expression evaluates to true, which means that the loop iterates five times.</p>
        <p id="p_06224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06224"> After the conditional expression has been evaluated, the ++ operator adds 1 to the value of $  count.  This new value of $  count  is then printed. This means that when the loop is first executed, the call to  print prints 1, even though the value of $  count  was 0 when the conditional expression was evaluated.</p>
        <p id="p_06225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06225"> Problems with Single-Line Conditional Statements</p>
        <p id="p_06226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06226"> Although single-line conditional statements that contain loops are useful, there are problems. Consider Listing 8.2, which you've just seen. It is easy to forget that $  count  has to be initialized to one less than the first value you want to use in the loop, and that the conditional expression has to use the &lt; operator, not the &lt;= operator.</p>
        <p id="p_06227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06227"> For example, take a look at the following:</p>
        <p id="p_06228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06228"> $count = 1;</p>
        <p id="p_06229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06229"> print ("$count\n") while ($count++ &lt; 5);</p>
        <p id="p_06230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06230"> Here, you have to look closely to see that the first value printed is 2, not 1. Here is another loop containing a mistake:</p>
        <p id="p_06231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06231"> $count = 0;</p>
        <p id="p_06232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06232"> print ("$count\n") while ($count++ &lt;= 5);</p>
        <p id="p_06233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06233"> This loop iterates six times, not five; the sixth time through the loop, $  count  has the value  5  when the conditional expression is evaluated. The expression evaluates to true, $  count  is incremented to 6, and print  therefore prints the value  6.</p>
        <p id="p_06234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06234"> Here is a related but slightly more subtle problem:</p>
        <p id="p_06235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06235"> $count = 0;</p>
        <p id="p_06236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06236"> print ("$count\n") while ($count++ &lt; 5);</p>
        <p id="p_06237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06237"> print ("The total number of iterations is $count.\n");</p>
        <p id="p_06238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06238"> This loop iterates five times, which is what you want. However, after the conditional expression is evaluated for the final time, the value of $  count  becomes  6,  as follows:</p>
        <p id="p_06239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06239"> • Before the conditional expression is evaluated, $  count  has the value  5.</p>
        <p id="p_06240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06240"> • Because the value of $  count  is not less than 5, the conditional expression evaluates to false, which terminates the loop.</p>
        <p id="p_06241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06241"> • After the conditional expression is evaluated, the ++ operator adds one to $  count,  giving it the value 6.</p>
        <p id="p_06242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06242"> This means that the final  print  statement prints the following, which is probably not what you want:</p>
        <p id="p_06243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06243"> The total number of iterations is 6.</p>
        <p id="p_06244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06244"> Don't</p>
        <p id="p_06245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06245"> DO use the  for  statement as a convenient way to write a concise, compact loop. It is discussed in the next section.</p>
        <p id="p_06246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06246"> DON’T use the ++ operator to produce a loop in a single-line conditional statement unless it's absolutely necessary. It's just too easy to go wrong with it.</p>
        <p id="p_06247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06247"> Looping Using the for Statement</p>
        <p id="p_06248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06248"> Many of the programs that you've seen so far use the  while  statement to create a program loop. Here is a simple example:</p>
        <p id="p_06249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06249"> $count = 1;</p>
        <p id="p_06250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06250"> while ($count &lt;= 5) {</p>
        <p id="p_06251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06251"> # statements inside the loop go here $count++;</p>
        <p id="p_06252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06252"> }</p>
        <p id="p_06253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06253"> This loop contains three items that control it:</p>
        <p id="p_06254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06254"> 1. A statement that sets the initial value of the loop. In this loop, the scalar variable $  count  is used to</p>
        <p id="p_06255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06255"> control the number of iterations of the loop, and the statement</p>
        <p id="p_06256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06256"> $count = 1;</p>
        <p id="p_06257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06257"> sets the initial value of $  count  to 1. Statements such as this are called  loop initializers.</p>
        <p id="p_06258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06258"> 2. A conditional expression that checks to see whether to continue iterating the loop. In this case, the conditional expression</p>
        <p id="p_06259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06259"> $count &lt;= 5</p>
        <p id="p_06260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06260"> is evaluated; if it is false, the loop is terminated.</p>
        <p id="p_06261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06261"> 3. A statement that changes the value of the variable which is tested in the conditional expression. In this loop, the statement</p>
        <p id="p_06262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06262"> count++;</p>
        <p id="p_06263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06263"> adds 1 to the value of $  count,  which is the scalar variable being tested in the conditional expression. Statements such as this are called  loop iterators.</p>
        <p id="p_06264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06264"> Perl enables you to put the three components that control a loop together on a single line using a  for statement. For example, the following statement is equivalent to the loop you've been looking at:</p>
        <p id="p_06265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06265"> for ($count=l; $count &lt;= 5; $count++) {</p>
        <p id="p_06266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06266"> # statements inside the loop go here</p>
        <p id="p_06267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06267"> }</p>
        <p id="p_06268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06268"> Here, the three controlling components-the loop initializer, the conditional expression, and the loop iterator-appear together, and are separated by semicolons.</p>
        <p id="p_06269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06269"> The syntax of the for statement is</p>
        <p id="p_06270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06270"> for (exprl; expr2; expr3) { statement_block</p>
        <p id="p_06271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06271"> }</p>
        <p id="p_06272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06272"> exprl  is the loop initializer. It is evaluated only once, before the start of the loop.</p>
        <p id="p_06273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06273"> expr2  is the conditional expression that terminates the loop. The conditional expression in  expr2  behaves just like the ones in  while  and  if  statements. If its value is 0 (false), the loop is terminated, and if its value is nonzero, the loop is executed.</p>
        <p id="p_06274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06274"> statement_block  is the collection of statements that is executed if (and when)  expr2  has a nonzero value.</p>
        <p id="p_06275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06275"> expr3 is executed once per iteration of the loop and is executed after the last statement in</p>
        <p id="p_06276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06276"> statement block  is executed.</p>
        <p id="p_06277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06277"> NOTE</p>
        <p id="p_06278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06278"> If you know the C programming language, the  for  statement will be familiar to you. The for statement in Perl is syntactically identical to the  for  statement in C.</p>
        <p id="p_06279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06279"> Listing  8.3  is a program based on the example  for  statement you've just seen.</p>
        <p id="p_06280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06280"> Type</p>
        <p id="p_06281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06281"> Listing 8.3. A program that prints the numbers from 1 to 5 using the for statement.</p>
        <p id="p_06282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06282"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06283"> 2  :</p>
        <p id="p_06284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06284"> 3: for ($count=l; $count &lt;= 5; $count++) {</p>
        <p id="p_06285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06285"> 4: print ("$count\n");</p>
        <p id="p_06286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06286"> 5: }</p>
        <p id="p_06287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06287"> Output</p>
        <p id="p_06288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06288"> $ program8_3 1 2</p>
        <p id="p_06289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06289"> 3</p>
        <p id="p_06290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06290"> 4</p>
        <p id="p_06291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06291"> 5</p>
        <p id="p_06292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06292"> $</p>
        <p id="p_06293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06293"> Analysis</p>
        <p id="p_06294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06294"> Line 3 of the program is the start of the  for  statement. The first expression defined in the  f o statement, $  count  =  1 ,  is the loop initializer; it is executed before the loop is iterated.</p>
        <p id="p_06295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06295"> The second expression defined in the  for  statement,  $count &lt;= 5,  tests whether to continue iterating the loop.</p>
        <p id="p_06296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06296"> The third expression defined in the  for  statement,  $count++,  is evaluated after the last statement in the loop, line 4, is executed.</p>
        <p id="p_06297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06297"> As you can see from the output, the loop is iterated five times.</p>
        <p id="p_06298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06298"> TIP</p>
        <p id="p_06299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06299"> Use the  for  statement instead of  while  or  until  whenever possible; when you use the  for  statement, it is easier to avoid infinite loops.</p>
        <p id="p_06300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06300"> For example, when you use  awhile  statement, it's easy to forget to iterate the loop. The following is an example:</p>
        <p id="p_06301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06301"> $count = 1;</p>
        <p id="p_06302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06302"> while ($count &lt;= 5) {</p>
        <p id="p_06303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06303"> print ("$count\n");</p>
        <p id="p_06304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06304"> }</p>
        <p id="p_06305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06305"> The equivalent statement using  for  is</p>
        <p id="p_06306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06306"> for ($count = 1; $count &lt;= 5; ) {</p>
        <p id="p_06307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06307"> print ("$count\n");</p>
        <p id="p_06308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06308"> }</p>
        <p id="p_06309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06309"> When you use the  for  statement, it is easier to notice that the loop iterator is missing.</p>
        <p id="p_06310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06310"> Using the Comma Operator in a for Statement</p>
        <p id="p_06311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06311"> Some loops need to perform more than one action before iterating. For example, consider the following loop, which reads four lines of input from the standard input file and prints three of them:</p>
        <p id="p_06312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06312"> $line = &lt;STDIN&gt;; $count = 1;</p>
        <p id="p_06313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06313"> while ($count &lt;= 3) {</p>
        <p id="p_06314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06314"> print ($line); $line = &lt;STDIN&gt;; $count++;</p>
        <p id="p_06315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06315"> }</p>
        <p id="p_06316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06316"> This loop needs two loop initializers and two loop iterators: one of each for the variable $  count,  and one of each to read another line of input from  STD IN.</p>
        <p id="p_06317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06317"> At first glance, you might think that you can't write this loop using the  for  statement. However, you can use the comma operator to combine the two loop initializers and the two loop iterators into single expressions. Listing 8.4 does this.</p>
        <p id="p_06318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06318"> Type</p>
        <p id="p_06319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06319"> Listing 8.4.  A  program that uses the for statement to read four input lines and write three of them.</p>
        <p id="p_06320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06320"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06321"> 2  :</p>
        <p id="p_06322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06322"> 3: for ($line = &lt;STDIN&gt;, $count = 1; $count &lt;= 3;</p>
        <p id="p_06323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06323"> 4: $line = &lt;STDIN&gt;, $count++) {</p>
        <p id="p_06324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06324"> 5: print ($line);</p>
        <p id="p_06325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06325"> 6: }</p>
        <p id="p_06326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06326"> Output</p>
        <p id="p_06327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06327"> $ program8_4</p>
        <p id="p_06328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06328"> This is my first line.</p>
        <p id="p_06329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06329"> This is my first line.</p>
        <p id="p_06330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06330"> line. line .</p>
        <p id="p_06331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06331"> ne.</p>
        <p id="p_06332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06332"> ne.</p>
        <p id="p_06333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06333"> not written out.</p>
        <p id="p_06334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06334"> $</p>
        <p id="p_06335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06335"> Analysis</p>
        <p id="p_06336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06336"> The loop initializer in this  for  statement is the expression</p>
        <p id="p_06337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06337"> $line = &lt;STDIN&gt;, $count = 1</p>
        <p id="p_06338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06338"> The comma operator in this expression tells the Perl interpreter to evaluate the first half of the expression-the part to the left of the comma-and then evaluate the second half. The first half of this expression reads a line from the standard input file and assigns it to  $line;  the second half of the expression assigns 1 to</p>
        <p id="p_06339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06339"> $count.</p>
        <p id="p_06340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06340"> The loop iterator also consists of two parts:</p>
        <p id="p_06341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06341"> $line = &lt;STDIN&gt;, $count++</p>
        <p id="p_06342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06342"> This expression reads a line from the standard input file and adds 1 to the variable keeping track of when to terminate the loop, which is $  count.</p>
        <p id="p_06343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06343"> Warning</p>
        <p id="p_06344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06344"> Don't use the  for  statement if you have a large number of loop initializers or loop iterators, because statements that contain a large number of comma operators are difficult to read.</p>
        <p id="p_06345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06345"> Looping Through a List: The for each Statement</p>
        <p id="p_06346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06346"> One common use of loops is to perform an operation on every element of a list stored in an array variable. For example, the following loop checks whether any element of the list stored in the array variable  Swords is the word  the:</p>
        <p id="p_06347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06347"> $count</p>
        <p id="p_06348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06348"> i;</p>
        <p id="p_06349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06349"> while ($count &lt;= Swords) {</p>
        <p id="p_06350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06350"> if ($words[$count-l] eq "the") {</p>
        <p id="p_06351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06351"> print ("found the word  'the'\n");</p>
        <p id="p_06352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06352"> }</p>
        <p id="p_06353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06353"> $count++;</p>
        <p id="p_06354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06354"> As you've seen, you can use the for statement to simplify this loop, as follows:</p>
        <p id="p_06355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06355"> for ($count = 1; $count &lt;= Swords; $count++) { if ($words[$count-l] eq "the") {</p>
        <p id="p_06356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06356"> print ("found the word  'the'\n");</p>
        <p id="p_06357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06357"> }</p>
        <p id="p_06358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06358"> Perl provides an even simpler way to do the same thing, using the  f oreach  statement. The following loop, which uses  f oreach,  is identical to the preceding one:</p>
        <p id="p_06359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06359"> foreach $word (Swords) {</p>
        <p id="p_06360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06360"> if ($word eq "the") {</p>
        <p id="p_06361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06361"> print ("found the word  'the'\n");</p>
        <p id="p_06362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06362"> }</p>
        <p id="p_06363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06363"> The syntax for the  foreach  statement is</p>
        <p id="p_06364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06364"> foreach localvar (listexpr) {</p>
        <p id="p_06365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06365"> statement_block;</p>
        <p id="p_06366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06366"> }</p>
        <p id="p_06367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06367"> Here,  listexpr  is any list or array variable, and  statement_block  is a collection of statements that is executed every time the loop iterates.</p>
        <p id="p_06368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06368"> localvar  is a scalar variable that is defined only for the duration of the  f oreach  statement. The first time the loop is executed,  localvar  is assigned the value of the first element of the list in  listexpr. Each subsequent time the loop is executed,  localvar  is assigned the value of the next element of</p>
        <p id="p_06369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06369"> listexpr.</p>
        <p id="p_06370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06370"> Listing 8.5 shows how this works.</p>
        <p id="p_06371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06371"> Type</p>
        <p id="p_06372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06372"> Listing 8.5.  A  demonstration of the f oreach statement.</p>
        <p id="p_06373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06373"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06374"> 2  :</p>
        <p id="p_06375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06375"> 3: @words = ("Here", "is", "a", "list.");</p>
        <p id="p_06376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06376"> 4: foreach $word (Swords) {</p>
        <p id="p_06377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06377"> 5: print ("$word\n");</p>
        <p id="p_06378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06378"> 6: }</p>
        <p id="p_06379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06379"> Output</p>
        <p id="p_06380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06380"> $ program8_5</p>
        <p id="p_06381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06381"> Here</p>
        <p id="p_06382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06382"> is</p>
        <p id="p_06383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06383"> a</p>
        <p id="p_06384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06384"> list.</p>
        <p id="p_06385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06385"> $</p>
        <p id="p_06386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06386"> Analysis</p>
        <p id="p_06387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06387"> The  f oreach  statement in line 4 assigns a word from  @list  to the local variable  $word.  The first time the loop is executed, the value stored in  $word  is the string  Here.  The second time the loop is executed, the value stored in  $word  is  is.  Subsequent iterations assign  a  and  list . to  $word.</p>
        <p id="p_06388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06388"> The loop defined by the  f oreach  statement terminates after all of the words in the list have been assigned</p>
        <p id="p_06389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06389"> to  $word.</p>
        <p id="p_06390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06390"> NOTE</p>
        <p id="p_06391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06391"> In Perl, the  for  statement and the  foreach  statement are actually synonymous: you can use  for  wherever  f oreach  is expected, and vice versa.</p>
        <p id="p_06392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06392"> The f oreach Local Variable</p>
        <p id="p_06393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06393"> Note that the scalar variable defined in the  f oreach  statement is defined only for the duration of the loop. If a value is assigned to the scalar variable prior to the execution of the  f oreach  statement, this value is restored after the  f oreach  is executed. Listing  8.6  shows how this works.</p>
        <p id="p_06394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06394"> Type</p>
        <p id="p_06395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06395"> Listing 8.6.  A  program that uses the same name inside and outside a f oreach statement.</p>
        <p id="p_06396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06396"> "list",  "of",  "words") in the list:  \n");</p>
        <p id="p_06397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06397"> r</p>
        <p id="p_06398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06398"> 10: print("The value of temp is now $temp\n");</p>
        <p id="p_06399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06399"> Output</p>
        <p id="p_06400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06400"> $ program8_6</p>
        <p id="p_06401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06401"> Here are the words in the list: This is a list of words The value of temp is now 1 $</p>
        <p id="p_06402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06402"> Analysis</p>
        <p id="p_06403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06403"> Line 3 assigns 1 to the scalar variable  $temp.</p>
        <p id="p_06404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06404"> The  f oreach  statement that prints the words in the list is defined in lines 6-8. This statement assigns the elements of  @list  to  $temp,  one per iteration of the loop.</p>
        <p id="p_06405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06405"> After the loop is terminated, the original value of  $temp  is restored, which is 1. This value is printed by line 10.</p>
        <p id="p_06406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06406"> Variables (such as  $temp  in lines 6-8) that are only defined for part of a program are known as  local variables',  variables that are defined throughout a program are known as  global variables.  You'll see more examples of local variables on  Day  9 ,  "Using Subroutines."</p>
        <p id="p_06407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06407"> TIP</p>
        <p id="p_06408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06408"> It is not a good idea to use  $temp  the way it is used in Listing 8.6, namely, as both a local and a global variable. You might forget that the value of the global variable-in the case of  $temp,  the value 1-is overwritten by the value assigned in the  f oreach  statement.</p>
        <p id="p_06409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06409"> Conversely, you might forget that the value assigned to  $temp  in the f oreach  statement is lost when the  f oreach  is finished.</p>
        <p id="p_06410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06410"> It is better to define a new scalar variable name for the local variable, to avoid confusion.</p>
        <p id="p_06411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06411"> Changing the Value of the Local Variable</p>
        <p id="p_06412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06412"> Note that changing the value of the local variable inside a  f oreach  statement also changes the value of the corresponding element of the list. For example:</p>
        <p id="p_06413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06413"> @list = (1, 2, 3, 4, 5); foreach $temp (@list) {</p>
        <p id="p_06414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06414"> if ($temp == 2) {</p>
        <p id="p_06415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06415"> $temp = 20;</p>
        <p id="p_06416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06416"> }</p>
        <p id="p_06417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06417"> }</p>
        <p id="p_06418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06418"> In this loop, when  $temp  is equal to  2, $temp  is reset to  2 0.  Therefore, the list stored in the array variable @ list  becomes  (1, 20, 3, 4, 5).</p>
        <p id="p_06419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06419"> Use this feature with caution, because it is not obvious that the value of  @list  has changed.</p>
        <p id="p_06420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06420"> Using Returned Lists in the foreach Statement</p>
        <p id="p_06421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06421"> So far, all of the examples of the  foreach  statement that you've seen have iterated using the contents of an array variable. For example, consider the following:</p>
        <p id="p_06422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06422"> @list = ("This", "is", "a", "list");</p>
        <p id="p_06423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06423"> foreach $temp (@list) {</p>
        <p id="p_06424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06424"> print ("$temp ");</p>
        <p id="p_06425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06425"> }</p>
        <p id="p_06426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06426"> This loop assigns  This  to  $temp  the first time through the loop, and then assigns  is, a,  and  list  to $temp  on subsequent iterations.</p>
        <p id="p_06427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06427"> You also can use list constants or the return values from functions in  foreach  statements. For example, the preceding statements can be written as follows:</p>
        <p id="p_06428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06428"> foreach $temp ("This", "is", "a", "list") {</p>
        <p id="p_06429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06429"> print("$temp ");</p>
        <p id="p_06430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06430"> }</p>
        <p id="p_06431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06431"> As before,  $temp  is assigned  This, is, a,  and  list  in successive iterations of the  f oreach  loop.</p>
        <p id="p_06432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06432"> Listing 8.7 shows how you can use the return value from a function as a loop iterator.</p>
        <p id="p_06433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06433"> Type</p>
        <p id="p_06434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06434"> Listing 8.7. A program that prints out the words in a line in reverse-sorted order.</p>
        <p id="p_06435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06435"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06436"> 2  :</p>
        <p id="p_06437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06437"> 3: $line = &lt;STDIN&gt;;</p>
        <p id="p_06438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06438"> 4: $line =~  s/ A \s+//;</p>
        <p id="p_06439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06439"> 5:  $line =~  s/\s+$//;</p>
        <p id="p_06440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06440"> 6:  foreach $word (reverse sort  split(/[\t ]+/,  $line)) {</p>
        <p id="p_06441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06441"> 7: print ("$word ");</p>
        <p id="p_06442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06442"> 8: }</p>
        <p id="p_06443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06443"> 9 : print  ("\n");</p>
        <p id="p_06444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06444"> Output</p>
        <p id="p_06445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06445"> $ program8_7 here is my test line test my line is here $</p>
        <p id="p_06446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06446"> Analysis</p>
        <p id="p_06447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06447"> Before splitting the input line into words using  split,  this program first removes the leading</p>
        <p id="p_06448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06448"> and trailing white space. (If leading and trailing space is not removed,  split  creates an empty word.) Line 4 removes leading spaces and tabs from the input line. Line 5 removes any trailing spaces and tabs as well as the closing newline character.</p>
        <p id="p_06449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06449"> Lines 6-8 contain the  f oreach  loop. The list used in this loop is created as follows:</p>
        <p id="p_06450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06450"> 1. First,  split  breaks the input line into words. The list returned by  split  is  ("here", "is", "my", "test", "line").</p>
        <p id="p_06451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06451"> 2. The list returned by  split  is passed to the built-in function  sort,  which sorts the list. The list returned by  sort  is  ("here", "is", "line", "my", "test").</p>
        <p id="p_06452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06452"> 3. The list returned by  sort  is passed to another built-in function,  reverse.  This reverses the sorted list, producing the list  ("test", "my", "line", "is", "here").</p>
        <p id="p_06453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06453"> 4. Each element of the list returned by  reverse  is assigned, in turn, to the local scalar variable $word,  starting with  "test " and proceeding from there.</p>
        <p id="p_06454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06454"> Line 7 prints the current value stored in  $word.  Each time the  f oreach  loop iterates, a different value in the list is printed.</p>
        <p id="p_06455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06455"> NOTE</p>
        <p id="p_06456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06456"> The code fragment</p>
        <p id="p_06457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06457"> foreach $word (reverse sort split (/[\t ]+/, $line))</p>
        <p id="p_06458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06458"> shows why omitting parentheses when calling built-in functions can sometimes be useful. If all the parentheses are included, this becomes</p>
        <p id="p_06459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06459"> foreach $word (reverse(sort(split(/[\t ]+/, $line))))</p>
        <p id="p_06460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06460"> which is not as readable.</p>
        <p id="p_06461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06461"> The do Statement</p>
        <p id="p_06462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06462"> So far, all of the loops you've seen test the conditional expression before executing the loop. Perl enables you to write loops that always execute at least once using the  do  statement.</p>
        <p id="p_06463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06463"> The syntax for the  do  statement is</p>
        <p id="p_06464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06464"> do {</p>
        <p id="p_06465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06465"> statement block</p>
        <p id="p_06466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06466"> } while_or_until (condexpr);</p>
        <p id="p_06467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06467"> As in other conditional statements, such as the  if  statement and the  while  statement, statement_block  is a block of statements to be executed, and  condexpr  is a conditional expression.</p>
        <p id="p_06468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06468"> while_or_unt il  is either the  while  keyword or the  until  keyword. If you use  while, statement_block  loops while  condexpr  is true. For example:</p>
        <p id="p_06469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06469"> do {</p>
        <p id="p_06470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06470"> $line = &lt;STDIN&gt;;</p>
        <p id="p_06471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06471"> } while ($line ne</p>
        <p id="p_06472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06472"> This loops while $  line  is non-empty (in other words, while the program has not reached the end of file). If you use  until, statement_block  loops until  condexpr  is true. For example:</p>
        <p id="p_06473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06473"> do {</p>
        <p id="p_06474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06474"> $line = &lt;STDIN&gt;;</p>
        <p id="p_06475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06475"> } until ($line eq</p>
        <p id="p_06476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06476"> This reads from the standard input file until  $line  is empty (again, until end of file is reached).</p>
        <p id="p_06477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06477"> Listing  8.8  is a simple example of a program that uses a  do  statement.</p>
        <p id="p_06478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06478"> Type</p>
        <p id="p_06479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06479"> Listing 8.8.  A  simple example of a do statement.</p>
        <p id="p_06480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06480"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06481"> 2  :</p>
        <p id="p_06482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06482"> 3: $count = 1;</p>
        <p id="p_06483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06483"> 4: do {</p>
        <p id="p_06484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06484"> 5: print ("$count\n");</p>
        <p id="p_06485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06485"> 6 :</p>
        <p id="p_06486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06486"> $count++;</p>
        <p id="p_06487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06487"> 7: } until ($count &gt; 5);</p>
        <p id="p_06488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06488"> Output</p>
        <p id="p_06489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06489"> $ program8_8 1 2</p>
        <p id="p_06490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06490"> 3</p>
        <p id="p_06491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06491"> 4</p>
        <p id="p_06492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06492"> 5 $</p>
        <p id="p_06493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06493"> Analysis</p>
        <p id="p_06494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06494"> Lines variable $  count</p>
        <p id="p_06495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06495"> 4-7 contain the  do  statement, which loops five times. Line 7 tests whether the counting is greater than 5.</p>
        <p id="p_06496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06496"> NOTE</p>
        <p id="p_06497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06497"> The  do  statement can also be used to call subroutines. See  Day 9 , "Using Subroutines," for more information.</p>
        <p id="p_06498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06498"> Exiting a Loop Using the last Statement</p>
        <p id="p_06499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06499"> Normally, you exit a loop by testing the conditional expression that is part of the loop. For example, if a loop is defined by the  while  statement, as in the following, the program exits the loop when the conditional expression at the top of the loop, $  count  &lt;=  1 0 ,  is false:</p>
        <p id="p_06500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06500"> while ($count &lt;= 10) {</p>
        <p id="p_06501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06501"> # statements go here</p>
        <p id="p_06502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06502"> }</p>
        <p id="p_06503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06503"> In the preceding case, the program can exit the loop only after executing all of the statements in it. Perl enables you to define an exit point anywhere in the loop using a special  last  statement.</p>
        <p id="p_06504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06504"> The syntax for the  last  statement is simple:</p>
        <p id="p_06505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06505"> last;</p>
        <p id="p_06506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06506"> To see how the  last  statement works, take a look at Listing 8.9, which adds a list of numbers supplied by means of the standard input file.</p>
        <p id="p_06507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06507"> Type</p>
        <p id="p_06508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06508"> Listing 8.9. A program that exits using the last statement.</p>
        <p id="p_06509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06509"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06510"> 2  :</p>
        <p id="p_06511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06511"> 3: $total = 0;</p>
        <p id="p_06512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06512"> 4: while (1) {</p>
        <p id="p_06513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06513"> 5: $line = &lt;STDIN&gt;;</p>
        <p id="p_06514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06514"> 6: if ($line eq "") {</p>
        <p id="p_06515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06515"> 7 : last;</p>
        <p id="p_06516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06516"> 8: }</p>
        <p id="p_06517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06517"> 9 : chop ($line);</p>
        <p id="p_06518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06518"> 10: gnumbers = split (/ [\t  ]+/,  $line);</p>
        <p id="p_06519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06519"> 11: foreach $number (gnumbers) {</p>
        <p id="p_06520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06520"> 12: if ($number =~ / [ A 0  —  9]/)  {</p>
        <p id="p_06521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06521"> 13: print STDERR ("$number is not a number\n");</p>
        <p id="p_06522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06522"> 14: }</p>
        <p id="p_06523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06523"> 15 :</p>
        <p id="p_06524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06524"> 16: }</p>
        <p id="p_06525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06525"> $total += $number;</p>
        <p id="p_06526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06526"> }</p>
        <p id="p_06527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06527"> 17 :</p>
        <p id="p_06528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06528"> 18: print ("The total is $total.\n");</p>
        <p id="p_06529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06529"> Output</p>
        <p id="p_06530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06530"> $ program8_9 4 5 7 2 11 6 A D</p>
        <p id="p_06531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06531"> The total is 35. $</p>
        <p id="p_06532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06532"> Analysis</p>
        <p id="p_06533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06533"> The loop that reads and adds numbers starts on line 4. The conditional expression at the top of this loop is the number 1. Because this is a nonzero number, this conditional expression always evaluates to true. Normally, this means that the  while  statement loops forever; however, because this program contains a  last  statement, the loop eventually terminates.</p>
        <p id="p_06534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06534"> Line 6 checks whether the program has reached the end of the standard input file. To do this, it checks whether the line read from the standard input file, now stored in $  line,  is empty. (Recall that the Ctrl+D character, written here as  A D,  marks the standard input file as empty.)</p>
        <p id="p_06535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06535"> If the line is empty, line 7, the  last  statement, is executed. This statement tells the Perl interpreter to terminate executing the loop and to continue with the first statement after the loop, which is line 18.</p>
        <p id="p_06536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06536"> Lines 10-16 add the numbers on the input line to the total stored in the scalar variable  $total.  Line 10 breaks the line into individual numbers, and lines 11-16 add each number, in turn, to  $total.</p>
        <p id="p_06537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06537"> Line 12 checks whether each number actually consists of the digits 0-9. The pattern [  A 0-9  ] matches anything that is not a digit; if the program finds such a character, it flags the number as erroneous. (The program can produce empty words if leading or trailing spaces or tabs exist in the line; this is not a problem, because [ A   0 — 9  ] doesn't match an empty word.)</p>
        <p id="p_06538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06538"> NOTE</p>
        <p id="p_06539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06539"> You can use the  last  statement with a single-line conditional statement. For example,</p>
        <p id="p_06540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06540"> last if ($count == 5);</p>
        <p id="p_06541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06541"> terminates the loop if the value of $  count  is 5.</p>
        <p id="p_06542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06542"> Warning</p>
        <p id="p_06543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06543"> You cannot use the  last  statement inside the  do  statement. Although the  do  statement behaves like the other control structures, it is actually implemented differently.</p>
        <p id="p_06544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06544"> Using next to Start the Next Iteration of a Loop</p>
        <p id="p_06545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06545"> In Perl, the  last  statement terminates the execution of a loop. To terminate a particular iteration of a loop, use the  next  statement.</p>
        <p id="p_06546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06546"> Like  last,  the syntax for the  next  statement is simple:</p>
        <p id="p_06547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06547"> next;</p>
        <p id="p_06548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06548"> Listing 8.10 is an example that uses the  next  statement. It sums up the numbers from 1 to a user-specified upper limit and also produces a separate sum of the numbers divisible by 2.</p>
        <p id="p_06549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06549"> Listing 8.10. A program that sums the numbers from 1 to a specified number and also sums the even numbers.</p>
        <p id="p_06550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06550"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06551"> 2  :</p>
        <p id="p_06552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06552"> 3: print ("Enter the last number in the sum:\n");</p>
        <p id="p_06553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06553"> 4 :</p>
        <p id="p_06554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06554"> $limit</p>
        <p id="p_06555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06555"> &lt;STDIN&gt;;</p>
        <p id="p_06556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06556"> $ program8_10</p>
        <p id="p_06557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06557"> Enter the last number in the sum:</p>
        <p id="p_06558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06558"> 7</p>
        <p id="p_06559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06559"> The sum of the numbers 1 to 7 is 28 The sum of the even numbers is 12 $</p>
        <p id="p_06560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06560"> l The loop in lines 8-15 adds the numbers together. The start of the  for  statement in line 8 loops five times; the counter variable, $  count,  is assigned the values 1, 2, 3, 4, and 5 in successive iterations.</p>
        <p id="p_06561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06561"> Line 9 adds to the total of all the numbers. This statement is always executed.</p>
        <p id="p_06562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06562"> Line 10 tests whether the current number-the current value of  $count-is  even or odd. If $  count is  even, the conditional expression</p>
        <p id="p_06563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06563"> $count % 2 == 1</p>
        <p id="p_06564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06564"> is false, and program execution continues with line 14. If the current value of $  count  is odd, the Perl interpreter executes line 12, the  next  statement. This statement tells the Perl in-terpreter to start the next iteration of the loop.</p>
        <p id="p_06565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06565"> Note that the loop iterator in the  for  statement,  $count++,  is still executed, even though the  next statement skips over part of the loop. This ensures that the program does not go into an infinite loop.</p>
        <p id="p_06566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06566"> Because the  next  statement is executed when the value of $  count  is odd, line 14 is skipped in this case. This means that the value of $  count  is added only when it is even.</p>
        <p id="p_06567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06567"> Warning</p>
        <p id="p_06568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06568"> Be careful when you use  next  in a  while  or  until  loop. The following example goes into an infinite loop:</p>
        <p id="p_06569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06569"> $count = 0;</p>
        <p id="p_06570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06570"> while ($count &lt;= 10) {</p>
        <p id="p_06571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06571"> if ($count == 5) {</p>
        <p id="p_06572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06572"> next;</p>
        <p id="p_06573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06573"> }</p>
        <p id="p_06574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06574"> $count++;</p>
        <p id="p_06575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06575"> }</p>
        <p id="p_06576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06576"> When $  count  is  5,  the program tells Perl to start the next iteration of the loop. However, the value of $  count  is not changed, which means that the expression $  count == 5  is still true.</p>
        <p id="p_06577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06577"> To get rid of this problem, you need to increment $  count  before using  next,  as in the following:</p>
        <p id="p_06578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06578"> $count = 0;</p>
        <p id="p_06579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06579"> while ($count &lt;= 10) {</p>
        <p id="p_06580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06580"> if ($count == 5) {</p>
        <p id="p_06581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06581"> $count++; next;</p>
        <p id="p_06582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06582"> }</p>
        <p id="p_06583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06583"> $count++;</p>
        <p id="p_06584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06584"> }</p>
        <p id="p_06585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06585"> This, by the way, is why many programming purists dislike statements such as  next  and last-it's too easy to lose track of where you are and what needs to be updated.</p>
        <p id="p_06586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06586"> The  next  statement enables you to check for and ignore unusual conditions when reading input. For example, Listing 8.11 counts the number of words in the input read from the standard input file. It uses the next  statement to skip blank lines.</p>
        <p id="p_06587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06587"> Listing 8.11.  A  word-counting program that uses the next statement.</p>
        <p id="p_06588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06588"> Output</p>
        <p id="p_06589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06589"> $ program8_ll</p>
        <p id="p_06590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06590"> Here is my test input.</p>
        <p id="p_06591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06591"> It contains some words.</p>
        <p id="p_06592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06592"> A D</p>
        <p id="p_06593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06593"> The total number of words is 9 $</p>
        <p id="p_06594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06594"> Analysis</p>
        <p id="p_06595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06595"> After line 4 has read a line of input and checked that it is not empty (which means that the end of file has not been reached), the program then gets rid of leading spaces and tabs (line 5) and trailing spaces, tabs, and the trailing newline (line 6). If a line is blank, lines 5 and 6 turn it into the empty string, for which line 7 tests.</p>
        <p id="p_06596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06596"> Line 7 contains the  next  statement as part of a single-line conditional statement. If the line is now empty, the  next  statement tells the program to go to the beginning of the loop and read in the next line of input.</p>
        <p id="p_06597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06597"> Warning</p>
        <p id="p_06598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06598"> You cannot use the  next  statement inside the  do  statement. Although the  do  statement behaves like the other control structures, it is actually implemented differently.</p>
        <p id="p_06599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06599"> The redo Statement</p>
        <p id="p_06600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06600"> Perl enables you to tell the Perl interpreter to restart an iteration of a loop using the  redo  statement. Like  last  and  next,  the syntax for the  redo  statement is simple:</p>
        <p id="p_06601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06601"> redo;</p>
        <p id="p_06602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06602"> For an example, look at Listing 8.12, which counts the number of words in three non-blank input lines.</p>
        <p id="p_06603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06603"> Type</p>
        <p id="p_06604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06604"> Listing 8.12. A word-counting program that uses the redo statement.</p>
        <p id="p_06605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06605"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06606"> Output</p>
        <p id="p_06607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06607"> $ program8_12 Here is my test input.</p>
        <p id="p_06608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06608"> It contains some words.</p>
        <p id="p_06609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06609"> A D</p>
        <p id="p_06610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06610"> The total number of words is 9</p>
        <p id="p_06611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06611"> $</p>
        <p id="p_06612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06612"> Analysis</p>
        <p id="p_06613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06613"> Line 5 reads a line of input from the standard input file. If this line is empty, the conditional expression in line 6 is true, and the  last  statement exits the loop. (This ensures that the program behaves properly when there are less than three lines of input.)</p>
        <p id="p_06614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06614"> Line 7 removes the leading blanks and tabs from this line of input, and line 8 removes the trailing white space. If the resulting line is now empty, the line must originally have been blank. Because this program does not want to include a blank line as one of the three lines in which to count words, line 9 invokes the redo  statement, which tells the program to start this loop over. The program returns to line 4, the  for statement, but does not increment the value of $  count.</p>
        <p id="p_06615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06615"> Warning</p>
        <p id="p_06616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06616"> You cannot use the  redo  statement inside the  do  statement. Although the  do  statement behaves like the other control structures, it is actually implemented differently.</p>
        <p id="p_06617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06617"> Note that the  redo  statement is not recommended, because it is too easy to lose track of how many times a program goes through a loop. For example, in Listing  8.12,  a quick glance at the  for  statement in line  4 seems to indicate that the program only loops three times; however, the  redo  statement might change that.</p>
        <p id="p_06618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06618"> Listing 8.13 shows an alternative way to solve this problem.</p>
        <p id="p_06619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06619"> Type</p>
        <p id="p_06620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06620"> Listing 8.13. A program that counts the words in three non-blank lines of input without using the redo statement.</p>
        <p id="p_06621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06621"> 10 :</p>
        <p id="p_06622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06622"> $nonblanklines += 1;</p>
        <p id="p_06623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06623"> Swords = split(/[\t ]+/, $line);</p>
        <p id="p_06624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06624"> 11 :</p>
        <p id="p_06625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06625"> 12: $total += Swords;</p>
        <p id="p_06626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06626"> 13: }</p>
        <p id="p_06627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06627"> 14: last if ($nonblanklines == 3);</p>
        <p id="p_06628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06628"> 15: };</p>
        <p id="p_06629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06629"> 16: print ("The total number of words is $total\n");</p>
        <p id="p_06630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06630"> Output</p>
        <p id="p_06631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06631"> $ program8_13 Here is my test input.</p>
        <p id="p_06632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06632"> It contains some words.</p>
        <p id="p_06633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06633"> A D</p>
        <p id="p_06634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06634"> The total number of words is 9.</p>
        <p id="p_06635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06635"> $</p>
        <p id="p_06636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06636"> Analysis</p>
        <p id="p_06637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06637"> This program is identical to the previous one, but it is much easier to understand. It uses a more meaningful variable name-$nonblanklines-which implies that blank lines are a special case.</p>
        <p id="p_06638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06638"> As in Listing 8.12, if the line is a blank line, lines 7 and 8 turn it into an empty line by removing all white space. When this happens, the condition in line 10 fails, and  $nonblanklines  is not incremented.</p>
        <p id="p_06639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06639"> Using Labeled Blocks for Multilevel Jumps</p>
        <p id="p_06640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06640"> As you've seen, the  last, next,  and  redo  statements enable you to exit a loop from anywhere inside its statement block, as follows:</p>
        <p id="p_06641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06641"> while (1) {</p>
        <p id="p_06642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06642"> $line</p>
        <p id="p_06643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06643"> &lt;STDIN&gt;;</p>
        <p id="p_06644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06644"> last if ($line eq</p>
        <p id="p_06645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06645"> }</p>
        <p id="p_06646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06646"> If the loop is inside another loop, the  last, next,  and  redo  statements quit the inner loop only; for example:</p>
        <p id="p_06647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06647"> while ($linel = &lt;FILE1&gt;) {</p>
        <p id="p_06648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06648"> while ($line2 = &lt;FILE2&gt;) {</p>
        <p id="p_06649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06649"> last if ($line2 eq "") {</p>
        <p id="p_06650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06650"> Here, the  last  statement only quits the inner  while  loop. The outer  while  loop, which reads from the file represented by  FI LEI,  continues executing.</p>
        <p id="p_06651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06651"> To quit from more than one loop at once, do the following:</p>
        <p id="p_06652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06652"> 1. Assign a label to the outer loop (the one from which you want to quit).</p>
        <p id="p_06653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06653"> 2.  When you use  last, next,  or  redo,  specify the label you just assigned.</p>
        <p id="p_06654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06654"> Listing  8.14  shows an example of a  last  statement that specifies a label.</p>
        <p id="p_06655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06655"> Listing  8.14. A  program that uses a label.</p>
        <p id="p_06656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06656"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06657"> 2  :</p>
        <p id="p_06658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06658"> 3: $total = 0;</p>
        <p id="p_06659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06659"> 4: $ firstcounter = 0;</p>
        <p id="p_06660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06660"> 5: DONE: while ($firstcounter &lt; 10) {</p>
        <p id="p_06661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06661"> 6 :</p>
        <p id="p_06662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06662"> $secondcounter = 1;</p>
        <p id="p_06663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06663"> while ($secondcounter &lt;= 10) {</p>
        <p id="p_06664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06664"> 7 :</p>
        <p id="p_06665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06665"> 8: $total++;</p>
        <p id="p_06666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06666"> 9: if ($firstcounter == 4 &amp;&amp; $secondcounter == 7)</p>
        <p id="p_06667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06667"> {</p>
        <p id="p_06668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06668"> 10: last DONE;</p>
        <p id="p_06669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06669"> 11 : }</p>
        <p id="p_06670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06670"> 12: $secondcounter++;</p>
        <p id="p_06671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06671"> 13: }</p>
        <p id="p_06672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06672"> 14: $firstcounter++;</p>
        <p id="p_06673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06673"> 15: }</p>
        <p id="p_06674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06674"> 16: print ( " $total\n");</p>
        <p id="p_06675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06675"> Output</p>
        <p id="p_06676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06676"> $ program8_14</p>
        <p id="p_06677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06677"> 47</p>
        <p id="p_06678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06678"> $</p>
        <p id="p_06679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06679"> Analysis</p>
        <p id="p_06680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06680"> The outer  while  loop starting in line 5 has the label  DONE   assigned to it. This label consists of an alphabetic character followed by one or more alphanumeric characters or underscores. The colon (:) character following the label indicates that the label is assigned to the following statement (in this case, the while  statement).</p>
        <p id="p_06681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06681"> When the conditional expression in line 9 is true, line 10 is executed. This statement tells the Perl interpreter to jump out of the loop labeled  DONE  and continue execution with the first statement after this loop. (By the way, this code fragment is just a rather complicated way of assigning 47 to  $t ot al.)</p>
        <p id="p_06682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06682"> Warning</p>
        <p id="p_06683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06683"> Make sure that you do not use a label which has another meaning in Perl. For example, the statement</p>
        <p id="p_06684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06684"> if: while ($x == 0) { # this is an error in</p>
        <p id="p_06685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06685"> Perl</p>
        <p id="p_06686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06686"> }</p>
        <p id="p_06687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06687"> is flagged as erroneous, because the Perl interpreter doesn't realize that the  i f  is not the start of an  i f  statement.</p>
        <p id="p_06688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06688"> You can avoid this problem by using uppercase letters for label names (such as  DONE).</p>
        <p id="p_06689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06689"> Note that labels can be identical to file variable names:</p>
        <p id="p_06690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06690"> FILEl: while ($line = &lt;FILE1&gt;) {</p>
        <p id="p_06691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06691"> }</p>
        <p id="p_06692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06692"> The Perl interpreter has no problem distinguishing the label FILEl from the file variable FILEl, because it is always possible to determine which is which from the context.</p>
        <p id="p_06693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06693"> Using next and redo with Labels</p>
        <p id="p_06694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06694"> You can use  next  and  redo  with labels as well, as shown in the following example:</p>
        <p id="p_06695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06695"> next LABEL; redo LABEL;</p>
        <p id="p_06696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06696"> This  next  statement indicates that the next iteration of the loop labeled  LABEL  is to be executed. This redo  statement indicates that the current iteration of the loop labeled  LABEL  is to be restarted.</p>
        <p id="p_06697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06697"> The continue Block</p>
        <p id="p_06698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06698"> In a  for  statement, the expression following the second semicolon is executed each time the end of the loop is reached or whenever a  next  statement is executed. For example:</p>
        <p id="p_06699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06699"> for ($i</p>
        <p id="p_06700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06700"> 1; $i &lt;= 10; $i++) {</p>
        <p id="p_06701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06701"> print  ("$i\n");</p>
        <p id="p_06702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06702"> }</p>
        <p id="p_06703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06703"> In this example, the expression  $i++,  which adds 1 to  $i,  is executed after the  print  function is called.</p>
        <p id="p_06704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06704"> Similarly, you can define statements that are to be executed whenever the end of a  while  loop or an until  loop is reached. To carry out this task, specify a  continue  statement after the loop.</p>
        <p id="p_06705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06705"> $i = l;</p>
        <p id="p_06706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06706"> while ($i &lt;= 10) {</p>
        <p id="p_06707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06707"> print ( "$i\n");</p>
        <p id="p_06708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06708"> }</p>
        <p id="p_06709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06709"> continue {</p>
        <p id="p_06710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06710"> $i++;</p>
        <p id="p_06711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06711"> }</p>
        <p id="p_06712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06712"> A continue  statement must be followed by a statement block, which is a collection of zero or more statements enclosed in brace characters. This statement block contains the state-ments to be executed at the bottom of each loop. In this example, the statement</p>
        <p id="p_06713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06713"> $i++;</p>
        <p id="p_06714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06714"> is executed after each call to  print.  This  while  loop therefore behaves like the  for  loop you've just seen.</p>
        <p id="p_06715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06715"> The  continue  statement is executed even if a pass through the loop is prematurely ended by a  next statement. It is not executed, however, if the loop is terminated by a  last  statement.</p>
        <p id="p_06716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06716"> TIP</p>
        <p id="p_06717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06717"> Usually, it is better to use a  for  statement than to use  continue with a  while  or an  until  statement, because the  for  statement is easier to follow.</p>
        <p id="p_06718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06718"> The goto Statement</p>
        <p id="p_06719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06719"> For the sake of completeness, Perl provides a  goto  statement.</p>
        <p id="p_06720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06720"> The syntax of the  goto  statement is</p>
        <p id="p_06721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06721"> goto label;</p>
        <p id="p_06722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06722"> label  is a label associated with a statement, as defined in the earlier section, "Using Labeled Blocks for Multilevel Jumps." The statement to which  label  is assigned cannot be in the middle of a  do  statement or inside a subroutine. (You'll learn about subroutines on  Day  9 .)</p>
        <p id="p_06723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06723"> Listing 8.15 is an example of a simple program that uses  goto.</p>
        <p id="p_06724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06724"> Type</p>
        <p id="p_06725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06725"> Listing 8.15. A program that uses the goto statement.</p>
        <p id="p_06726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06726"> Output</p>
        <p id="p_06727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06727"> $ program8_15</p>
        <p id="p_06728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06728"> Here is a line of input.</p>
        <p id="p_06729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06729"> Here is a line of input.</p>
        <p id="p_06730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06730"> A D</p>
        <p id="p_06731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06731"> $</p>
        <p id="p_06732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06732"> Analysis</p>
        <p id="p_06733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06733"> This program just reads and writes lines of input until the standard input file is exhausted. If the line read into $  line  is not empty, line 6 tells the Perl interpreter to jump back to the line to which the NEXTLINE  label is assigned, which is line  3.</p>
        <p id="p_06734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06734"> Note that lines 3-7 are equivalent to the following statement:</p>
        <p id="p_06735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06735"> print ($line) while ($line = &lt;STDIN&gt;);</p>
        <p id="p_06736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06736"> TIP</p>
        <p id="p_06737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06737"> There is almost never any need to use the  goto  statement. In fact, using  goto  often makes it more difficult to follow the logic of the program. For this reason, using  goto  is not recommended.</p>
        <p id="p_06738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06738"> Summary</p>
        <p id="p_06739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06739"> Today you learned about the more complex control structures supported in Perl.</p>
        <p id="p_06740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06740"> Single-line conditional statements enable you to put a conditional expression on the same line as the statement to be executed if the condition is satisfied. This enables you to write more concise programs.</p>
        <p id="p_06741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06741"> The  for  statement enables you to put the loop initializer, the loop iterator, and the conditional expression together on the same line. This makes it more difficult to write code that goes into an infinite loop.</p>
        <p id="p_06742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06742"> The  f oreach  statement enables a program to loop based on the contents of a list. When the loop is first executed, the first element in the list is assigned to a local scalar variable that is only defined for the duration of the loop. Subsequent iterations of the loop assign subsequent elements of the list to this local scalar variable.</p>
        <p id="p_06743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06743"> The  do  statement enables you to write a loop that executes at least once. Its terminating conditional expression appears at the bottom of the loop, not the top.</p>
        <p id="p_06744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06744"> The  last  statement tells the Perl interpreter to exit the loop and continue execution with the first statement after the loop. The  next  statement tells the Perl interpreter to skip the rest of this iteration of a loop and start with the next one. The  redo  statement tells the Perl interpreter to restart this iteration of a loop,  last, next,  and  redo  cannot be used with the  do  statement.</p>
        <p id="p_06745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06745"> You can assign a label to a statement, which enables you to use  last, next,  and  redo  to exit or restart an outer loop from inside an inner loop.</p>
        <p id="p_06746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06746"> The  continue  statement enables you to define code to be executed each time a loop iterates.</p>
        <p id="p_06747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06747"> The  goto  statement enables you to jump to any labeled statement in your program.</p>
        <p id="p_06748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06748"> Q&amp;A</p>
        <p id="p_06749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06749"> Q: Which control structure is the best one to use as a loop?</p>
        <p id="p_06750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06750"> A: It depends on what you want to do.</p>
        <p id="p_06751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06751"> • The  f oreach  structure is the best way to perform operations on every element of a list.</p>
        <p id="p_06752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06752"> • The  for  statement is the best way to perform an operation a set number of times.</p>
        <p id="p_06753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06753"> • The  while  statement is the best way to perform a loop until a particular condition occurs.</p>
        <p id="p_06754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06754"> • The  do  statement is useful if you want to perform a loop at least once. (However, it is not as useful as the others, because you cannot use  last, next,  or  redo  with it.)</p>
        <p id="p_06755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06755"> Q: Why does Perl bother with the  next, last,  and  redo  statements, when the  if-elsif-else</p>
        <p id="p_06756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06756"> structure can do the job just as well?</p>
        <p id="p_06757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06757"> A: The  last  and  next  statements are ideal for loops that check for exceptional conditions. For</p>
        <p id="p_06758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06758"> example:</p>
        <p id="p_06759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06759"> for ($count = 1; $count &lt;= 3; $count++) {</p>
        <p id="p_06760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06760"> $line = &lt;STDIN&gt;; last if ($line eq $line =~  s/ A [\t ]+//;</p>
        <p id="p_06761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06761"> $line =~  s/[\t  ] +\n$//;</p>
        <p id="p_06762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06762"> @words = split (/ [\t  ]+/,  $line) ;</p>
        <p id="p_06763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06763"> $total += Swords;</p>
        <p id="p_06764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06764"> }</p>
        <p id="p_06765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06765"> If the  last  statement did not exist, the only way to implement this would be with another level of nesting and another condition in the  for  statement, as follows:</p>
        <p id="p_06766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06766"> for ($count = 1; $count &lt;= 3 &amp;&amp; $line ne $count++) {</p>
        <p id="p_06767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06767"> $line = &lt;STDIN&gt;; if ($line ne "") {</p>
        <p id="p_06768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06768"> $line =~  s/ A [\t ]+//;</p>
        <p id="p_06769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06769"> $line</p>
        <p id="p_06770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06770"> s/[\t  ]+\n$//;</p>
        <p id="p_06771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06771"> Swords = split(/[\t ]+/, $line); $total += Swords;</p>
        <p id="p_06772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06772"> }</p>
        <p id="p_06773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06773"> }</p>
        <p id="p_06774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06774"> If your program has to check for several exceptional conditions, you might need several levels of if  statements to handle them unless you use  next  or  last.</p>
        <p id="p_06775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06775"> On the other hand, the  redo  statement should be avoided whenever possible, because it is difficult to follow program logic when it is used.</p>
        <p id="p_06776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06776"> Q: Is the goto statement ever the best way to solve a problem?</p>
        <p id="p_06777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06777"> A: Almost never. Avoid using the  goto  statement if at all possible.</p>
        <p id="p_06778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06778"> Q: Why is the conditional expression last in single-line conditional statements?</p>
        <p id="p_06779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06779"> A: This is to avoid a problem found in the C programming language. In C, you don't need to put</p>
        <p id="p_06780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06780"> braces around the statement block in a conditional statement if the block consists of only one line. For example, the following is legal:</p>
        <p id="p_06781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06781"> if (x == 0)</p>
        <p id="p_06782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06782"> printf ("x is zero\n");</p>
        <p id="p_06783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06783"> With this syntax, it is easy to accidentally forget to add the braces when you add another statement to the statement block, as follows:</p>
        <p id="p_06784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06784"> if (x == 0)</p>
        <p id="p_06785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06785"> printf ("x is zero\n");</p>
        <p id="p_06786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06786"> printf ("this statement is always printed\n");</p>
        <p id="p_06787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06787"> If you glance at this code quickly, you might think that the second call to  print f  is executed only if  x  is 0. However, this code is really</p>
        <p id="p_06788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06788"> if (x == 0)</p>
        <p id="p_06789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06789"> printf ("x is zero\n");</p>
        <p id="p_06790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06790"> printf ("this statement is always printed\n");</p>
        <p id="p_06791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06791"> In Perl, this problem does not exist because the only way to write the first statement is</p>
        <p id="p_06792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06792"> print ("x is zero\n") if (x == 0);</p>
        <p id="p_06793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06793"> Q: Is a continue block executed if a redo statement restarts the loop?</p>
        <p id="p_06794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06794"> A: No. The  continue  block is executed only when an iteration of a loop is successfully completed</p>
        <p id="p_06795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06795"> (by reaching the bottom of a loop or a  next  statement).</p>
        <p id="p_06796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06796"> Workshop</p>
        <p id="p_06797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06797"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_06798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06798"> Quiz</p>
        <p id="p_06799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06799"> 1. How many times does the following loop iterate?</p>
        <p id="p_06800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06800"> for ($count = 0; $count &lt; 7; $count++) {</p>
        <p id="p_06801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06801"> print ( "$count\n");</p>
        <p id="p_06802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06802"> }</p>
        <p id="p_06803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06803"> 2. How many times does the following loop iterate?</p>
        <p id="p_06804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06804"> $count = 1; do {</p>
        <p id="p_06805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06805"> print ( "$count\n");</p>
        <p id="p_06806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06806"> } until ($count++ &gt; 10) ;</p>
        <p id="p_06807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06807"> 3. How many times does the following loop iterate?</p>
        <p id="p_06808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06808"> for ($count = 1; $count &lt;= 10; $count++) {</p>
        <p id="p_06809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06809"> last if ($count == 5);</p>
        <p id="p_06810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06810"> }</p>
        <p id="p_06811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06811"> 4. How many times does the following loop iterate?</p>
        <p id="p_06812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06812"> $restart = 0;</p>
        <p id="p_06813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06813"> for ($count = 1; $count &lt;= 5; $count++) {</p>
        <p id="p_06814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06814"> redo if ($restart++ == 1);</p>
        <p id="p_06815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06815"> }</p>
        <p id="p_06816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06816"> 5.  Write a single-line conditional statement that quits a loop if  $x  equals  done.</p>
        <p id="p_06817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06817"> 6.  Write a single-line conditional statement that restarts a loop if the first element of the list  @list  is</p>
        <p id="p_06818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06818"> 26 .</p>
        <p id="p_06819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06819"> 7.  Write a single-line conditional statement that goes to the next iteration of the loop labeled  LABEL  if $scalar  equals #.</p>
        <p id="p_06820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06820"> 8. Write a single-line conditional statement that prints the digits from 1 to 10. (Use a scalar variable, and assume that it has not been previously defined.)</p>
        <p id="p_06821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06821"> 9.  What does the  continue  statement do?</p>
        <p id="p_06822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06822"> Exercises</p>
        <p id="p_06823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06823"> 1. Write a program that uses the  do  statement to print the numbers from 1 to 10.</p>
        <p id="p_06824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06824"> 2. Write a program that uses the for statement to print the numbers from 1 to 10.</p>
        <p id="p_06825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06825"> 3.  Write a program that uses a loop to read and write five lines of input. Use the  last  statement to exit</p>
        <p id="p_06826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06826"> the loop if there are less than five lines to read.</p>
        <p id="p_06827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06827"> 4. Write a program that loops through the numbers 1 to 20, printing the even-numbered values. Use the next  statement to skip over the odd-numbered values.</p>
        <p id="p_06828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06828"> 5. Write a program that uses the  f oreach  statement to check each word in the standard input file. Print the line numbers of all occurrences of the word  the  (in uppercase, lowercase, or mixed case).</p>
        <p id="p_06829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06829"> 6. Write a program that uses a  while  loop and a  continue  statement to print the integers from 10 down to 1.</p>
        <p id="p_06830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06830"> 7.  BUG BUSTER:  What is wrong with the following code?</p>
        <p id="p_06831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06831"> $count = 1; do {</p>
        <p id="p_06832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06832"> print ( "$count\n"); last if ($count == 10);</p>
        <p id="p_06833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06833"> $count++;</p>
        <p id="p_06834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06834"> } while (1);</p>
        <p id="p_06835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06835"> Chapter 9</p>
        <p id="p_06836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06836"> Using Subroutines</p>
        <p id="p_06837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06837"> CONTENTS</p>
        <p id="p_06838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06838"> •  What Is a Subroutine?</p>
        <p id="p_06839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06839"> •  Defining and Invoking a Subroutine</p>
        <p id="p_06840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06840"> o  Forward References to Subroutines</p>
        <p id="p_06841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06841"> •  Returning a Value from a Subroutine</p>
        <p id="p_06842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06842"> o  Return Values and Conditional Expressions</p>
        <p id="p_06843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06843"> •  The  return  Statement</p>
        <p id="p_06844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06844"> •  Using Local Variables in Subroutines</p>
        <p id="p_06845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06845"> o  Initializing Local Variables</p>
        <p id="p_06846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06846"> •  Passing Values to a Subroutine</p>
        <p id="p_06847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06847"> o  Passing a List to a Subroutine</p>
        <p id="p_06848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06848"> •  Calling Subroutines from Other Subroutines</p>
        <p id="p_06849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06849"> •  Recursive Subroutines</p>
        <p id="p_06850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06850"> •  Passing Arrays by Name Using Aliases</p>
        <p id="p_06851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06851"> •  Using the  do  Statement with Subroutines</p>
        <p id="p_06852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06852"> •  Specifying the Sort Order</p>
        <p id="p_06853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06853"> •  Predefined Subroutines</p>
        <p id="p_06854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06854"> o  Creating Startup Code Using  BEGIN</p>
        <p id="p_06855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06855"> o  Creating Termination Code Using  END o  Handling Non-Existent Subroutines Using  AUTOLOAD</p>
        <p id="p_06856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06856"> •  Summary</p>
        <p id="p_06857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06857"> •  Q&amp;A</p>
        <p id="p_06858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06858"> •  Workshop</p>
        <p id="p_06859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06859"> o  Quiz o  Exercises</p>
        <p id="p_06860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06860"> Today's lesson shows you how to use subroutines to divide your program into smaller, more manageable modules. Today, you learn about the following:</p>
        <p id="p_06861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06861"> • What a subroutine is</p>
        <p id="p_06862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06862"> • How to define subroutines</p>
        <p id="p_06863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06863"> • How to invoke subroutines</p>
        <p id="p_06864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06864"> • How to return a value from a subroutine</p>
        <p id="p_06865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06865"> • How to use the return statement</p>
        <p id="p_06866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06866"> • How to use local variables in subroutines</p>
        <p id="p_06867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06867"> • How to pass arguments to subroutines</p>
        <p id="p_06868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06868"> • How to call subroutines from other subroutines</p>
        <p id="p_06869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06869"> • The meaning of recursive subroutines</p>
        <p id="p_06870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06870"> • How to pass arrays by name in subroutines using aliasing</p>
        <p id="p_06871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06871"> • How to use the do statement with subroutines</p>
        <p id="p_06872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06872"> • How to use subroutines to change the sort order used by  sort</p>
        <p id="p_06873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06873"> • How to provide startup and termination code using  BEGIN  and  END</p>
        <p id="p_06874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06874"> • How to  use AUTOLOAD</p>
        <p id="p_06875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06875"> What Is a Subroutine?</p>
        <p id="p_06876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06876"> In Perl, a  subroutine  is a separate body of code designed to perform a particular task. A Perl program executes this body of code by calling or invoking the subroutine; the act of invoking a subroutine is called a  subroutine invocation.</p>
        <p id="p_06877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06877"> Subroutines serve two useful purposes:</p>
        <p id="p_06878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06878"> • They break down your program into smaller parts, making it easier to read and understand.</p>
        <p id="p_06879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06879"> • They enable you to use one piece of code to perform the same task multiple times, eliminating needless duplication.</p>
        <p id="p_06880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06880"> Defining and Invoking a Subroutine</p>
        <p id="p_06881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06881"> Listing 9.1 shows how a subroutine works. This program calls a subroutine that reads a line from the standard input file and breaks it into numbers. The program then adds the numbers together.</p>
        <p id="p_06882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06881"/>
        <p id="p_06883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06883"> Listing 9.1. A program that uses a subroutine.</p>
        <p id="p_06884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06884"> 1:  #!/usr/local/bin/perl</p>
        <p id="p_06885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06885"> 2 :</p>
        <p id="p_06886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06886"> 3: $total  =  0;</p>
        <p id="p_06887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06887"> 4:  &amp;getnumbers;</p>
        <p id="p_06888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06888"> 5: foreach  $number (@numbers) {</p>
        <p id="p_06889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06889"> 6:</p>
        <p id="p_06890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06890"> $total  += $number;</p>
        <p id="p_06891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06891"> 7: }</p>
        <p id="p_06892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06892"> 8: print ("the total is $total\n");</p>
        <p id="p_06893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06893"> 9:</p>
        <p id="p_06894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06894"> 10: sub getnumbers {</p>
        <p id="p_06895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06895"> 11: $line = &lt;STDIN&gt;;</p>
        <p id="p_06896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06896"> 12: $line =~  s/ A \s+|\s*\n$//g;</p>
        <p id="p_06897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06897"> 13: @numbers = split  (/\s  +  /,  $line);</p>
        <p id="p_06898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06898"> 14: }</p>
        <p id="p_06899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06899"> OuTPUt</p>
        <p id="p_06900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06900"> $ program9_l 11 8 16 4 the total is 39 $</p>
        <p id="p_06901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06901"> Lines 10-14 are an example of a subroutine. The keyword  sub  tells the Perl interpreter that this is a subroutine definition. The  getnumbers  immediately following  sub  is the name of the subroutine; the Perl program uses this name when invoking the subroutine.</p>
        <p id="p_06902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06902"> The program starts execution in the normal way, beginning with line 3. Line 4 invokes the subroutine getnumbers;  the &amp; character tells the Perl interpreter that the following name is the name of a subroutine. (This ensures that the Perl interpreter does not confuse subroutine names with the names of scalar or array variables.)</p>
        <p id="p_06903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06903"> The Perl interpreter executes line 4 by jumping to the first executable statement inside the subroutine, which is line 11. The interpreter then executes lines 11-13.</p>
        <p id="p_06904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06904"> Analysis</p>
        <p id="p_06905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06905"> Lines 11-13 create the array  @numbers  as follows:</p>
        <p id="p_06906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06906"> • Line 11 reads a line of input from the standard input file.</p>
        <p id="p_06907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06907"> • Line 12 removes the leading and trailing white space (including the trailing newline) from the input line.</p>
        <p id="p_06908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06908"> • Line 13 then breaks the input line into numbers and assigns the resulting list of numbers to</p>
        <p id="p_06909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06909"> @number s.</p>
        <p id="p_06910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06910"> After line 13 is finished, the Perl interpreter jumps back to the main program and executes the line immediately following the subroutine call, which is line 5.</p>
        <p id="p_06911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06911"> Lines 5-7 add the numbers together by using the  for each  statement to loop through the list stored in @numbers.  (Note that this program does not check whether a particular element of @ numbers actually consists of digits. Because character strings that are not digits are converted to 0 in expressions, this isn't a significant problem.)</p>
        <p id="p_06912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06912"> The syntax for a subroutine definition is</p>
        <p id="p_06913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06913"> sub subname {</p>
        <p id="p_06914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06914"> statement block</p>
        <p id="p_06915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06915"> }</p>
        <p id="p_06916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06916"> subname  is a placeholder for the name of the subroutine. Like all Perl names,  subname  consists of an alphabetic character followed by one or more letters, digits, or underscores.</p>
        <p id="p_06917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06917"> statement_block  is the body of the subroutine and consists of one or more Perl statements. Any statement that can appear in the main part of a Perl program can appear in a subroutine.</p>
        <p id="p_06918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06918"> NOTE</p>
        <p id="p_06919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06919"> The Perl interpreter never confuses a subroutine name with a scalar variable name or any other name, because it can always tell from the context which name you are referring to. This means that you can have a subroutine and a scalar variable with the same name. For example:</p>
        <p id="p_06920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06920"> $word = 0;</p>
        <p id="p_06921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06921"> &amp;word;</p>
        <p id="p_06922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06922"> Here, when the Perl interpreter sees the &amp; character in the second statement, it realizes that the second statement is calling the subroutine named  word.</p>
        <p id="p_06923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06923"> Warning</p>
        <p id="p_06924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06924"> When you are defining names for your subroutines, it's best not to use a name belonging to a built-in Perl function that you plan to use.</p>
        <p id="p_06925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06925"> For example, you could, if you want, define a subroutine named split.  The Perl interpreter can always distinguish an invocation of the subroutine  split  from an invocation of the library function  split,  because the name of the subroutine is preceded by an &amp; when it is invoked, as follows:</p>
        <p id="p_06926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06926"> @words = &amp;split(l, 2); # subroutine ©words = split  (/\s  +  /,  $line); # library function</p>
        <p id="p_06927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06927"> However, it's easy to leave off the &amp; by mistake (especially if you are used to programming in C, where subroutine calls do not start with an &amp;). To avoid such problems, use subroutine names that don't correspond to the names of library functions.</p>
        <p id="p_06928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06928"> Perl subroutines can appear anywhere in a program, even in the middle of a conditional statement. For example, Listing 9.2 is a perfectly legal Perl program.</p>
        <p id="p_06929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06929"> Listing 9.2. A program containing a subroutine in the middle of the main program.</p>
        <p id="p_06930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06930"> 1: #!/usr/local/bin/perl</p>
        <p id="p_06931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06931"> 2 :</p>
        <p id="p_06932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06932"> 3: while (1) {</p>
        <p id="p_06933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06933"> 4: &amp;readaline;</p>
        <p id="p_06934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06934"> 5 :</p>
        <p id="p_06935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06935"> last if ($line eq</p>
        <p id="p_06936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06936"> 6:</p>
        <p id="p_06937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06937"> sub readaline {</p>
        <p id="p_06938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06938"> 7: $line = &lt;STDIN&gt;;</p>
        <p id="p_06939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06939"> 8 : }</p>
        <p id="p_06940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06940"> 9 : print ($line);</p>
        <p id="p_06941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06941"> 10 : }</p>
        <p id="p_06942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06942"> 11: print ("done\n") ;</p>
        <p id="p_06943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06943"> $ program9_2</p>
        <p id="p_06944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06944"> Here is a line of input. Here is a line of input. A D</p>
        <p id="p_06945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06945"> done</p>
        <p id="p_06946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06946"> $</p>
        <p id="p_06947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06947"> Analysis</p>
        <p id="p_06948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06948"> This program just reads lines of input from the standard input file and writes them straight back out to the standard output file.</p>
        <p id="p_06949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06949"> Line 4 calls the subroutine readaline. When you examine this subroutine, which is contained in lines 6-8, you can see that it reads a line of input and assigns it to the scalar variable $ line.</p>
        <p id="p_06950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06950"> When readaline is finished, program execution continues with line 5. When line 5 is executed, the program skips over the subroutine definition and continues with line 9. The code inside the subroutine is never directly executed, even if it appears in the middle of a program; lines 6-8 can be executed only by a subroutine invocation, such as that found in line 4.</p>
        <p id="p_06951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06951"> TIP</p>
        <p id="p_06952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06952"> Although subroutines can appear anywhere in a program, it usually is best to put all your subroutines at either the beginning of the program or the end. Following this practice makes your programs easier to read.</p>
        <p id="p_06953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06953"> Forward References to Subroutines</p>
        <p id="p_06954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06954"> As you have seen, the Perl interpreter uses the &amp; character to indicate that a subroutine is being specified in a statement. In Perl 5, you do not need to supply an &amp; character when calling a subroutine if you have already defined the subroutine.</p>
        <p id="p_06955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06955"> sub readaline {</p>
        <p id="p_06956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06956"> $line = &lt;STDIN&gt;;</p>
        <p id="p_06957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06957"> }</p>
        <p id="p_06958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06958"> readaline;</p>
        <p id="p_06959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06959"> Because the Perl interpreter already knows that  readaline  is a subroutine, you don't need to specify the &amp; when calling it.</p>
        <p id="p_06960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06960"> If you prefer to list all your subroutines at the end of your program, you can still omit the &amp; character provided you supply a forward reference for your subroutine, as shown in the following:</p>
        <p id="p_06961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06961"> sub readaline; # forward reference</p>
        <p id="p_06962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06962"> readaline;</p>
        <p id="p_06963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06963"> sub readaline {</p>
        <p id="p_06964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06964"> $line = &lt;STDIN&gt;;</p>
        <p id="p_06965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06965"> }</p>
        <p id="p_06966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06966"> The forward reference tells the Perl interpreter that  readaline  is the name of a subroutine. This</p>
        <p id="p_06967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06967"> means that you no longer need to supply the &amp; when you call  readaline.</p>
        <p id="p_06968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06968"> Warning</p>
        <p id="p_06969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06969"> Occasionally, calling a subroutine without specifying the &amp; character might not behave the way you expect. If your program is behaving strangely, or you are not sure whether or not to use the &amp; character, supply the &amp; character with your call.</p>
        <p id="p_06970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06970"> Returning a Value from a Subroutine</p>
        <p id="p_06971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06971"> Take another look at the  getnumbers  subroutine from Listing 9.1.</p>
        <p id="p_06972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06972"> sub getnumbers {</p>
        <p id="p_06973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06973"> $line = &lt;STDIN&gt;;</p>
        <p id="p_06974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06974"> $line =~  s/ A \s+|\s*\n$//g; @numbers =  split(/\s+/,  $temp);</p>
        <p id="p_06975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06975"> Although this subroutine is useful, it suffers from one serious limitation: it overwrites any existing list stored in the array variable  @numbers  (as well as any value stored in  $line  or  $temp).  This overwriting can lead to problems. For example, consider the following:</p>
        <p id="p_06976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06976"> @numbers =  ("the", "a", "an");</p>
        <p id="p_06977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06977"> ^getnumbers;</p>
        <p id="p_06978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06978"> print  ("The  value of \@numbers is: @numbers\n");</p>
        <p id="p_06979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06979"> When the subroutine  getnumbers  is invoked, the value of  @numbers  is overwritten. If you just examine this portion of the program, it is not obvious that this is what is happening.</p>
        <p id="p_06980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06980"> To get around this problem, you can employ a useful property of subroutines in Perl: The value of the last expression evaluated by the subroutine is automatically considered to be the subroutine's  return value.</p>
        <p id="p_06981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06981"> For example, in the subroutine  getnumbers  from Listing 9.1, the last expression evaluated is</p>
        <p id="p_06982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06982"> @numbers =  split(/\s+/, $temp);</p>
        <p id="p_06983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06983"> The value of this expression is the list of numbers obtained by splitting the line of input. This means that this list of numbers is the return value for the subroutine.</p>
        <p id="p_06984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06984"> To see how to use a subroutine return value, look at Listing 9.3, which modifies the word-counting program to use the return value from the subroutine  getnumbers.</p>
        <p id="p_06985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06985"> Listing 9.3. A program that uses a subroutine return value.</p>
        <p id="p_06986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06986"> 1:  #!/usr/local/bin/perl</p>
        <p id="p_06987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06987"> 2 :</p>
        <p id="p_06988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06988"> 3: $total = 0;</p>
        <p id="p_06989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06989"> 4:  @numbers = &amp;getnumbers;</p>
        <p id="p_06990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06990"> 5: foreach  $number (@numbers) {</p>
        <p id="p_06991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06991"> 6:  $total  += $number;</p>
        <p id="p_06992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06992"> 7: }</p>
        <p id="p_06993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06993"> 8: print  ("the total is $total\n");</p>
        <p id="p_06994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06994"> 9:</p>
        <p id="p_06995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06995"> 10:  sub getnumbers {</p>
        <p id="p_06996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06996"> 11: $line = &lt;STDIN&gt;;</p>
        <p id="p_06997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06997"> 12: $line =~  s/ A \s+|\s*\n$//g;</p>
        <p id="p_06998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06998"> 13: split  (/\s  +  /,  $line);  # this is the return value</p>
        <p id="p_06999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_06999"> 14 :</p>
        <p id="p_07000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07000"> }</p>
        <p id="p_07001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07001"> OuTPUt</p>
        <p id="p_07002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07002"> $ program9_3 11 8 16 4 the total is 39 $</p>
        <p id="p_07003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07003"> Analysis</p>
        <p id="p_07004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07004"> Line 4, once again, calls the subroutine  get numbers.  As before, the array variable @ numbers  is assigned the list of numbers read from the standard input file; however, in this program, the assignment is in the main body of the program, not in the subroutine. This makes the program easier to read.</p>
        <p id="p_07005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07005"> The only other difference between this program and Listing  9.1  is that the call to  split  in line  13  no longer assigns anything to @ numbers.  In fact, it doesn't assign the list returned by  split  to any variable at all, because it does not need to. Line  13  is the last expression evaluated in  getnumbers, so it automatically becomes the return value from  getnumbers.  Therefore, when line  4  calls getnumbers,  the list returned by  split  is assigned to the array variable  @numbers.</p>
        <p id="p_07006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07006"> NOTE</p>
        <p id="p_07007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07007"> If the idea of evaluating an expression without assigning it confuses you, there’s nothing wrong with creating a variable inside the subroutine just for the purpose of containing the return value. For example:</p>
        <p id="p_07008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07008"> sub getnumbers {</p>
        <p id="p_07009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07009"> $line = &lt;STDIN&gt;;</p>
        <p id="p_07010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07010"> $line =~  s/ /s \s+ |\s*\n$//g;</p>
        <p id="p_07011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07011"> @retval =  split(/\s+/,  $temp); # the return value }</p>
        <p id="p_07012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07012"> Here, it is obvious that the return value is the contents of</p>
        <p id="p_07013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07013"> @retval.</p>
        <p id="p_07014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07014"> The only drawback to doing this is that assigning the list returned by  split  to  Qretval  is slightly less efficient. In larger</p>
        <p id="p_07015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07015"> programs, such efficiency costs are worth it, because subroutines become much more comprehensible.</p>
        <p id="p_07016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07016"> Using a special return variable also eliminates an entire class of errors, which you will see in "Return Values and Conditional Expressions," later today.</p>
        <p id="p_07017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07017"> You can use a return value of a subroutine any place an expression is expected. For example:</p>
        <p id="p_07018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07018"> foreach $number (&amp;getnumbers) {</p>
        <p id="p_07019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07019"> print ( "$number\n");</p>
        <p id="p_07020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07020"> }</p>
        <p id="p_07021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07021"> This  foreach  statement iterates on the list of numbers returned by  get numbers.  Each element of the list is assigned to  $number  in turn, which means that this loop prints all the numbers in the list, each on its own line.</p>
        <p id="p_07022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07022"> Listing 9.4 shows another example that uses the return value of a subroutine in an expression. This time, the return value is used as an array subscript.</p>
        <p id="p_07023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07023"> Type</p>
        <p id="p_07024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07024"> Listing 9.4. A program that uses a return value as an array subscript.</p>
        <p id="p_07025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07025"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07026"> 2 :</p>
        <p id="p_07027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07027"> 3: srand() ;</p>
        <p id="p_07028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07028"> 4: print ("Random number tester.\n") ;</p>
        <p id="p_07029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07029"> 5: for ($count = 1; $count &lt;= 100; $count++) {</p>
        <p id="p_07030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07030"> 6 :</p>
        <p id="p_07031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07031"> 7: }</p>
        <p id="p_07032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07032"> $randnum[&amp;intrand] += 1;</p>
        <p id="p_07033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07033"> 8: print ("Totals for the digits 0 through  9:\n");</p>
        <p id="p_07034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07034"> 9: print ("@randnum\n");</p>
        <p id="p_07035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07035"> 10  :</p>
        <p id="p_07036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07036"> 11: sub intrand {</p>
        <p id="p_07037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07037"> 12: $num = int(rand(10));</p>
        <p id="p_07038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07038"> 13: }</p>
        <p id="p_07039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07039"> $ progam9_4</p>
        <p id="p_07040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07040"> Random number tester.</p>
        <p id="p_07041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07041"> Totals for the digits 0 through 9: 10 9 11 10 8 8 12 11 9 12 $</p>
        <p id="p_07042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07042"> Analysis</p>
        <p id="p_07043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07043"> This program uses the following three built-in functions:</p>
        <p id="p_07044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07044"> The subroutine  intrand  first calls  rand  to get a random number greater than  0  and less than  10. The return value from  rand  is passed to  int  to remove the fractional portion of the number; this means, for example, that  4.77135  becomes  4.  This number becomes the return value returned by</p>
        <p id="p_07045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07045"> intrand.</p>
        <p id="p_07046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07046"> Line  6  calls  intrand.  The return value from  intrand,  an integer between  0  and  9,  serves as the subscript into the array variable  randnum.  If the return value from  intrand  is  7, $randnum [ 7 ] has its value increased by one.</p>
        <p id="p_07047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07047"> As a consequence, at any given time, the nth value of  @randnum  contains the number of occurrences of  n  as a random number.</p>
        <p id="p_07048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07048"> Line 9 prints out the number of occurrences of each of the 10 numbers. Each number should occur approximately the same number of times (although not necessarily exactly the same number of times).</p>
        <p id="p_07049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07049"> Return Values and Conditional Expressions</p>
        <p id="p_07050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07050"> Because the return value of a subroutine is always the last expression evaluated, the return value might not always be what you expect.</p>
        <p id="p_07051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07051"> Consider the simple program in Listing 9.5. This program, like the one in Listing 9.3, reads an input line, breaks it into numbers, and adds the numbers. This program, however, attempts to do all the work inside the subroutine  get_total.</p>
        <p id="p_07052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07052"> Listing 9.5. A program illustrating a potential problem with return values from subroutines.</p>
        <p id="p_07053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07053"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07054"> 2 :</p>
        <p id="p_07055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07055"> 3: $total = &amp;get_total;</p>
        <p id="p_07056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07056"> 4: print("The total is $total\n");</p>
        <p id="p_07057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07057"> 5 :</p>
        <p id="p_07058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07058"> 6: sub get_total {</p>
        <p id="p_07059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07059"> 7: $value = 0;</p>
        <p id="p_07060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07060"> 8: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_07061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07061"> 9: $inputline =~  s/ A \s+|\s*\n$//g;</p>
        <p id="p_07062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07062"> 10: @subwords =  split(/\s+/,  $inputline);</p>
        <p id="p_07063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07063"> 11: $index = 0;</p>
        <p id="p_07064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07064"> 12 :</p>
        <p id="p_07065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07065"> while ($subwords[$index] ne "") {</p>
        <p id="p_07066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07066"> $value += $subwords[$index++];</p>
        <p id="p_07067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07067"> 13 :</p>
        <p id="p_07068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07068"> 14: }</p>
        <p id="p_07069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07069"> 15: }</p>
        <p id="p_07070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07070"> Output</p>
        <p id="p_07071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07071"> $ program9_5 11 8 16 4 the total is $</p>
        <p id="p_07072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07072"> Analysis</p>
        <p id="p_07073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07073"> Clearly, this program is supposed to assign the contents the scalar variable $  total.  However, when line 4 tries to print the $ total  is actually the empty string. What has happened?</p>
        <p id="p_07074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07074"> of the scalar variable $  value  to total, you see that the value of</p>
        <p id="p_07075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07075"> The problem is in the subroutine  get_total.  In  get_total,  as in all other subroutines, the return value is the value of the last expression evaluated. However, in  get_total,  the last expression evaluated is not the last expression in the program.</p>
        <p id="p_07076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07076"> The last expression to be evaluated in  get_total  is the conditional expression in line  12,  which is</p>
        <p id="p_07077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07077"> $subwords[$index] ne</p>
        <p id="p_07078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07078"> The loop in lines 12-14 iterates until the value of this expression is  0.  When the value of this expression is  0,  the loop terminates and the subroutine terminates. This means that the value of the last expression evaluated in the subroutine is  0  and that the return value of the subroutine is  0.  Because  0 is treated as the null string by  print (0  and the null string are equivalent in Perl), line 4 prints the following, which isn't what the program is supposed to do:</p>
        <p id="p_07079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07079"> the total is</p>
        <p id="p_07080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07080"> Listing 9.6 shows how you can get around this problem.</p>
        <p id="p_07081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07081"> Type</p>
        <p id="p_07082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07082"> Listing 9.6. A program that corrects the problem that occurs in Listing 9.5.</p>
        <p id="p_07083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07083"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07084"> 2 :</p>
        <p id="p_07085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07085"> 3: $total = &amp;get_total;</p>
        <p id="p_07086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07086"> 4: print("The total is $total.\n");</p>
        <p id="p_07087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07087"> 5: sub get_total {</p>
        <p id="p_07088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07088"> 6: $value = 0;</p>
        <p id="p_07089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07089"> 7: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_07090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07090"> 8: $inputline =~ s/ A \s+|\s*\n$//g;</p>
        <p id="p_07091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07091"> 9: @subwords = split  (/\s  +  /,  $inputline);</p>
        <p id="p_07092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07092"> 10 : $index = 0;</p>
        <p id="p_07093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07093"> 11: while ($subwords[$index] ne "") {</p>
        <p id="p_07094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07094"> 12: $value += $subwords[$index++];</p>
        <p id="p_07095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07095"> 13: }</p>
        <p id="p_07096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07096"> 14: $retval = $value;</p>
        <p id="p_07097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07097"> 15: }</p>
        <p id="p_07098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07098"> OuTPUt</p>
        <p id="p_07099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07099"> $ program9_6 11 8 16 4 the total is 39. $</p>
        <p id="p_07100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07100"> Analysis</p>
        <p id="p_07101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07101"> This program is identical to Listing 9.5 except for one difference: line 15 has been added. This line assigns the total stored in  $value  to the scalar variable  $retval.</p>
        <p id="p_07102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07102"> Line 15 ensures that the value of the last expression evaluated in the subroutine  get_total  is, in fact, the total which is supposed to become the return value. This means that line 3 now assigns the correct total to $  total,  which in turn means that line  4  now prints the correct result.</p>
        <p id="p_07103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07103"> Note that you don't really need to assign to  $retval.  The subroutine  get_total  can just as easily be the following:</p>
        <p id="p_07104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07104"> sub get_total {</p>
        <p id="p_07105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07105"> $value = 0;</p>
        <p id="p_07106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07106"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_07107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07107"> $inputline =~  s/ A \s+ |\s*\n$//g; Qsubwords =  split(/\s+/,  $inputline); $index = 0;</p>
        <p id="p_07108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07108"> while ($subwords[$index] ne "") {</p>
        <p id="p_07109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07109"> $value += $subwords[$index++];</p>
        <p id="p_07110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07110"> }</p>
        <p id="p_07111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07111"> $value;</p>
        <p id="p_07112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07112"> }</p>
        <p id="p_07113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07113"> Here, the final expression evaluated by the subroutine is simply $  value.  The value of this expression is the current value stored in $  value,  which is the sum of the numbers in the line.</p>
        <p id="p_07114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07114"> TIP</p>
        <p id="p_07115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07115"> Subroutines, such as  get_total  in Listing  9.6,  which assign their return value at the very end are known as  single-exit modules.</p>
        <p id="p_07116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07116"> Single-exit modules avoid problems like those you saw in Listing 9.5, and they usually are much easier to read. For these reasons, it is a good idea to assign to the return value at the very end of the subroutine, unless there are overwhelming reasons not to do so.</p>
        <p id="p_07117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07117"> The return Statement</p>
        <p id="p_07118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07118"> Another way to ensure that the return value from a subroutine is the value you want is to use the return  statement.</p>
        <p id="p_07119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07119"> The syntax for the  return  statement is</p>
        <p id="p_07120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07120"> return (retval);</p>
        <p id="p_07121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07121"> retval  is the value you want your subroutine to return. It can be either a scalar value (including the result of an expression) or a list.</p>
        <p id="p_07122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07122"> Listing  9.7  provides an example of the use of the  return  statement.</p>
        <p id="p_07123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07123"> Listing 9.7. A program that uses the return statement.</p>
        <p id="p_07124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07124"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07125"> 2 :</p>
        <p id="p_07126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07126"> 3: $total = &amp;get_total;</p>
        <p id="p_07127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07127"> 4: if ($total eq "error") {</p>
        <p id="p_07128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07128"> 5 :</p>
        <p id="p_07129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07129"> print ("No input supplied.\n");</p>
        <p id="p_07130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07130"> 6: } else {</p>
        <p id="p_07131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07131"> 7: print("The total is $total.\n");</p>
        <p id="p_07132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07132"> 8 : }</p>
        <p id="p_07133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07133"> 9:</p>
        <p id="p_07134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07134"> 10: sub get_total {</p>
        <p id="p_07135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07135"> 11: $value = 0;</p>
        <p id="p_07136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07136"> 12: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_07137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07137"> 13: $inputline =~  s/ A \s+|\s*\n$//g;</p>
        <p id="p_07138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07138"> 14:  if  ($inputline eq "") {</p>
        <p id="p_07139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07139"> 15: return  ("error");</p>
        <p id="p_07140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07140"> 16: }</p>
        <p id="p_07141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07141"> 17: @subwords = split  (/\s  +  /,  $inputline);</p>
        <p id="p_07142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07142"> 18 : $index = 0;</p>
        <p id="p_07143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07143"> 19: while ($subwords[$index] ne "") {</p>
        <p id="p_07144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07144"> 20: $value += $subwords[$index++];</p>
        <p id="p_07145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07145"> 21 : }</p>
        <p id="p_07146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07146"> 22: $retval = $value;</p>
        <p id="p_07147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07147"> 23: }</p>
        <p id="p_07148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07148"> Output</p>
        <p id="p_07149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07149"> $ program9_7 A D</p>
        <p id="p_07150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07150"> No input supplied.</p>
        <p id="p_07151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07151"> $</p>
        <p id="p_07152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07152"> Analysis</p>
        <p id="p_07153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07153"> This program is similar to the one in Listing 9.6. The only difference is that this program checks whether an input line exists.</p>
        <p id="p_07154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07154"> If the input line does not exist, the conditional expression in line 14 becomes true, and line 15 is executed. Line 15 exits the subroutine with the return value  error;  this means that  error  is assigned to $  total  in line  3.</p>
        <p id="p_07155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07155"> This program shows why allowing scalar variables to store either numbers or character strings is useful. When the subroutine  get_total  detects the error, it can assign a value that is not an integer to $  total,  which makes it easier to determine that something has gone wrong. Other programming languages, which only enable you to assign either a number or a character string to a particular variable, do not offer this flexibility.</p>
        <p id="p_07156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07156"> Using Local Variables in Subroutines</p>
        <p id="p_07157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07157"> The subroutine  get_total  in Listing 9.7 defines several variables that are used only inside the subroutine: the array variable @  subwords,  and the four scalar variables $  input line, $ value, $index,  and  $retval.</p>
        <p id="p_07158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07158"> If you know for certain that these variables are going to be used only inside the subroutine, you can tell Perl to define these variables as  local variables.</p>
        <p id="p_07159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07159"> In Perl 5, there are two statements used to define local variables:</p>
        <p id="p_07160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07160"> • The  my  statement, which defines variables that exist only inside a subroutine.</p>
        <p id="p_07161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07161"> • The  local  statement, which defines variables that do not exist inside the main program, but inside the subroutine and any subroutines called by the subroutine. (Calling subroutines from other subroutines is discussed later today.)</p>
        <p id="p_07162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07162"> In Perl  4,  the  my  statement is not defined, so you must use  local  to define a variable that is not known to the main program.</p>
        <p id="p_07163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07163"> Listing  9.8  shows how you can use  my  to define a variable that exists only inside a subroutine.</p>
        <p id="p_07164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07164"> NOTE</p>
        <p id="p_07165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07165"> If you are using Perl  4,  replace  my  with  local  in all the remaining examples in this chapter. For example, in Listing 9.8, replace  my  with  local  in lines  13  and  14,  which produces</p>
        <p id="p_07166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07166"> local ($total, $inputline, @subwords); local ($index, $retval);</p>
        <p id="p_07167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07167"> In Perl,  my  and  local  behave identically and use the same syntax. The only difference between them is that variables created using  my  are not known outside the subroutine.</p>
        <p id="p_07168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07168"> Listing 9.8. A program that uses local variables.</p>
        <p id="p_07169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07169"> 1: 2 :</p>
        <p id="p_07170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07170"> 3 :</p>
        <p id="p_07171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07171"> 4 :</p>
        <p id="p_07172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07172"> 5 : 6 :</p>
        <p id="p_07173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07173"> 7 :</p>
        <p id="p_07174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07174"> 8  :</p>
        <p id="p_07175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07175"> #!/usr/local/bin/perl</p>
        <p id="p_07176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07176"> $total = 0; while (1) {</p>
        <p id="p_07177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07177"> $linetotal = &amp;get_total;</p>
        <p id="p_07178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07178"> last if ($linetotal eq "done");</p>
        <p id="p_07179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07179"> print ("Total for this line: $linetotal\n");</p>
        <p id="p_07180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07180"> $total += $linetotal;</p>
        <p id="p_07181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07181"> 9: }</p>
        <p id="p_07182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07182"> 10: print ("Total for all lines: $total\n");</p>
        <p id="p_07183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07183"> 11 :</p>
        <p id="p_07184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07184"> 12: sub get_total {</p>
        <p id="p_07185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07185"> 13: my ($total, $inputline, @subwords);</p>
        <p id="p_07186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07186"> 14 :</p>
        <p id="p_07187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07187"> my ($index, $retval);</p>
        <p id="p_07188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07188"> Output</p>
        <p id="p_07189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07189"> $ program9_8 11 8 16 4</p>
        <p id="p_07190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07190"> Total for this line: 39 7 20 6 1</p>
        <p id="p_07191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07191"> Total for this line: 34 A D</p>
        <p id="p_07192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07192"> Total for all lines: 73</p>
        <p id="p_07193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07193"> $</p>
        <p id="p_07194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07194"> Analyse</p>
        <p id="p_07195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07195"> This program uses two copies of the scalar variable $  total.  One copy of $  total  is defined in the main program and keeps a running total of all of the numbers in all of the lines.</p>
        <p id="p_07196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07196"> The scalar variable $  total  is also defined in the subroutine  get_total;  in this subroutine,</p>
        <p id="p_07197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07197"> $ total  refers to the total for a particular line, and line 13 defines it as a local variable. Because this copy of $  total  is only defined inside the subroutine, the copy of $  total  defined in the main program is not affected by line 15 (which assigns  0  to $  total).</p>
        <p id="p_07198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07198"> Warning</p>
        <p id="p_07199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07199"> Because a local variable is not known outside the subroutine, the local variable is destroyed when the subroutine is completed. If the subroutine is called again, a new copy of the local variable is defined.</p>
        <p id="p_07200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07200"> This means that the following code does not work:</p>
        <p id="p_07201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07201"> sub subroutine_count { my($number_of_calls);</p>
        <p id="p_07202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07202"> $number_of_calls += 1;</p>
        <p id="p_07203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07203"> }</p>
        <p id="p_07204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07204"> This subroutine does not return the number of times subrout ine_count  has been called. Because a new copy of $number_of_calls  is defined every time the subroutine is called,  $number_of_calls  is always assigned the value 1.</p>
        <p id="p_07205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07205"> Local variables can appear anywhere in a program, provided they are defined before they are used. It is good programming practice to put all your local definitions at the beginning of your subroutine.</p>
        <p id="p_07206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07206"> Initializing Local Variables</p>
        <p id="p_07207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07207"> If you want, you can assign a value to a local variable when you declare it. For example:</p>
        <p id="p_07208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07208"> sub my_sub {</p>
        <p id="p_07209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07209"> my($scalar) = 43;</p>
        <p id="p_07210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07210"> " a "</p>
        <p id="p_07211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07211"> r</p>
        <p id="p_07212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07212"> my(@array)</p>
        <p id="p_07213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07213"> ("here's",</p>
        <p id="p_07214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07214"> "list");</p>
        <p id="p_07215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07215"> # code goes here</p>
        <p id="p_07216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07216"> }</p>
        <p id="p_07217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07217"> Here, the local scalar variable  $scalaris  given an initial value of 43, and the local array variable @ array  is initialized to contain the list  ("here's", "a", "list").</p>
        <p id="p_07218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07218"> Passing Values to a Subroutine</p>
        <p id="p_07219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07219"> You can make your subroutines more flexible by allowing them to accept values passed from the main program; these values passed from the main program are known as  arguments.</p>
        <p id="p_07220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07220"> Listing 9.9 provides a very simple example of a subroutine that accepts three arguments.</p>
        <p id="p_07221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07221"> Listing 9.9. A program that uses a subroutine to print three numbers and their total.</p>
        <p id="p_07222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07222"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07223"> 2 :</p>
        <p id="p_07224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07224"> 3: print ("Enter three numbers, one at a time:\n");</p>
        <p id="p_07225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07225"> 4: $numberl = &lt;STDIN&gt;;</p>
        <p id="p_07226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07226"> 5: chop ($numberl);</p>
        <p id="p_07227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07227"> 6: $number2 = &lt;STDIN&gt;;</p>
        <p id="p_07228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07228"> 7: chop ($number2);</p>
        <p id="p_07229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07229"> 8: $number3 = &lt;STDIN&gt;;</p>
        <p id="p_07230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07230"> 9: chop ($number3);</p>
        <p id="p_07231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07231"> 10: &amp;printnum ($numberl, $number2, $number3);</p>
        <p id="p_07232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07232"> 11 :</p>
        <p id="p_07233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07233"> 12: sub printnum {</p>
        <p id="p_07234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07234"> 13: my($numberl, $number2, $number3) = @_;</p>
        <p id="p_07235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07235"> 14: my($total);</p>
        <p id="p_07236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07236"> 15: print ("The numbers you entered: ");</p>
        <p id="p_07237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07237"> 16: print ("$numberl $number2 $number3\n");</p>
        <p id="p_07238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07238"> 17: $total = $numberl + $number2 + $number3;</p>
        <p id="p_07239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07239"> 18: print ("The total: $total\n");</p>
        <p id="p_07240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07240"> 19: }</p>
        <p id="p_07241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07241"> $ program9_9</p>
        <p id="p_07242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07242"> Enter three numbers, one at a time: 5</p>
        <p id="p_07243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07243"> 11</p>
        <p id="p_07244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07244"> 4</p>
        <p id="p_07245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07245"> The numbers you entered: 5 11 4 The total: 20 $</p>
        <p id="p_07246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07246"> Analysis</p>
        <p id="p_07247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07247"> Line 10 calls the subroutine  printnum.  Three arguments are passed to  printnum:  the value stored in  $n umber  1 ,  the value stored in $  number 2,  and the value stored in  $n umber 3.  Note that arguments are passed to subroutines in the same way they are passed to built-in library functions.</p>
        <p id="p_07248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07248"> Line 13 defines local copies of the scalar variables  $number 1, $number2,  and  $number3.  It then assigns the contents of the system variable to these scalar variables. is created whenever a subroutine is called with arguments; it contains a list consisting of the arguments in the order in which they are passed. In this case,  printnum  is called with arguments  5,11,  and  4,  which means that contains the list (5, 11, 4).</p>
        <p id="p_07249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07249"> The assignment in line 13 assigns the list to the local scalar variables that have just been defined. This assignment works just like any other assignment of a list to a set of scalar variables. The first element of the list,  5,  is assigned to the first variable,  $numberl;  the second element of the list,  11,  is assigned to  $number2;  and the final element,  4,  is assigned to  $number3.</p>
        <p id="p_07250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07250"> NOTE</p>
        <p id="p_07251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07251"> After the array variable has been created, it can be used anywhere any other array variable can be used. This means that you do not need to assign its contents to local variables.</p>
        <p id="p_07252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07252"> The following subroutine is equivalent to the subroutine in lines 12-19 of Listing 9.9:</p>
        <p id="p_07253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07253"> sub printnum { my($total)  ;</p>
        <p id="p_07254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07254"> print  ("The  numbers you entered: "); print  ("$_[0] $_[1] $_[2]\n");</p>
        <p id="p_07255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07255"> $total = $_[0] + $_[1]  + $_ [  2]; print  ("The total: $total\n");</p>
        <p id="p_07256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07256"> }</p>
        <p id="p_07257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07257"> Here, $_ [  0  ] refers to the first element of the array variable $_ [ 1 ] refers to the second element, and $_ [ 2 ] refers to the third element.</p>
        <p id="p_07258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07258"> This subroutine is a little more efficient, but it is harder to read.</p>
        <p id="p_07259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07259"> TIP</p>
        <p id="p_07260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07260"> It usually is better to define local variables and assign to them because then your subroutines will be easier to understand.</p>
        <p id="p_07261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07261"> Listing 9.10 is another example of a program that passes arguments to a subroutine. This program uses the same subroutine to count the number of words and the number of characters in a file.</p>
        <p id="p_07262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07262"> Listing 9.10. Another example of a subroutine with arguments passed to it.</p>
        <p id="p_07263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07263"> #!/usr/local/bin/perl</p>
        <p id="p_07264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07264"> 1:</p>
        <p id="p_07265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07265"> 2 :</p>
        <p id="p_07266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07266"> 3 :</p>
        <p id="p_07267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07267"> 4 :</p>
        <p id="p_07268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07268"> 5 : 6 :</p>
        <p id="p_07269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07269"> 7 :</p>
        <p id="p_07270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07270"> 8  : 9: 10  : 11 : 12 :</p>
        <p id="p_07271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07271"> 13 :</p>
        <p id="p_07272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07272"> 14 :</p>
        <p id="p_07273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07273"> 15 : 16:</p>
        <p id="p_07274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07274"> 17 :</p>
        <p id="p_07275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07275"> 18 : 19: 20  : 21 : 22  :</p>
        <p id="p_07276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07276"> $wordcount = $charcount = 0;</p>
        <p id="p_07277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07277"> $charpattern =</p>
        <p id="p_07278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07278"> $wordpattern =  "\\s+"; while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_07279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07279"> $charcount += &amp;count($line, $charpattern);</p>
        <p id="p_07280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07280"> $line =~  s/ A \s+|\s+$//g;</p>
        <p id="p_07281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07281"> $wordcount += &amp;count($line, $wordpattern);</p>
        <p id="p_07282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07282"> }</p>
        <p id="p_07283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07283"> print  ("Totals:  $wordcount words, $charcount  characters\n");</p>
        <p id="p_07284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07284"> sub count {</p>
        <p id="p_07285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07285"> my ($line, $pattern) = @_; my  ($count); if  ($pattern eq "") {</p>
        <p id="p_07286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07286"> Qitems = split  (//,  $line);</p>
        <p id="p_07287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07287"> } else {</p>
        <p id="p_07288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07288"> Qitems = split  (/$pattern/,  $line);</p>
        <p id="p_07289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07289"> }</p>
        <p id="p_07290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07290"> $count = @items;</p>
        <p id="p_07291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07291"> OuTPUt</p>
        <p id="p_07292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07292"> $ program9_10</p>
        <p id="p_07293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07293"> This is a line of input.</p>
        <p id="p_07294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07294"> Here is another line.</p>
        <p id="p_07295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07295"> Totals: 10 words, 47 characters $</p>
        <p id="p_07296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07296"> Analysis</p>
        <p id="p_07297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07297"> This program reads lines from the standard input file until the file is exhausted. Each line has its characters counted and its words counted.</p>
        <p id="p_07298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07298"> Line  7  determines the number of characters in a line by calling the subroutine  count.  This subroutine is passed the line of input and the string stored in  $charpattern,  which is the empty string. Inside the subroutine  count,  the local variable  $pattern  receives the pattern passed to it by the call in line  7.  This means that the value stored in  $pattern  is also the empty string.</p>
        <p id="p_07299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07299"> Lines 16-20 split the input line. The pattern specified in the call to  split  has the value stored in $pattern  substituted into it. Because  $pattern  currently contains the empty string, the pattern used to split the line is / / , which splits the input line into individual characters. As a result, each element of the resulting list stored in @  items  is a character in the input line.</p>
        <p id="p_07300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07300"> The total number of elements in the list-in other words, the total number of characters in the input line-is assigned to $  count  by line  17.  Because this is the last expression evaluated in the subroutine, the resulting total number of characters is returned by the subroutine. Line 8 adds this total to the scalar variable  $charcount.</p>
        <p id="p_07301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07301"> Line 8 then removes the leading and trailing white space; this white space is included in the total number of characters-because spaces, tabs, and the trailing newline character count as characters-but is not included when the line is broken into words.</p>
        <p id="p_07302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07302"> Line 9 calls the subroutine  count  again, this time with the pattern stored in  $wordpattern,  which is \  s+.  (Recall that you need to use two backslashes in a string to represent a single backslash, because the \ character is the escape character in strings.) This value, representing one or more whitespace characters, is assigned to  $pattern  inside the subroutine, and the pattern passed to split  therefore becomes  /\s + /.</p>
        <p id="p_07303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07303"> When  split  is called with this pattern, @  items  is assigned a list of words. The total number of words in the list is assigned to $  count  and is returned; line 11 adds this returned value to the total</p>
        <p id="p_07304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07304"> number of words.</p>
        <p id="p_07305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07305"> Passing a List to a Subroutine</p>
        <p id="p_07306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07306"> If you want, you can pass a list to a subroutine. For example, the following subroutine adds the elements of a list together and prints the result:</p>
        <p id="p_07307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07307"> sub addlist {</p>
        <p id="p_07308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07308"> my (@list) = @_;</p>
        <p id="p_07309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07309"> $total = 0;</p>
        <p id="p_07310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07310"> foreach $item (@list) {</p>
        <p id="p_07311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07311"> $total += $item;</p>
        <p id="p_07312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07312"> }</p>
        <p id="p_07313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07313"> print ("The total is $total\n");</p>
        <p id="p_07314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07314"> }</p>
        <p id="p_07315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07315"> To invoke this subroutine, pass it an array variable, a list, or any combination of lists and scalar values.</p>
        <p id="p_07316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07316"> &amp;addlist (@mylist);</p>
        <p id="p_07317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07317"> &amp;addlist ("14",  "6",  "11");</p>
        <p id="p_07318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07318"> &amp;addlist ($valuel, @sublist, $value2);</p>
        <p id="p_07319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07319"> In each case, the values and lists supplied in the call to  addlist  are merged into a single list and then passed to the subroutine.</p>
        <p id="p_07320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07320"> Because values are merged into a single list when a list is passed to a subroutine, you can only define one list as an argument for a subroutine. The subroutine</p>
        <p id="p_07321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07321"> sub twolists {</p>
        <p id="p_07322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07322"> my (@listl, @list2) = @_;</p>
        <p id="p_07323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07323"> }</p>
        <p id="p_07324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07324"> isn't useful because it always assigns the empty list to @ list 2 ,  and because  @list 1  absorbs all of the contents of @_.</p>
        <p id="p_07325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07325"> This means that if you want to have both scalar variables and a list as arguments to a subroutine, the list must appear last, as follows:</p>
        <p id="p_07326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07326"> sub twoargs {</p>
        <p id="p_07327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07327"> my ($scalar, @list) = @_;</p>
        <p id="p_07328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07328"> }</p>
        <p id="p_07329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07329"> If you call this subroutine using</p>
        <p id="p_07330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07330"> &amp;twoargs(47, @mylist);</p>
        <p id="p_07331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07331"> the value  47  is assigned to  $scalar,  and  @mylist  is assigned to  @list.</p>
        <p id="p_07332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07332"> If you want, you can call  twoargs  with a single list, as follows:</p>
        <p id="p_07333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07333"> &amp;twoargs(@mylist);</p>
        <p id="p_07334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07334"> Here, the first element of  @mylist  is assigned to $  scalar,  and the rest of  @mylist  is assigned to @list.</p>
        <p id="p_07335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07335"> |note</p>
        <p id="p_07336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07336"> If you find this confusing, it might help to realize that passing arguments to a subroutine follows the same rules as assignment does. For example, you can have</p>
        <p id="p_07337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07337"> ($scalar, @listl) = @list2;</p>
        <p id="p_07338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07338"> because  $scalaris  assigned the first element of @ list 2.</p>
        <p id="p_07339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07339"> However, you can’t have this:</p>
        <p id="p_07340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07340"> (@listl, $scalar) = @list2;</p>
        <p id="p_07341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07341"> because all of @  1 i s 11  would be assigned to @  1 i st 2  and $ scalar  would be assigned the null string.</p>
        <p id="p_07342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07342"> Calling Subroutines from Other Subroutines</p>
        <p id="p_07343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07343"> In Perl, you can call subroutines from other subroutines. To call a subroutine from another subroutine, use the same subroutine-invocation syntax you've been using all along. Subroutines that are called by other subroutines are known as  nested subroutines  (because one call is "nested" inside the other).</p>
        <p id="p_07344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07344"> Listing 9.11 is an example of a program that contains a nested subroutine. It is a fairly simple modification of Listing 9.10 and counts the number of words and characters in three lines of standard input. It also demonstrates how to return multiple values from a subroutine.</p>
        <p id="p_07345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07345"> Listing 9.11. An example of a nested subroutine.</p>
        <p id="p_07346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07346"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07347"> 2 :</p>
        <p id="p_07348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07348"> 3: ($wordcount, $charcount) = &amp;getcounts(3);</p>
        <p id="p_07349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07349"> 4: print ("Totals for three lines: ");</p>
        <p id="p_07350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07350"> 5: print ("$wordcount words, $charcount characters\n");</p>
        <p id="p_07351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07351"> 6 :</p>
        <p id="p_07352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07352"> 7: sub getcounts {</p>
        <p id="p_07353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07353"> 37: }</p>
        <p id="p_07354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07354"> Output</p>
        <p id="p_07355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07355"> $ program9_ll</p>
        <p id="p_07356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07356"> This is a line of input.</p>
        <p id="p_07357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07357"> Here is another line.</p>
        <p id="p_07358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07358"> Here is the last line.</p>
        <p id="p_07359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07359"> Totals for three lines: 15 words, 70 characters $</p>
        <p id="p_07360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07360"> Analysis</p>
        <p id="p_07361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07361"> The main body of this program now consists of only five lines of code, including the special header comment and a blank line. This is because most of the actual work is being done inside the subroutines. (This is common in large programs. Most of these programs call a few main subroutines, which in turn call other subroutines. This approach makes programs easier to read, because each subroutine is compact and concise.)</p>
        <p id="p_07362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07362"> Line 3 calls the subroutine  get counts,  which retrieves the line and character count for the three lines from the standard input file. Because a list containing two elements is returned by  get count s, a standard "list to scalar variable" assignment can be used to assign the returned list directly to</p>
        <p id="p_07363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07363"> $wordcount  and  $charcount.</p>
        <p id="p_07364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07364"> The subroutine  get counts  is similar to the main body of the program in Listing 9.10. The only difference is that the  while  loop has been modified to loop only the number of times specified by the argument passed to  getcounts,  which is stored in the local variable  $numlines.</p>
        <p id="p_07365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07365"> The subroutine  getcounts  actually does the word and character counting by calling a nested subroutine,  count.  This subroutine is identical to the subroutine of the same name in List-ing 9.10.</p>
        <p id="p_07366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07366"> NOTE</p>
        <p id="p_07367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07367"> The variable is a local variable that is defined inside the subroutine. When a subroutine calls a nested subroutine, a new copy of is created for the nested subroutine.</p>
        <p id="p_07368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07368"> For example, in Listing 9.11, when  get counts  calls  count,  a new copy of is created for  count,  and the variable in get counts  is not changed.</p>
        <p id="p_07369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07369"> Recursive Subroutines</p>
        <p id="p_07370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07370"> In Perl, not only can subroutines call other subroutines, but subroutines actually can call themselves. A subroutine that calls itself is known as a  recursive subroutine.</p>
        <p id="p_07371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07371"> You can use a subroutine as a recursive subroutine if the following two conditions are true:</p>
        <p id="p_07372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07372"> • All variables the subroutine uses are local (except those which are not changed by the subroutine).</p>
        <p id="p_07373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07373"> • The subroutine contains code that, one way or another, determines when it should stop calling itself.</p>
        <p id="p_07374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07374"> When all the variables that a subroutine uses are local, the subroutine creates a new copy of the variables each time it calls itself. This ensures that there is no confusion or overlap.</p>
        <p id="p_07375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07375"> Listing 9.12 is an example of a program that contains a recursive subroutine. This program accepts a list of numbers and operands that is to be evaluated from right to left, as if the list is a stack whose top is the left end of the list. For example, if the input is</p>
        <p id="p_07376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07376"> - 955 * 26 +11 8</p>
        <p id="p_07377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07377"> this program adds  11  and  8,  multiplies the result by  2 6,  and subtracts that result from  955.  This is equivalent to the following Perl expression:</p>
        <p id="p_07378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07378"> 955 - 26 * (11 + 8)</p>
        <p id="p_07379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07379"> Type</p>
        <p id="p_07380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07380"> Listing 9.12. A program that uses a recursive subroutine to perform arithmetic.</p>
        <p id="p_07381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07381"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07382"> 2 :</p>
        <p id="p_07383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07383"> 26:</p>
        <p id="p_07384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07384"> $result = $operandl / $operand2;</p>
        <p id="p_07385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07385"> 27: }</p>
        <p id="p_07386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07386"> 28: }</p>
        <p id="p_07387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07387"> Output</p>
        <p id="p_07388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07388"> $ program9_12</p>
        <p id="p_07389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07389"> 98 * 4 + 12 11</p>
        <p id="p_07390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07390"> The result is 6.</p>
        <p id="p_07391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07391"> $</p>
        <p id="p_07392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07392"> Analysis</p>
        <p id="p_07393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07393"> This program starts off by reading a line of input from the standard input file and breaking it into its components, which are stored as a list in the array variable @ 1  i st.</p>
        <p id="p_07394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07394"> When given the input</p>
        <p id="p_07395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07395"> 98 * 4 + 12 11</p>
        <p id="p_07396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07396"> lines 3-5 produce the following list, which is assigned to @ list:</p>
        <p id="p_07397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07397"> "98",</p>
        <p id="p_07398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07398"> n * ii</p>
        <p id="p_07399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07399"> "  4 ", " + ", " 12 ",</p>
        <p id="p_07400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07400"> " 11 ")</p>
        <p id="p_07401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07401"> Line 6 calls the subroutine  right calc  for the first time,  right calc  requires one argument, an index value that tells the subroutine what part of the list to work on. Because the first argument here is zero, rightcalc  starts with the first element in the list.</p>
        <p id="p_07402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07402"> Line 10 assigns the argument passed to  rightcalc  to the local variable $  index.  When rightcalc  is called for the first time, $  index  is  0.</p>
        <p id="p_07403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07403"> Lines 13-17 are the heart of this subroutine, because they control whether to call  right calc recursively. The basic logic is that a list such as</p>
        <p id="p_07404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07404"> "98",</p>
        <p id="p_07405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07405"> H * ii</p>
        <p id="p_07406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07406"> "4", "+", "12",</p>
        <p id="p_07407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07407"> "11")</p>
        <p id="p_07408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07408"> can be broken into three parts: the first operator, the first operand, 98; and a sublist (the rest of the list). Note that the sublist</p>
        <p id="p_07409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07409"> ii * ii</p>
        <p id="p_07410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07410"> "  4  " , ii  +  ii ^ " 12 ",</p>
        <p id="p_07411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07411"> "11")</p>
        <p id="p_07412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07412"> is itself a complete set of operators and operands; because this program is required to perform its arithmetic starting from the right, this sublist must be calculated first.</p>
        <p id="p_07413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07413"> Line 13 checks whether there is a sublist that needs to be evaluated first. To do this, it checks whether there are more than three elements in the list. If there are only three elements in the list, the list consists of only one operator and two operands, and the arithmetic can be performed right away. If there are more than three elements in the list, a sublist exists.</p>
        <p id="p_07414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07414"> To evaluate the sublist when it exists, line 16 calls  rightcalc  recursively. The index value passed to this second copy of  rightcalc  is  2;  this ensures that the first element of the list examined by the second copy of  rightcalc  is the element with subscript 2, which is *.</p>
        <p id="p_07415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07415"> At this point, the following is the chain of subroutine invocations, their arguments, and the part of the list on which they are working:</p>
        <p id="p_07416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07416"> Level 1 Main program</p>
        <p id="p_07417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07417"> Level 2  rightcalc(0)-list  ("-",  "98",  "*",   "4",  " +  ",</p>
        <p id="p_07418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07418"> " 12 ", " 11 ")</p>
        <p id="p_07419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07419"> Level 3  rightcalc(2)-list  ("*",   "4",   " +  ",   "12", "11")</p>
        <p id="p_07420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07420"> When this copy of  rightcalc  reaches line 13, it checks whether the sublist being worked on has just three elements. Because this sublist has five elements, line 16 calls yet another copy of rightcalc,  this time setting the value of $  index  to  4.  The following is the chain of subroutine invocations after this third call:</p>
        <p id="p_07421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07421"> Level 1 Main program</p>
        <p id="p_07422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07422"> When the third copy of this subroutine reaches line 13, it checks whether this portion of the list contains only three elements. Because it does, the conditional expression in line 13 is true. At this point, line 14 is executed for the first time (by any copy of  rightcalc);  it takes the value stored in $ index-in  this case,  4,  adds  2  to it, and uses the result as the subscript into  @list.  This assigns  11, the seventh element of @  1 ist , to  $operand2.</p>
        <p id="p_07423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07423"> Lines 18-27 perform an arithmetic operation. Line 18 adds one to the value in  $ index  to retrieve the location of the first operand; this operand is assigned to  $operandl.  In this copy of  rightcalc, the subscript is  5  (4+1), and the sixth element of  @list, 12,  is assigned to  $operandl.</p>
        <p id="p_07424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07424"> Line 19 uses  $ index  as the subscript into the list to access the arithmetic operator for this operation. In this case, the fifth element of  $ index  (subscript 4) is +, and the expression in line 19 is true. Line 20 then adds  $operandl  to  $operand2,  yielding  $result,  which is  2 3.  This value is returned by this copy of  rightcalc.</p>
        <p id="p_07425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07425"> When the third copy of  rightcalc  returns, execution continues with the second copy of rightcalc  because the second copy called the third copy. Line 16 of the second copy assigns the return value of the third copy,  2 3,  to  $operand2.  The following is the state of the program after line 16 has finished executing:</p>
        <p id="p_07426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07426"> Level 1 Main program</p>
        <p id="p_07427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07427"> Level 2  rightcalc (0 )-list  "98", "*", "4",  " +  ",</p>
        <p id="p_07428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07428"> "12", "11")</p>
        <p id="p_07429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07429"> Level 3  rightcalc(2)-list  ("*", "4",  " +  ",  "12", "11"),</p>
        <p id="p_07430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07430"> $operand2 is 23</p>
        <p id="p_07431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07431"> The Perl interpreter now executes lines 18-27. Because  $ index  is  2  in this copy of  rightcalc, line 18 assigns the fourth element of  @list, 4,  to  $operandl.  Line 21 is true in this case because the operator is  *;  this means that line 22 multiplies  $operandl (4)  by  $operand2 (23),  yielding 92,  which is assigned to  $ re suit.</p>
        <p id="p_07432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07432"> At this point, the second copy of  rightcalc  is finished, and program execution returns to line 16. This assigns the return value from the second copy,  92,  to  $operand2.</p>
        <p id="p_07433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07433"> The following is the state of the program after the second copy of  rightcalc  is finished:</p>
        <p id="p_07434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07434"> Level 1 Main program</p>
        <p id="p_07435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07435"> Level 2  rightcalc (0 )-list ("-", "98", "*", "4",  " +  ",</p>
        <p id="p_07436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07436"> "12", "11"), $operand2 is 92</p>
        <p id="p_07437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07437"> Now you're almost finished; the program is executing only one copy of  rightcalc.  Because $index  is  0  in this copy of  rightcalc,  line 18 assigns  98  to  $operandl.  Line 23 is true in this case because the operator here is -; line 24 then takes 98 and subtracts 92 from it, yielding a final</p>
        <p id="p_07438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07438"> result of 6.</p>
        <p id="p_07439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07439"> This final result of 6 is passed to the main program and is assigned to  $result.  (Note that there is no conflict between $  result  in the main program and the various copies  of$resultin   right calc because  $result  is defined as a local variable in  rightcalc.)  Line 7, finally, prints this result.</p>
        <p id="p_07440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07440"> NOTE</p>
        <p id="p_07441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07441"> Recursive subroutines are useful when handling complicated data structures such as trees. You will see examples of such complicated data structures on  Day  10 ,  "Associative Arrays."</p>
        <p id="p_07442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07442"> Passing Arrays by Name Using Aliases</p>
        <p id="p_07443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07443"> As you have seen, Perl enables you to pass an array as an argument to a subroutine.</p>
        <p id="p_07444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07444"> &amp;my_sub(@array);</p>
        <p id="p_07445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07445"> When the subroutine  my_sub  is called, the list stored in the array variable @  array  is copied to the variable defined in the subroutine.</p>
        <p id="p_07446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07446"> sub my_sub {</p>
        <p id="p_07447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07447"> my (@subarray) = @_;</p>
        <p id="p_07448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07448"> $arraylength = Qsubarray;</p>
        <p id="p_07449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07449"> }</p>
        <p id="p_07450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07450"> If the array being passed is large, it might take some time (and considerable space) to create a copy of the array. If your application is operating under time or space limitations, or you just want to make it more efficient, you can specify that the array is to be passed by name.</p>
        <p id="p_07451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07451"> The following is an example of a similar subroutine that refers to an array by name:</p>
        <p id="p_07452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07452"> sub my_sub {</p>
        <p id="p_07453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07453"> my (*subarray) = @_;</p>
        <p id="p_07454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07454"> $arraylength = @subarray;</p>
        <p id="p_07455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07455"> }</p>
        <p id="p_07456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07456"> The *  sub array  definition tells the Perl interpreter to operate on the actual list passed to my_sub  instead of making a copy.</p>
        <p id="p_07457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07457"> To call this subroutine, specify * instead of @ with the array variable name, as in the following:</p>
        <p id="p_07458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07458"> @myarray = (1, 2, 3, 4, 5); &amp;my_sub(*myarray) ;</p>
        <p id="p_07459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07459"> Specifying  *myarray  instead of  @myarray  indicates that the actual contents of  @myarray  are to be used (and modified if desired) in  my_sub.  In fact, while the subroutine is being executed, the name @ subarray  becomes identical to the name  @myarray.  This process of creating another name to refer to the same variable is known as  aliasing.  @  subarray  is now an alias of  @myarray.</p>
        <p id="p_07460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07460"> When  my_sub  terminates, @  subarray  stops being an alias of  @myarray.  When  my_sub  is called again with a different argument, as in</p>
        <p id="p_07461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07461"> &amp;my_sub(*anotherarray);</p>
        <p id="p_07462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07462"> the variable @ subarray  in  my_sub  becomes an alias for  @anotherarray,  which means that you can use the array variable @ subarray  to access the storage in  @anotherarray.</p>
        <p id="p_07463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07463"> Aliasing arrays in this manner has one distinct advantage and one distinct drawback. The advantage is that your program becomes more efficient. You don't need to copy the entire list from your main program to the subroutine. The disadvantage is that your program becomes more difficult to follow. You have to remember, for example, that changing the contents of @  sub array  in the subroutine my_sub  also changes the contents of  @myarray  and @  another array.  It is easy to lose track of which name refers to which variable.</p>
        <p id="p_07464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07464"> There is also another problem with aliasing: aliasing affects all variables with the same name, not just array variables.</p>
        <p id="p_07465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07465"> For example, consider Listing 9.13, which defines a scalar variable named  $f oo  and an array named @foo,  and then aliases  @foo.  As you'll see, the program aliases  $foo  as well.</p>
        <p id="p_07466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07466"> Type</p>
        <p id="p_07467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07467"> Listing 9.13. A program that demonstrates aliasing.</p>
        <p id="p_07468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07468"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07469"> 2 :</p>
        <p id="p_07470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07470"> 3: $foo = 26;</p>
        <p id="p_07471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07471"> 4: @foo = ("here's", "a", "list");</p>
        <p id="p_07472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07472"> 5: &amp;testsub (*foo);</p>
        <p id="p_07473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07473"> 6: print ("The value of \$foo is now $foo\n");</p>
        <p id="p_07474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07474"> 7 :</p>
        <p id="p_07475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07475"> 8: sub testsub {</p>
        <p id="p_07476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07476"> 9: local (*printarray) = @_;</p>
        <p id="p_07477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07477"> 10: foreach $element (Qprintarray) {</p>
        <p id="p_07478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07478"> 11: print ("$element\n");</p>
        <p id="p_07479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07479"> 12 : }</p>
        <p id="p_07480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07480"> 13: $printarray = 61;</p>
        <p id="p_07481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07481"> 14: }</p>
        <p id="p_07482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07482"> Output</p>
        <p id="p_07483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07483"> $ program9_13 here ' s</p>
        <p id="p_07484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07484"> a</p>
        <p id="p_07485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07485"> list</p>
        <p id="p_07486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07486"> The value of $foo is now 61</p>
        <p id="p_07487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07487"> $</p>
        <p id="p_07488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07488"> Analysis</p>
        <p id="p_07489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07489"> Line 5 calls the subroutine  test sub.  The argument,  *f oo,  indicates that the array  @f oo is to be passed to  test sub  and aliased.</p>
        <p id="p_07490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07490"> The local variable definition in line 9 indicates that the array variable  @printarrayisto  become an alias of the array variable  @foo.  This means that the name  print array  is defined to be equivalent to the name  too.</p>
        <p id="p_07491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07491"> As a consequence, the scalar variable  $pr intar ray  becomes an alias of the scalar variable  $f oo. Because of this, line 13, which seems to assign  61  to  $printarray,  actually assigns  61  to  $foo. This modified value is printed by line 6 of the main program.</p>
        <p id="p_07492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07492"> NOTE</p>
        <p id="p_07493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07493"> Aliasing enables you to pass more than one list to a subroutine.</p>
        <p id="p_07494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07494"> @arrayl = (1,  2 ,  3);</p>
        <p id="p_07495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07495"> @array2 = (4, 5, 6);</p>
        <p id="p_07496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07496"> &amp;two_array_sub (*arrayl, *array2); sub two_array_sub { my (*subarrayl, *subarray2) = @_;</p>
        <p id="p_07497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07497"> }</p>
        <p id="p_07498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07498"> In this case, the names  arrayl  and  array2  are passed to two_array_sub. subarrayl  becomes an alias for  arrayl, and  subarray2  becomes an alias for  array2.</p>
        <p id="p_07499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07499"> Using the do Statement with Subroutines</p>
        <p id="p_07500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07500"> Perl enables you to use the  do  statement to invoke a subroutine. For example, the following statements are identical:</p>
        <p id="p_07501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07501"> &amp;my_sub(1,  2 ,  3) ;</p>
        <p id="p_07502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07502"> do my_sub(l, 2, 3);</p>
        <p id="p_07503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07503"> There is no real reason to use the  do  statement in this context.</p>
        <p id="p_07504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07504"> Specifying the Sort Order</p>
        <p id="p_07505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07505"> By default, the built-in function  sort  sorts in alphabetical order. The following is an example:</p>
        <p id="p_07506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07506"> @list = ("words", "to", "sort");</p>
        <p id="p_07507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07507"> @list2 = sort (@list);</p>
        <p id="p_07508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07508"> Here,  @list2  is assigned (  "sort" , "to", "words").</p>
        <p id="p_07509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07509"> If you want, you can write a subroutine that defines how sorting is to be accomplished. To understand how to do this, first you need to know a little about how sorting works.</p>
        <p id="p_07510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07510"> When  sort  is given a list to sort, it determines the sort order of the elements of the list by repeatedly comparing pairs of elements. To compare a pair of elements,  sort  calls a special internal subroutine and passes it a pair of arguments. Although the subroutine is not accessible from a Perl program, it basically behaves as follows:</p>
        <p id="p_07511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07511"> sub sort_criteria {</p>
        <p id="p_07512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07512"> if ($a gt $b) {</p>
        <p id="p_07513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07513"> retval = -1;</p>
        <p id="p_07514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07514"> } elsif ($a eq $b) {</p>
        <p id="p_07515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07515"> retval = 0;</p>
        <p id="p_07516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07516"> } else</p>
        <p id="p_07517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07517"> retval = 1;</p>
        <p id="p_07518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07518"> }</p>
        <p id="p_07519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07519"> $retval;</p>
        <p id="p_07520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07520"> }</p>
        <p id="p_07521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07521"> This subroutine compares two values, which are stored in  $a  and  $b.  It returns  -1  if the first value is greater, 0 if the values are equal, and 1 if the second value is greater. (This, by the way, is how the cmp  operator works; in fact, the preceding subroutine could compare the two values using a single cmp  operator.)</p>
        <p id="p_07522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07522"> To define your own sorting rules, you must write a subroutine whose behavior is identical to the preceding subroutine. This subroutine must use two global variables named  $a  and  $b  to represent the two items in the list currently being compared, and the subroutine must return one of the following values:</p>
        <p id="p_07523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07523"> -1  If $  a  is to appear before  $b  in the resulting sorted list</p>
        <p id="p_07524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07524"> 0  If  $a  is to be treated as equal to  $b</p>
        <p id="p_07525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07525"> 1  If $  a  is to appear after  $b  in the resulting sorted list</p>
        <p id="p_07526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07526"> NOTE</p>
        <p id="p_07527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07527"> Even though  $a  and  $b  are global variables that are used by the sorting subroutine, you still can define global variables of your own named  $a  and  $b  without risking their being overwritten.</p>
        <p id="p_07528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07528"> The built-in function  sort  saves any existing values of  $a  and $b  before sorting, and then it restores them when sorting is completed.</p>
        <p id="p_07529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07529"> After you have written the subroutine, you must specify the subroutine name when calling the function sort.  For example, if you define a function named  too  that provides a set of sorting rules, the following statement sorts a list using the rules defined in  too:</p>
        <p id="p_07530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07530"> @list2 = sort too (@listl);</p>
        <p id="p_07531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07531"> Listing 9.14 shows how you can define your own sort criteria. This program sorts a list in the normal order, except that it puts strings starting with a digit last. (By default, strings starting with a number appear before strings starting with a letter, and before some-but not all-special characters.) Strings that begin with a digit are assumed to be numbers and are sorted in numerical order.</p>
        <p id="p_07532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07532"> Listing 9.14. A program that defines sort criteria.</p>
        <p id="p_07533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07533"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07534"> 2 :</p>
        <p id="p_07535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07535"> 5:  print  ("@list2\n");</p>
        <p id="p_07536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07536"> 6 :</p>
        <p id="p_07537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07537"> 7: sub  num_last {</p>
        <p id="p_07538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07538"> 8: my ($num_a, $num_b);</p>
        <p id="p_07539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07539"> 9:</p>
        <p id="p_07540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07540"> 22 : }</p>
        <p id="p_07541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07541"> $ program9_14 test test2 14 26</p>
        <p id="p_07542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07542"> $</p>
        <p id="p_07543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07543"> Analysis</p>
        <p id="p_07544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07544"> Line 4 sorts the program according to the sort criteria defined in the subroutine num last.  This subroutine is defined in lines 7-22.</p>
        <p id="p_07545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07545"> This subroutine first determines whether the items are strings that begin with a digit. Line 10 sets the local variable  $num_a  to a nonzero value if the value stored in  $a  starts with a digit; similarly, line 11 sets  $num_b  to a nonzero value if the value of  $b  starts with a digit.</p>
        <p id="p_07546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07546"> Lines  12  and  13  handle the case in which both  $num_a  and  $num_b  are true. In this case, the two strings are assumed to be digits, and the numeric comparison operator &lt;=&gt; compares their values. The result of the &lt;=&gt; operation is -1 if the first number is larger, 0 if they are equal, and 1 if the second number is larger.</p>
        <p id="p_07547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07547"> If  $num_a  is true but  $num_b  is false, line 15 sets the return value for this subroutine to  1,  indicating that the string that does not start with a digit,  $b,  is to be treated as greater. Similarly, line 17 sets the return value to  -1  if  $b  starts with a digit and  $a  does not.</p>
        <p id="p_07548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07548"> If neither string starts with a digit, line 19 uses the normal sort criterion-alphabetical order-to determine which value is larger. Here, the  cmp  operator is useful. It returns -1  if the first string is alphabetically greater, 0 if the strings are equal, and 1 if the second string is alphabetically greater.</p>
        <p id="p_07549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07549"> Predefined Subroutines</p>
        <p id="p_07550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07550"> Perl 5 defines three special subroutines that are executed at specific times.</p>
        <p id="p_07551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07551"> • The  BEGIN  subroutine, which is called when your program starts running</p>
        <p id="p_07552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07552"> • The  END  subroutine, which is called when your program terminates</p>
        <p id="p_07553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07553"> • The  AUTOLOAD  subroutine, which is called when your program can't find a subroutine it is supposed to execute</p>
        <p id="p_07554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07554"> Creating Startup Code Using  BEGIN</p>
        <p id="p_07555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07555"> Perl 5 enables you to create code that is executed when your program is started. To do this, create a special subroutine named  BEGIN.  For example:</p>
        <p id="p_07556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07556"> BEGIN {</p>
        <p id="p_07557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07557"> print("Hi! Welcome to Perl!\n");</p>
        <p id="p_07558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07558"> }</p>
        <p id="p_07559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07559"> When your program begins execution, the following line appears on your screen:</p>
        <p id="p_07560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07560"> Hi! Welcome to Perl!</p>
        <p id="p_07561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07561"> The  BEGIN  subroutine behaves just like any other Perl subroutine. For example, you can define local variables for it or call other subroutines from it.</p>
        <p id="p_07562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07562"> NOTE</p>
        <p id="p_07563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07563"> If you like, you can define multiple  BEGIN  subroutines. These subroutines are called in the order in which they appear in the program.</p>
        <p id="p_07564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07564"> Creating Termination Code Using  END</p>
        <p id="p_07565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07565"> Perl 5 enables you to create code to be executed when your program terminates execution. To do this, define an  END  subroutine, as in the following example:</p>
        <p id="p_07566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07566"> END {</p>
        <p id="p_07567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07567"> print("Thank you for using Perl!\n") ;</p>
        <p id="p_07568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07568"> }</p>
        <p id="p_07569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07569"> The code contained in the  END  subroutine is always executed by your program, even if the program is terminated using  die.  For example, the code</p>
        <p id="p_07570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07570"> die("Prepare to die!\n"); END {</p>
        <p id="p_07571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07571"> print("Ha! You can't kill me!\n") ;</p>
        <p id="p_07572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07572"> }</p>
        <p id="p_07573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07573"> displays the following on your screen:</p>
        <p id="p_07574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07574"> Prepare to die!</p>
        <p id="p_07575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07575"> Ha! You can't kill me!</p>
        <p id="p_07576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07576"> NOTE</p>
        <p id="p_07577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07577"> You can define multiple  END  subroutines in your program. In this case, the subroutines are executed in reverse order of appearance, with the last one executed first.</p>
        <p id="p_07578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07578"> Handling Non-Existent Subroutines Using  AUTOLOAD</p>
        <p id="p_07579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07579"> Perl 5 enables you to define a special subroutine named  AUTOLOAD  that is called whenever the Perl interpreter is told to call a subroutine that does not exist. Listing 9.15 illustrates the use of  AUTOLOAD.</p>
        <p id="p_07580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07580"> Listing 9.15. A program that uses AUTOLOAD.</p>
        <p id="p_07581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07581"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07582"> 2 :</p>
        <p id="p_07583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07583"> 3: &amp;nothere("hi",  46);</p>
        <p id="p_07584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07584"> 4 :</p>
        <p id="p_07585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07585"> 5:  AUTOLOAD {</p>
        <p id="p_07586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07586"> 6: print("subroutine $AUTOLOAD not found\n") ;</p>
        <p id="p_07587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07587"> 7: print("arguments passed:  @_\n");</p>
        <p id="p_07588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07588"> }</p>
        <p id="p_07589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07589"> Output</p>
        <p id="p_07590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07590"> $ program9_15</p>
        <p id="p_07591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07591"> subroutine main::nothere not found arguments passed: hi 46 $</p>
        <p id="p_07592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07592"> This program tries to call the non-existent subroutine  nothere.  When the Perl interpreter discovers that  nothere  does not exist, it calls the  AUTOLOAD  subroutine.</p>
        <p id="p_07593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07593"> Line 6 uses a special scalar variable, $  AUTOLOAD,  which contains the name of the subroutine you tried to call. (The  main  : : text that appears before the subroutine name,  nothere,  is the name of the package in which the subroutine is found. By default, all your code is placed in one package, called main,  so you normally won't need to worry about packages. For more information on creating other packages, see  Day 19 ,  "Object-Oriented Programming in Perl.")</p>
        <p id="p_07594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07594"> When  AUTOLOAD  is called, the arguments that were to be passed to the non-existent subroutine are passed to  AUTOLOAD  instead. This means that the @ array variable contains the list ( "hi" , 4 6), because these are the arguments that were to be passed to  nothere.</p>
        <p id="p_07595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07595"> Analysis</p>
        <p id="p_07596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07596"> TIP</p>
        <p id="p_07597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07597"> AUTOLOAD  is useful if you plan to organize your Perl program into modules, because you can use it to ensure that crucial subroutines from other files actually exist when you need them. For more information on organizing Perl programs into modules, see  Day  19 .</p>
        <p id="p_07598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07598"> Summary</p>
        <p id="p_07599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07599"> Today, you learned about subroutines, which are separated chunks of code intended to perform specific tasks. A subroutine can appear anywhere in your program.</p>
        <p id="p_07600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07600"> To invoke a subroutine, specify its name preceded by the &amp; character. In Perl 5, the &amp; character is not required if the subroutine exists, or if a forward reference is defined.</p>
        <p id="p_07601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07601"> A subroutine can return a value (either a scalar value or a list). This return value is the value of the last expression evaluated inside the subroutine. If this last expression is at the end of the subroutine, the subroutine is a single-exit module.</p>
        <p id="p_07602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07602"> You can define local variables for use inside subroutines. These local variables exist only while the subroutine is being executed. When a subroutine finishes, its local variables are destroyed; if it is invoked again, new copies of the local variables are defined.</p>
        <p id="p_07603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07603"> You can pass values to subroutines; these values are called arguments. You can pass as many arguments as you like, but only one of these arguments can be a list. If a list is passed to a subroutine, it must be the last argument passed.</p>
        <p id="p_07604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07604"> The arguments passed to a subroutine are converted into a list and assigned to a special system variable, @_. One copy of exists for each list of arguments passed to a subroutine (that is, is a local variable).</p>
        <p id="p_07605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07605"> Subroutines can call other subroutines (nested subroutines) and even can call themselves (recursive subroutines).</p>
        <p id="p_07606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07606"> You can pass an array variable to a subroutine by name by defining an alias for the variable name.</p>
        <p id="p_07607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07607"> This alias affects all variables of that name.</p>
        <p id="p_07608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07608"> You can use the  do  statement to invoke a subroutine, although there is no real reason to do so.</p>
        <p id="p_07609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07609"> You can define a subroutine that specifies the order in which the elements of a list are to be sorted. To use the sort criteria defined by a subroutine, include its name with the call to  sort.</p>
        <p id="p_07610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07610"> The BEGIN subroutine is always executed before your program begins execution. The END subroutine is always executed when your program terminates, even if it was killed off using  die.  The AUTOLOAD subroutine is executed if your program tries to call a subroutine that does not exist.</p>
        <p id="p_07611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07611"> Q&amp;A</p>
        <p id="p_07612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07612"> Q: How many levels of nested subroutines can a program have?</p>
        <p id="p_07613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07613"> A: This depends on the amount of memory in your machine. Normally, it is large enough to only</p>
        <p id="p_07614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07614"> be an issue when you are using recursive subroutines.</p>
        <p id="p_07615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07615"> Q: Which is better: passing entire lists or passing array variables by name?</p>
        <p id="p_07616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07616"> A: As with so many issues in programming, this depends on the situation. If your program needs</p>
        <p id="p_07617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07617"> to be space-efficient or to run as quickly as possible, passing array variables by name might be the best choice.</p>
        <p id="p_07618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07618"> Another option is to use the global array variable both inside and outside the subroutine. This works well if the array variable is the central repository for program data.</p>
        <p id="p_07619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07619"> Q: When are global variables a good idea? When is it better to pass the contents of a</p>
        <p id="p_07620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07620"> variable to a subroutine?</p>
        <p id="p_07621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07621"> A: If your subroutine is a general-purpose subroutine that performs a task such as breaking a</p>
        <p id="p_07622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07622"> scalar value into words, it's a good idea to pass the value as an argument. For example:</p>
        <p id="p_07623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07623"> sub breakline { local ($line) = @_;</p>
        <p id="p_07624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07624"> Qwords = split  (/\s  +  /,  $line);</p>
        <p id="p_07625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07625"> }</p>
        <p id="p_07626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07626"> If you do not pass the line as an argument,  breakline  will be able to work only with the line stored in a particular scalar variable, which makes it less useful.</p>
        <p id="p_07627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07627"> On the other hand, if your program stores information in a central array, there's no reason to pass the array or the array name to a subroutine that processes the array. For example, if you are using the array  @occursto  count all the occurrences of the digits 0 through 9 in a file, there's no reason to pass  @occurs  to a subroutine. For example:</p>
        <p id="p_07628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07628"> sub printcount {</p>
        <p id="p_07629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07629"> for ($count = 0; $count &lt;= 9; $count++) { print  ("$occurs[$count]\n");</p>
        <p id="p_07630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07630"> }</p>
        <p id="p_07631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07631"> }</p>
        <p id="p_07632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07632"> Because  printcount  is not likely to be used with any array except  Qoccurs,  there's no need to pass it as an argument.</p>
        <p id="p_07633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07633"> Q: When Perl defines an alias for an array-variable name in a subroutine, such as</p>
        <p id="p_07634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07634"> @localname for @name in a subroutine, why does it also define the alias $localname for $name?</p>
        <p id="p_07635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07635"> A: Strictly speaking, the * character in an alias represents any character that precedes a variable</p>
        <p id="p_07636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07636"> name (such as @ or $).</p>
        <p id="p_07637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07637"> For example, consider the following subroutine and the corresponding statement that calls it:</p>
        <p id="p_07638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07638"> sub arraybyname { local (*localname) = @_;</p>
        <p id="p_07639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07639"> }</p>
        <p id="p_07640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07640"> arraybyname (*name);</p>
        <p id="p_07641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07641"> When the Perl interpreter sees the reference to  * local name  in the subroutine, it replaces the alias following the * with the name for which the alias is defined. In this case, the Perl interpreter replaces  *localname  with  *name.</p>
        <p id="p_07642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07642"> The Perl interpreter then determines, from context, whether  *name  is an array variable, a scalar variable, or something else. In this case,  *name  is intended to be an array variable, which means that  *name  becomes  @name.</p>
        <p id="p_07643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07643"> Workshop</p>
        <p id="p_07644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07644"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_07645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07645"> Quiz</p>
        <p id="p_07646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07646"> 1. Define the following terms:</p>
        <p id="p_07647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07647"> a. subroutine</p>
        <p id="p_07648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07648"> b. invocation</p>
        <p id="p_07649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07649"> c. argument</p>
        <p id="p_07650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07650"> d. single-exit module</p>
        <p id="p_07651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07651"> e. aliasing</p>
        <p id="p_07652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07652"> 2. Consider the following program:</p>
        <p id="p_07653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07653"> # ! /usr/loca1/bin/perl</p>
        <p id="p_07654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07654"> $total = 0;</p>
        <p id="p_07655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07655"> @list = (1, 2, 3);</p>
        <p id="p_07656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07656"> @list2 = &amp;my_sub; sub my_sub { local ($total);</p>
        <p id="p_07657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07657"> $total = 1;</p>
        <p id="p_07658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07658"> @list = (4, 5, 6) ;</p>
        <p id="p_07659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07659"> }</p>
        <p id="p_07660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07660"> What are the values stored in the following variables at the end of this program?</p>
        <p id="p_07661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07661"> a.  $total</p>
        <p id="p_07662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07662"> b.  @list</p>
        <p id="p_07663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07663"> c. @list2</p>
        <p id="p_07664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07664"> 3. What does the following subroutine return?</p>
        <p id="p_07665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07665"> sub subl {</p>
        <p id="p_07666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07666"> $count = $sum = 0; while ($count &lt;= 10) {</p>
        <p id="p_07667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07667"> $sum += $count;</p>
        <p id="p_07668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07668"> $count++;</p>
        <p id="p_07669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07669"> }</p>
        <p id="p_07670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07670"> }</p>
        <p id="p_07671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07671"> 4. What is the value of @ 1  i s t  at the end of the following program?</p>
        <p id="p_07672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07672"> # ! /usr/loca1/bin/perl</p>
        <p id="p_07673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07673"> @list = (1, 2, 3);</p>
        <p id="p_07674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07674"> &amp;testsub(*list);</p>
        <p id="p_07675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07675"> sub testsub {</p>
        <p id="p_07676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07676"> local (*sublist) = @_;</p>
        <p id="p_07677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07677"> $sublist[1] =5;</p>
        <p id="p_07678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07678"> }</p>
        <p id="p_07679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07679"> Exercises</p>
        <p id="p_07680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07680"> 1. Write a subroutine that takes two arguments, adds them together, and returns the result.</p>
        <p id="p_07681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07681"> 2. Write a subroutine that counts the number of occurrences of the letter t in a string (which is passed to the subroutine). The subroutine must return the number of occurrences.</p>
        <p id="p_07682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07682"> 3. Write a subroutine that takes two filenames as its arguments and returns a nonzero value if the two files have identical contents. Return  0  if the files differ.</p>
        <p id="p_07683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07683"> 4. Write a subroutine that simulates the roll of a die (that is, it generates a random number between 1 and 6) and returns the number.</p>
        <p id="p_07684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07684"> 5. Write a subroutine that uses recursion to print a list in reverse order. The subroutine must recursively call itself to print the entire list; each invocation must print one word of the list. (Assume that the first call to your subroutine passes the value  0  and the list to be printed.)</p>
        <p id="p_07685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07685"> 6. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_07686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07686"> #!/usr/loca1/bin/perl</p>
        <p id="p_07687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07687"> for ($count = 1; $count &lt;= 10; $count++) {</p>
        <p id="p_07688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07688"> &amp;print_ten ($count);</p>
        <p id="p_07689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07689"> }</p>
        <p id="p_07690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07690"> sub print_ten { local ($multiplier) =</p>
        <p id="p_07691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07691"> for ($count = 1; $count &lt;= 10; $count++) {</p>
        <p id="p_07692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07692"> $printval = $multiplier * 10 + $count; print ("$printval\n");</p>
        <p id="p_07693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07693"> }</p>
        <p id="p_07694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07694"> }</p>
        <p id="p_07695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07695"> 7. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_07696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07696"> #!/usr/loca1/bin/perl</p>
        <p id="p_07697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07697"> $line = &lt;STDIN&gt;;</p>
        <p id="p_07698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07698"> @words = split(/\s+/, $line);</p>
        <p id="p_07699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07699"> $searchword = &lt;STDIN&gt;;</p>
        <p id="p_07700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07700"> &amp;search_for_word (©words, $searchword); sub search_for_word {</p>
        <p id="p_07701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07701"> local (©searchlist, $searchword) = @_;</p>
        <p id="p_07702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07702"> foreach $word (@searchlist) {</p>
        <p id="p_07703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07703"> return (1) if ($word eq $searchword);</p>
        <p id="p_07704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07704"> }</p>
        <p id="p_07705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07705"> $retval = 0;</p>
        <p id="p_07706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07706"> }</p>
        <p id="p_07707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07707"> 8. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_07708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07708"> #!/usr/loca1/bin/perl</p>
        <p id="p_07709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07709"> $line = &lt;STDIN&gt;;</p>
        <p id="p_07710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07710"> ©words = &amp;split_line($line) ; print ("@words\n") ;</p>
        <p id="p_07711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07711"> sub split_line { local ($line) = local (@words);</p>
        <p id="p_07712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07712"> @words = split(/\s+/, $line); if (@words == 0) {</p>
        <p id="p_07713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07713"> @words = ("empty list");</p>
        <p id="p_07714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07714"> }</p>
        <p id="p_07715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07715"> }</p>
        <p id="p_07716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07716"> Chapter 10</p>
        <p id="p_07717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07717"> Associative Arrays</p>
        <p id="p_07718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07718"> CONTENTS</p>
        <p id="p_07719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07719"> •  Limitations of Array Variables</p>
        <p id="p_07720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07720"> •  Definition</p>
        <p id="p_07721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07721"> •  Referring to Associative Array Elements</p>
        <p id="p_07722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07722"> •  Adding Elements to an Associative Array</p>
        <p id="p_07723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07723"> •  Creating Associative Arrays</p>
        <p id="p_07724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07724"> •  Copying Associative Arrays from Array Variables</p>
        <p id="p_07725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07725"> •  Adding and Deleting Array Elements</p>
        <p id="p_07726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07726"> •  Listing Array Indexes and Values</p>
        <p id="p_07727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07727"> •  Looping Using an Associative Array</p>
        <p id="p_07728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07728"> •  Creating Data Structures Using Associative Arrays</p>
        <p id="p_07729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07729"> o  Linked Lists o  Structures o  Trees o  Databases</p>
        <p id="p_07730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07730"> o  Example: A Calculator Program</p>
        <p id="p_07731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07731"> •  Summary</p>
        <p id="p_07732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07732"> •  O&amp;A</p>
        <p id="p_07733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07733"> •  Workshop</p>
        <p id="p_07734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07734"> o  Quiz o  Exercises</p>
        <p id="p_07735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07735"> Today's lesson shows you how to use associative arrays. You'll learn the following:</p>
        <p id="p_07736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07736"> • What an associative array is</p>
        <p id="p_07737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07737"> • How to access and create an associative array</p>
        <p id="p_07738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07738"> • How to copy to and from an associative array</p>
        <p id="p_07739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07739"> • How to add and delete associative array elements</p>
        <p id="p_07740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07740"> • How to list array indexes and values</p>
        <p id="p_07741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07741"> • How to loop using an associative array</p>
        <p id="p_07742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07742"> • How to build data structures using associative arrays</p>
        <p id="p_07743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07743"> To start, take a look at some of the problems that using array variables creates. Once you have seen some of the difficulties created by array variables in certain contexts, you'll see how associative arrays can eliminate these difficulties.</p>
        <p id="p_07744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07744"> Limitations of Array Variables</p>
        <p id="p_07745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07745"> In the array variables you've seen so far, you can access an element of a stored list by specifying a subscript. For example, the following statement accesses the third element of the list stored in the array variable</p>
        <p id="p_07746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07746"> @array:</p>
        <p id="p_07747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07747"> $scalar = $array[2];</p>
        <p id="p_07748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07748"> The subscript  2  indicates that the third element of the array is to be referenced.</p>
        <p id="p_07749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07749"> Although array variables are useful, they have one significant drawback: it's often difficult to remember which element of an array stores what. For example, suppose you want to write a program that counts the number of occurrences of each capitalized word in an input file. You can do this using array variables, but it's very difficult. Listing 10.1 shows you what you have to go through to do this.</p>
        <p id="p_07750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07750"> Listing 10.1. A program that uses array variables to keep track of capitalized words in an input file.</p>
        <p id="p_07751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07751"> 1:</p>
        <p id="p_07752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07752"> 2  :</p>
        <p id="p_07753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07753"> 3 :</p>
        <p id="p_07754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07754"> 4 :</p>
        <p id="p_07755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07755"> 5 : 6 :</p>
        <p id="p_07756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07756"> 7 :</p>
        <p id="p_07757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07757"> 8  : 9: 10 : 11 : 12 :</p>
        <p id="p_07758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07758"> #!/usr/local/bin/perl</p>
        <p id="p_07759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07759"> while ($inputline = &lt;STDIN&gt;) {</p>
        <p id="p_07760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07760"> while ($inputline =~ /\b[A-Z]\S+/g) {</p>
        <p id="p_07761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07761"> $word = $&amp;;</p>
        <p id="p_07762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07762"> $word =~ s/; # remove punctuation</p>
        <p id="p_07763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07763"> for ($count = 1; $count &lt;= Swordlist;</p>
        <p id="p_07764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07764"> $count++) {</p>
        <p id="p_07765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07765"> $found = 0;</p>
        <p id="p_07766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07766"> if ($wordlist[$count-l] eq $word) {</p>
        <p id="p_07767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07767"> $found = 1;</p>
        <p id="p_07768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07768"> $wordcount[$count-l] += 1;</p>
        <p id="p_07769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07769"> 13:</p>
        <p id="p_07770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07770"> last;</p>
        <p id="p_07771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07771"> 14 : 15: 16:</p>
        <p id="p_07772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07772"> 17 :</p>
        <p id="p_07773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07773"> 18 : 19: 20  : 21 : 22  : 23:</p>
        <p id="p_07774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07774"> 24 :</p>
        <p id="p_07775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07775"> 25 : 26:</p>
        <p id="p_07776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07776"> }</p>
        <p id="p_07777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07777"> }</p>
        <p id="p_07778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07778"> if ($found == 0) {</p>
        <p id="p_07779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07779"> $oldlength = @wordlist; $wordlist[$oldlength] = $word; $wordcount[$oldlength] = 1;</p>
        <p id="p_07780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07780"> }</p>
        <p id="p_07781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07781"> }</p>
        <p id="p_07782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07782"> }</p>
        <p id="p_07783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07783"> print ("Capitalized words and number of occurrences:\n"); for ($count = 1; $count &lt;= Swordlist; $count++) {</p>
        <p id="p_07784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07784"> print ("$wordlist[$count-l]: $wordcount[$count-l]\n");</p>
        <p id="p_07785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07785"> }</p>
        <p id="p_07786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07786"> Output</p>
        <p id="p_07787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07787"> $ programlO_l</p>
        <p id="p_07788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07788"> Here is a line of Input.</p>
        <p id="p_07789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07789"> This Input contains some Capitalized words. A D</p>
        <p id="p_07790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07790"> Capitalized words and number of occurrences: Here: 1 Input: 2 This: 1</p>
        <p id="p_07791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07791"> Capitalized: 1</p>
        <p id="p_07792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07792"> $</p>
        <p id="p_07793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07793"> Analysis</p>
        <p id="p_07794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07794"> ! This program reads one line of input at a time from the standard input file. The loop starting on line 4 matches each capitalized word in the line; the loop iterates once for each match, and it assigns the match being examined in this particular iteration to the scalar variable $word.</p>
        <p id="p_07795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07795"> Once any closing punctuation has been removed by line 6, the program must then check whether this word has been seen before. Lines 7-15 do this by examining each element of the list @wordlist in turn. If an element of @wordlist is identical to the word stored in $word, the corresponding element of @wordcount is incremented.</p>
        <p id="p_07796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07796"> If no element of @wordlist matches $word, lines 16-20 add a new element to @wordlist and @wordcount.</p>
        <p id="p_07797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07797"> Definition</p>
        <p id="p_07798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07798"> As you can see, using array variables creates several problems. First, it's not obvious which element of @wordlist in Listing 10.1 corresponds to which capitalized word. In the example shown, $wordlist [ 0 ] contains Here because this is the first capitalized word in the input file, but this is not obvious to the reader.</p>
        <p id="p_07799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07799"> Worse still, the program has no way of knowing which element of @wordlist contains which word. This means that every time the program reads a new word, it has to check the entire list to see if the word has already been found. This becomes time-consuming as the list grows larger.</p>
        <p id="p_07800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07800"> All of these problems with array variables exist because elements of array variables are accessed by numeric subscripts. To get around these problems, Perl defines another kind of array, which enables you to access array variables using any scalar value you like. These arrays are called  associative arrays.</p>
        <p id="p_07801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07801"> To distinguish an associative array variable from an ordinary array variable, Perl uses the % character as the first character of an associative array-variable name, instead of the @ character. As with other variable names, the first character following the % must be a letter, and subsequent characters can be letters, digits, or underscores.</p>
        <p id="p_07802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07802"> The following are examples of associative array-variable names:</p>
        <p id="p_07803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07803"> %assocarray</p>
        <p id="p_07804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07804"> %al</p>
        <p id="p_07805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07805"> %my_really_long_but_legal_array_variable_name</p>
        <p id="p_07806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07806"> NOTE</p>
        <p id="p_07807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07807"> Use the same name for an associative array variable and an ordinary array variable. For example, you can define an array variable named @arrayname  and an associative array variable named</p>
        <p id="p_07808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07808"> %arrayname.</p>
        <p id="p_07809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07809"> The @ and % characters ensure that the Perl interpreter can tell one variable name from another.</p>
        <p id="p_07810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07810"> Referring to Associative Array Elements</p>
        <p id="p_07811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07811"> The main difference between associative arrays and ordinary arrays is that associative array subscripts can be any scalar value. For example, the following statement refers to an element of the associative array % fruit:</p>
        <p id="p_07812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07812"> $fruit{"bananas"} = 1;</p>
        <p id="p_07813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07813"> The subscript for this array element is bananas. Any scalar value can be a subscript. For example:</p>
        <p id="p_07814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07814"> $fruit{"black_currant"}</p>
        <p id="p_07815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07815"> $number{3.14159}</p>
        <p id="p_07816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07816"> $integer{-7}</p>
        <p id="p_07817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07817"> A scalar variable can be used as a subscript, as follows:</p>
        <p id="p_07818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07818"> $fruit{$my_fruit}</p>
        <p id="p_07819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07819"> Here, the contents of $my_f ruit become the subscript into the associative array %f ruit.</p>
        <p id="p_07820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07820"> When an array element is referenced, as in the previous example, the name of the array element is preceded by a $ character, not the % character. As with array variables, this tells the Perl interpreter that this is a single scalar item and is to be treated as such.</p>
        <p id="p_07821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07821"> NOTE</p>
        <p id="p_07822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07822"> Subscripts for associative array elements are always enclosed in brace brackets ({ }), not square brackets ([ ]). This ensures that the Perl interpreter is always able to distinguish associative array elements from other array elements.</p>
        <p id="p_07823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07823"> Adding Elements to an Associative Array</p>
        <p id="p_07824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07824"> The easiest way to create an associative array item is just to assign to it. For example, the statement</p>
        <p id="p_07825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07825"> $fruit{"bananas"} = 1;</p>
        <p id="p_07826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07826"> assigns 1 to the element bananas of the associative array % fruit. If this element does not exist, it is created. If the array % fruit has not been referred to before, it also is created.</p>
        <p id="p_07827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07827"> This feature makes it easy to use associative arrays to count occurrences of items. For example, Listing 10.2 shows how you can use associative arrays to count the number of capitalized words in an input file. Note how much simpler this program is than the one in Listing 10.1, which accomplishes the same task.</p>
        <p id="p_07828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07828"> Type</p>
        <p id="p_07829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07829"> Listing 10.2. A program that uses an associative array to count the number of capitalized words in a file.</p>
        <p id="p_07830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07830"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07831"> 2 :</p>
        <p id="p_07832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07832"> 3: while ($inputline = &lt;STDIN&gt;) {</p>
        <p id="p_07833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07833"> 4: while ($inputline =~ /\b[A-Z]\S+/g) {</p>
        <p id="p_07834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07834"> 5: $word = $&amp;;</p>
        <p id="p_07835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07835"> 6: $word =~ s/# remove  punctuation</p>
        <p id="p_07836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07836"> 7:  $wordlist{$word} += 1;</p>
        <p id="p_07837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07837"> 8 : }</p>
        <p id="p_07838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07838"> 9</p>
        <p id="p_07839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07839"> }</p>
        <p id="p_07840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07840"> 10</p>
        <p id="p_07841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07841"> 11 :</p>
        <p id="p_07842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07842"> 12:</p>
        <p id="p_07843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07843"> 13:</p>
        <p id="p_07844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07844"> print ("Capitalized words and number of occurrences:\n"); foreach $capword (keys(%wordlist)) {</p>
        <p id="p_07845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07845"> print ("$capword: $wordlist{$capword}\n");</p>
        <p id="p_07846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07846"> }</p>
        <p id="p_07847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07847"> Output</p>
        <p id="p_07848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07848"> $ programl0_2</p>
        <p id="p_07849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07849"> Here is a line of Input.</p>
        <p id="p_07850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07850"> This Input contains some Capitalized words. A D</p>
        <p id="p_07851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07851"> Capitalized words and number of occurrences: This: 1 Input: 2 Here: 1</p>
        <p id="p_07852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07852"> Capitalized: 1</p>
        <p id="p_07853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07853"> $</p>
        <p id="p_07854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07854"> Analysis</p>
        <p id="p_07855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07855"> As you can see, this program is much simpler than the one in Listing 10.1. The previous program required 20 lines of code to read input and store the counts for each word; this program requires only seven.</p>
        <p id="p_07856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07856"> As before, this program reads one line of input at a time from the standard input file. The loop starting in line 4 iterates once for each capitalized word found in the input line; each match is assigned, in turn, to the scalar variable $word.</p>
        <p id="p_07857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07857"> Line 7 uses the associative array %wordlist to keep track of the capitalized words. Because associative arrays can use any value as a subscript for an element, this line uses the word itself as a subscript. Then, the element of the array corresponding to the word has 1 added to its value.</p>
        <p id="p_07858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07858"> For example, when the word Here is read in, the associative array element $wordlist { "Here" }</p>
        <p id="p_07859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07859"> has 1 added to its value.</p>
        <p id="p_07860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07860"> Lines 11-13 print the elements of the associative array. Line 11 contains a call to a special built-in function,  keys.  This function returns a list consisting of the subscripts of the associative array; the f oreach  statement then loops through this list, iterating once for each element of the associative array. Each subscript of the associative array is assigned, in turn, to the local variable  $capword; this example, this means that  $capword  is assigned  Here, Input, Capitalized,  and  This-one per each iteration of the  for each  loop.</p>
        <p id="p_07861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07861"> Warning</p>
        <p id="p_07862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07862"> An important fact to remember is that associative arrays  always  are stored in "random" order. (Actually, it's the order that ensures fastest access, but, effectively, it is random.) This means that if you use keys to access all of the elements of an associative array, there is no guarantee that the elements will appear in any given order. In particular, the elements do not always appear in the order in which they are created.</p>
        <p id="p_07863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07863"> To control the order in which the associative array elements appear, use  sort  to sort the elements returned by  keys.</p>
        <p id="p_07864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07864"> foreach $capword (sort keys(%wordlist)) {</p>
        <p id="p_07865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07865"> print ("$capword: $wordlist{$capword}\n");</p>
        <p id="p_07866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07866"> }</p>
        <p id="p_07867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07867"> When line 10 of Listing 10.2 is modified to include a call to sort, the associative array elements appear in sorted order.</p>
        <p id="p_07868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07868"> Creating Associative Arrays</p>
        <p id="p_07869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07869"> You can create an associative array with a single assignment. To do this, alternate the array subscripts and their values. For example:</p>
        <p id="p_07870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07870"> %fruit = ("apples", 17, "bananas", 9, "oranges", "none");</p>
        <p id="p_07871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07871"> This assignment creates an associative array of three elements:</p>
        <p id="p_07872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07872"> • An element with subscript apples, whose value is 17</p>
        <p id="p_07873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07873"> • An element with subscript bananas, whose value is 9</p>
        <p id="p_07874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07874"> An element with subscript oranges, whose value is none</p>
        <p id="p_07875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07875"> Warning</p>
        <p id="p_07876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07876"> Again, it is important to remember that the elements of associative arrays are not guaranteed to be in any particular order, even if you create the entire array at once.</p>
        <p id="p_07877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07877"> NOTE</p>
        <p id="p_07878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07878"> Perl version 5 enables you to use either =&gt; or , to separate array subscripts and values when you assign a list to an associative array. For example:</p>
        <p id="p_07879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07879"> %fruit = ("apples" =&gt; 17, "bananas" =&gt; 9, "oranges" =&gt; "none");</p>
        <p id="p_07880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07880"> This statement is identical to the previous one, but is easier to understand; the use of =&gt; makes it easier to see which subscript is associated with which value.</p>
        <p id="p_07881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07881"> As with any associative array, you always can add more elements to the array later on. For example:</p>
        <p id="p_07882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07882"> $fruit{"cherries"} = 5;</p>
        <p id="p_07883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07883"> This adds a fourth element, cherries, to the associative array %f ruit, and gives it the value 5.</p>
        <p id="p_07884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07884"> Copying Associative Arrays from Array Variables</p>
        <p id="p_07885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07885"> The list of subscripts and values assigned to % fruit in the previous example is an ordinary list like any other. This means that you can create an associative array from the contents of an array variable. For example:</p>
        <p id="p_07886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07886"> @fruit = ("apples", 6, "cherries", 8, "oranges", 11); %fruit = @fruit;</p>
        <p id="p_07887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07887"> The second statement creates an associative array of three elements-apples, cherries, and oranges-and assigns it to % fruit.</p>
        <p id="p_07888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07888"> Warning</p>
        <p id="p_07889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07889"> If you are assigning a list or the contents of an array variable to an associative array, make sure that the list contains an even number of elements, because each pair of elements corresponds to the subscript and the value of an associative array element.</p>
        <p id="p_07890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07890"> Similarly, you can copy one associative array into another. For example:</p>
        <p id="p_07891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07891"> %fruitl = ("apples", 6, "cherries", 8, "oranges", 11); %fruit2 = %fruitl;</p>
        <p id="p_07892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07892"> You can assign an associative array to an ordinary array variable in the same way. For example:</p>
        <p id="p_07893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07893"> %fruit = ("grapes", 11, "lemons",  27);</p>
        <p id="p_07894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07894"> @fruit = %fruit;</p>
        <p id="p_07895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07895"> However, this might not be as useful, because the order of the array elements is not defined. Here, the array variable @ fruit is assigned either the four-element list</p>
        <p id="p_07896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07896"> ("grapes", 11, "lemons", 27)</p>
        <p id="p_07897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07897"> or the list</p>
        <p id="p_07898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07898"> ("lemons", 27, "grapes", 11)</p>
        <p id="p_07899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07899"> depending on how the associative array is sorted.</p>
        <p id="p_07900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07900"> You can also assign to several scalar variables and an associative array at the same time.</p>
        <p id="p_07901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07901"> ($varl, $var2, %myarray) = @list;</p>
        <p id="p_07902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07902"> Here, the first element of @list is assigned to $varl, the second to $var2, and the rest to %myarray.</p>
        <p id="p_07903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07903"> Finally, an associative array can be created from the return value of a built-in function or user-defined subroutine that returns a list. Listing 10.3 is an example of a simple program that does just that. It takes the return value from split, which is a list, and assigns it to an associative array variable.</p>
        <p id="p_07904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07904"> Type</p>
        <p id="p_07905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07905"> Listing 10.3. A program that uses the return value from a built-in function to create an associative array.</p>
        <p id="p_07906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07906"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07907"> 2 :</p>
        <p id="p_07908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07908"> 3: $inputline = &lt;STDIN&gt;;</p>
        <p id="p_07909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07909"> 4: $inputline =~ s/ A \s+|\s+\n$//g;</p>
        <p id="p_07910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07910"> 5: %fruit = split (/\s+/, $inputline);</p>
        <p id="p_07911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07911"> 6: print ("Number of bananas: $fruit{\"bananas\"}\n");</p>
        <p id="p_07912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07912"> Output</p>
        <p id="p_07913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07913"> $ programl0_3</p>
        <p id="p_07914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07914"> oranges 5 apples 7 bananas 11 cherries 6 Number of bananas: 11 $</p>
        <p id="p_07915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07915"> Analysis</p>
        <p id="p_07916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07916"> I This program reads a line of input from the standard input file and eliminates the leading and trailing white space. Line 5 then calls split, which breaks the line into words. In this example, split returns the following list:</p>
        <p id="p_07917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07917"> ("oranges", 5, "apples", 7, "bananas", 11,</p>
        <p id="p_07918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07918"> "cherries", 6)</p>
        <p id="p_07919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07919"> This list is then assigned to the associative array % fruit. This assignment creates an associative array with four elements:</p>
        <p id="p_07920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07920"> Element Value</p>
        <p id="p_07921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07921"> oranges 5</p>
        <p id="p_07922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07922"> apples 7</p>
        <p id="p_07923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07923"> bananas 11</p>
        <p id="p_07924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07924"> cherries 6</p>
        <p id="p_07925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07925"> Line 6 then prints the value of the element bananas, which is 11.</p>
        <p id="p_07926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07926"> Adding and Deleting Array Elements</p>
        <p id="p_07927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07927"> As you've seen, you can add an element to an associative array by assigning to an element not previously seen, as follows:</p>
        <p id="p_07928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07928"> $fruit{"lime"} = 1;</p>
        <p id="p_07929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07929"> This statement creates a new element of %f ruit with index lime and gives it the value 1.</p>
        <p id="p_07930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07930"> To delete an element, use the built-in function delete. For example, the following statement deletes the element orange from the array % fruit:</p>
        <p id="p_07931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07931"> delete ($fruit{"orange"}) ;</p>
        <p id="p_07932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07932"> Don't</p>
        <p id="p_07933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07933"> DO use the  delete  function to delete an element of an associative array; it's the only way to delete elements.</p>
        <p id="p_07934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07934"> DON'T use the built-in functions  push, pop, shift,  or  splice  with associative arrays because the position of any particular element in the array is not guaranteed.</p>
        <p id="p_07935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07935"> Listing Array Indexes and Values</p>
        <p id="p_07936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07936"> As you saw in Listing 10.2, the keys function retrieves a list of the subscripts used in an associative array. The following is an example:</p>
        <p id="p_07937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07937"> %fruit = ("apples", 9,</p>
        <p id="p_07938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07938"> "bananas", 23,</p>
        <p id="p_07939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07939"> "cherries", 11);</p>
        <p id="p_07940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07940"> @fruitsubs = keys(%fruits);</p>
        <p id="p_07941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07941"> Here, @f ruitsubs is assigned the list consisting of the elements apples, bananas, and cherries. Note once again that this list is in no particular order. To retrieve the list in alphabetical order, use sort on the list.</p>
        <p id="p_07942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07942"> @fruitindexes = sort keys(%fruits));</p>
        <p id="p_07943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07943"> This produces the list ("apples", "bananas", "cherries").</p>
        <p id="p_07944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07944"> To retrieve a list of the values stored in an associative array, use the built-in function values. The following is an example:</p>
        <p id="p_07945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07945"> %fruit = ("apples", 9,</p>
        <p id="p_07946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07946"> "bananas", 23,</p>
        <p id="p_07947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07947"> "cherries", 11);</p>
        <p id="p_07948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07948"> @fruitvalues = values(%fruits);</p>
        <p id="p_07949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07949"> Here, @f ruitvalues contains the list (9, 23, 11), not necessarily in this order.</p>
        <p id="p_07950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07950"> Looping Using an Associative Array</p>
        <p id="p_07951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07951"> As you've seen, you can use the built-in function keys with the f oreach statement to loop through an associative array. The following is an example:</p>
        <p id="p_07952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07952"> %records = ("Maris", 61, "Aaron", 755, "Young", 511); foreach $holder (keys(%records)) {</p>
        <p id="p_07953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07953"> # stuff goes here</p>
        <p id="p_07954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07954"> }</p>
        <p id="p_07955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07955"> The variable $holder is assigned Aaron, Maris, and Young on successive iterations of the loop</p>
        <p id="p_07956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07956"> (although not necessarily in that order).</p>
        <p id="p_07957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07957"> This method of looping is useful, but it is inefficient. To retrieve the value associated with a subscript, the program must look it up in the array again, as follows:</p>
        <p id="p_07958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07958"> foreach $holder (keys(%records)) {</p>
        <p id="p_07959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07959"> $record = %records{$holder};</p>
        <p id="p_07960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07960"> }</p>
        <p id="p_07961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07961"> Perl provides a more efficient way to work with associative array subscripts and their values, using the built-in function each, as follows:</p>
        <p id="p_07962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07962"> %records = ("Maris", 61, "Aaron", 755, "Young", 511); while (($holder, $record) = each(%records)) {</p>
        <p id="p_07963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07963"> # stuff goes here</p>
        <p id="p_07964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07964"> }</p>
        <p id="p_07965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07965"> Every time the each function is called, it returns a two-element list. The first element of the list is the subscript for a particular element of the associative array. The second element is the value associated with that particular subscript.</p>
        <p id="p_07966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07966"> For example, the first time each is called in the preceding code fragment, the pair of scalar variables ($holder, $record) is assigned one of the lists ("Maris", 61),("Aaron" , 755),or ("Young", 511). (Because associative arrays are not stored in any particular order, any of these lists could be assigned first.) If ("Maris", 61) is returned by the first call to each,Maris is assigned to $holder and 61 is assigned to $ record.</p>
        <p id="p_07967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07967"> When each is called again, it assigns a different list to the pair of scalar variables specified. Subsequent calls to each assign further lists, and so on until the associative array is exhausted. When there are no more elements left in the associative array, each returns the empty list.</p>
        <p id="p_07968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07968"> NOTE</p>
        <p id="p_07969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07969"> Don't add a new element to an associative array or delete an element from it if you are using the  each  statement on it. For example, suppose you are looping through the associative array  %records using the following loop:</p>
        <p id="p_07970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07970"> while (($holder, $record) = each (%records)) {</p>
        <p id="p_07971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07971"> # code goes here</p>
        <p id="p_07972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07972"> }</p>
        <p id="p_07973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07973"> Adding a new record to  %records,  such as $records{"Rose"} = 4256; or deleting a record, as in</p>
        <p id="p_07974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07974"> delete $records{"Cobb"} ;</p>
        <p id="p_07975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07975"> makes the behavior of each unpredictable. This should be avoided.</p>
        <p id="p_07976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07976"> Creating Data Structures Using Associative Arrays</p>
        <p id="p_07977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07977"> You can use associative arrays to simulate a wide variety of data structures found in high-level programming languages. This section describes how you can implement the following data structures in Perl using associative arrays:</p>
        <p id="p_07978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07978"> • Linked lists</p>
        <p id="p_07979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07979"> • Structures</p>
        <p id="p_07980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07980"> • Trees</p>
        <p id="p_07981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07981"> • Databases</p>
        <p id="p_07982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07982"> NOTE</p>
        <p id="p_07983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07983"> The remainder of today's lesson describes applications of associative arrays but does not introduce any new features of Perl. If you are not interested in applications of associative arrays, you can skip to the next chapter without suffering any loss of general instruction.</p>
        <p id="p_07984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07984"> Linked Lists</p>
        <p id="p_07985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07985"> A  linked list  is a simple data structure that enables you to store items in a particular order. Each element of the linked list contains two fields:</p>
        <p id="p_07986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07986"> • The value associated with this element</p>
        <p id="p_07987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07987"> • A reference, or  pointer , to the next element in the list</p>
        <p id="p_07988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07988"> Also, a special  header variable  points to the first element in the list.</p>
        <p id="p_07989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07989"> Pictorially, a linked list can be represented as in Figure 10.1. As you can see, each element of the list points to the next.</p>
        <p id="p_07990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07990"> Figure 10.1: A  linked list.</p>
        <p id="p_07991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07991"> In Perl, a linked list can easily be implemented using an associative array because the value of one associative array element can be the subscript for the next. For example, the following associative array is actually a linked list of words in alphabetical order:</p>
        <p id="p_07992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07992"> In this example, the scalar variable $header contains the first word in the list. This word, abel, is also the subscript of the first element of the associative array. The value of the first element of this array, baker, is the subscript for the second element, and so on, as illustrated in Figure 10.2.</p>
        <p id="p_07993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07993"> Figure 10.2:  A linked list o f words in alphabetical order.</p>
        <p id="p_07994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07994"> The value of the last element of the subscript, delta, is the null string. This indicates the end of the list.</p>
        <p id="p_07995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07995"> Linked lists are most useful in applications where the amount of data to be processed is not known, or grows as the program is executed. Listing 10.4 is an example of one such application. It uses a linked list to print the words of a file in alphabetical order.</p>
        <p id="p_07996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07996"> Listing 10.4. A program that uses an associative array to build a linked list.</p>
        <p id="p_07997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07997"> 1: #!/usr/local/bin/perl</p>
        <p id="p_07998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07998"> 2 :</p>
        <p id="p_07999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_07999"> 3:</p>
        <p id="p_08000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08000"> # initialize list to empty</p>
        <p id="p_08001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08001"> 4:</p>
        <p id="p_08002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08002"> 5:</p>
        <p id="p_08003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08003"> 6:</p>
        <p id="p_08004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08004"> 7:</p>
        <p id="p_08005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08005"> 8:</p>
        <p id="p_08006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08006"> 9:</p>
        <p id="p_08007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08007"> 10:</p>
        <p id="p_08008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08008"> 11 :</p>
        <p id="p_08009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08009"> 12:</p>
        <p id="p_08010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08010"> 13:</p>
        <p id="p_08011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08011"> 14:</p>
        <p id="p_08012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08012"> 15:</p>
        <p id="p_08013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08013"> 16:</p>
        <p id="p_08014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08014"> 17:</p>
        <p id="p_08015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08015"> 18:</p>
        <p id="p_08016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08016"> 19:</p>
        <p id="p_08017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08017"> 20 :</p>
        <p id="p_08018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08018"> 21:</p>
        <p id="p_08019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08019"> 22 :</p>
        <p id="p_08020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08020"> 23:</p>
        <p id="p_08021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08021"> 24:</p>
        <p id="p_08022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08022"> 25:</p>
        <p id="p_08023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08023"> 26:</p>
        <p id="p_08024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08024"> 27:</p>
        <p id="p_08025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08025"> 28:</p>
        <p id="p_08026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08026"> 29 :</p>
        <p id="p_08027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08027"> $header = "";</p>
        <p id="p_08028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08028"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_08029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08029"> # remove leading and trailing spaces $line =~ s/ A \s+|\s+$//g;</p>
        <p id="p_08030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08030"> @words = split (/\s+/, $line); foreach $word (@words) {</p>
        <p id="p_08031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08031"> # remove closing punctuation, if any $word =~ s/[</p>
        <p id="p_08032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08032"> # convert all words to lower case $word =~ tr/A-Z/a-z/; &amp;add_word_to_list($word);</p>
        <p id="p_08033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08033"> }</p>
        <p id="p_08034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08034"> }</p>
        <p id="p_08035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08035"> &amp;print_list;</p>
        <p id="p_08036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08036"> sub add_word_to_list { local($word) = local($pointer) ;</p>
        <p id="p_08037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08037"> # if list is empty, add first item</p>
        <p id="p_08038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08038"> if ($header eq "") {</p>
        <p id="p_08039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08039"> $header = $word;</p>
        <p id="p_08040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08040"> $wordlist{$word} = return;</p>
        <p id="p_08041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08041"> }</p>
        <p id="p_08042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08042"> # if word identical to first element in list.</p>
        <p id="p_08043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08043"> 30:</p>
        <p id="p_08044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08044"> 31:</p>
        <p id="p_08045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08045"> 32:</p>
        <p id="p_08046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08046"> 33:</p>
        <p id="p_08047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08047"> 34:</p>
        <p id="p_08048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08048"> 35:</p>
        <p id="p_08049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08049"> 36:</p>
        <p id="p_08050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08050"> 37:</p>
        <p id="p_08051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08051"> 38:</p>
        <p id="p_08052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08052"> 39:</p>
        <p id="p_08053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08053"> 40:</p>
        <p id="p_08054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08054"> 41:</p>
        <p id="p_08055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08055"> 42:</p>
        <p id="p_08056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08056"> 43:</p>
        <p id="p_08057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08057"> 44:</p>
        <p id="p_08058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08058"> 45:</p>
        <p id="p_08059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08059"> 46:</p>
        <p id="p_08060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08060"> 47:</p>
        <p id="p_08061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08061"> 48:</p>
        <p id="p_08062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08062"> 49: }</p>
        <p id="p_08063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08063"> 50:</p>
        <p id="p_08064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08064"> # do nothing</p>
        <p id="p_08065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08065"> return if ($header eq $word);</p>
        <p id="p_08066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08066"> # see whether word should be the new</p>
        <p id="p_08067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08067"> # first word in the list if ($header gt $word) {</p>
        <p id="p_08068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08068"> $wordlist{$word} = $header;</p>
        <p id="p_08069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08069"> $header = $word;</p>
        <p id="p_08070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08070"> return;</p>
        <p id="p_08071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08071"> }</p>
        <p id="p_08072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08072"> # find place where word belongs $pointer = $header;</p>
        <p id="p_08073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08073"> while ($wordlist{$pointer} ne "" &amp;&amp;</p>
        <p id="p_08074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08074"> $wordlist{$pointer} It $word) { $pointer = $wordlist{$pointer};</p>
        <p id="p_08075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08075"> }</p>
        <p id="p_08076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08076"> # if word already seen, do nothing return if ($word eq $wordlist{$pointer}); $wordlist{$word} = $wordlist{$pointer}; $wordlist{$pointer} = $word;</p>
        <p id="p_08077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08077"> 51: sub print_list {</p>
        <p id="p_08078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08078"> 52: local ($pointer);</p>
        <p id="p_08079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08079"> 53: print ("Words in this file:\n");</p>
        <p id="p_08080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08080"> 54: $pointer = $header;</p>
        <p id="p_08081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08081"> 55: while ($pointer ne "") {</p>
        <p id="p_08082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08082"> 56:</p>
        <p id="p_08083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08083"> print  ("$pointer\n");</p>
        <p id="p_08084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08084"> 57:</p>
        <p id="p_08085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08085"> $pointer = $wordlist{$pointer};</p>
        <p id="p_08086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08086"> 58: }</p>
        <p id="p_08087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08087"> 59: }</p>
        <p id="p_08088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08088"> Output</p>
        <p id="p_08089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08089"> $ programl0_4 Here are some words.</p>
        <p id="p_08090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08090"> Here are more words.</p>
        <p id="p_08091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08091"> Here are still more words. A D</p>
        <p id="p_08092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08092"> Words in this file:</p>
        <p id="p_08093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08093"> are</p>
        <p id="p_08094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08094"> here</p>
        <p id="p_08095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08095"> more</p>
        <p id="p_08096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08096"> some</p>
        <p id="p_08097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08097"> still</p>
        <p id="p_08098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08098"> words</p>
        <p id="p_08099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08099"> $</p>
        <p id="p_08100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08100"> Analysis</p>
        <p id="p_08101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08101"> I The logic of this program is a little complicated, but don't despair. Once you understand how this works, you have all the information you need to build any data structure you like, no matter how complicated.</p>
        <p id="p_08102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08102"> This program is divided into three parts, as follows:</p>
        <p id="p_08103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08103"> • The main program, which reads input and transforms it into the desired format</p>
        <p id="p_08104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08104"> • The subroutine add_word_to_list, which builds the linked list of sorted words</p>
        <p id="p_08105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08105"> • The subroutine print_list, which prints the list of words</p>
        <p id="p_08106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08106"> Lines 3-17 contain the main program. Line 4 initializes the list of words by setting the header variable $ header to the null string. The loop beginning in line 5 reads one line of input at a time. Line 7 removes leading and trailing spaces from the line, and line 8 splits the line into words.</p>
        <p id="p_08107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08107"> The inner f oreach loop in lines 9-15 processes one word of the input line at a time. If the final character of a word is a punctuation character, line 11 removes it; this ensures that, for example, word. (word with a period) is considered identical to word (without a period). Line 13 converts the word to all lowercase characters, and line 14 passes the word to the subroutine add_word_to_list.</p>
        <p id="p_08108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08108"> This subroutine first executes line 24, which checks whether the linked list of words is empty. If it is, line 25 assigns this word to $ header, and line 26 creates the first element of the list, which is stored in the associative array %wordlist. In this example, the first word read in is here (Here converted to lowercase), and the list looks like Figure 10.3.</p>
        <p id="p_08109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08109"> Figure 10.3:  The linked list with one element in it.</p>
        <p id="p_08110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08110"> At this point, the header variable $header contains the value here, which is also the subscript for the element of %wordlist that has just been created. This means that the program can reference %wordlist by using $header as a subscript, as follows:</p>
        <p id="p_08111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08111"> $wordlist{$header}</p>
        <p id="p_08112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08112"> Variables such as $header that contain a reference to another data item are called  pointers.  Here, $header points to the first element of %wordlist.</p>
        <p id="p_08113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08113"> If the list is not empty, line 31 checks whether the first item of the list is identical to the word currently being checked. To do this, it compares the current word to the contents of $header, which is the first item in the list. If the two are identical, there is no need to add the new word to the list, because it is already there; therefore, the subroutine returns without doing anything.</p>
        <p id="p_08114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08114"> The next step is to check whether the new word should be the first word in the list, which is the case if the new word is alphabetically ahead of the existing first word. Line 34 checks this.</p>
        <p id="p_08115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08115"> If the new word is to go first, the list is adjusted as follows:</p>
        <p id="p_08116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08116"> 1. A new list element is created. The subscript of this element is the new word, and its value is the existing first word.</p>
        <p id="p_08117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08117"> 2. The new word is assigned to the header variable.</p>
        <p id="p_08118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08118"> To see how this adjustment works, consider the sample input provided. In this example, the second word to be processed is are. Because are belongs before here, the array element $wordlist { "are" } is created, and is given the value here. The header variable $header is assigned the value are. This means the list now looks like Figure 10.4.</p>
        <p id="p_08119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08119"> Figure 10.4:  The linked list with two elements in it.</p>
        <p id="p_08120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08120"> The header variable $header now points to the list element with the subscript are, which is $wordlist { "are" }. The value of $wordlist { "are" } is here, which means that the program can access $wordlist { "here" } from $wordlist{ "are" }. For example:</p>
        <p id="p_08121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08121"> $reference = $wordlist{"are"}; print ("$wordlist{$reference}\n") ;</p>
        <p id="p_08122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08122"> The value here is assigned to $reference, and print prints $wordlist {$reference}, which is $wordlist { "here" }.</p>
        <p id="p_08123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08123"> Because you can access $wordlist{ "here" } from $wordlist{ "are" }, $wordlist { "are" } is a pointer to $wordlist { "here" }.</p>
        <p id="p_08124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08124"> If the word does not belong at the front of the list, lines 40-44 search for the place in the list where the word does belong, using a local variable, $pointer. Lines 41-44 loop until the value stored in $wordlist {$pointer} is greater than or equal to $word. For example, Figure 10.5 illustrates where line 42 is true when the subroutine processes more.</p>
        <p id="p_08125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08125"> Figure 10.5:  The linked list when more is processed.</p>
        <p id="p_08126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08126"> Note that because the list is in alphabetical order, the value stored in $pointer is always less than the value stored in $word.</p>
        <p id="p_08127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08127"> If the word being added is greater than any word in the list, the conditional expression in line 41 eventually becomes true. This occurs, for example, when the subroutine processes some, as in Figure 10 . 6 .</p>
        <p id="p_08128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08128"> Figure 10.6:  The linked list when some is processed.</p>
        <p id="p_08129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08129"> Once the location of the new word has been determined, line 46 checks whether the word already is in the list. If it is, there is no need to do anything.</p>
        <p id="p_08130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08130"> If the word does not exist, lines 47 and 48 add the word to the list. First, line 47 creates a new element of %wordlist, which is $wordlist{$word}; its value is the value of $wordlist{$pointer}. This means that $wordlist {$word} and $wordlist {$pointer} now point to the same word, as in Figure 10.7.</p>
        <p id="p_08131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08131"> Figure 10.7:  The linked list as a new word is being added.</p>
        <p id="p_08132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08132"> Next, line 48 sets the value of $wordlist {$pointer} to the value stored in $word. This means that $wordlist {$pointer} now points to the new element, $wordlist {$word}, that was just created, as in Figure 10.8.</p>
        <p id="p_08133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08133"> Figure 10.8:  The linked list after the new word is added.</p>
        <p id="p_08134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08134"> Once the input file has been completely processed, the subroutine print_list prints the list, one element at a time. The local variable $pointer contains the current value being printed, and $wordlist {$pointer} contains the next value to be printed.</p>
        <p id="p_08135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08135"> NOTE</p>
        <p id="p_08136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08136"> Normally, you won't want to use a linked list in a program. It's easier just to use  sort  and  keys  to loop through an associative array in alphabetical order, as follows:</p>
        <p id="p_08137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08137"> foreach $word (sort keys(%wordlist)) {</p>
        <p id="p_08138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08138"> # print the sorted list, or whatever }</p>
        <p id="p_08139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08139"> However, the basic idea of a pointer, which is introduced here, is useful in other data structures, such as trees, which are described later on.</p>
        <p id="p_08140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08140"> Structures</p>
        <p id="p_08141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08141"> Many programming languages enable you to define collections of data called  structures.  Like lists, structures are collections of values; each element of a structure, however, has its own name and can be accessed by that name.</p>
        <p id="p_08142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08142"> Perl does not provide a way of defining structures directly. However, you can simulate a structure using an associative array. For example, suppose you want to simulate the following variable definition written in the C programming language:</p>
        <p id="p_08143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08143"> struct {</p>
        <p id="p_08144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08144"> int fieldl; int field2; int field3;</p>
        <p id="p_08145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08145"> } mystructvar;</p>
        <p id="p_08146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08146"> This C statement defines a variable named mystructvar, which contains three elements, named fieldl, field2, and field3.</p>
        <p id="p_08147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08147"> To simulate this using an associative array, all you need to do is define an associative array with three</p>
        <p id="p_08148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08148"> elements, and set the subscripts for these elements to f ieldl, f ield2, and f ield3. The following is an example:</p>
        <p id="p_08149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08149"> %mystructvar = ("fieldl",</p>
        <p id="p_08150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08150"> "field2", "",</p>
        <p id="p_08151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08151"> "field3",  "");</p>
        <p id="p_08152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08152"> Like the preceding C definition, this associative array, named %mystructvar, has three elements. The subscripts for these elements are fieldl, field2, and field3. The definition sets the initial values for these elements to the null string.</p>
        <p id="p_08153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08153"> As with any associative array, you can reference or assign the value of an element by specifying its subscript, as follows:</p>
        <p id="p_08154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08154"> $mystructvar{"fieldl"} = 17;</p>
        <p id="p_08155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08155"> To define other variables that use the same "structure," all you need to do is create other arrays that use the same subscript names.</p>
        <p id="p_08156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08156"> Trees</p>
        <p id="p_08157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08157"> Another data structure that is often used in programs is a  tree.  A tree is similar to a linked list, except that each element of a tree points to more than one other element.</p>
        <p id="p_08158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08158"> The simplest example of a tree is a  binary tree.  Each element of a binary tree, called a  node,  points to two other elements, called the  left child  and the  right child.  Each of these children points to two children of its own, and so on, as illustrated in Figure 10.9.</p>
        <p id="p_08159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08159"> Figure 10.9:  A binary tree.</p>
        <p id="p_08160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08160"> Note that the tree, like a linked list, is a one-way structure. Nodes point to children, but children don't point to their parents.</p>
        <p id="p_08161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08161"> The following terminology is used when describing trees:</p>
        <p id="p_08162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08162"> • Because each of the children of a node is a tree of its own, the left child and the right child are often called the  left subtree  and the  right subtree  of the node. (The terms  left branch  and  right branch  are also used.)</p>
        <p id="p_08163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08163"> • The "first" node of the tree (the node that is not a child of another node), is called the  root  of the tree.</p>
        <p id="p_08164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08164"> • Nodes that have no children are called  leaf nodes.</p>
        <p id="p_08165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08165"> There are several ways of implementing a tree structure using associative arrays. To illustrate one way of doing so, suppose that you wish to create a tree whose root has the value alpha and whose children have the values beta and gamma, as in Figure 10.10.</p>
        <p id="p_08166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08166"> Figure 10.10:  A binary tree with three nodes.</p>
        <p id="p_08167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08167"> Here, the left child of alpha is beta, and the right child of alpha is gamma.</p>
        <p id="p_08168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08168"> The problem to be solved is this: How can a program associate both beta and gamma with alpha? If the associative array that is to represent the tree is named %tree, do you assign the value of $tree { " alpha " } to be beta, or gamma, or both? How do you show that an element points to two other elements?</p>
        <p id="p_08169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08169"> There are several solutions to this problem, but one of the most elegant is as follows: Append the character strings left and right, respectively, to the name of a node in order to retrieve its children. For example, define alphaleft to point to beta and alpharight to point to gamma. In this scheme, if beta has children, betaleft and betaright point to their locations; similarly, gammalef t and gammaright point to the locations of the children of gamma, and so on.</p>
        <p id="p_08170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08170"> Listing 10.5 is an example of a program that creates a binary tree using this method and then traverses  it (accesses every node in the tree).</p>
        <p id="p_08171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08171"> Type</p>
        <p id="p_08172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08172"> Listing 10.5. A program that uses an associative array to represent a binary tree.</p>
        <p id="p_08173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08173"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08174"> 2:</p>
        <p id="p_08175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08175"> 3: $rootname</p>
        <p id="p_08176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08176"> 4: %tree = (</p>
        <p id="p_08177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08177"> 5:</p>
        <p id="p_08178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08178"> 6:</p>
        <p id="p_08179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08179"> 7:</p>
        <p id="p_08180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08180"> 8:</p>
        <p id="p_08181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08181"> 9:</p>
        <p id="p_08182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08182"> = "parent"; parentleft", parentright" childlleft", childlright" child21eft", child2right"</p>
        <p id="p_08183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08183"> childl",</p>
        <p id="p_08184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08184"> "child2", grandchildl", "grandchild2", grandchild3", "grandchild4");</p>
        <p id="p_08185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08185"> 10: # traverse tree, printing its elements</p>
        <p id="p_08186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08186"> 11: &amp;print_tree($rootname);</p>
        <p id="p_08187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08187"> 12:</p>
        <p id="p_08188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08188"> 13: sub print_tree {</p>
        <p id="p_08189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08189"> 14: local ($nodename) =</p>
        <p id="p_08190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08190"> 15: local ($leftchildname, $rightchildname);</p>
        <p id="p_08191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08191"> 16:</p>
        <p id="p_08192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08192"> 17: $leftchildname = $nodename . "left";</p>
        <p id="p_08193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08193"> 18: $rightchildname = $nodename . "right";</p>
        <p id="p_08194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08194"> 19: if ($tree{$leftchildname} ne "") {</p>
        <p id="p_08195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08195"> 20: &amp;print_tree($tree{$leftchildname});</p>
        <p id="p_08196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08196"> 21 : }</p>
        <p id="p_08197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08197"> 22: print ("$nodename\n");</p>
        <p id="p_08198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08198"> 23: if ($tree{$rightchildname} ne "") {</p>
        <p id="p_08199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08199"> 24: &amp;print_tree($tree{$rightchildname});</p>
        <p id="p_08200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08200"> 25: }</p>
        <p id="p_08201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08201"> 26: }</p>
        <p id="p_08202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08202"> Output</p>
        <p id="p_08203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08203"> $ programl0_5</p>
        <p id="p_08204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08204"> grandchildl</p>
        <p id="p_08205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08205"> childl</p>
        <p id="p_08206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08206"> grandchild2</p>
        <p id="p_08207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08207"> parent</p>
        <p id="p_08208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08208"> grandchild3</p>
        <p id="p_08209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08209"> child2</p>
        <p id="p_08210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08210"> grandchild4</p>
        <p id="p_08211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08211"> $</p>
        <p id="p_08212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08212"> Analysis</p>
        <p id="p_08213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08213"> This program creates the tree depicted in Figure 10.11.</p>
        <p id="p_08214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08214"> Figure 10.11:  The tree created by Listing 10.5.</p>
        <p id="p_08215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08215"> The associative array %tree stores the tree, and the scalar variable $rootname holds the name of the root of the tree. (Note that the grandchild nodes, such as grandchildl, are leaf nodes. There is no need to explicitly create grandchildlleft, grandchildlright, and so on because the value of any undefined associative array element is, by default, the null string.)</p>
        <p id="p_08216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08216"> After the tree has been created, the program calls the subroutine print_tree to traverse it and print its values. print_tree does this as follows:</p>
        <p id="p_08217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08217"> 1. Line 17 appends left to the name of the node being examined to produce the name of the left child, which is stored in $leftchildname. For example, if the root node, parent, is being examined, the value stored in $leftchildname is parentleft.</p>
        <p id="p_08218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08218"> 2 .  Similarly, line 18 appends right to the node name and stores the result in $rightchildname.</p>
        <p id="p_08219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08219"> 3 .  Line 19 checks whether the current node has a left child, which is true if</p>
        <p id="p_08220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08220"> $tree {$lef tchildname} is defined. (For example, parent has a left child, because $tree { "parentleft" } is defined.) If the current node has a left child, line 20 recursively calls print_tree to print the left subtree (the left child and its children).</p>
        <p id="p_08221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08221"> 4 .  Line 22 prints the name of the current node.</p>
        <p id="p_08222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08222"> 5 .  Line 23 checks whether the current node has a right child. If it does, line 24 recursively calls print_tree to print the right subtree.</p>
        <p id="p_08223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08223"> Note that print_tree prints the names of the nodes of the tree in the following order: left subtree, node, right subtree. This order of traversal is called  infix mode  or  infix traversal.  If you move line 22 to precede line 19, the node is printed first, followed by the left subtree and the right subtree; this order of traversal is called  prefix mode.  If you move line 22 to follow line 25, the node is printed after the subtrees are printed; this is called  postfix mode.</p>
        <p id="p_08224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08224"> Databases</p>
        <p id="p_08225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08225"> As you have seen, you can build a tree using an associative array. To do this, you build the associative array subscripts by joining character strings together (such as joining the node name and "left"). You can use this technique of joining strings together to use associative arrays to build other data structures.</p>
        <p id="p_08226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08226"> For example, suppose you want to create a database that contains the lifetime records of baseball players. Each record is to consist of the following:</p>
        <p id="p_08227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08227"> • For non-pitchers, a record consists of games played (GP), home runs (HR), runs batted in (RBI) and batting average (AYG). For example, the record on Lou Gehrig would read as follows:</p>
        <p id="p_08228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08228"> Gehrig: 2164 GP, 493 HR, 1991 RBI, .340 BA</p>
        <p id="p_08229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08229"> • For pitchers, a record consists of games pitched (GP), wins (W), and earned run average (ERA). For example, the record on Lefty Grove would read as follows:</p>
        <p id="p_08230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08230"> Grove: 616 GP, 300 W, 3.05 ERA</p>
        <p id="p_08231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08231"> To create a database containing player and pitcher records, you need the following fields:</p>
        <p id="p_08232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08232"> • A name field, for the player's name</p>
        <p id="p_08233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08233"> • A key indicating whether the player was a pitcher</p>
        <p id="p_08234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08234"> • The fields defined above</p>
        <p id="p_08235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08235"> You can use an associative array to simulate this in Perl. To do this, build the subscripts for the associative array by concatenating the name of the player with the name of the field being stored by this element of the array. For example, if the associative array is named  %playerbase, $playerbase { "GehrigRBI " }, it contains the career RBI total for Lou Gehrig.</p>
        <p id="p_08236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08236"> Listing 10.6 shows how to build a player database and how to sequentially print fields from each of the player records.</p>
        <p id="p_08237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08237"> Listing 10.6. A program that builds and prints a database.</p>
        <p id="p_08238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08238"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08239"> 2:</p>
        <p id="p_08240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08240"> 3: ©pitcherfields = ("NAME", "KEY", "GP", "W", "ERA");</p>
        <p id="p_08241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08241"> 4: ©playerfields = ("NAME", "KEY", "GP", "HR", "RBI", "BA");</p>
        <p id="p_08242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08242"> 5:</p>
        <p id="p_08243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08243"> 6: # Build the player database by reading from standard input.</p>
        <p id="p_08244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08244"> 7: # %playerbase contains the database, ©playerlist the list of</p>
        <p id="p_08245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08245"> 8: # players (for later sequential access).</p>
        <p id="p_08246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08246"> 9: $playercount = 0;</p>
        <p id="p_08247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08247"> 10: while ($input = &lt;STDIN&gt;) {</p>
        <p id="p_08248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08248"> 11 :</p>
        <p id="p_08249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08249"> 12:</p>
        <p id="p_08250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08250"> 13:</p>
        <p id="p_08251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08251"> 14:</p>
        <p id="p_08252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08252"> 15:</p>
        <p id="p_08253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08253"> 16:</p>
        <p id="p_08254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08254"> 17:</p>
        <p id="p_08255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08255"> 18:</p>
        <p id="p_08256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08256"> 19:</p>
        <p id="p_08257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08257"> 20 :</p>
        <p id="p_08258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08258"> 21:</p>
        <p id="p_08259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08259"> 22 :</p>
        <p id="p_08260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08260"> 23:</p>
        <p id="p_08261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08261"> 24:</p>
        <p id="p_08262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08262"> 25:</p>
        <p id="p_08263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08263"> 26:</p>
        <p id="p_08264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08264"> 27:</p>
        <p id="p_08265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08265"> 28:</p>
        <p id="p_08266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08266"> 29:</p>
        <p id="p_08267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08267"> 30:</p>
        <p id="p_08268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08268"> 31:</p>
        <p id="p_08269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08269"> 32:</p>
        <p id="p_08270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08270"> 33:</p>
        <p id="p_08271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08271"> 34:</p>
        <p id="p_08272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08272"> 35 :</p>
        <p id="p_08273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08273"> }</p>
        <p id="p_08274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08274"> $input =~ s/ A \s+|\s+$//g;</p>
        <p id="p_08275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08275"> ©words = split (/\s+/, $input);</p>
        <p id="p_08276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08276"> $playerlist[$playercount++] = $words[0]; if ($words[l] eq "player") {</p>
        <p id="p_08277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08277"> ©fields = ©playerfields;</p>
        <p id="p_08278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08278"> } else {</p>
        <p id="p_08279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08279"> ©fields = ©pitcherfields;</p>
        <p id="p_08280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08280"> }</p>
        <p id="p_08281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08281"> for ($count = 1; $count &lt;= ©words; $count++) {</p>
        <p id="p_08282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08282"> $playerbase{$words[0].$fields[$count-l]} = $words[$count-l] ;</p>
        <p id="p_08283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08283"> }</p>
        <p id="p_08284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08284"> # now, print out pitcher win totals and player home run totals foreach $player (©playerlist) { print ("$player: ") ;</p>
        <p id="p_08285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08285"> if ($playerbase{$player."KEY"} eq "player") {</p>
        <p id="p_08286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08286"> $value = $playerbase{$player."HR"}; print ("$value home runs\n");</p>
        <p id="p_08287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08287"> } else {</p>
        <p id="p_08288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08288"> $value = $playerbase{$player."W"}; print ("$value wins\n");</p>
        <p id="p_08289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08289"> }</p>
        <p id="p_08290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08290"> }</p>
        <p id="p_08291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08291"> Output</p>
        <p id="p_08292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08292"> $ programl0_6</p>
        <p id="p_08293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08293"> A D</p>
        <p id="p_08294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08294"> Gehrig: 493 home runs Ruth: 714 home runs Grove: 300 wins Williams: 521 home runs Koufax: 165 wins $</p>
        <p id="p_08295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08295"> Analysis</p>
        <p id="p_08296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08296"> I This program has been designed so this in mind, lines 3 and 4 define the fields that records.</p>
        <p id="p_08297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08297"> that it is easy to add new fields to the database. With are to be used when building the player and pitcher</p>
        <p id="p_08298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08298"> Lines 9-23 build the database. First, line 9 initializes $playercount to 0; this global variable keeps track of the number of players in the database.</p>
        <p id="p_08299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08299"> Lines 10-12 read a line from the standard input file, check whether the file is empty, remove leading and trailing white space from the line, and split the line into words.</p>
        <p id="p_08300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08300"> Line 13 adds the player name (the first word in the input line) to the list of player names stored in @playerlist. The counter $playercount then has 1 added to it; this reflects the new total number of players stored in the database.</p>
        <p id="p_08301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08301"> Lines 14-18 determine whether the new player is a pitcher or not. If the player is a pitcher, the names of the fields to be stored in this player record are to be taken from Qpitcherfields; otherwise, the names are to be taken from Qplayerfields. To simplify processing later on, another array variable, @f ields, is used to store the list of fields actually being used for this player.</p>
        <p id="p_08302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08302"> Lines 19-22 copy the fields into the associative array, one at a time. Each array subscript is made up of two parts: the name of the player and the name of the field being stored. For example, Sandy Koufax's pitching wins are stored in the array element  Kouf axW.  Note that neither the player name nor the field names appear in this loop; this means that you can add new fields to the list of fields without having to change this code.</p>
        <p id="p_08303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08303"> Lines 26-35 now search the database for all the win and home run totals just read in. Each iteration of the f oreach loop assigns a different player name to the local variable $player. Line 28 examines the contents of the array element named $player.  "KEY"  to determine whether the player is a pitcher.</p>
        <p id="p_08304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08304"> If the player is not a pitcher, lines 29-30 print out the player's home-run total by accessing the array element $player. "HR". If the player is a pitcher, the pitcher's win total is printed out by lines 32-33; these lines access the array element $player.  "W".</p>
        <p id="p_08305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08305"> Note that the database can be accessed randomly as well as sequentially. To retrieve, for example, Babe Ruth's lifetime batting average, you would access the array element</p>
        <p id="p_08306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08306"> $playerbase{  "RuthAVG"  }. If the record for a particular player is not stored in the database, attempting to access it will return the null string. For example, the following assigns the null string to $cobbavg because Ty Cobb is not in the player database:</p>
        <p id="p_08307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08307"> $cobbavg = $playerbase{"CobbAVG"};</p>
        <p id="p_08308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08308"> As you can see, associative arrays enable you to define databases with variable record lengths, accessible either sequentially or randomly. This gives you all the flexibility you need to use Perl as a database language.</p>
        <p id="p_08309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08309"> Example: A Calculator Program</p>
        <p id="p_08310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08310"> Listing 10.7 provides an example of what you can do with associative arrays and recursive subroutines. This program reads in an arithmetic expression, possibly spread over several lines, and builds a tree from it. The program then evaluates the tree and prints the result. The operators supported are  +,-,*,/,  and parentheses (to force precedence).</p>
        <p id="p_08311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08311"> This program is longer and more complicated than the programs you have seen so far, but stick with it. Once you understand this program, you will know enough to be able to write an entire compiler in Perl!</p>
        <p id="p_08312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08312"> Type</p>
        <p id="p_08313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08313"> Listing 10.7. A calculator program that uses trees.</p>
        <p id="p_08314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08314"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08315"> 2: # statements which initialize the program</p>
        <p id="p_08316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08316"> 3: $nextnodenum =1; # initialize node name generator</p>
        <p id="p_08317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08317"> &amp;get_next_item;</p>
        <p id="p_08318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08318"> # read first value from file</p>
        <p id="p_08319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08319"> 5: $treeroot = &amp;build_expr;</p>
        <p id="p_08320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08320"> 6: $result = &amp;get_result ($treeroot);</p>
        <p id="p_08321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08321"> 7: print ("the result is $result\n");</p>
        <p id="p_08322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08322"> 8: # Build an expression.</p>
        <p id="p_08323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08323"> 9: sub build_expr {</p>
        <p id="p_08324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08324"> 10: local ($currnode, $leftchild, $rightchild);</p>
        <p id="p_08325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08325"> 11: local ($operator);</p>
        <p id="p_08326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08326"> 12: $leftchild = &amp;build_add_operand;</p>
        <p id="p_08327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08327"> 13: if (&amp;is_next_item("+") || &amp;is_next_item("-")) {</p>
        <p id="p_08328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08328"> 14: $operator = &amp;get_next_item;</p>
        <p id="p_08329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08329"> 15: $rightchild = &amp;build_expr;</p>
        <p id="p_08330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08330"> 16: $currnode = &amp;get_new_node ($operator.</p>
        <p id="p_08331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08331"> $leftchild, $rightchild);</p>
        <p id="p_08332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08332"> } else {</p>
        <p id="p_08333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08333"> $currnode = $leftchild;</p>
        <p id="p_08334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08334"> 21 : }</p>
        <p id="p_08335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08335"> 22: # Build an operand for a + or - operator.</p>
        <p id="p_08336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08336"> 23: sub build_add_operand {</p>
        <p id="p_08337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08337"> 24: local ($currnode, $leftchild, $rightchild);</p>
        <p id="p_08338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08338"> 25: local ($operator);</p>
        <p id="p_08339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08339"> 26: $leftchild = &amp;build_mult_operand;</p>
        <p id="p_08340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08340"> if (&amp;is_next_item("*") || &amp;is_next_item("/")) {</p>
        <p id="p_08341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08341"> 27:</p>
        <p id="p_08342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08342"> 28 :</p>
        <p id="p_08343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08343"> 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44 : 45: 46: 47: 48 : 49: 50: 51: 52: 53: 54 :</p>
        <p id="p_08344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08344"> $operator = &amp;get_next_item; $rightchild = &amp;build_add_operand; $currnode = &amp;get_new_node ($operator, $leftchild, $rightchild);</p>
        <p id="p_08345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08345"> } else {</p>
        <p id="p_08346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08346"> $currnode = $leftchild;</p>
        <p id="p_08347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08347"> }</p>
        <p id="p_08348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08348"> }</p>
        <p id="p_08349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08349"> # Build an operand for the * or / operator, sub build_mult_operand {</p>
        <p id="p_08350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08350"> local ($currnode); if (&amp;is_next_item("(")) {</p>
        <p id="p_08351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08351"> # handle parentheses &amp;get_next_item; # get rid of "("</p>
        <p id="p_08352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08352"> $currnode = &amp;build_expr; if (! &amp;is_next_item(")")) {</p>
        <p id="p_08353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08353"> die ("Invalid expression");</p>
        <p id="p_08354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08354"> }</p>
        <p id="p_08355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08355"> &amp;get_next_item; # get rid of ")"</p>
        <p id="p_08356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08356"> } else {</p>
        <p id="p_08357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08357"> $currnode = &amp;get_new_node(&amp;get_next_item,</p>
        <p id="p_08358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08358"> I! If !1 H \ . r Jr</p>
        <p id="p_08359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08359"> }</p>
        <p id="p_08360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08360"> $currnode; # ensure correct return value</p>
        <p id="p_08361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08361"> }</p>
        <p id="p_08362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08362"> # Check whether the last item read matches</p>
        <p id="p_08363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08363"> # a particular operator.</p>
        <p id="p_08364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08364"> 55: 56: 57: 58 : 59: 60: 61: 62: 63: 64: 65: 66 : 67: 68 : 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79:</p>
        <p id="p_08365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08365"> sub is_next_item {</p>
        <p id="p_08366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08366"> local ($expected) =</p>
        <p id="p_08367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08367"> $curritem eq $expected;</p>
        <p id="p_08368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08368"> }</p>
        <p id="p_08369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08369"> # Return the last item read; read another item, sub get_next_item {</p>
        <p id="p_08370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08370"> local ($retitem);</p>
        <p id="p_08371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08371"> $retitem = $curritem;</p>
        <p id="p_08372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08372"> $curritem = &amp;read_item;</p>
        <p id="p_08373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08373"> $retitem;</p>
        <p id="p_08374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08374"> }</p>
        <p id="p_08375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08375"> # This routine actually handles reading from the standard</p>
        <p id="p_08376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08376"> # input file. sub read_item {</p>
        <p id="p_08377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08377"> local ($line); if ($curritem eq "EOF") {</p>
        <p id="p_08378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08378"> # we are already at end of file; do nothing return;</p>
        <p id="p_08379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08379"> }</p>
        <p id="p_08380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08380"> while ($wordsread == @words) {</p>
        <p id="p_08381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08381"> $line = &lt;STDIN&gt;; if ($line eq "") {</p>
        <p id="p_08382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08382"> $curritem = "EOF"; return;</p>
        <p id="p_08383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08383"> }</p>
        <p id="p_08384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08384"> 80:</p>
        <p id="p_08385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08385"> $line =~ s/\ (/ ( /g;</p>
        <p id="p_08386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08386"> 81:</p>
        <p id="p_08387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08387"> 82:</p>
        <p id="p_08388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08388"> 83:</p>
        <p id="p_08389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08389"> 84 : 85: 86 : 87: }</p>
        <p id="p_08390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08390"> $line =~  s/\)/ ) /g;</p>
        <p id="p_08391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08391"> $line =~ s/ A \s+|\s+$//g; @words = split (/\s+/, $line); $wordsread = 0;</p>
        <p id="p_08392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08392"> }</p>
        <p id="p_08393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08393"> $curritem = $words[$wordsread++];</p>
        <p id="p_08394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08394"> 88: # Create a tree node</p>
        <p id="p_08395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08395"> 89: sub get_new_node {</p>
        <p id="p_08396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08396"> 90:</p>
        <p id="p_08397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08397"> 91:</p>
        <p id="p_08398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08398"> 92:</p>
        <p id="p_08399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08399"> 93:</p>
        <p id="p_08400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08400"> 94 : 95: 96: 97: }</p>
        <p id="p_08401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08401"> local ($value, $leftchild, $rightchild) = local ($nodenum);</p>
        <p id="p_08402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08402"> $nodenum = $nextnodenum++;</p>
        <p id="p_08403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08403"> $tree{$nodenum} = $value;</p>
        <p id="p_08404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08404"> $tree{$nodenum . "left"} = $leftchild; $tree{$nodenum . "right"} = $rightchild; $nodenum; # return value</p>
        <p id="p_08405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08405"> @</p>
        <p id="p_08406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08406"> 98: # Calculate the result.</p>
        <p id="p_08407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08407"> 99: sub get_result {</p>
        <p id="p_08408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08408"> 100: local ($node) =</p>
        <p id="p_08409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08409"> 101: local ($nodevalue, $result);</p>
        <p id="p_08410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08410"> 102: $nodevalue = $tree{$node};</p>
        <p id="p_08411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08411"> 103:</p>
        <p id="p_08412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08412"> 104:</p>
        <p id="p_08413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08413"> 105:</p>
        <p id="p_08414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08414"> 106:</p>
        <p id="p_08415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08415"> 107:</p>
        <p id="p_08416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08416"> if ($nodevalue eq "") {</p>
        <p id="p_08417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08417"> die ("Bad tree");</p>
        <p id="p_08418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08418"> } elsif ($nodevalue eq "+") {</p>
        <p id="p_08419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08419"> $result = &amp;get_result($tree{$node &amp;get_result($tree{$node</p>
        <p id="p_08420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08420"> "left"}) + "right"}) ;</p>
        <p id="p_08421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08421"> 122 : }</p>
        <p id="p_08422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08422"> $ programl0_7 11 + 5 *</p>
        <p id="p_08423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08423"> (4 - 3)</p>
        <p id="p_08424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08424"> A D</p>
        <p id="p_08425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08425"> the result is 16</p>
        <p id="p_08426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08426"> $</p>
        <p id="p_08427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08427"> Analysis</p>
        <p id="p_08428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08428"> This program is divided into two main parts: a part that reads the input and produces a tree, and a part that calculates the result by traversing the tree.</p>
        <p id="p_08429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08429"> The subroutines build_expr, build_add_operand, and build_mult_operand build the tree. To see how they do this, first look at Figure 10.12 to see what the tree for the example, 11 + 5* (4 - 3), should look like.</p>
        <p id="p_08430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08430"> Figure 10.12:  The tree for the example in Listing 10.7.</p>
        <p id="p_08431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08431"> When this tree is evaluated, the nodes are searched in postfix order. First, the left subtree of the root is evaluated, then the right subtree, and finally the operation at the root.</p>
        <p id="p_08432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08432"> The rules followed by the three subroutines are spelled out in the following description:</p>
        <p id="p_08433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08433"> 1. An expression consists of one of the following:</p>
        <p id="p_08434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08434"> 1. An add_operand</p>
        <p id="p_08435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08435"> 2 .  An add_operand, a + or - operator, and an expression</p>
        <p id="p_08436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08436"> 2 .  An add_operand consists of one of the following:</p>
        <p id="p_08437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08437"> 1. A mult_operand</p>
        <p id="p_08438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08438"> 2 .  A mult_operand, a * or / operator, and an add_operand</p>
        <p id="p_08439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08439"> 3 .  A mult_operand consists of one of the following:</p>
        <p id="p_08440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08440"> 1. A number (a group of digits)</p>
        <p id="p_08441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08441"> 2 .  An expression enclosed in parentheses</p>
        <p id="p_08442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08442"> The subroutine build_expr handles all occurrences of condition 1; it is called (possibly recursively) whenever an expression is processed. Condition la covers the case in which the expression contains no + or - operators (unless they are enclosed in parentheses). Condition lb handles expressions that contain one or more + or - operators.</p>
        <p id="p_08443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08443"> The subroutine build_add_operand handles condition 2; it is called whenever an add_operand is processed. Condition 2a covers the case in which the add operand contains no * or / operators (except possibly in parentheses). Condition 2b handles add operands that contain one or more * or / operators.</p>
        <p id="p_08444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08444"> The subroutine build_mult_operand handles condition 3 and is called whenever a mult_operand is processed. Condition 3a handles multiplication operands that consist of a number. Condition 3b handles multiplication operators that consist of an expression in parentheses; to obtain the subtree for this expression, build_mult_operand calls build_expr recursively and then treats the returned subtree as a child of the node currently being built.</p>
        <p id="p_08445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08445"> Note that the tree built by build_expr, build_mult_operand, and build_add_operand is slightly different from the tree you saw in Listing 10.5. In that tree, the value of the node could also be used as the subscript into the associative array. In this tree, the value of the node might not be unique. To get around this problem, a separate counter creates numbers for each node, which are used when</p>
        <p id="p_08446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08446"> building the subscripts. For each node numbered n (where n is an integer), the following are true:</p>
        <p id="p_08447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08447"> • $tree {n} contains the value of the node, which is the number or operator associated with the node.</p>
        <p id="p_08448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08448"> • $tree {n. " left" } contains the number of the left child of the node.</p>
        <p id="p_08449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08449"> • $tree {n. " right" } contains the number of the right child of the node.</p>
        <p id="p_08450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08450"> The subroutines is_next_item, get_next_item, and read_item read the input from the standard input file and break it into numbers and operators. The subroutine get_next_item "pre-reads" the next item and stores it in the global variable $curritem; this lets is_next_item check whether the next item to be read matches a particular operator. To read an item, get_next_item calls read_item, which reads lines of input, breaks them into words, and returns the next word to be read.</p>
        <p id="p_08451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08451"> The subroutine get_new_node creates a tree node. To do this, it uses the contents of the global variable $nextnodenum to build the associative array subscripts associated with the node. $nextnodenum always contains a positive integer n, which means that the value associated with this node (which is a number or operator) is stored in $tree{n}. The location of the left and right children, if any, are stored in $tree {n. " lef t" } and $tree {n. " right" }.</p>
        <p id="p_08452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08452"> The subroutine get_result traverses the tree built by build_expr in postfix order (subtrees first), performing the arithmetic operations as it does so. get_result returns the final result, which is then printed.</p>
        <p id="p_08453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08453"> Note that the main part of this program is only eight lines long! This often happens in more complex programs. The main part of the program just calls subroutines, and the subroutines do the actual work.</p>
        <p id="p_08454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08454"> NOTE</p>
        <p id="p_08455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08455"> This program is just the tip of the iceberg: you can use associative arrays to simulate any data structure in any programming language.</p>
        <p id="p_08456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08456"> Summary</p>
        <p id="p_08457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08457"> In today's lesson, you learned about associative arrays, which are arrays whose subscripts can be any scalar value.</p>
        <p id="p_08458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08458"> You can copy a list to an associative array, provided there is an even number of elements in the list. Each pair of elements in the list is treated as an associated array subscript and its value. You also can copy an associative array to an ordinary array variable.</p>
        <p id="p_08459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08459"> To add an element to an associative array, just assign a value to an element whose subscript has not been previously seen. To delete an element, call the built-in function delete.</p>
        <p id="p_08460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08460"> The following three built-in functions enable you to use associative arrays with f oreach loops:</p>
        <p id="p_08461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08461"> • The built-in function keys retrieves each associative array subscript in turn.</p>
        <p id="p_08462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08462"> • The built-in function values retrieves each associative array value in turn.</p>
        <p id="p_08463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08463"> • The built-in function each retrieves each subscript-value pair in turn (as a two-element list).</p>
        <p id="p_08464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08464"> Associative arrays are not guaranteed to be stored in any particular order. To guarantee a particular order, use sort with keys, values, or each.</p>
        <p id="p_08465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08465"> Associative arrays can be used to simulate a wide variety of data structures, including linked lists, structures, trees, and databases.</p>
        <p id="p_08466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08466"> Q&amp;A</p>
        <p id="p_08467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08467"> Q: Are pointers implemented in Perl?</p>
        <p id="p_08468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08468"> A: Yes, if you are using Perl 5; they are discussed on  Day 18 ,  "References in Perl 5." Perl 4 does not</p>
        <p id="p_08469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08469"> support pointers.</p>
        <p id="p_08470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08470"> Q: How can I implement more complicated data structures using associative arrays?</p>
        <p id="p_08471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08471"> A:  All you need to do is design the structure you want to implement, name each of the fields in the</p>
        <p id="p_08472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08472"> structure, and use the name-concatenation trick to build your associative array subscript names.</p>
        <p id="p_08473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08473"> Q: What do I do if I want to build a tree that has multiple values at each node?</p>
        <p id="p_08474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08474"> A: There are many ways to do this. One way is to append  valuel, value2,  and so on, to the name</p>
        <p id="p_08475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08475"> of each node; for example, if the node is named  n7, n7valuel  could be the associative array subscript for the first value associated with the node,  n7 value2  could be the subscript for the second, and so on.</p>
        <p id="p_08476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08476"> Q: What do I do if I want to build a tree that has more than two children per node?</p>
        <p id="p_08477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08477"> A:  Again, there are many ways to do this. A possible solution is to use  childl, child2, child3,</p>
        <p id="p_08478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08478"> and so on, instead of  left  and  right.</p>
        <p id="p_08479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08479"> Q: How do I destroy a tree that I have created?</p>
        <p id="p_08480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08480"> A: To destroy a tree, write a subroutine that traverses the tree in postfix order (subtrees first). Destroy</p>
        <p id="p_08481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08481"> each subtree (by recursively calling your subroutine), and then destroy the node you are looking at by calling  delete.</p>
        <p id="p_08482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08482"> Note that you shouldn't use  keys  or  each  to access each element of the loop before deleting it. Deleting an element affects how the associative array is stored, which means that  keys  and  each might not behave the way you want them to.</p>
        <p id="p_08483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08483"> If you want to destroy the entire associative array in which the tree is stored, you can use the undef  function, which is described on  Day 14 ,  "Scalar-Conversion and List-Manipulation Functions."</p>
        <p id="p_08484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08484"> Workshop</p>
        <p id="p_08485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08485"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_08486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08486"> Quiz</p>
        <p id="p_08487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08487"> 1. Define the following terms:</p>
        <p id="p_08488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08488"> 1. associative array</p>
        <p id="p_08489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08489"> 2 .  pointer</p>
        <p id="p_08490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08490"> 3.  linked list</p>
        <p id="p_08491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08491"> 4.  binary tree</p>
        <p id="p_08492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08492"> 5.  node</p>
        <p id="p_08493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08493"> 6. child</p>
        <p id="p_08494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08494"> 2 .  What are the elements of the associative array created by the following assignment?</p>
        <p id="p_08495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08495"> %list = ("17.2", "hello", "there", "46", "e+6",  "88");</p>
        <p id="p_08496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08496"> 3.  What happens when you assign an associative array to an ordinary array variable?</p>
        <p id="p_08497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08497"> 4.  How can you create a linked list using an associative array?</p>
        <p id="p_08498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08498"> 5.  How many times does the following loop iterate?</p>
        <p id="p_08499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08499"> %list = ("first", "1", "second",  "2",  "third",  "3"); foreach $word (keys(%list)) {</p>
        <p id="p_08500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08500"> last if ($word == "second");</p>
        <p id="p_08501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08501"> }</p>
        <p id="p_08502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08502"> Exercises</p>
        <p id="p_08503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08503"> 1. Write a program that reads lines of input consisting of two words per line, such as bananas 16</p>
        <p id="p_08504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08504"> and creates an associative array from these lines. (The first word is to be the subscript and the second word the value.)</p>
        <p id="p_08505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08505"> 2 .  Write a program that reads a file and searches for lines of the form index word</p>
        <p id="p_08506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08506"> where word is a word to be indexed. Each indexed word is to be stored in an associative array, along with the line number on which it first occurs. (Subsequent occurrences can be ignored.) Print the resulting index.</p>
        <p id="p_08507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08507"> 3.  Modify the program created in Exercise 2 to store every occurrence of each index line. (Hint: Try building the associative array subscripts using the indexed word, a non-printable character, and a number.) Print the resulting index.</p>
        <p id="p_08508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08508"> 4.  Write a program that reads input lines consisting of a student name and five numbers representing the student's marks in English, history, mathematics, science, and geography, as follows:</p>
        <p id="p_08509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08509"> Jones 61 67 75 80 72</p>
        <p id="p_08510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08510"> Use an associative array to store these numbers in a database, and then print out the names of all students with failing grades (less than 50) along with the subjects they failed.</p>
        <p id="p_08511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08511"> 5.  BUG BUSTER: What is wrong with the following code?</p>
        <p id="p_08512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08512"> %list = ("Fred", 61, "John", 72,</p>
        <p id="p_08513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08513"> "Jack", 59, "Mary",  80);</p>
        <p id="p_08514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08514"> $surname = "Smith";</p>
        <p id="p_08515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08515"> foreach $firstname (keys (%list)) {</p>
        <p id="p_08516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08516"> %list{$firstname." ".$surname} = %list{$firstname};</p>
        <p id="p_08517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08517"> }</p>
        <p id="p_08518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08518"> Chapter 11</p>
        <p id="p_08519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08519"> Formatting Your Output</p>
        <p id="p_08520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08520"> CONTENTS</p>
        <p id="p_08521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08521"> •  Defining a Print Format</p>
        <p id="p_08522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08522"> •  Displaying a Print Format</p>
        <p id="p_08523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08523"> •  Displaying Values in a Print Format</p>
        <p id="p_08524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08524"> o  Creating a General-Purpose Print Format</p>
        <p id="p_08525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08525"> o  Choosing a Value-Field Format</p>
        <p id="p_08526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08526"> o  Printing Value-Field Characters o  Using the Multiline Field Format</p>
        <p id="p_08527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08527"> •  Writing to Other Output Files</p>
        <p id="p_08528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08528"> o  Saving the Default File Variable</p>
        <p id="p_08529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08529"> •  Specifying a Page Header</p>
        <p id="p_08530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08530"> o  Changing the Header Print Format</p>
        <p id="p_08531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08531"> •  Setting the Page Length</p>
        <p id="p_08532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08532"> o  Using  print  with Pagination</p>
        <p id="p_08533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08533"> •  Formatting Long Character Strings</p>
        <p id="p_08534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08534"> o  Eliminating Blank Lines When Formatting</p>
        <p id="p_08535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08535"> o  Supplying an Indefinite Number of Lines</p>
        <p id="p_08536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08536"> •  Lormatting Output Using  print f</p>
        <p id="p_08537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08537"> •  Summary</p>
        <p id="p_08538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08538"> •  O&amp;A</p>
        <p id="p_08539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08539"> •  Workshop</p>
        <p id="p_08540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08540"> o  Quiz o  Exercises</p>
        <p id="p_08541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08541"> The Perl programs you've seen so far produce output using the  print  function, which writes raw, unformatted text to a file.</p>
        <p id="p_08542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08542"> Perl also enables you to produce formatted output, using print formats and the built-in function  write. Today's lesson describes how to produce formatted output. You'll learn the following:</p>
        <p id="p_08543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08543"> • How to define a print format (also sometimes known as a "picture format")</p>
        <p id="p_08544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08544"> • How to use the  write  function</p>
        <p id="p_08545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08545"> • How to add formatted values to a print format</p>
        <p id="p_08546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08546"> • Which value-field formats are available</p>
        <p id="p_08547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08547"> • How to write to other output files</p>
        <p id="p_08548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08548"> • How to specify page headers and the page length</p>
        <p id="p_08549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08549"> • How to format long character strings</p>
        <p id="p_08550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08550"> • How to use the built-in function  pr int f</p>
        <p id="p_08551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08551"> Defining a Print Format</p>
        <p id="p_08552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08552"> The following is an example of a simple print format:</p>
        <p id="p_08553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08553"> format MYFORMAT =</p>
        <p id="p_08554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08554"> Here is the text I want to display.</p>
        <p id="p_08555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08555"> This defines the print format  MYFORMAT. The syntax for print formats is</p>
        <p id="p_08556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08556"> format formatname = lines_of_output</p>
        <p id="p_08557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08557"> The special keyword format tells the Perl interpreter that the following lines are a print-format definition. The  formatname  is a placeholder for the name of the print format being defined (for example, MYFORMAT).  This name must start with an alphabetic character and can consist of any sequence of letters, digits, or underscores.</p>
        <p id="p_08558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08558"> The  lines_of_output  consists of one or more lines of text that are to be printed when the print format is utilized; these lines are sometimes called  picture lines.  In the  MYFORMAT  example, there are three lines of text printed: two lines containing = characters, and the line</p>
        <p id="p_08559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08559"> Here is the text I want to display.</p>
        <p id="p_08560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08560"> A print-format definition is terminated with a line containing a period character. This line can contain nothing else; there can be no white space, and the period must be the first character on the line.</p>
        <p id="p_08561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08561"> Like subroutines, print-format definitions can appear anywhere in program code (even, for example, in the</p>
        <p id="p_08562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08562"> middle of a conditional statement). However, it usually is best to cluster them either at the beginning or the end of the program.</p>
        <p id="p_08563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08563"> Displaying a Print Format</p>
        <p id="p_08564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08564"> To display output using a print format, you need to do two things:</p>
        <p id="p_08565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08565"> • Set the system variable $ ~ to the format you want to use</p>
        <p id="p_08566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08566"> • Call the built-in function  write</p>
        <p id="p_08567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08567"> Listing 11.1 is an example of a simple program that displays output using a print format.</p>
        <p id="p_08568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08568"> Type</p>
        <p id="p_08569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08569"> Listing 11.1. A program that uses a print format.</p>
        <p id="p_08570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08570"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08571"> 2  :</p>
        <p id="p_08572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08572"> 3: $~ = "MYFORMAT";</p>
        <p id="p_08573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08573"> 4: write;</p>
        <p id="p_08574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08574"> 5:</p>
        <p id="p_08575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08575"> 6: format MYFORMAT =</p>
        <p id="p_08576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08576"> 7 ;  ===================================</p>
        <p id="p_08577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08577"> 8: Here is the text I want to display.</p>
        <p id="p_08578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08578"> 9 :  ===================================</p>
        <p id="p_08579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08579"> 10 : .</p>
        <p id="p_08580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08580"> Output</p>
        <p id="p_08581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08581"> $ programll_l</p>
        <p id="p_08582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08582"> Here is the text I want to display.</p>
        <p id="p_08583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08583"> $</p>
        <p id="p_08584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08584"> Line 3 of this program assigns the character string  MYFORMAT  to the system variable $~. This tells the Perl interpreter that  MYFORMAT  is the print format to use when calling  write.</p>
        <p id="p_08585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08585"> Line 4 calls  write,  which sends the text defined in  MYFORMAT  to the standard output file.</p>
        <p id="p_08586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08586"> Lines 6-10 contain the definition of the print format  MYFORMAT.</p>
        <p id="p_08587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08587"> Analysis</p>
        <p id="p_08588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08588"> NOTE</p>
        <p id="p_08589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08589"> If you don't specify a print format by assigning to $~, the Perl interpreter assumes that the print format to use has the same name as the file variable being written to. In this example program, if line 3 had not specified  MYFORMAT  as the print format to use, the Perl interpreter would have tried to use a print format named  STDOUT when executing the call to  write  in line  4,  because the call to  write is writing to the standard output file</p>
        <p id="p_08590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08590"> Displaying Values in a Print Format</p>
        <p id="p_08591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08591"> Of course, the main reason to use print formats is to format values stored in scalar variables or array variables to produce readable output. Perl enables you to do this by specifying  value fields  as part of a format definition.</p>
        <p id="p_08592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08592"> Each value field specifies a value: the name of a scalar variable, for example, or an expression. When the write  statement is invoked, the value is displayed in the format specified by the value field.</p>
        <p id="p_08593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08593"> Listing 11.2 shows how value fields work. This program keeps track of the number of occurrences of the letters  a, e, i, o,  and  u  in a text file.</p>
        <p id="p_08594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08594"> Type</p>
        <p id="p_08595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08595"> Listing 11.2. A program that uses value fields to print output.</p>
        <p id="p_08596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08596"> 1:  #!/usr/local/bin/perl</p>
        <p id="p_08597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08597"> 2  : 3:</p>
        <p id="p_08598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08598"> 4 :</p>
        <p id="p_08599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08599"> 5 : 6 :</p>
        <p id="p_08600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08600"> 7 :</p>
        <p id="p_08601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08601"> 8  : 9: 10 : 11 : 12 : 13:</p>
        <p id="p_08602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08602"> 14 :</p>
        <p id="p_08603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08603"> 15 : 16:</p>
        <p id="p_08604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08604"> 17 :</p>
        <p id="p_08605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08605"> 18 : 19: 20  : 21 : 22  : 23:</p>
        <p id="p_08606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08606"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_08607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08607"> $line =~ s/[ A aeiou]//g;</p>
        <p id="p_08608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08608"> @vowels = split(//, $line); foreach $vowel (@vowels) {</p>
        <p id="p_08609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08609"> $vowelcount{$vowel} += 1;</p>
        <p id="p_08610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08610"> }</p>
        <p id="p_08611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08611"> }</p>
        <p id="p_08612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08612"> $~ = "VOWELFORMAT";</p>
        <p id="p_08613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08613"> write;</p>
        <p id="p_08614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08614"> format VOWELFORMAT =</p>
        <p id="p_08615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08615"> Number of vowels found in text file:</p>
        <p id="p_08616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08616"> a: @-</p>
        <p id="p_08617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08617"> e: @-</p>
        <p id="p_08618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08618"> $vowelcount{"a"}, $vowelcount{"e"}</p>
        <p id="p_08619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08619"> i: @-</p>
        <p id="p_08620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08620"> o: @-</p>
        <p id="p_08621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08621"> $vowelcount{"i"}, $vowelcount{"o"}</p>
        <p id="p_08622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08622"> u: @-</p>
        <p id="p_08623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08623"> $vowelcount{"u"}</p>
        <p id="p_08624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08624"> Output</p>
        <p id="p_08625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08625"> $ programll_2</p>
        <p id="p_08626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08626"> This is a test file.</p>
        <p id="p_08627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08627"> This test file contains some vowels.</p>
        <p id="p_08628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08628"> The quick brown fox jumped over the lazy dog. A D</p>
        <p id="p_08629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08629"> Number of vowels found in text file: a: 3 e: 10</p>
        <p id="p_08630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08630"> i: 7 o: 7</p>
        <p id="p_08631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08631"> u: 2</p>
        <p id="p_08632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08632"> $</p>
        <p id="p_08633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08633"> Analysis</p>
        <p id="p_08634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08634"> This program reads one line of input at a time. Line 4 removes everything that is not  a, e, i, o, or  u  from the input line, and line 5 splits the remaining characters into the array @ vowels.  Each element of @ vowels  is one character of the input line.</p>
        <p id="p_08635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08635"> Lines  6-8  count the vowels in the input line by examining the elements of @  vowel s  and adding to the associative array  %vowelcount.</p>
        <p id="p_08636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08636"> Line 10 sets the current print format to  VOWELFORMAT;  line 11 prints using  VOWELFORMAT.</p>
        <p id="p_08637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08637"> The print format  VOWELFORMAT  is defined in lines 13-23. Line 16 is an example of a print format line that contains value fields; in this case, two value fields are defined. Each value field has the format @ &lt;&lt;&lt;&lt;&lt;, which indicates six left-justified characters. (For a complete description of the possible value fields, see the section called "Choosing a Value-Field Format," later today.)</p>
        <p id="p_08638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08638"> When one or more value fields appear in a print-format line, the next line must define the value or values to be printed in this value field. Because line 16 defines two value fields, line 17 defines the two values to be printed. These values are  $vowelcount { "a"  } and  $vowelcount { "e"  }, which are the number of occurrences of  a  and  e,  respectively.</p>
        <p id="p_08639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08639"> Similarly, line 18 defines two more value fields to be printed, and line 19 indicates that the values to be printed in these fields are $  vowel count { " i  " } and $  vowel count { " o  " }. Finally, line 20 defines a fifth value field, and line 21 specifies that  $vowelcount { "u"  } is to be printed in this field.</p>
        <p id="p_08640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08640"> NOTE</p>
        <p id="p_08641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08641"> Three things to note about the values that are specified for value-field formats:</p>
        <p id="p_08642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08642"> • The lines containing values to be printed are not themselves printed. For example, in Listing 11.2, lines 16, 18, and 20 are printed, but lines 17, 19, and 21 are not.</p>
        <p id="p_08643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08643"> • The Perl interpreter ignores spacing when it looks for values corresponding to value fields. Many people prefer to line up their values with the corresponding value fields on the previous line, but there is no need to do so.</p>
        <p id="p_08644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08644"> • The number of values specified must match the number of value fields defined on the previous line</p>
        <p id="p_08645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08645"> Creating a General-Purpose Print Format</p>
        <p id="p_08646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08646"> One disadvantage of print formats as defined in Perl is that scalar-variable names are included as part of the definition. For example, in the following definition, the scalar variable  $winnum  is built into the print format definition  MYFORMAT:</p>
        <p id="p_08647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08647"> format MYFORMAT =</p>
        <p id="p_08648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08648"> The winning number is @ $winnum</p>
        <p id="p_08649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08649"> When  write  is called with this print format, as in the following, you have to remember that  $winnum  is being used by  MYFORMAT.</p>
        <p id="p_08650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08650"> $~ = "MYFORMAT"; write;</p>
        <p id="p_08651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08651"> If, later on, you accidentally delete all references to  $winnum  in the program, the call to  write  will stop working properly.</p>
        <p id="p_08652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08652"> One way to get around this problem is to call  write  from within a subroutine, and to use variables local to the subroutine in the print format that  write  uses. Listing  11.3  is a program that does this. It reads a file from the standard input file and prints out the number of occurrences of the five most frequently occurring letters.</p>
        <p id="p_08653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08653"> Type</p>
        <p id="p_08654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08654"> Listing 11.3. A program that calls write from within a subroutine.</p>
        <p id="p_08655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08655"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08656"> 2  :</p>
        <p id="p_08657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08657"> 3: while</p>
        <p id="p_08658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08658"> 4 :</p>
        <p id="p_08659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08659"> 5 :</p>
        <p id="p_08660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08660"> 6 :</p>
        <p id="p_08661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08661"> 7 :</p>
        <p id="p_08662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08662"> 8  :</p>
        <p id="p_08663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08663"> 9:</p>
        <p id="p_08664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08664"> 10 : }</p>
        <p id="p_08665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08665"> 11 :</p>
        <p id="p_08666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08666"> 12: $~ = "WRITEHEADER";</p>
        <p id="p_08667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08667"> 13: write;</p>
        <p id="p_08668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08668"> 14: $count = 0;</p>
        <p id="p_08669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08669"> 15: foreach $letter (reverse sort occurrences 16: (keys(%lettercount) ) ) {</p>
        <p id="p_08670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08670"> 17: &amp;write_letter($letter, $lettercount{$letter})</p>
        <p id="p_08671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08671"> 18: last if (++$count == 5);</p>
        <p id="p_08672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08672"> 19: }</p>
        <p id="p_08673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08673"> 20  :</p>
        <p id="p_08674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08674"> 21: sub occurrences {</p>
        <p id="p_08675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08675"> 22: $lettercount{$a} &lt;=&gt; $lettercount{$b};</p>
        <p id="p_08676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08676"> ($line = &lt;STDIN&gt;) {</p>
        <p id="p_08677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08677"> $line =~ tr/A-Z/a-z/;</p>
        <p id="p_08678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08678"> $line =~  s/[ A a-z]//g;</p>
        <p id="p_08679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08679"> Sletters = split(//, $line); foreach $letter (Sletters) {</p>
        <p id="p_08680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08680"> $lettercount{$letter} += 1;</p>
        <p id="p_08681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08681"> }</p>
        <p id="p_08682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08682"> 26:</p>
        <p id="p_08683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08683"> 31: The five most frequently occurring letters are:</p>
        <p id="p_08684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08684"> $ programll_3</p>
        <p id="p_08685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08685"> This is a test file.</p>
        <p id="p_08686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08686"> This test file contains some input.</p>
        <p id="p_08687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08687"> The quick brown fox jumped over the lazy dog. A D</p>
        <p id="p_08688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08688"> i: 8</p>
        <p id="p_08689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08689"> 7</p>
        <p id="p_08690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08690"> s :</p>
        <p id="p_08691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08691"> o: 6</p>
        <p id="p_08692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08692"> $</p>
        <p id="p_08693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08693"> Like the vowel-counting program in Listing 11.2, this program processes one line of input at a time. Line 4 translates all uppercase alphabetic characters into lowercase, so that they can be included in the letter count. Line 5 gets rid of all characters that are not letters, including any white space.</p>
        <p id="p_08694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08694"> Line 6 splits the line into its individual letters; lines 7-9 examine each letter and increment the appropriate letter counters, which are stored in the associative array %  let ter count.</p>
        <p id="p_08695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08695"> Lines 12 and 13 print the following line by setting the current print format to  WRITEHEADER  and calling</p>
        <p id="p_08696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08696"> write:</p>
        <p id="p_08697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08697"> Analysis</p>
        <p id="p_08698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08698"> The five most frequently occurring letters are:</p>
        <p id="p_08699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08699"> Except in very special cases, never mix calls to  write  with calls to print.  Your program should use one printing function or the other, not both</p>
        <p id="p_08700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08700"> Lines 15-19 sort the array %  let ter count  in order of occurrence. The first letter to appear in the f oreach  loop is the letter that appears most often in the file. To sort the array in order of occurrence, lines 15 and 16 specify that sorting is to be performed according to the rules defined in the subroutine occurrences.  This subroutine tells the Perl interpreter to use the values of the associative array elements as the sort criterion.</p>
        <p id="p_08701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08701"> Line 17 passes the letter and its occurrence count to the subroutine  write_letter.  This subroutine sets the current print format to  WRITELETTER;  this print format refers to the local scalar variables $  letter and  $value,  which contain the values passed to  write_letter  by line 17. This means that each call to write_letter  prints the letter and value currently being examined by the  f oreach  loop.</p>
        <p id="p_08702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08702"> Note that the first value field in the print format  WRITELETTER  contains only a single character, @. This indicates that the write field is only one character long (which makes sense, because this is a single letter).</p>
        <p id="p_08703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08703"> Line 18 ensures that the  f oreach  loop quits after the five most frequently used letters have been examined and printed.</p>
        <p id="p_08704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08704"> TIP</p>
        <p id="p_08705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08705"> Some programs, such as the one in Listing 11.3, use more than one print-format definition. To make it easier to see which print format is being used by a particular call to  write,  always keep the print format specification statement and the  write  call together. For example:</p>
        <p id="p_08706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08706"> $~ = "WRITEFORMAT"; write;</p>
        <p id="p_08707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08707"> Here, it is obvious that the call to  write  is using the print format</p>
        <p id="p_08708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08708"> WRITEFORMAT</p>
        <p id="p_08709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08709"> Formats and Local Variables</p>
        <p id="p_08710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08710"> In Listing  11.3,   you might have noticed that the subroutine  write_letter  calls a subroutine to write out a letter and its value:</p>
        <p id="p_08711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08711"> sub write_letter {</p>
        <p id="p_08712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08712"> local($letter, $value) = @_;</p>
        <p id="p_08713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08713"> $~ = "WRITELETTER"; write;</p>
        <p id="p_08714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08714"> }</p>
        <p id="p_08715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08715"> This subroutine works properly even though the  WRITELETTER  print format is defined outside the subroutine.</p>
        <p id="p_08716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08716"> Note, however, that local variables defined using  my  cannot be written out using a print format unless the format is defined inside the subroutine. (To see this for yourself, change line 25 of Listing 11.3 to the following and run the program again:</p>
        <p id="p_08717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08717"> my($letter,$value) = @_;</p>
        <p id="p_08718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08718"> You will notice that the letter counts do not appear.) This limitation is a result of the way local variables defined using  my  are stored by the Perl interpreter. To avoid this difficulty, use  local  instead of  my  when you define local variables that are to be written out using  write.  (For a discussion of  local  and  my,  see Day  9 ,  "Using Subroutines.")</p>
        <p id="p_08719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08719"> Perl 4 users will not run into this problem, because  my  is not defined for that version of the language.</p>
        <p id="p_08720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08720"> NOTE</p>
        <p id="p_08721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08721"> In versions of Perl 5 earlier than version 5.001, local valuables defined using  my  cannot be written out at all. Even in version 5.001, variables defined using  my  might not behave in the way you expect them to. As a consequence, it is best to avoid using  my  with print formats</p>
        <p id="p_08722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08722"> Choosing a Value-Field Format</p>
        <p id="p_08723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08723"> Now that you know how print formats and  write  work, it's time to look at the value-field formats that are available. Table 11.1 lists these formats.</p>
        <p id="p_08724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08724"> Table 11.1. Valid value-field formats.</p>
        <p id="p_08725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08725"> NOTE</p>
        <p id="p_08726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08726"> In  left-justified  output, the value being displayed appears at the left end of the value field. In  right-justified  output, the value being displayed appears at the right end of the value field</p>
        <p id="p_08727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08727"> In each of the field formats, the first character is a  line-fill character.  It indicates whether text formatting is required. If the @ character is specified as the line fill character, text formatting is not performed. (For a discussion of text formatting, see the section titled "Formatting Fong Character Strings," later today.)</p>
        <p id="p_08728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08728"> In all cases, except for the multiline value field @ *, the width of the field is equal to the number of characters specified. The @ character is included when counting the number of characters in the value field. For example, the following field is five characters wide-one @ character and four &gt; characters:</p>
        <p id="p_08729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08729"> Similarly, the following field is seven characters wide-four before the decimal point, two after the decimal point, and the decimal point itself:</p>
        <p id="p_08730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08730"> @###.##</p>
        <p id="p_08731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08731"> Listing 11.4 illustrates how you can use the value field formats to produce a neatly printed report. The report is redirected to a file for later printing.</p>
        <p id="p_08732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08732"> Type</p>
        <p id="p_08733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08733"> Listing  11.4. A  program that uses the various value-field formats.</p>
        <p id="p_08734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08734"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08735"> 2  :</p>
        <p id="p_08736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08736"> 3: $company = &lt;STDIN&gt;;</p>
        <p id="p_08737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08737"> 4: $~ = "COMPANY";</p>
        <p id="p_08738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08738"> 5: write;</p>
        <p id="p_08739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08739"> 6 :</p>
        <p id="p_08740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08740"> 7: $grandtotal = 0;</p>
        <p id="p_08741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08741"> 8: $custline = &lt;STDIN&gt;;</p>
        <p id="p_08742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08742"> 9: while ($custline ne "") {</p>
        <p id="p_08743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08743"> 10: $total = 0;</p>
        <p id="p_08744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08744"> 11: ($customer, $date) = split  (/#/,  $custline);</p>
        <p id="p_08745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08745"> 12: $~ = "CUSTOMER";</p>
        <p id="p_08746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08746"> 13: write;</p>
        <p id="p_08747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08747"> 14: while (1) {</p>
        <p id="p_08748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08748"> 15: $orderline = &lt;STDIN&gt;;</p>
        <p id="p_08749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08749"> 16: if ($orderline eq "" || $orderline =~  /#/)  {</p>
        <p id="p_08750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08750"> 17: $custline = $orderline;</p>
        <p id="p_08751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08751"> 18: last;</p>
        <p id="p_08752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08752"> 19:</p>
        <p id="p_08753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08753"> }</p>
        <p id="p_08754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08754"> ($item, $cost)</p>
        <p id="p_08755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08755"> split(/:/, $orderline);</p>
        <p id="p_08756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08756"> 20  : 21 : 22  : 23:</p>
        <p id="p_08757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08757"> 24 :</p>
        <p id="p_08758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08758"> 25 : 26:</p>
        <p id="p_08759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08759"> 27 :</p>
        <p id="p_08760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08760"> 28 : 29: 30 : 31: 32 : 33:</p>
        <p id="p_08761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08761"> 34 :</p>
        <p id="p_08762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08762"> 35 : 36:</p>
        <p id="p_08763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08763"> 37 :</p>
        <p id="p_08764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08764"> 38 : 39: 40 : 41: 42 : 43:</p>
        <p id="p_08765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08765"> 44 :</p>
        <p id="p_08766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08766"> 45 :</p>
        <p id="p_08767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08767"> $~ = "ORDERLINE"; write;</p>
        <p id="p_08768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08768"> $total += $cost;</p>
        <p id="p_08769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08769"> }</p>
        <p id="p_08770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08770"> &amp;write_total ("Total:", $total); $grandtotal += $total;</p>
        <p id="p_08771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08771"> }</p>
        <p id="p_08772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08772"> &amp;write_total ("Grand total:", $grandtotal);</p>
        <p id="p_08773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08773"> sub write_total {</p>
        <p id="p_08774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08774"> local ($totalstring, $total) = @_; $~ = "TOTAL"; write;</p>
        <p id="p_08775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08775"> }</p>
        <p id="p_08776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08776"> format COMPANY =</p>
        <p id="p_08777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08777"> ************* @1 I I I I I I N I I I I I I III I I I I M I 1.1 I</p>
        <p id="p_08778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08778"> $company</p>
        <p id="p_08779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08779"> format CUSTOMER =</p>
        <p id="p_08780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08780"> $customer, $date</p>
        <p id="p_08781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08781"> format  ORDERLINE =</p>
        <p id="p_08782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08781"/>
        <p id="p_08783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08783"> @####.##</p>
        <p id="p_08784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08784"> 46:  $item,  $cost</p>
        <p id="p_08785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08785"> 48: format TOTAL</p>
        <p id="p_08786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08786"> 4 9: @</p>
        <p id="p_08787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08787"> @#####.##</p>
        <p id="p_08788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08788"> 50: $totalstring, $total</p>
        <p id="p_08789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08789"> 51:</p>
        <p id="p_08790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08790"> 52: .</p>
        <p id="p_08791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08791"> $ programll_4 &gt;report Consolidated Widgets, Inc. John Doe#Feb 11, 1994</p>
        <p id="p_08792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08792"> 1 flying widget:171.42</p>
        <p id="p_08793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08793"> 1 crawling widget:89.99 Mary Smith#May 4, 1994</p>
        <p id="p_08794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08794"> 2 swimming widgets:203.43 A D</p>
        <p id="p_08795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08795"> $</p>
        <p id="p_08796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08796"> The following report is written to the  report  file:</p>
        <p id="p_08797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08797"> -k'k'k'k-k-k-k-k'k-k'k-k'k</p>
        <p id="p_08798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08798"> Consolidated Widgets, Inc.</p>
        <p id="p_08799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08799"> 'k-k-k-k'k-k-k-k'k-k-k-k'k</p>
        <p id="p_08800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08800"> John Doe</p>
        <p id="p_08801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08801"> Feb 11, 1994</p>
        <p id="p_08802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08802"> 1 flying widget</p>
        <p id="p_08803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08803"> 171.42</p>
        <p id="p_08804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08804"> 1 crawling widget</p>
        <p id="p_08805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08805"> 89.99</p>
        <p id="p_08806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08806"> Total:</p>
        <p id="p_08807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08807"> 261.41</p>
        <p id="p_08808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08808"> Mary Smith</p>
        <p id="p_08809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08809"> May 4, 1994</p>
        <p id="p_08810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08810"> 2 swimming widgets</p>
        <p id="p_08811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08811"> 203.43</p>
        <p id="p_08812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08812"> Total:</p>
        <p id="p_08813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08813"> 203.43</p>
        <p id="p_08814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08814"> Grand total:</p>
        <p id="p_08815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08815"> 464.84</p>
        <p id="p_08816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08816"> Analysis</p>
        <p id="p_08817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08817"> This program starts off by reading the company name from the standard input file and then writing it out. Line 5 writes the company name using the print format  COMPANY,  which uses a centered output field to display the company name in the center of the line.</p>
        <p id="p_08818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08818"> After the company name has been printed, the program starts processing data for one customer at a time. Each customer record is assumed to consist of a customer name and date followed by lines of orders. The customer name record uses a # character as the field separator, and the order records use : characters as the separator; this enables the program to distinguish one type of record from the other.</p>
        <p id="p_08819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08819"> Line 13 prints the customer information using the  CUSTOMER  print format. This format contains two fields: a left-justified output field for the customer name, and a right-justified output field for the date of the transaction.</p>
        <p id="p_08820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08820"> Line 22 prints an order line using the  ORDERLINE  print format. This print format also contains two fields: a left-justified output field indicating the item ordered, and a numeric field to display the cost of the item.</p>
        <p id="p_08821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08821"> The value field format @  ####.#  # indicates that the cost is to be displayed as a floating-point number. This number is defined as containing at most five digits before the decimal point, and two digits after.</p>
        <p id="p_08822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08822"> Linally, the print format  TOTAL  prints the customer total and the grand total. Because this print format is used inside a subroutine, the same print format can be used to print both totals.</p>
        <p id="p_08823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08823"> Normally, any floating-point number you print is rounded up when necessary. For example, when you print 43.999 in the value field @#.##, it appears as 44.00.</p>
        <p id="p_08824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08824"> However, a floating-point number whose last decimal place is 5 might or might not round correctly. For example, if you are writing using the value field @#.##, some numbers whose third and last decimal place is 5 will round and others will not. This happens because some floating-point numbers cannot be stored exactly, and the nearest equivalent number that can be stored is a slightly smaller number (which rounds down, not up)</p>
        <p id="p_08825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08825"> Printing Value-Field Characters</p>
        <p id="p_08826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08826"> As you have seen, certain characters such as @, &lt;, and &gt; are treated as value fields when they are encountered in print formats. Listing 11.5 shows how to actually print one of these special characters using</p>
        <p id="p_08827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08827"> write.</p>
        <p id="p_08828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08828"> Type</p>
        <p id="p_08829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08829"> Listing 11.5.  A  program that prints a value-field character.</p>
        <p id="p_08830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08830"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08831"> 2  :</p>
        <p id="p_08832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08832"> 3: format SPECIAL =</p>
        <p id="p_08833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08833"> 4: This line contains the special character @.</p>
        <p id="p_08834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08834"> 5 : " @ "</p>
        <p id="p_08835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08835"> 6 :</p>
        <p id="p_08836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08836"> 7 :</p>
        <p id="p_08837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08837"> 8: $~ = "SPECIAL";</p>
        <p id="p_08838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08838"> 9: write;</p>
        <p id="p_08839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08839"> Output</p>
        <p id="p_08840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08840"> $ programll_5</p>
        <p id="p_08841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08841"> This line contains the special character @.</p>
        <p id="p_08842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08842"> $</p>
        <p id="p_08843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08843"> Analysis</p>
        <p id="p_08844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08844"> The print format line in line 4 contains the special character @, which is a one-character value field. Line 5 specifies that the string @ is to be displayed in this value field when the line is printed.</p>
        <p id="p_08845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08845"> Using the Multiline Field Format</p>
        <p id="p_08846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08846"> Listing 11.6 uses the multiline field format @ * to write a character string over several lines.</p>
        <p id="p_08847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08847"> Type</p>
        <p id="p_08848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08848"> Listing 11.6. A program that writes a string using the multiline field format.</p>
        <p id="p_08849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08849"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08850"> 2  :</p>
        <p id="p_08851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08851"> 3: @input = &lt;STDIN&gt;;</p>
        <p id="p_08852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08852"> 4: $string = join("", @input);</p>
        <p id="p_08853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08853"> 5: $~ = "MULTILINE";</p>
        <p id="p_08854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08854"> 6: write;</p>
        <p id="p_08855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08855"> 7 :</p>
        <p id="p_08856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08856"> 8: format MULTILINE =</p>
        <p id="p_08857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08857"> 9:  ******  contents of the  input  file:  ******</p>
        <p id="p_08858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08858"> 10  : @*</p>
        <p id="p_08859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08859"> 11: $string</p>
        <p id="p_08860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08860"> 12: *****************************************</p>
        <p id="p_08861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08861"> 13: .</p>
        <p id="p_08862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08862"> Output</p>
        <p id="p_08863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08863"> $ programll_6</p>
        <p id="p_08864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08864"> Here is a line of input.</p>
        <p id="p_08865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08865"> Here is another line.</p>
        <p id="p_08866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08866"> Here is the last line.</p>
        <p id="p_08867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08867"> A D</p>
        <p id="p_08868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08868"> ******  contents of the input file:  ****** Here is a line of input.</p>
        <p id="p_08869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08869"> Here is another line.</p>
        <p id="p_08870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08870"> Here is the last line.</p>
        <p id="p_08871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08871"> itiritirititiritiritiritiritirit'k-k'k-k'k-k-k'k-k'k-k'k-k'k-k'k-k'k-k'k-k'k-k'k-k</p>
        <p id="p_08872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08872"> $</p>
        <p id="p_08873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08873"> Analysis</p>
        <p id="p_08874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08874"> Line  3  reads the entire input file into the array variable @  input.  Each element of the list stored in @  input  is one line of the input file.</p>
        <p id="p_08875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08875"> Line 4 joins the input lines into a single character string, stored in $  string.  This character string still contains the newline characters that end each line.</p>
        <p id="p_08876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08876"> Line  6  calls  write  using the print format  MULTILINE.  The @ * value field in this print-format definition indicates that the value stored in $  string  is to be written out using as many lines as necessary. This ensures that the entire string stored in $  string  is written out.</p>
        <p id="p_08877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08877"> If a character string contains a newline character, the only way to display the entire string using  write  is to use the @ * multiline value field. If you use any other value field, only the part of the string preceding the first newline character is displayed</p>
        <p id="p_08878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08878"> Writing to Other Output Files</p>
        <p id="p_08879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08879"> So far, all of the examples that have used the function  write  have written to the standard output file. However, you can use  write  also to send output to other files.</p>
        <p id="p_08880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08880"> The simplest way to do this is to pass the file to write to as an argument to  write.  For example, to write to the file represented by the file variable  MYFILE  using the print format  MYFILE,  you can use the following statement:</p>
        <p id="p_08881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08881"> write (MYFILE);</p>
        <p id="p_08882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08882"> Here,  write  writes to the file named  MYFILE  using the default print format, which is also  MYFILE.  This is tidy and efficient, but somewhat restricting because, in this case, you can't use $ ~ to choose the print format to use.</p>
        <p id="p_08883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08883"> The $~ system variable only works with the  default file variable,  which is the file variable to which  write sends output. To change the default file variable, and therefore change the file that $~ affects, call the built-in function  select,  as follows:</p>
        <p id="p_08884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08884"> select (MYFILE);</p>
        <p id="p_08885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08885"> select  sets the default file variable to use when writing. For example, to write to the file represented by the file variable  MYFILE  using the print format  MYFORMAT,  you can use the following statements:</p>
        <p id="p_08886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08886"> select(MYFILE);</p>
        <p id="p_08887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08887"> $~ = "MYFORMAT"; write;</p>
        <p id="p_08888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08888"> Here, the built-in function  select  indicates that the file to be written to is the file represented by the file variable  MYFILE.  The statement</p>
        <p id="p_08889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08889"> $</p>
        <p id="p_08890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08890"> "MYFORMAT" ;</p>
        <p id="p_08891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08891"> selects the print format to be associated with this particular file handle; in this case, the print format MYFORMAT is now associated with the file variable MYFILE.</p>
        <p id="p_08892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08892"> NOTE</p>
        <p id="p_08893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08893"> This is worth repeating: Each file variable has its own current print format. An assignment to $ ~ only changes the print format for the current file variable (the last one passed to  select)</p>
        <p id="p_08894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08894"> Because  select  has changed the file to be written to, the call to  write  no longer writes to the standard output file. Instead, it writes to  MYFILE.  Calls to  write  continue to write to  MYFILE  until the following statement is seen:</p>
        <p id="p_08895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08895"> select(STDOUT);</p>
        <p id="p_08896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08896"> This statement resets the write file to be the standard output file.</p>
        <p id="p_08897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08897"> Warning</p>
        <p id="p_08898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08898"> Changing the write file using select not only affects  write;  it also affects  print.  For example, consider the following:</p>
        <p id="p_08899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08899"> select (MYFILE);</p>
        <p id="p_08900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08900"> print ("Here is a line of  text.\n");</p>
        <p id="p_08901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08901"> This call to  print  writes to  MYFILE,  not to the standard output file. As with  write,  calls to  print  continue to write to  MYFILE  until another call to  select  is seen</p>
        <p id="p_08902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08902"> The  select  function is useful if you want to be able to use the same subroutine to write to more than one file at a time. Listing 11.7 is an example of a simple program that does this.</p>
        <p id="p_08903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08903"> Listing 11.7. A program that uses the select function.</p>
        <p id="p_08904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08904"> 1: #!/usr/local/bin/perl</p>
        <p id="p_08905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08905"> 3: open (FILE1, "&gt;filel");</p>
        <p id="p_08906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08906"> 4: $string = "junk";</p>
        <p id="p_08907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08907"> 5: select (FILE1);</p>
        <p id="p_08908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08908"> 6: &amp;writeline;</p>
        <p id="p_08909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08909"> 7: select (STDOUT);</p>
        <p id="p_08910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08910"> 8: &amp;writeline;</p>
        <p id="p_08911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08911"> 9: close (FILE1);</p>
        <p id="p_08912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08912"> 10 :</p>
        <p id="p_08913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08913"> 11: sub 12 :</p>
        <p id="p_08914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08914"> 13:</p>
        <p id="p_08915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08915"> 14: }</p>
        <p id="p_08916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08916"> 15 :</p>
        <p id="p_08917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08917"> 16: format WRITELINE =</p>
        <p id="p_08918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08918"> 17: I am writing @&lt;&lt;&lt;&lt;&lt; to my output files.</p>
        <p id="p_08919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08919"> 18: $string</p>
        <p id="p_08920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08920"> 19: .</p>
        <p id="p_08921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08921"> writeline {</p>
        <p id="p_08922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08922"> $~ = "WRITELINE"; write;</p>
        <p id="p_08923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08923"> Output</p>
        <p id="p_08924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08924"> $ programll_7</p>
        <p id="p_08925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08925"> I am writing junk to my output files.</p>
        <p id="p_08926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08926"> $</p>
        <p id="p_08927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08927"> Analysis</p>
        <p id="p_08928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08928"> I  Line  5  of this program calls  select,  which sets the default file variable to  FILE1.  Now, all calls to  write  or  print  write to  FILE,  not the standard output file.</p>
        <p id="p_08929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08929"> Line 6 calls  writeline  to write a line. This subroutine sets the current print format for the default file variable to  WRITELINE.  This means that the file  FILE1  now is using the print format  WRITELINE,  and, therefore, the subroutine writes the following line to the file  FI LEI  (which is  f ilel):</p>
        <p id="p_08930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08930"> I am writing junk to my output files.</p>
        <p id="p_08931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08931"> Line 7 sets the default file variable back to the standard output file variable,  STDOUT.  This means that write  and  print  now send output to the standard output file. Note that the current print format for the standard output file is  STDOUT  (the default), not  WRITELINE;  the assignment to $~ in the subroutine WRITELINE  affects only  FILE1,  not  STDOUT.</p>
        <p id="p_08932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08932"> Line  8  calls  writeline  again; this time, the subroutine writes a line to the standard output file. The assignment</p>
        <p id="p_08933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08933"> $~ = "WRITELINE";</p>
        <p id="p_08934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08934"> in line 12 associates the print format  WRITELINE  with the standard output file. This means that WRITELINE  is now associated with both  STDOUT  and  FILE1.</p>
        <p id="p_08935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08935"> At this point, the call to  write  in line  13  writes the line of output that you see on the standard output file.</p>
        <p id="p_08936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08936"> Don't</p>
        <p id="p_08937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08937"> DO,  whenever possible, call  select  and assign to $~ immediately before calling  write, as follows:</p>
        <p id="p_08938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08938"> select (MYFILE);</p>
        <p id="p_08939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08939"> $~ = "MYFORMAT"; write;</p>
        <p id="p_08940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08940"> Keeping these statements together makes it clear which file is being written to and which print format is being used.</p>
        <p id="p_08941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08941"> DON’T  use  select  and $~ indiscriminately, because you might lose track of which print format goes with which file variable, and you might forget which file variable is the default for printing</p>
        <p id="p_08942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08942"> Saving the Default File Variable</p>
        <p id="p_08943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08943"> When  select  changes the default file variable, it returns an internal representation of the file variable that was last selected. For example:</p>
        <p id="p_08944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08944"> $oldfile = select(NEWFILE);</p>
        <p id="p_08945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08945"> This call to  select  is setting the current file variable to  NEWFILE.  The old file variable is now stored in $oldf ile.  To restore the previous default file variable, you can call  select  as follows:</p>
        <p id="p_08946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08946"> select ($oldfile);</p>
        <p id="p_08947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08947"> At this point, the default file variable reverts back to its original value (what it was before  NEWFILE  was selected).</p>
        <p id="p_08948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08948"> The internal representation of the file variable returned by  select  is not necessarily the name of the file variable</p>
        <p id="p_08949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08949"> You can use the return value from  select  to create subroutines that write to the file you want to write with, using the print format you want to use, without affecting the rest of the program. For example:</p>
        <p id="p_08950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08950"> sub write_to_stdout {</p>
        <p id="p_08951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08951"> local ($savefile, $saveformat); $savefile = select (STDOUT); $saveformat = $~;</p>
        <p id="p_08952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08952"> $~ = "MYFORMAT"; write;</p>
        <p id="p_08953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08953"> $~ = $saveformat; select($savefile);</p>
        <p id="p_08954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08954"> }</p>
        <p id="p_08955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08955"> This subroutine calls  select  to set the default output file to  STDOUT,  the standard output file. The return value from  select,  the previous default file, is saved in $  save file.</p>
        <p id="p_08956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08956"> Now that the default output file is  STDOUT,  the next step is to save the current print format being used to write to  STDOUT.  The subroutine does this by saving the present value of $~ in another local variable.</p>
        <p id="p_08957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08957"> $savef ormat.  After this is saved, the subroutine can set the current print format to  MYFORMAT.  The call to  write  now writes to the standard output file using  MYFORMAT.</p>
        <p id="p_08958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08958"> After the call to  write  is complete, the subroutine puts things back the way they were. The first step is to reset $~ to the value stored in $  save format.  The final step is to set the default output file back to the file variable whose representation is saved in $  save file.</p>
        <p id="p_08959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08959"> Note that the call to  select  must appear after the assignment to $~. If the call to  select  had been first, the assignment to $~ would change the print format associated with the original default file variable, not</p>
        <p id="p_08960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08960"> STDOUT.</p>
        <p id="p_08961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08961"> As you can see, this subroutine doesn't need to know what the default values outside the subroutine are. Also, it does not affect the default values outside the subroutine.</p>
        <p id="p_08962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08962"> Specifying a Page Header</p>
        <p id="p_08963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08963"> If you are sending your output to a printer, you can make your output look smarter by supplying text to appear at the top of every page in your output. This special text is called a  page header.</p>
        <p id="p_08964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08964"> If a page header is defined for a particular output file,  write  automatically paginates the output to that file. When the number of lines printed is greater than the length of a page,  write  starts a new page.</p>
        <p id="p_08965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08965"> To define a page header for a file, create a print format definition with the name of  f ilename_TOP,  where filename  is a placeholder for the name of the file variable corresponding to the file to which you are writing. For example, to define a header for writing to standard output, define a print format named STDOUT_TOP,  as follows:</p>
        <p id="p_08966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08966"> format STDOUT_TOP =</p>
        <p id="p_08967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08967"> Consolidated Widgets Inc. 1994 Annual Report</p>
        <p id="p_08968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08968"> In this case, when the Perl interpreter starts a new page of standard output, the contents of the print format STDOUT_TOP  are printed automatically.</p>
        <p id="p_08969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08969"> Print formats that generate headers can contain value fields which are replaced by scalar values, just like any other print format. One particular value that is often used in page headers is the current page number, which is stored in the system variable $%. For example:</p>
        <p id="p_08970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08970"> format STDOUT_TOP = Page @&lt;&lt;.</p>
        <p id="p_08971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08971"> $</p>
        <p id="p_08972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08972"> In this case, when the first page is printed, the program prints the following header at the top of the page:</p>
        <p id="p_08973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08973"> Page 1.</p>
        <p id="p_08974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08974"> NOTE</p>
        <p id="p_08975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08975"> By default, $% is initially set to zero and is incremented every time a new page begins.</p>
        <p id="p_08976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08976"> To change the pagination, change the value of $% before (or during) printing</p>
        <p id="p_08977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08977"> Changing the Header Print Format</p>
        <p id="p_08978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08978"> To change the name of the print format that prints a page header for a particular file, change the value stored in the special system variable $  A .</p>
        <p id="p_08979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08979"> As with $~, only the value for the current default file can be changed. For example, to use the print format MYHEADER  as the header file for the file  MYFILE,  add the following statements:</p>
        <p id="p_08980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08980"> $oldfile = select(MYFILE); $ A  = "MYHEADER"; select($oldfile) ;</p>
        <p id="p_08981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08981"> These statements set  MYFILE  to be the current default file, change the header for  MYFILE  to be the print format  MYHEADER,  and then reset the current default file to its original value.</p>
        <p id="p_08982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08982"> Setting the Page Length</p>
        <p id="p_08983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08983"> By default, the page length is 60 lines. To specify a different page length, change the value stored in the system variable $=:</p>
        <p id="p_08984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08984"> $= = 66;</p>
        <p id="p_08985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08985"> # set the page length to 66 lines</p>
        <p id="p_08986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08986"> This assignment must appear before the first  write  statement.</p>
        <p id="p_08987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08987"> Warning</p>
        <p id="p_08988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08988"> If the page length is changed in the middle of the program, the new page length will not be used until a new page is started</p>
        <p id="p_08989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08989"> Listing 11.8 shows how you can set the page length and define a page-header print format for your output file.</p>
        <p id="p_08990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08990"> - page @&lt;</p>
        <p id="p_08991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08991"> 16: .</p>
        <p id="p_08992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08992"> 17: format WRITELINE = 18: @»»»»»»»»&gt; 19: $line</p>
        <p id="p_08993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08993"> 20 : .</p>
        <p id="p_08994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08994"> Output</p>
        <p id="p_08995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08995"> Suppose that you supply the following input:</p>
        <p id="p_08996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08996"> $ programll_8</p>
        <p id="p_08997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08997"> Here is a line of input.</p>
        <p id="p_08998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08998"> Here is another line.</p>
        <p id="p_08999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_08999"> Here is the last line.</p>
        <p id="p_09000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09000"> A D</p>
        <p id="p_09001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09001"> $</p>
        <p id="p_09002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09002"> The following output is written to the file  f i 1 e 1:</p>
        <p id="p_09003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09003"> - page 1</p>
        <p id="p_09004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09004"> Here is a line of input. Here is another line.</p>
        <p id="p_09005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09005"> Here is the last line.</p>
        <p id="p_09006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09006"> Analysis</p>
        <p id="p_09007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09007"> Line  3  opens the file  f ilel  for output and associates it with the file variable  OUTFILE.</p>
        <p id="p_09008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09008"> Line 4 sets the current default file to  OUTFILE.  Now, when  write  or  print  is called with no file variable supplied, the output is sent to  OUTFILE.</p>
        <p id="p_09009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09009"> Line 5 indicates that  WRITELINE  is the print format to be used when writing to the file  OUTFILE.  To do this, it assigns  WRITELINE  to the system variable $~. This assignment does not affect the page header.</p>
        <p id="p_09010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09010"> Line  6  indicates that  TOP_OF_PAGE  is the print format to be used when printing the page headers for the file  OUTFILE.  This assignment does not affect the print format used to write to the body of the page.</p>
        <p id="p_09011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09011"> Line 7 sets the page length to 60 lines. This page length takes effect immediately, because no output has been written to  OUTFILE.</p>
        <p id="p_09012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09012"> Using print with Pagination</p>
        <p id="p_09013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09013"> Normally, you won't want to use  print  if you are using pagination, because the Perl interpreter keeps track of the current line number on the page by monitoring the calls to  write.  If you must use a call to  print  in your program and you want to ensure that the page counter includes the call in its line count, adjust the system variable $-. This system variable indicates the number of lines between the current line and the bottom of the page. When $- reaches 0, a top-of-form character is generated, which starts a new page.</p>
        <p id="p_09014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09014"> The following is a code fragment that calls  print  and then adjusts the $- variable:</p>
        <p id="p_09015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09015"> print ("Here is a line of output\n");</p>
        <p id="p_09016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09016"> $- -= i;</p>
        <p id="p_09017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09017"> When $ - has 1 subtracted from its value, the page counter becomes correct.</p>
        <p id="p_09018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09018"> Formatting Long Character Strings</p>
        <p id="p_09019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09019"> As you've seen, the @ * value field prints multiple lines of text. However, this field prints the output exactly as it is stored in the character string. For example, consider Listing 11.9, which uses @ * to write a multiline character string.</p>
        <p id="p_09020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09020"> Type</p>
        <p id="p_09021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09021"> Listing 11.9. A program that illustrates the limitations of the @ * value field.</p>
        <p id="p_09022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09022"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09023"> 2  :</p>
        <p id="p_09024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09024"> 3: $string = "Here\nis an unbalanced line of\ntext.\n";</p>
        <p id="p_09025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09025"> 4: $~ = "OUTLINE";</p>
        <p id="p_09026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09026"> 5: write;</p>
        <p id="p_09027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09027"> 6 :</p>
        <p id="p_09028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09028"> 7: format OUTLINE =</p>
        <p id="p_09029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09029"> 8 : @*</p>
        <p id="p_09030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09030"> 9: $string</p>
        <p id="p_09031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09031"> 10 : .</p>
        <p id="p_09032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09032"> Output</p>
        <p id="p_09033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09033"> $ programll_9 Here</p>
        <p id="p_09034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09034"> is an unbalanced line of text.</p>
        <p id="p_09035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09035"> $</p>
        <p id="p_09036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09036"> Analysis</p>
        <p id="p_09037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09037"> This call to  write  displays the character string stored in $  string  exactly as is. Perl enables you to define value fields in print-format definitions that format text. To do this, replace the initial @ character in the value field with a  A  character. When text formatting is specified, the Perl interpreter tries to fit as many words as possible into the output line.</p>
        <p id="p_09038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09038"> Listing 11.10 is an example of a simple program that does this.</p>
        <p id="p_09039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09039"> Type</p>
        <p id="p_09040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09040"> Listing 11.10. A program that uses a value field that does formatting.</p>
        <p id="p_09041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09041"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09042"> 2 :</p>
        <p id="p_09043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09043"> 3: $string = "Here\nis an unbalanced line of\ntext.\n";</p>
        <p id="p_09044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09044"> 4: $~ = "OUTLINE";</p>
        <p id="p_09045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09045"> 5: write;</p>
        <p id="p_09046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09046"> 6 :</p>
        <p id="p_09047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09047"> 7: format OUTLINE =</p>
        <p id="p_09048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09048"> 8  :</p>
        <p id="p_09049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09049"> 9: $string</p>
        <p id="p_09050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09050"> 10 : .</p>
        <p id="p_09051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09051"> Output</p>
        <p id="p_09052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09052"> $ programll_10 Here is an unbalanced line $</p>
        <p id="p_09053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09053"> Analysis</p>
        <p id="p_09054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09054"> I Line 5 calls  write  using the print format  OUTLINE.  This print format contains that specifies that formatting is to take place; this means that the Perl interpreter tries to fit as possible into the line of output. In this case, the first line  Here  and the four-word string  is unbalanced line  fit into the output line.</p>
        <p id="p_09055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09055"> a value field many words as</p>
        <p id="p_09056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09056"> an</p>
        <p id="p_09057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09057"> Note that there are two characters left over in the output line after the four words have been filled in. These characters are not filled, because the next word is not short enough to fit into the space remaining. Only entire words are filled.</p>
        <p id="p_09058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09058"> One other feature of the line-filling operation is that the substring printed out is actually deleted from the scalar variable  $string.  This means that the value of  $string  is now  of\ntext. \n.  This happens because subsequent lines of output in the same print-format definition can be used to print the rest of the string.</p>
        <p id="p_09059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09059"> NOTE</p>
        <p id="p_09060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09060"> Because the line-filling  write  operation updates the value used, the value must be contained in a scalar variable and cannot be the result of an expression</p>
        <p id="p_09061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09061"> To see how multiple lines of formatted output work, look at Listing 11.11. This program reads a quotation from the standard input file and writes it out on three formatted lines of output.</p>
        <p id="p_09062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09062"> Listing 11.11. A program that writes out multiple formatted lines of output.</p>
        <p id="p_09063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09063"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09064"> 2  :</p>
        <p id="p_09065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09065"> 3: Squotation = &lt;STDIN&gt;;</p>
        <p id="p_09066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09066"> 4: $quotation = join("", gquotation); 5: $~ = "QUOTATION";</p>
        <p id="p_09067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09067"> 6: write;</p>
        <p id="p_09068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09068"> 7 :</p>
        <p id="p_09069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09069"> 8: format QUOTATION =</p>
        <p id="p_09070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09070"> 9: Quotation for the day:</p>
        <p id="p_09071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09071"> 10 : 11 : 12 :</p>
        <p id="p_09072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09072"> 13 :</p>
        <p id="p_09073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09073"> 14 :</p>
        <p id="p_09074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09074"> 15 :</p>
        <p id="p_09075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09075"> $quotation</p>
        <p id="p_09076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09076"> $quotation</p>
        <p id="p_09077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09077"> 16:</p>
        <p id="p_09078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09078"> $quotation</p>
        <p id="p_09079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09079"> Output</p>
        <p id="p_09080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09080"> $ programll_ll</p>
        <p id="p_09081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09081"> Any sufficiently advanced programming language is indistinguishable from magic. A D</p>
        <p id="p_09082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09082"> Quotation for the day:</p>
        <p id="p_09083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09083"> Any sufficiently advanced programming language is indistinguishable from magic.</p>
        <p id="p_09084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09084"> Analysis</p>
        <p id="p_09085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09085"> The print format  QUOTATION  defines three value fields on which formatting is to be employed. Each of the three value fields uses the value of the scalar variable $  quotation.</p>
        <p id="p_09086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09086"> Before  write  is called, $  quotation  contains the entire quotation with newline characters appearing at the end of each input line. When  write  is called, the first value field in the print format uses as much of the quotation as possible. This means that the following substring is written to the standard output file:</p>
        <p id="p_09087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09087"> Any sufficiently advanced programming language is</p>
        <p id="p_09088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09088"> After the substring is written, it is removed from $  quotation,  which now contains the following:</p>
        <p id="p_09089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09089"> indistinguishable from magic.</p>
        <p id="p_09090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09090"> Because the written substring has been removed from  $quotat ion,  the remainder of the string can be</p>
        <p id="p_09091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09091"> used in subsequent output lines. Because the next value field in the print format also wants to use $ quotation,  the remainder of the string appears on the second output line and is deleted. $  quotation is now the empty string.</p>
        <p id="p_09092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09092"> This means that the third value field, which also refers to $  quotation,  is replaced by the empty string, and a blank line is written out.</p>
        <p id="p_09093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09093"> Warning</p>
        <p id="p_09094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09094"> The scalar variable containing the output to be printed is changed by a write  operation. If you need to preserve the information, copy it to another scalar variable before calling  write</p>
        <p id="p_09095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09095"> Eliminating Blank Lines When Formatting</p>
        <p id="p_09096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09096"> You can eliminate blank lines such as the one generated by Listing 11.11. To do this, put a ~ character at the beginning of any output line that is to be printed only when needed.</p>
        <p id="p_09097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09097"> Listing 11.12 modifies the quotation-printing program to print lines only when they are not blank.</p>
        <p id="p_09098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09098"> ■23</p>
        <p id="p_09099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09099"> Listing 11.12. A program that writes out multiple formatted lines of output and suppresses blank lines.</p>
        <p id="p_09100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09100"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09101"> 2  :</p>
        <p id="p_09102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09102"> 3: @quotation = &lt;STDIN&gt;;</p>
        <p id="p_09103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09103"> 4: $quotation = join("", @quotation);</p>
        <p id="p_09104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09104"> 5: $~ = "QUOTATION";</p>
        <p id="p_09105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09105"> 6: write;</p>
        <p id="p_09106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09106"> 7 :</p>
        <p id="p_09107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09107"> 8: format QUOTATION =</p>
        <p id="p_09108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09108"> 9: Quotation for the day:</p>
        <p id="p_09109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09109"> 10 : 11 : 12 : 13:</p>
        <p id="p_09110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09110"> 14 :</p>
        <p id="p_09111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09111"> 15 : 16:</p>
        <p id="p_09112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09112"> 17 :</p>
        <p id="p_09113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09113"> 18 :</p>
        <p id="p_09114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09114"> $quotation</p>
        <p id="p_09115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09115"> $quotation</p>
        <p id="p_09116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09116"> $quotation</p>
        <p id="p_09117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09117"> Output</p>
        <p id="p_09118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09118"> $ programll_12</p>
        <p id="p_09119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09119"> Any sufficiently advanced programming language is indistinguishable from magic. A D</p>
        <p id="p_09120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09120"> Quotation for the day:</p>
        <p id="p_09121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09121"> Any sufficiently advanced programming language is indistinguishable from magic.</p>
        <p id="p_09122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09122"> $</p>
        <p id="p_09123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09123"> Analysis</p>
        <p id="p_09124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09124"> If the quotation is too short to require all the lines, remaining lines are left blank. In this case, the quotation requires only two lines of output, so the third isn't printed.</p>
        <p id="p_09125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09125"> The program is identical to the one in Listing 11.11 in all other respects. In particular, the value of $quotation  after the call to  write  is still the empty string.</p>
        <p id="p_09126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09126"> Supplying an Indefinite Number of Lines</p>
        <p id="p_09127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09127"> While Listing 11.12 suppresses blank lines, it imposes an upper limit of thr ee lines. Quotations longer than three lines are not printed in their entirety. To indicate that the formatted output is to use as many lines as necessary, specify two ~ characters at the beginning of the output line containing the value field. Listing 11.13 modifies the quotation program to allow quotations of any length.</p>
        <p id="p_09128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09128"> Type</p>
        <p id="p_09129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09129"> Listing 11.13. A program that writes out as many formatted lines of output as necessary.</p>
        <p id="p_09130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09130"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09131"> 2  :</p>
        <p id="p_09132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09132"> 3: Squotation = &lt;STDIN&gt;;</p>
        <p id="p_09133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09133"> 4: $quotation = join("", @quotation); 5: $~ = "QUOTATION";</p>
        <p id="p_09134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09134"> 6: write;</p>
        <p id="p_09135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09135"> 7 :</p>
        <p id="p_09136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09136"> 8: format QUOTATION =</p>
        <p id="p_09137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09137"> 9: Quotation for the day:</p>
        <p id="p_09138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09138"> 10: -</p>
        <p id="p_09139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09139"> 11 : 12 :</p>
        <p id="p_09140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09140"> 13 :</p>
        <p id="p_09141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09141"> 14 :</p>
        <p id="p_09142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09142"> $quotation</p>
        <p id="p_09143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09143"> Output</p>
        <p id="p_09144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09144"> $ programll_13</p>
        <p id="p_09145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09145"> Any sufficiently advanced programming language is indistinguishable from magic. A D</p>
        <p id="p_09146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09146"> Quotation for the day:</p>
        <p id="p_09147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09147"> Any sufficiently advanced programming language is indistinguishable from magic.</p>
        <p id="p_09148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09148"> $</p>
        <p id="p_09149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09149"> The ~ ~ characters at the beginning of the output field indicate that multiple copies of the output line are to be supplied. The output line is to be printed until there is nothing more to print.</p>
        <p id="p_09150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09150"> In Listing 11.13, two copies of the line are needed.</p>
        <p id="p_09151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09151"> Analysis</p>
        <p id="p_09152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09152"> Formatting Output Using printf</p>
        <p id="p_09153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09153"> If you want to write output that looks reasonable without going to all the trouble of using  write  and print  formats, Perl provides a built-in function,  printf,  that prints formatted output.</p>
        <p id="p_09154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09154"> NOTE</p>
        <p id="p_09155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09155"> If you are familiar with the  C  programming language, the behavior of printf  in Perl will be familiar; the Perl  printf  and the  C printf are basically the same</p>
        <p id="p_09156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09156"> The arguments passed to the  printf  function are as follows:</p>
        <p id="p_09157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09157"> • The string to be printed, which can contain one or mor e field specifiers</p>
        <p id="p_09158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09158"> •  One value for each field specifier appearing in the string to be printed</p>
        <p id="p_09159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09159"> When  printf  sees a field specifier, it substitutes the corresponding value in the  printf  argument list. The representation of the substituted value in the string depends on the field specifier that is supplied.</p>
        <p id="p_09160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09160"> Field specifiers consist of the % character followed by a single character that represents the format to use</p>
        <p id="p_09161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09161"> when printing. Table 11.2 lists the field-specifier formats and the field-specifier character that represents each.</p>
        <p id="p_09162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09162"> Table 11.2. Field specifiers for printf.</p>
        <p id="p_09163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09163"> Here is a simple example of a call to  print f:</p>
        <p id="p_09164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09164"> printf ("The number I want to print is %d.\n", $number);</p>
        <p id="p_09165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09165"> The string to be printed contains one field specifier,  %d,  which represents an integer. The value stored in $ number  is substituted for the field specifier and printed.</p>
        <p id="p_09166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09166"> Field specifiers also support a variety of options, as follows:</p>
        <p id="p_09167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09167"> • If you are printing an integer using the  d, o, u,  or  x  format, you can put an  1  character in front of the field-specifier character (as in, for example,  %ld).  This character specifies that the number is a decimal integer in the machine's "long integer" format (corresponding to the  C  type  long).  This is useful if your integer is large or might be.</p>
        <p id="p_09168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09168"> • A positive integer following the % character indicates the minimum width of the field. For example, %2 0 s  prints a character string in a field of 20 characters. If the string is not large enough to fill the entire field, it is right justified (placed at the right end of the field) and padded with blanks. (If the integer starts with a leading 0, as in  %0 8d,  the field is padded with zeros, not blanks.)</p>
        <p id="p_09169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09169"> • A negative integer following the % character indicates the width of the field and requests left justification. For example, %-15s prints a character string in a field of 15 characters, and it fills the right end of the field with blanks if the string is not large enough.</p>
        <p id="p_09170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09170"> • If you are using a field specifier that prints a floating-point number  (%e, %f,  or  %g),  you can specify the number of digits that are to appear after the decimal point. To do this, specify a floating-point number after the % character. For example:</p>
        <p id="p_09171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09171"> %8.3f</p>
        <p id="p_09172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09172"> • Here, the number preceding the decimal point is the field width (as before), and the number after the decimal point is the number of decimal places to print.</p>
        <p id="p_09173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09173"> Warning</p>
        <p id="p_09174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09174"> If a floating-point number contains more digits than the field specifier wants, the number is rounded to the number of decimal places needed. For example, if 4 3 .4 99 is being printed using the field  %5 .2f,  the number actually printed is  4 3.50.</p>
        <p id="p_09175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09175"> As with the write value field  @##.##,printf  might not always round up when it is handling numbers whose last decimal place is 5. This happens because some floating-point numbers cannot be stored exactly, and the nearest equivalent number that can be stored is a slightly smaller number (which rounds down, not up). For example,</p>
        <p id="p_09176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09176"> 4 3.495  when printed by %  5.2 f  might print  43.49,  depending on how  4 3.495  is stored</p>
        <p id="p_09177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09177"> • If you are using a field specifier that prints an integer, character, or string, supplying a floating-point number after the % character specifies the maximum length of the value to be printed. In the following example a character string is printed in a 15-character field, but the string itself can be at most 10 characters long:</p>
        <p id="p_09178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09178"> %15.10s</p>
        <p id="p_09179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09179"> • This guarantees that at least five spaces will appear in the printed line.</p>
        <p id="p_09180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09180"> NOTE</p>
        <p id="p_09181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09181"> You can use  pr int f  to print to other files. To do this, specify the file variable corresponding to the file to which you want to print, just as you would with  print  or  write</p>
        <p id="p_09182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09182"> printf MYFILE ("I am printing %d.\n", $value);</p>
        <p id="p_09183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09183"> This means that changing the current default file using  select affects  printf.</p>
        <p id="p_09184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09184"> Summary</p>
        <p id="p_09185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09185"> Perl enables you to format your output using print-format definitions and the built-in function  write.  In print-format definitions, you can specify value fields that are to be replaced by either the contents of scalar variables or the values of expressions.</p>
        <p id="p_09186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09186"> Value fields indicate how to print the contents of a scalar variable or the value of an expression. With a value field, you can specify that the value is to be left justified (blanks added on the right), right justified (blanks added on the left), centered, or displayed as a floating-point number.</p>
        <p id="p_09187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09187"> You also can define value fields that format a multiline character string. Blank lines can be suppressed, and the field can be defined to use as many output lines as necessary.</p>
        <p id="p_09188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09188"> The built-in function  select  enables you to change the default file to which  write  and  print  send output.</p>
        <p id="p_09189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09189"> You can break your output into pages by defining a special header print format that prints header information at the top of each page.</p>
        <p id="p_09190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09190"> The following system variables enable you to control how  write  sends output to a file:</p>
        <p id="p_09191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09191"> • The system variable $ ~ contains the name of the print format being used by the current default file.</p>
        <p id="p_09192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09192"> • The system variable $  A  contains the name of the print format being used as a page header by the</p>
        <p id="p_09193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09193"> current default file.</p>
        <p id="p_09194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09194"> • The system variable $= contains the number of lines per printed page.</p>
        <p id="p_09195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09195"> • The system variable $- contains the number of lines left on the current page.</p>
        <p id="p_09196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09196"> The built-in function  pr int f  enables you to format an individual line of text using format specifiers.</p>
        <p id="p_09197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09197"> Q&amp;A</p>
        <p id="p_09198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09198"> Q: Which is better, write or printf?</p>
        <p id="p_09199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09199"> A: It depends on what you want to do. If you want to print reports or control pagination, you'll need to</p>
        <p id="p_09200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09200"> use  write.  If you just want individual lines of output to look neat,  print f  might be what you need.</p>
        <p id="p_09201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09201"> Q: How do I generate a page break?</p>
        <p id="p_09202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09202"> A: To do this, set $- to zero. This generates a top-of-form character.</p>
        <p id="p_09203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09203"> Q: Why do value fields that format text modify the contents of the scalar variable containing the</p>
        <p id="p_09204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09204"> text?</p>
        <p id="p_09205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09205"> A: When formatted text is printed, the printed text is removed from the scalar variable, and the part of</p>
        <p id="p_09206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09206"> the string that is not printed is retained. This enables you to use other calls to  write  to print the remainder of the text. In fact, you can print the rest of the text in the scalar variable using a completely different print format.</p>
        <p id="p_09207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09207"> Q: How many print formats can I define?</p>
        <p id="p_09208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09208"> A: Basically, as many as you like, provided the resulting Perl program can still fit in your machine.</p>
        <p id="p_09209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09209"> Workshop</p>
        <p id="p_09210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09210"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try to understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_09211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09211"> Quiz</p>
        <p id="p_09212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09212"> 1. Define value fields that print the following:</p>
        <p id="p_09213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09213"> a. Ten left-justified characters</p>
        <p id="p_09214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09214"> b. Five right-justified characters</p>
        <p id="p_09215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09215"> c. Two centered characters</p>
        <p id="p_09216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09216"> d. A floating-point number with five digits before the decimal point and three after it</p>
        <p id="p_09217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09217"> e. A field that prints as many formatted lines of 30 left-justified characters as necessary</p>
        <p id="p_09218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09218"> 2. What do these fields print?</p>
        <p id="p_09219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09219"> a. @&lt;&lt;&lt;&lt;</p>
        <p id="p_09220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09220"> b.  @111111</p>
        <p id="p_09221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09221"> c. @</p>
        <p id="p_09222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09222"> d.  @*</p>
        <p id="p_09223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09223"> 3.  What do these  pr int f  field specifiers print?</p>
        <p id="p_09224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09224"> a. %5d</p>
        <p id="p_09225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09225"> b. %11.4f</p>
        <p id="p_09226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09226"> c. %010d</p>
        <p id="p_09227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09227"> d. %-12s</p>
        <p id="p_09228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09228"> e. %x</p>
        <p id="p_09229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09229"> 4. Why do certain floating-point numbers have round-off problems?</p>
        <p id="p_09230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09230"> 5. How do you create a page header for an output file?</p>
        <p id="p_09231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09231"> Exercises</p>
        <p id="p_09232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09232"> 1. Write a program that prints the powers of 2 from  2**1   to  2**10.  Use  write  and a print format to print them three to a line. Align the lines so that the right end of each number is lined up with the right end of the corresponding number on the previous line.</p>
        <p id="p_09233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09233"> 2.  Repeat Exercise  1  using  print f.</p>
        <p id="p_09234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09234"> 3.  Write a program that reads text and formats it into 40-character lines, left-justified. Put lines of asterisks above and below the text.</p>
        <p id="p_09235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09235"> 4. Write a program that reads a set of dollar values such as  71.43  (one per line). Write out two values per line (the first and second on the first line, and so on). Total each of the resulting columns, and produce a grand total.</p>
        <p id="p_09236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09236"> 5.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_09237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09237"> #!/usr/local/bin/perl</p>
        <p id="p_09238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09238"> format STDOUT =</p>
        <p id="p_09239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09239"> @*</p>
        <p id="p_09240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09240"> while ($line = &lt;STDIN&gt;) { chop ($line); if ($line eq "") {</p>
        <p id="p_09241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09241"> print ("cblank line&gt;\n"); next;</p>
        <p id="p_09242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09242"> }</p>
        <p id="p_09243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09243"> write;</p>
        <p id="p_09244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09244"> }</p>
        <p id="p_09245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09245"> Chapter 12</p>
        <p id="p_09246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09246"> Working with the File System</p>
        <p id="p_09247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09247"> CONTENTS</p>
        <p id="p_09248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09248"> •  File Input and Output Functions</p>
        <p id="p_09249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09249"> o  Basic Input and Output Functions</p>
        <p id="p_09250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09250"> o  Skipping and Rereading Data o  System Read and Write Functions</p>
        <p id="p_09251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09251"> o  Reading Characters Using  qetc o  Reading a Binary File Using  binmode</p>
        <p id="p_09252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09252"> •  Directory-Manipulation Functions</p>
        <p id="p_09253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09253"> o  The  mkdir  Function o  The  chdir  Function o  The  opendir  Function o  The  closedir  Function</p>
        <p id="p_09254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09254"> o  The  readdir  Function o  The telldir and seekdir Functions</p>
        <p id="p_09255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09255"> o  The  rewinddir  Function</p>
        <p id="p_09256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09256"> o  The  rmdir  Function</p>
        <p id="p_09257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09257"> •  File-Attribute Functions</p>
        <p id="p_09258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09258"> o  File-Relocation Functions o  Link and Symbolic Link Functions</p>
        <p id="p_09259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09259"> o  File-Permission Functions o  Miscellaneous Attribute Functions</p>
        <p id="p_09260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09260"> •  Using DBM Files</p>
        <p id="p_09261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09261"> o  The  dbmopen  Function o  The  dbmclose  Function</p>
        <p id="p_09262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09262"> •  Summary</p>
        <p id="p_09263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09263"> •  Q&amp;A</p>
        <p id="p_09264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09264"> •  Workshop</p>
        <p id="p_09265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09265"> o  Quiz o  Exercises</p>
        <p id="p_09266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09266"> Today's lesson teaches you how to manipulate your machine's file system using some of Perl's built-in library functions. Today, you learn about the following:</p>
        <p id="p_09267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09267"> • The file input and output functions</p>
        <p id="p_09268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09268"> • The directory-manipulation functions</p>
        <p id="p_09269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09269"> • The file-attribute manipulation functions</p>
        <p id="p_09270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09270"> • The DBM file functions</p>
        <p id="p_09271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09271"> Warning</p>
        <p id="p_09272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09272"> Many of the functions described in today's lesson use features of the UNIX operating system. If you are using Perl on a machine that is not running UNIX, some of these functions might not be defined or might behave differently.</p>
        <p id="p_09273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09273"> Check the documentation supplied with your version of Perl for details on which functions are supported or emulated on your machine</p>
        <p id="p_09274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09274"> File Input and Output Functions</p>
        <p id="p_09275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09275"> The following sections describe the built-in library functions that read information from files and write information to files. These library functions perform the following tasks:</p>
        <p id="p_09276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09276"> • Basic input and output</p>
        <p id="p_09277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09277"> • Skipping or re-reading data from a file</p>
        <p id="p_09278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09278"> • Reading individual characters from a file</p>
        <p id="p_09279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09279"> • Indicating that a file is a binary file</p>
        <p id="p_09280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09280"> Basic Input and Output Functions</p>
        <p id="p_09281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09281"> Some of the input and output functions supplied by Perl have been discussed in earlier chapters. These are</p>
        <p id="p_09282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09282"> • open,  which lets a program access a file</p>
        <p id="p_09283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09283"> •  close,  which terminates file access</p>
        <p id="p_09284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09284"> • print,  which writes a string to a file</p>
        <p id="p_09285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09285"> • write,  which writes information to a file using a print format</p>
        <p id="p_09286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09286"> • pr int f , which formats a string and sends it to a file</p>
        <p id="p_09287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09287"> The following sections briefly describe these functions again, along with some features of these functions that have not been discussed previously.</p>
        <p id="p_09288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09288"> The open Function</p>
        <p id="p_09289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09289"> The  open  function enables a Perl program to access a file. It associates a special file variable with each</p>
        <p id="p_09290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09290"> accessed file. The following is an example:</p>
        <p id="p_09291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09291"> open (MYVAR, "/u/jqpublic/file");</p>
        <p id="p_09292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09292"> Here,  open  requests access to the file  /u  /  jqpublic / f ile,  and it associates the file  MYVAR  with this file after it is open,  open  returns a nonzero value if the open succeeds, and zero if the open fails.</p>
        <p id="p_09293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09293"> By default,  open  opens a file for reading only. To open a file for writing, put a &gt; character in front of the filename, as follows:</p>
        <p id="p_09294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09294"> open (MYVAR, "&gt;/u/jqpublic/file");</p>
        <p id="p_09295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09295"> To append information to an existing file, put two &gt; characters in front of the filename, as follows:</p>
        <p id="p_09296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09296"> open (MYVAR, "&gt;&gt;/u/jqpublic/file");</p>
        <p id="p_09297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09297"> To treat the open file as a command to which to pipe data, put a pipe (|) character in front of the filename, as follows:</p>
        <p id="p_09298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09298"> open (MAIL, "|mail dave") ;</p>
        <p id="p_09299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09299"> (For more information, refer to  Day  6 ,  "Reading from and Writing to Files.")</p>
        <p id="p_09300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09300"> Piping Input Using open</p>
        <p id="p_09301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09301"> The  open  function enables you to open files in several other ways not previously discussed. For example, to treat the open file as a command that is piping data to this program, put a | character after the filename. For example:</p>
        <p id="p_09302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09302"> open (CAT, "cat  file*|");</p>
        <p id="p_09303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09303"> This call to  open  executes the command  cat file*.  This command creates a temporary file consisting of the contents of all files whose name starts with  file;  these contents are joined (concatenated) into a single file. This file is treated as an input file that is accessible using the file variable</p>
        <p id="p_09304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09304"> CAT.</p>
        <p id="p_09305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09305"> $input</p>
        <p id="p_09306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09306"> &lt;CAT&gt;;</p>
        <p id="p_09307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09307"> Listing 12.1 is another example of a program that uses piped input. This program uses the output from the w  command to list the users who are currently logged on to the machine.</p>
        <p id="p_09308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09308"> Type</p>
        <p id="p_09309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09309"> Listing 12.1. A program that receives input from a piped command.</p>
        <p id="p_09310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09310"> 17: if ($user ne $prevuser) {</p>
        <p id="p_09311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09311"> 18: print ("\t$user");</p>
        <p id="p_09312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09312"> 19 :</p>
        <p id="p_09313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09313"> $prevuser = $user;</p>
        <p id="p_09314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09314"> }</p>
        <p id="p_09315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09315"> 20 :</p>
        <p id="p_09316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09316"> 21 : }</p>
        <p id="p_09317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09317"> Output</p>
        <p id="p_09318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09318"> $ programl2_l Current time: 4:25pm Users logged on: dave kilroy root zarquon</p>
        <p id="p_09319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09319"> $</p>
        <p id="p_09320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09320"> Analysis</p>
        <p id="p_09321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09321"> The  w  command lists the current time, the machine load, and the users logged onto the machine. It also lists the job time and the currently executing command for each user.</p>
        <p id="p_09322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09322"> Here is sample output for the  w  command:</p>
        <p id="p_09323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09323"> 4:25pm up 1 day, 6:37, 6 u</p>
        <p id="p_09324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09324"> ers, load average: 0.79, 0.36,</p>
        <p id="p_09325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09325"> kilroy ttyp5</p>
        <p id="p_09326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09326"> 9 : 03 am</p>
        <p id="p_09327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09327"> 2 :14</p>
        <p id="p_09328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09328"> 48 /usr/games/hack</p>
        <p id="p_09329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09329"> This Perl program takes the output from the  w  command and massages it to retrieve only the information needed: the current time and the users who are currently logged on.</p>
        <p id="p_09330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09330"> Line  3  starts the  w  command. The call to  open  specifies that the output from  w  is to be treated as input to this program, and that the file variable  WOUT  is to be used to access this input.</p>
        <p id="p_09331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09331"> Line 4 reads the first line of the input piped from  WOUT.  This is the line read:</p>
        <p id="p_09332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09332"> 4:25pm up 1 day, 6:37, 6 users, load average:  0.79, 0.36, 0.28</p>
        <p id="p_09333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09333"> The following two lines extract the current time from this line. First, line 5 removes the leading spaces. Then, line 6 removes everything after the first word, except for the trailing newline character. This leaves the time,  4 : 2 5pm,  along with the trailing newline, stored in  $t ime.</p>
        <p id="p_09334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09334"> Line 7 reads the second line from  WOUT.  Because this line contains no useful information, there is no need to assign it to any scalar variable.</p>
        <p id="p_09335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09335"> Line  8  reads the rest of the output from  w  to the array variable  @users.  After this output has been read, line  9  closes  WOUT,  which terminates the process that is running the  w  command.</p>
        <p id="p_09336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09336"> Each element of the list stored in  @users  contains one line of user information. Because this program needs only the first word of each line, lines 10-12 get rid of everything else (except, again, for the trailing newline character). After this loop is complete, the array in  @users  contains a list of users logged on.</p>
        <p id="p_09337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09337"> Line 13 prints the current time, as stored in  $time.  Note that  print  does not need to specify a trailing newline character, because  $time  contains one.</p>
        <p id="p_09338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09338"> Lines 16-21 sort the list of users in  @users  and prints them. Because a user can be logged on more than once,  $prevuser  stores the last user name printed. The value stored in  $user  is not printed unless it is not the same as the value stored in  $prevuser.</p>
        <p id="p_09339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09339"> Redirecting One File to Another</p>
        <p id="p_09340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09340"> Many UNIX shells enable you to direct both the standard output file and the standard error file to the same output file. For example, in the Bourne shell  sh,  the command</p>
        <p id="p_09341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09341"> $ foo &gt;filel 2&gt;&amp;1</p>
        <p id="p_09342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09342"> runs the command  foo  and stores the output from the standard output file and the standard error file in</p>
        <p id="p_09343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09343"> filel.</p>
        <p id="p_09344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09344"> Listing 12.2 shows how you can do this in Perl.</p>
        <p id="p_09345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09345"> Type</p>
        <p id="p_09346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09346"> Listing 12.2. A program that redirects the standard output and standard error files.</p>
        <p id="p_09347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09347"> Output</p>
        <p id="p_09348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09348"> This program produces no output.</p>
        <p id="p_09349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09349"> Analysis</p>
        <p id="p_09350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09350"> The following are the contents of the output file  f ilel:</p>
        <p id="p_09351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09351"> line 2 line 1</p>
        <p id="p_09352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09352"> As you can see, these lines aren't in the order intended. To understand what is happening, let's examine this program in more detail.</p>
        <p id="p_09353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09353"> Line  3  redirects the standard output file. To do this, it opens the output file  f ilel  and associates it with the file variable  STDOUT;  this closes the standard output file.</p>
        <p id="p_09354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09354"> Line  4  redirects the standard error file. The argument &gt;&amp;  STD OUT  tells the Perl interpreter to use the file already opened and associated with  STDOUT.  This means that the file variable  STDERR  refers to the same file as  STDOUT.</p>
        <p id="p_09355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09355"> Lines 5 and 6 write to  STDOUT  and  STDERR,  respectively. Because these file variables refer to the same file, both lines are written to f i 1 e 1. Unfortunately, they are written in the wrong order. What has happened?</p>
        <p id="p_09356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09356"> The problem arises because of how UNIX handles the writing of output. When you use print (or any other function) to write to a file such as the standard output file, what the UNIX operating system really does is copy the output to a special internal storage area called a  buffer.  (You can think of a buffer as a giant character string or as an array of characters.) Subsequent output operations continue writing to the buffer until it is full; when the buffer is full, the entire buffer is written out. Copying to a buffer and then writing out the entire buffer takes much less time than writing individual lines of text. (This is because, on most machines, input-output operations are slower than memory-access operations.)</p>
        <p id="p_09357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09357"> When a program ends, any non-empty buffers are written out. However, the system maintains separate buffers for STDERR and STDOUT, and it writes out the buffer for STDERR first. This means that line 2, which is stored in the STDERR buffer, appears before line 1, which is stored in the STDOUT buffer.</p>
        <p id="p_09358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09358"> To get around this problem, you can tell the Perl interpreter not to use a buffer for a particular file. To do this, do the following:</p>
        <p id="p_09359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09359"> 1. Select the file using the  select  function.</p>
        <p id="p_09360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09360"> 2. Assign 1 to the system variable $ |.</p>
        <p id="p_09361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09361"> The system variable $ | indicates whether a particular file is to be buffered (in other words, whether it should use a buffer or not). If $ | is assigned a nonzero value, no buffer is used. As with $~ and $  A , assigning to $ | affects the current default file, which is the file last specified in a call to  select  (or STDOUT, if  select  has not been called).</p>
        <p id="p_09362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09362"> Listing 12.3 shows how you can use $ | to ensure that your output lines appear in the correct order.</p>
        <p id="p_09363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09363"> Listing 12.3. A program that redirects standard input and output and turns off buffering.</p>
        <p id="p_09364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09364"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09365"> 2 :</p>
        <p id="p_09366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09366"> 3: open (STDOUT, "&gt;filel") || die ("open STDOUT failed");</p>
        <p id="p_09367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09367"> 4: open (STDERR, "&gt;&amp;STDOUT") || die ("open STDERR failed");</p>
        <p id="p_09368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09368"> 5: $1=1;</p>
        <p id="p_09369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09369"> 6:  select (STDERR);</p>
        <p id="p_09370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09370"> 7: $1 = 1;</p>
        <p id="p_09371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09371"> 8: print STDOUT ("line  l\n");</p>
        <p id="p_09372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09372"> 9:  print STDERR ("line  2\n");</p>
        <p id="p_09373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09373"> 10: close (STDOUT);</p>
        <p id="p_09374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09374"> 11: close (STDERR);</p>
        <p id="p_09375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09375"> Output</p>
        <p id="p_09376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09376"> This program produces no output.</p>
        <p id="p_09377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09377"> Analysis</p>
        <p id="p_09378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09378"> The contents of the output file  f ilel  are now the following:</p>
        <p id="p_09379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09379"> line 1 line 2</p>
        <p id="p_09380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09380"> Line 5 sets $ | to 1, which tells the Perl interpreter that the current default file does not need to be buffered. Because  select  has not yet been called, the current default file is  STDOUT,  which means that line 5 turns off buffering for the standard output file (which has been redirected to  f ilel).</p>
        <p id="p_09381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09381"> Line  6  sets the current default file to  STDERR,  and line  7  once again sets $ | to 1. This turns off buffering for the standard error file (which has also been redirected to  f ilel).</p>
        <p id="p_09382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09382"> Because buffering has been turned off for both  STDERR  and  STDOUT,  lines  8  and  9  write to  f ilel  right away. This means that the output lines appear in  f ilel  in the order in which they are printed.</p>
        <p id="p_09383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09383"> Specifying Read and Write Access</p>
        <p id="p_09384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09384"> To open a file for both read and write access, specify +&gt; before the filename, as follows:</p>
        <p id="p_09385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09385"> open (READWRITE,  "+&gt;filel");</p>
        <p id="p_09386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09386"> This opens the file named  f ilel  for both reading and writing. This enables you to overwrite portions of a file.</p>
        <p id="p_09387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09387"> Opening a file for reading and writing works best in conjunction with the library functions  seek  and tell,  which enable you to skip to the middle of a file. (For more information on seek and tell, refer to the section called "Skipping and Rereading Data," later in today's lesson.)</p>
        <p id="p_09388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09388"> NOTE</p>
        <p id="p_09389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09389"> You also can use +&lt; as the prefix to specify both reading and writing, as follows:</p>
        <p id="p_09390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09390"> open (READWRITE, "+&lt;filel");</p>
        <p id="p_09391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09391"> The prefix &lt;, by itself, specifies that the file is to be opened for reading. This means that the following two statements are identical:</p>
        <p id="p_09392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09392"> open (READONLY, "&lt;read"); open (READONLY, "read")</p>
        <p id="p_09393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09393"> The close Function</p>
        <p id="p_09394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09394"> The library function  close  was discussed on  Day  6 ,  "Reading from and Writing to Files." It closes a file opened by  open,  as follows:</p>
        <p id="p_09395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09395"> close (MYFILE) ;</p>
        <p id="p_09396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09396"> Here,  MYFILE  is the file variable (passed to  open)  that is associated with the open file.</p>
        <p id="p_09397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09397"> NOTE</p>
        <p id="p_09398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09398"> If you use  close  to close a pipe, the program will wait for the piped program to terminate. For example:</p>
        <p id="p_09399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09399"> open (MYPIPE, "cat  file*|"); close (MYPIPE);</p>
        <p id="p_09400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09400"> When  close  is called, the program suspends execution until the command  cat file*  is terminated</p>
        <p id="p_09401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09401"> The print, print f, and write Functions</p>
        <p id="p_09402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09402"> The  print, print f,  and  write  functions have been covered also in previous chapters, but I'll briefly recap them here.</p>
        <p id="p_09403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09403"> The  print  function is the simplest function. It writes to the file specified, or to the current default file if no file is specified. For example:</p>
        <p id="p_09404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09404"> print ("Hello, there!\n") ;</p>
        <p id="p_09405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09405"> print OUTFILE ("Hello, there!\n");</p>
        <p id="p_09406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09406"> The first statement writes to the current default file (which is  STDOUT  unless  select  has been called). The second statement writes to the file specified by  OUTFILE.</p>
        <p id="p_09407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09407"> The  pr int f  function formats a string and sends it to either the file specified or the current default file. For example, the statement</p>
        <p id="p_09408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09408"> printf OUTFILE ("You owe me  %8.2f",  $owing);</p>
        <p id="p_09409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09409"> takes the value stored in $  owing  and substitutes it for  %8.2 f  in the specified string.  %8.2  f  is an example of a  field specifier  and indicates that the value stored in $  owing  is to be treated as a floating¬ point number.</p>
        <p id="p_09410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09410"> The  write  function uses a print format to send formatted output to the file that is specified or to the current default file. For example:</p>
        <p id="p_09411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09411"> select (OUTFILE);</p>
        <p id="p_09412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09412"> $~ = "MYFORMAT"; write;</p>
        <p id="p_09413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09413"> This call to  write  uses the print format  MYFORMAT  to send output to the file  OUTFILE.</p>
        <p id="p_09414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09414"> For more information on  printf  or  write,  refer to  Day  11 ,  "Formatting Your Output."</p>
        <p id="p_09415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09415"> The select Function</p>
        <p id="p_09416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09416"> The  select  function also is covered on  Day 11 . This function is passed a file variable, which becomes the new current default file. For example:</p>
        <p id="p_09417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09417"> select (MYFILE);</p>
        <p id="p_09418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09418"> In this case,  MYFILE  is now the current default file, which means that calls to  print, write,  and print f  write to  MYFILE  unless a file variable is explicitly specified.</p>
        <p id="p_09419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09419"> The eof Function</p>
        <p id="p_09420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09420"> The library function  eof  checks whether the last input file read has been exhausted. If all of the input has been read,  eof  returns a nonzero value. If there is input remaining,  eof  returns zero.</p>
        <p id="p_09421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09421"> The  eof  function was first introduced on  Day  6 .  You might have noticed that, on that day, the examples that use  eof  use it without parentheses. This is because the behavior of  eof  is a little tricky if you are using it in conjunction with the &lt;&gt; operator; in this case,  eof  and  eof  () behave differently.</p>
        <p id="p_09422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09422"> Listing 12.4 shows how  eof  interacts with &lt;&gt;. It prints the contents of one or more input files whose names are supplied on the command line. A line of dashes is printed after each input file is completed.</p>
        <p id="p_09423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09423"> To run this program yourself, create two files named  f ilel  and  file 2.  Put the following in  f ilel:</p>
        <p id="p_09424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09424"> This is a line from the first file.</p>
        <p id="p_09425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09425"> Here is the last line of the first file.</p>
        <p id="p_09426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09426"> Then, put the following in  file2:</p>
        <p id="p_09427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09427"> This is a line from the second and last file.</p>
        <p id="p_09428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09428"> Here is the last line of the last file.</p>
        <p id="p_09429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09429"> Finally, specify  f ilel  and  file 2  on the command line when you run this program. For example, if you have called this program  program 12_4,  run it as follows:</p>
        <p id="p_09430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09430"> $ programl2_4 filel file2</p>
        <p id="p_09431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09431"> This will give you the output shown in the input-output example.</p>
        <p id="p_09432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09432"> Listing 12.4. A program that uses eof and &lt;&gt; together.</p>
        <p id="p_09433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09433"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09434"> 2  :</p>
        <p id="p_09435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09435"> 3: while ($line = &lt;&gt;) {</p>
        <p id="p_09436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09436"> 4: print ($line);</p>
        <p id="p_09437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09437"> 5: if (eof) {</p>
        <p id="p_09438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09438"> 6: print ("— end of current file  —\n");</p>
        <p id="p_09439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09439"> 7: }</p>
        <p id="p_09440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09440"> 8 : }</p>
        <p id="p_09441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09441"> Output</p>
        <p id="p_09442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09442"> $ programl2_4 filel file2</p>
        <p id="p_09443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09443"> This is a line from the first file.</p>
        <p id="p_09444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09444"> Here is the last line of the first file.</p>
        <p id="p_09445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09445"> — end of current file —</p>
        <p id="p_09446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09446"> This is a line from the second and last file. Here is the last line of the last file.</p>
        <p id="p_09447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09447"> — end of current file —</p>
        <p id="p_09448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09448"> $</p>
        <p id="p_09449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09449"> The &lt;&gt; operator in line 3 tells the program to read the next line of input from the input files supplied on the command line. Line 4 then prints the line.</p>
        <p id="p_09450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09450"> Line 5 calls  eof  without parentheses. This is the form of  eof  that you are familiar with. It returns true if the current input file has been completely read.</p>
        <p id="p_09451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09451"> Analysis</p>
        <p id="p_09452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09452"> Warning</p>
        <p id="p_09453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09453"> When you test for end-of-file, use either  eof  or  eof ()  but not both</p>
        <p id="p_09454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09454"> Compare the program in Listing 12.4 with Listing 12.5, which uses  eof ()  instead of  eof.</p>
        <p id="p_09455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09455"> Ty?e</p>
        <p id="p_09456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09456"> Listing 12.5. A program that uses eof () and &lt;&gt; together.</p>
        <p id="p_09457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09457"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09458"> 2 :</p>
        <p id="p_09459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09459"> 3: while ($line = &lt;&gt;) {</p>
        <p id="p_09460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09460"> 4: print ($line) ;</p>
        <p id="p_09461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09461"> 5: if  (eof())  {</p>
        <p id="p_09462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09462"> 6:  print ("— end of output  —\n");</p>
        <p id="p_09463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09463"> 7: }</p>
        <p id="p_09464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09464"> 8 : }</p>
        <p id="p_09465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09465"> Output</p>
        <p id="p_09466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09466"> $ programl2_5 filel file2</p>
        <p id="p_09467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09467"> This is a line from the first file.</p>
        <p id="p_09468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09468"> Here is the last line of the first file.</p>
        <p id="p_09469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09469"> This is a line from the second and last file.</p>
        <p id="p_09470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09470"> Here is the last line of the last file.</p>
        <p id="p_09471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09471"> — end of output —</p>
        <p id="p_09472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09472"> $</p>
        <p id="p_09473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09473"> Analysis</p>
        <p id="p_09474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09474"> Line 5 of this program calls  eof  with parentheses. Calls to  eof  with parentheses only return true when all of the files have been read. If the program is at the end of the first input file,  eof  () returns false because there is still input to be read.</p>
        <p id="p_09475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09475"> NOTE</p>
        <p id="p_09476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09476"> If you like, you can use  eof  with a particular file. For example:</p>
        <p id="p_09477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09477"> if (eof(MYFILE)) {</p>
        <p id="p_09478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09478"> # do end-of-file stuff }</p>
        <p id="p_09479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09479"> Here, the conditional expression returns true if all of  MYFILE  has been read.</p>
        <p id="p_09480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09480"> Also, note that the distinction between  eof  and  eof  () is only meaningful when you are using the &lt;&gt; operator. If you are just reading from a single file, it doesn't matter whether you supply parentheses or not. For example:</p>
        <p id="p_09481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09481"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_09482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09482"> # stuff goes here</p>
        <p id="p_09483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09483"> if (eof) { # you can also use eof () here</p>
        <p id="p_09484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09484"> # more stuff here }</p>
        <p id="p_09485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09485"> }</p>
        <p id="p_09486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09486"> Indirect File Variables</p>
        <p id="p_09487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09487"> When you call any of the functions described so far in today's lesson, you can indicate which file to use by specifying a file variable. However, these functions also enable you to supply a scalar variable in place of a file variable; when you do, the Perl interpreter treats the value stored in the scalar variable as the name of the file variable. For example, consider the following:</p>
        <p id="p_09488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09488"> $filename = "MYFILENAME";</p>
        <p id="p_09489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09489"> open ($filename.</p>
        <p id="p_09490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09490"> " &gt; f i 1 e 1" ) ;</p>
        <p id="p_09491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09491"> This call to  open  takes the value stored in  $f ilename-MYFILENAME-and  uses it as the file-variable name. This means that the file variable  MYFILENAME  is now associated with the output file  f ilel.</p>
        <p id="p_09492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09492"> Listing 12.6 is an example of a program that stores a file-variable name in a scalar variable and passes the library variable to Perl input and output functions.</p>
        <p id="p_09493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09493"> Type</p>
        <p id="p_09494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09494"> Listing 12.6. A program that uses a scalar variable to store a file variable name.</p>
        <p id="p_09495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09495"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09496"> 2 :</p>
        <p id="p_09497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09497"> 3: &amp;open_file("INFILE", "", "filel");</p>
        <p id="p_09498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09498"> 4: &amp;open_file("OUTFILE", "&gt;", "file2");</p>
        <p id="p_09499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09499"> 5: while ($line = &amp;read_from_file("INFILE") ) {</p>
        <p id="p_09500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09500"> 6: &amp;print_to_file("OUTFILE", $line) ;</p>
        <p id="p_09501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09501"> 7: }</p>
        <p id="p_09502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09502"> 8 :</p>
        <p id="p_09503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09503"> 9: sub open_file {</p>
        <p id="p_09504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09504"> 10: local ($filevar, $filemode, $filename) = @_;</p>
        <p id="p_09505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09505"> 11 :</p>
        <p id="p_09506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09506"> 12: open ($filevar, $filemode . $filename) ||</p>
        <p id="p_09507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09507"> 13: die ("Can't open $filename");</p>
        <p id="p_09508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09508"> 14 : }</p>
        <p id="p_09509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09509"> 15: sub read_from_file {</p>
        <p id="p_09510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09510"> 16: local ($filevar) =</p>
        <p id="p_09511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09511"> 17 :</p>
        <p id="p_09512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09512"> 18 :</p>
        <p id="p_09513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09513"> &lt;$filevar&gt;;</p>
        <p id="p_09514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09514"> 19: }</p>
        <p id="p_09515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09515"> 20: sub print_to_file {</p>
        <p id="p_09516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09516"> 21: local ($filevar, $line) = @_;</p>
        <p id="p_09517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09517"> 22  :</p>
        <p id="p_09518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09518"> 23: print $filevar ($line);</p>
        <p id="p_09519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09519"> 24 : }</p>
        <p id="p_09520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09520"> Output</p>
        <p id="p_09521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09521"> This program produces no output.</p>
        <p id="p_09522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09522"> Analysis</p>
        <p id="p_09523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09523"> This program is just a fancy way of copying the contents  offileltofile2.  Line 3 opens the input file,  f  ilel,  for reading by calling the subroutine  open_f  ile. This subroutine is passed the name of the file variable to use, which is  INFILE.</p>
        <p id="p_09524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09524"> Line 4 uses the same subroutine,  open_f ile,  to open the output file,  f ile2,  for writing. The file variable  OUTFILE  is used in this open operation.</p>
        <p id="p_09525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09525"> Line 5 calls  read_f rom_f ile  to read a line of input and passes it the file variable name  INFILE.</p>
        <p id="p_09526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09526"> Line 18 substitutes the value of  $f ilevar, INFILE,  into  &lt;$f ilevar&gt;,  yielding the result &lt;INFILE&gt;;  then, it reads a line from this input file. Because this line-reading operation is the last expression evaluated in the subroutine, the line read is returned by the subroutine and assigned to  $line.</p>
        <p id="p_09527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09527"> Line 6 then passes  OUTFILE  and the input line just read to the subroutine  print_to_f ile.</p>
        <p id="p_09528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09528"> NOTE</p>
        <p id="p_09529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09529"> All of the functions you've seen so far in this chapter-open, close, print, printf, write, select, and eof-enable  you to use a scalar variable in place of a file variable.</p>
        <p id="p_09530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09530"> The functions  open, close, write, select,  and  eof  also enable you to use an expression in place of a file variable. The value of the expression must be a character string that can be used as a file variable</p>
        <p id="p_09531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09531"> Skipping and Rereading Data</p>
        <p id="p_09532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09532"> In the programs you've seen so far,i nput files have always been read in order, starting with the first line of input and continuing on to the end. Perl provides two special functions,  seek  and  tell,  which enable you to skip forward or backward in a file so that you can skip or re-read data.</p>
        <p id="p_09533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09533"> The seek Function</p>
        <p id="p_09534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09534"> The  seek  function moves backward or forward in a file.</p>
        <p id="p_09535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09535"> The syntax for the  seek  function is</p>
        <p id="p_09536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09536"> seek (filevar, distance, relative_to);</p>
        <p id="p_09537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09537"> As you can see,  seek  requires three arguments:</p>
        <p id="p_09538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09538"> • filevar,  which is the file variable representing the file in which to skip</p>
        <p id="p_09539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09539"> • distance,  which is an integer representing the number of bytes (characters) to skip</p>
        <p id="p_09540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09540"> • relative_to,  which is either 0, 1, or 2</p>
        <p id="p_09541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09541"> If  relative_to  is 0, the number of bytes to skip is relative to the beginning of the file. If relative_to  is 1, the skip is relative to the current position in the file (the current position is the location of the next line to be read). If  relative_to  is 2, the skip is relative to the end of the file.</p>
        <p id="p_09542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09542"> For example, to skip back to the beginning of the file  MYFILE,  use the following:</p>
        <p id="p_09543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09543"> seek(MYFILE, 0, 0) ;</p>
        <p id="p_09544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09544"> The following statement skips forward 80 bytes:</p>
        <p id="p_09545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09545"> seek(MYFILE, 80, 1);</p>
        <p id="p_09546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09546"> The following statement skips backward 80 bytes:</p>
        <p id="p_09547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09547"> seek(MYFILE, -80, 1);</p>
        <p id="p_09548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09548"> And the following statement skips to the end of the file (which is useful when the file has been opened for reading and writing):</p>
        <p id="p_09549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09549"> seek(MYFILE, 0, 2);</p>
        <p id="p_09550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09550"> The  seek  function returns true (nonzero) if the skip was successful, and 0 if it failed. It is often used in conjunction with the  tell  function, described in the next section.</p>
        <p id="p_09551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09551"> The tell Function</p>
        <p id="p_09552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09552"> The  t ell  function returns the distance, in bytes, between the beginning of the file and the current position of the file (the location of the next line to be read).</p>
        <p id="p_09553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09553"> The syntax for the  tell  function is</p>
        <p id="p_09554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09554"> tell (filevar);</p>
        <p id="p_09555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09555"> f ilevar,  which is required, represents the file whose current position is needed.</p>
        <p id="p_09556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09556"> For example, the following statement retrieves the current position of the file  MYFILE:</p>
        <p id="p_09557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09557"> $offset  = tell (MYFILE);</p>
        <p id="p_09558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09558"> NOTE</p>
        <p id="p_09559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09559"> tell  and  seek  accept an expression in place of a file variable, provided the value of the expression is the name of a file variable</p>
        <p id="p_09560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09560"> You can use  tell  and  seek  to skip to a particular position in a file. For example, Listing 12.7 uses these functions to print pairs of lines twice each. (This is, of course, not the fastest way to do this.)</p>
        <p id="p_09561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09561"> Listing 12.7. A program that demonstrates seek and tell.</p>
        <p id="p_09562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09562"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09563"> 2 :</p>
        <p id="p_09564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09564"> "test");</p>
        <p id="p_09565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09565"> 3: @array = ("This", "is", "a",</p>
        <p id="p_09566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09566"> 4: open (TEMPFILE, "&gt;filel");</p>
        <p id="p_09567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09567"> 5: foreach $element (@array) {</p>
        <p id="p_09568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09568"> Output</p>
        <p id="p_09569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09569"> $ program!2_7</p>
        <p id="p_09570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09570"> This</p>
        <p id="p_09571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09571"> is</p>
        <p id="p_09572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09572"> This</p>
        <p id="p_09573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09573"> is</p>
        <p id="p_09574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09574"> a</p>
        <p id="p_09575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09575"> test</p>
        <p id="p_09576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09576"> a</p>
        <p id="p_09577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09577"> test</p>
        <p id="p_09578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09578"> $</p>
        <p id="p_09579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09579"> Lines 3-8 of this program create a temporary file named  f ilel  consisting of four lines: This, is, a,  and  test.  Line 9 opens this temporary file for reading.</p>
        <p id="p_09580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09580"> Lines 10-22 loop through the test file. Line 11 calls  tell  to obtain the current position of the file before reading the pair of lines. Lines 12-16 read the lines and print them (first testing whether the end of the file has been reached).</p>
        <p id="p_09581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09581"> Line 17 then calls  seek,  which positions the file at the point returned by  tell  in line 11. This means that the pair of lines read by lines 12 and 15 are read again by lines 18 and 20. Therefore, lines 19 and 21 print a second copy of the input lines.</p>
        <p id="p_09582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09582"> Analysis</p>
        <p id="p_09583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09583"> NOTE</p>
        <p id="p_09584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09584"> You cannot use  seek  and  tell  if the file variable actually refers to a pipe. For example, if you open a pipe using the statement</p>
        <p id="p_09585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09585"> open (MYPIPE, "cat  file*|");</p>
        <p id="p_09586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09586"> then the following statement makes no sense:</p>
        <p id="p_09587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09587"> $illegal = tell (MYPIPE)</p>
        <p id="p_09588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09588"> System Read and Write Functions</p>
        <p id="p_09589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09589"> In Perl, the easiest way to read input from a file is to use the  &lt;f ilevar&gt;  operator, where  f ilevar  is the file variable representing the file to read. Perl also provides two other functions that read from an input file:</p>
        <p id="p_09590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09590"> • read,  which is equivalent to the UNIX  f read  function</p>
        <p id="p_09591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09591"> • sysread,  which is equivalent to the  read  function</p>
        <p id="p_09592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09592"> Perl also enables you to write output using the built-in function  syswrite,  which calls the UNIX write  function.</p>
        <p id="p_09593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09593"> These functions are described in the following sections.</p>
        <p id="p_09594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09594"> The read Function</p>
        <p id="p_09595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09595"> The  read  function is designed to be equivalent to the UNIX function  tread.  It enables you to read an arbitrary number of characters (bytes) into a scalar variable.</p>
        <p id="p_09596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09596"> The syntax for the  read  function is</p>
        <p id="p_09597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09597"> read (filevar, result, length, skipval);</p>
        <p id="p_09598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09598"> Here,  filevar  is the file variable representing the file to read,  result  is the scalar variable (or array variable element) into which the bytes are to be stored, and  length  is the number of bytes to read.</p>
        <p id="p_09599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09599"> skipval  is an optional argument which specifies the number of bytes to skip before reading.</p>
        <p id="p_09600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09600"> For example:</p>
        <p id="p_09601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09601"> read (MYFILE, $scalar,  80);</p>
        <p id="p_09602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09602"> This call to  read  tries to read 80 bytes from the file represented by the file variable  MYFILE,  storing the resulting character string in $  scalar.  It returns the number of bytes actually read; if  MYFILE  is at end-of-file, it returns 0  (read  returns the null string if an error occurs).</p>
        <p id="p_09603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09603"> You can use  read  to append to an existing scalar variable by specifying a fourth argument, which indicates the number of bytes to skip in the scalar variable.</p>
        <p id="p_09604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09604"> read (MYFILE, $scalar, 40,  80);</p>
        <p id="p_09605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09605"> This call to  read  reads another 40 bytes from  MYFILE.  When copying these bytes into $  scalar, read  first skips the first 80 bytes already stored there.</p>
        <p id="p_09606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09606"> The sysread and syswrite Functions</p>
        <p id="p_09607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09607"> If you want to read data as quickly as possible, you can call  sysread  instead of  read.</p>
        <p id="p_09608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09608"> The syntax for the  sysread  function is</p>
        <p id="p_09609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09609"> sysread (filevar, result, length, skipval);</p>
        <p id="p_09610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09610"> These arguments are the same as for  read.</p>
        <p id="p_09611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09611"> For example:</p>
        <p id="p_09612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09612"> sysread (MYFILE, $scalar,  80);</p>
        <p id="p_09613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09613"> sysread (MYFILE, $scalar, 40, 80) ;</p>
        <p id="p_09614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09614"> sysread  is equivalent to the UNIX function  read.  The arguments to  sysread  are the same as those for the Perl  read  function.</p>
        <p id="p_09615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09615"> To write as quickly as possible, call the  syswrite  function, which is equivalent to the UNIX function</p>
        <p id="p_09616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09616"> write.</p>
        <p id="p_09617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09617"> The syntax of the  syswrite  function is</p>
        <p id="p_09618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09618"> syswrite (filevar, data, length, skipval);</p>
        <p id="p_09619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09619"> Here,  filevar  is the file to write to,  data  is the place where the data is located,  length  is the number of bytes to write, and  skipval  is the number of bytes to skip before writing.</p>
        <p id="p_09620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09620"> For instance, the following call writes the first 80 bytes  of$scalarto  the file specified by  MYFILE:</p>
        <p id="p_09621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09621"> syswrite (MYFILE, $scalar,  80);</p>
        <p id="p_09622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09622"> Similarly, the following statement skips the first 80 bytes stored in $  scalar,  and then writes the next 40 bytes to the file specified by  MYFILE:</p>
        <p id="p_09623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09623"> syswrite (MYFILE, $scalar, 40,  80);</p>
        <p id="p_09624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09624"> Warning</p>
        <p id="p_09625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09625"> Don't use  sysread  and  syswrite  unless you know what you are doing. For more information on these functions, refer to the UNIX system manual pages for the  read  and  write  functions</p>
        <p id="p_09626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09626"> Reading Characters Using getc</p>
        <p id="p_09627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09627"> Perl provides one other built-in function,  getc,  which reads a single character of input from a file. The syntax for calls to the  getc  function is</p>
        <p id="p_09628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09628"> char = getc (infile);</p>
        <p id="p_09629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09629"> inf ile  is the file from which to read, and  char  is the character returned. For example:</p>
        <p id="p_09630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09630"> $singlechar = getc(INFILE);</p>
        <p id="p_09631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09631"> This statement reads a character from the file represented by  INFILE  and stores it (as a character string) in the scalar variable  $singlechar.</p>
        <p id="p_09632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09632"> The  getc  is useful for "hot key" applications. These applications accept and process input one character at a time rather than one line at a time. Listing 12.8 is an example of such a program. It reads one character at a time and checks whether the character is alphanumeric. If it is, it writes out the next higher letter or number. For example, when you enter  a,  the program prints out  b,  and so on. In this example, the alphabetic letters  a  through  z  and the digits  0  through  9  are typed in.</p>
        <p id="p_09633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09633"> Listing 12.8. A program that demonstrates the use of getc.</p>
        <p id="p_09634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09634"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09635"> 8: print ($char);</p>
        <p id="p_09636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09636"> 9 : }</p>
        <p id="p_09637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09637"> 10: &amp;end_hot_keys;</p>
        <p id="p_09638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09638"> 11: print ( "\n");</p>
        <p id="p_09639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09639"> 12  :</p>
        <p id="p_09640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09640"> 13: sub</p>
        <p id="p_09641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09641"> 14 :</p>
        <p id="p_09642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09642"> 15 :</p>
        <p id="p_09643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09643"> 16: }</p>
        <p id="p_09644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09644"> start_hot_keys {</p>
        <p id="p_09645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09645"> system ("stty cbreak"); system ("stty -echo");</p>
        <p id="p_09646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09646"> Output</p>
        <p id="p_09647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09647"> $ programl2_8</p>
        <p id="p_09648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09648"> bcdefghijklmnopqrstuvwxyzal2345 678 90</p>
        <p id="p_09649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09649"> $</p>
        <p id="p_09650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09650"> The subroutine  start_hot_keys  modifies the runtime environment to support hot-key input. To do this, it uses two calls to the built-in function  system,  which simply takes its argument and executes it. The command  stty cbreak  tells the system to process input one character at a time, and the command  stty -echo  tells the system not to display characters typed at the keyboard.</p>
        <p id="p_09651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09651"> Analysis</p>
        <p id="p_09652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09652"> NOTE</p>
        <p id="p_09653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09653"> Some machines might not support hot keys or might use different commands to establish the hot-key environment. If you are on a machine that uses different commands to establish the environment, you still can run this program; just change the  stty  commands to whatever works on your machine</p>
        <p id="p_09654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09654"> The loop in lines 4-9 reads and writes one character per loop iteration. Line 5 starts off by reading a character from the standard input file using  getc.</p>
        <p id="p_09655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09655"> Line 6 tests whether the character read is a backslash. If it is, the loop terminates. If the character is not a backslash, the program continues with line 7. This line translates all alphanumeric characters to the next-highest letter or number; for example, it translates  g  to  h, E  to  F,  and  7  to  8.  The characters  z, Z,  and  9 are translated to  a, A,  and  0,  respectively.</p>
        <p id="p_09656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09656"> Line 8 prints out the translated character. Because the characters you type at the keyboard are not displayed, the program makes it look like your keyboard is malfunctioning. (It's quite disorienting!)</p>
        <p id="p_09657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09657"> The subroutine  end_hot_keys  restores the normal working environment by undoing the system calls that are performed by  start_hot_keys.</p>
        <p id="p_09658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09658"> Warning</p>
        <p id="p_09659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09659"> If you are using hot keys, when you clean up make sure you call stty-cbreak  before  calling  stty echo.  If you call  stty echo  first, your terminal might wind up not printing newline characters properly</p>
        <p id="p_09660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09660"> Reading a Binary File Using binmode</p>
        <p id="p_09661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09661"> If your machine distinguishes between text files and binary files (files that contain unprintable characters), your Perl program can tell the system that a particular file is a binary file. To do this, call the built-in function  binmode.</p>
        <p id="p_09662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09662"> The syntax for calling the  binmode  function is</p>
        <p id="p_09663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09663"> binmode  (filevar);</p>
        <p id="p_09664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09664"> f ilevar  is a file variable.</p>
        <p id="p_09665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09665"> binmode  expects a file variable (or an expression whose value is the name of a file variable). It must be called after the file is opened, but before the file is read.</p>
        <p id="p_09666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09666"> The following is an example of a call to  binmode:</p>
        <p id="p_09667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09667"> binmode (MYFILE);</p>
        <p id="p_09668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09668"> NOTE</p>
        <p id="p_09669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09669"> Normally, you won't need to use this function unless you are running in a DOS-like environment</p>
        <p id="p_09670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09670"> Directory-Manipulation Functions</p>
        <p id="p_09671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09671"> The input and output functions that you have seen earlier read and write data to files. Perl also provides a group of functions that enable you to manipulate UNIX directories. Functions exist that enable you to create, read, open, close, delete, and skip around in directories. The following sections describe these functions.</p>
        <p id="p_09672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09672"> The mkdir Function</p>
        <p id="p_09673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09673"> To create a new directory, call the function  mkdir. The syntax for the  mkdir  function is</p>
        <p id="p_09674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09674"> mkdir (dirname, permissions);</p>
        <p id="p_09675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09675"> mkdir  requires two arguments:</p>
        <p id="p_09676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09676"> • dirname,  which is the name of the directory to be created (which can be a character string or an expression whose value is a directory name)</p>
        <p id="p_09677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09677"> • permissions,  which is an octal (base-8) number specifying the access permissions for the new directory</p>
        <p id="p_09678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09678"> For example, to create a directory named  /u/ jqpublic/newdir,  you can use the following statement:</p>
        <p id="p_09679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09679"> mkdir ("/u/jqpublic/newdir",  0777);</p>
        <p id="p_09680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09680"> To create a subdirectory of the current working directory, just specify the new directory name, as follows:</p>
        <p id="p_09681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09681"> mkdir ("newdir",  0777);</p>
        <p id="p_09682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09682"> If the current working directory is  /u/ janedoe/mydir,  this creates a subdirectory named  /u</p>
        <p id="p_09683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09683"> /janedoe/mydir/newdir.</p>
        <p id="p_09684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09684"> The permissions value of  07 7 7  in both these examples grants read, write, and execute permissions to everybody. Table 12.1 lists each possible access permission and the octal number associated with it.</p>
        <p id="p_09685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09685"> Table 12.1. Access permissions for the mkdir function.</p>
        <p id="p_09686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09686"> You can combine access permissions by adding (or doing a logical  OR  operation on) the appropriate octal values in the table. For example, to grant read, write, and execute permission to the owner but only read permission to everybody else, specify  07 4 4  as the permission value.</p>
        <p id="p_09687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09687"> NOTE</p>
        <p id="p_09688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09688"> All of the permission values shown here are in octal notation, because a leading zero is specified. If you like, you can use decimal or hexadecimal here, but it won't be as easy to read.</p>
        <p id="p_09689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09689"> Also note that the permission value set here is affected by the current value of  umask.  See the description of the  umask  function later today for more information</p>
        <p id="p_09690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09690"> mkdir returns true (nonzero) if the directory is successfully created. It returns false (0) if the directory not.</p>
        <p id="p_09691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09691"> The chdir Function</p>
        <p id="p_09692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09692"> To set a directory to be the current working directory, use the function  chdir.</p>
        <p id="p_09693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09693"> The syntax for the  chdir  function is</p>
        <p id="p_09694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09694"> chdir (dirname);</p>
        <p id="p_09695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09695"> dir name  is the name of the new current working directory.</p>
        <p id="p_09696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09696"> chdir  returns true if the current directory is set properly, false if an error occurs.</p>
        <p id="p_09697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09697"> For example, to set the current working directory to  /u/ jqpublic/newdir,  use the following statement:</p>
        <p id="p_09698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09698"> chdir ( "/u/jqpublic/newdir");</p>
        <p id="p_09699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09699"> NOTE</p>
        <p id="p_09700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09700"> As with  mkdir,  the directory name passed to  chdir  can be either a character string or an expression whose value is a directory name. For example, the following sets the current directory to be</p>
        <p id="p_09701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09701"> /u/jqpublic/newdir:</p>
        <p id="p_09702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09702"> $dir = "/u/jqpublic/"; chdir ($dir . "newdir")</p>
        <p id="p_09703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09703"> The opendir Function</p>
        <p id="p_09704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09704"> You can have your program examine a list of the files contained in a directory. To do this, the first step is to call the built-in function  opendir.</p>
        <p id="p_09705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09705"> The syntax for the  opendir  function is</p>
        <p id="p_09706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09706"> opendir (dirvar, dirname);</p>
        <p id="p_09707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09707"> dirvar  is the name the program is to use to represent the directory, also known as a  directory variable , and  dirname  is the name of the directory to open (which can be a character string or the value of an expression).</p>
        <p id="p_09708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09708"> opendir  returns true if the open operation is successful, and it returns false otherwise.</p>
        <p id="p_09709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09709"> For example, to open the directory named  /u/ janedoe/mydir,  you can use the following statement:</p>
        <p id="p_09710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09710"> opendir (DIR, "/u/janedoe/mydir" ) ;</p>
        <p id="p_09711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09711"> This associates the directory variable  DIR  with the opened directory.</p>
        <p id="p_09712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09712"> NOTE</p>
        <p id="p_09713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09713"> If you like, you can use the same name as both a directory variable and a file variable.</p>
        <p id="p_09714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09714"> opendir (MYNAME, "/u/jqpublic/dir"); open (MYNAME, "/u/jqpublic/dir/file");</p>
        <p id="p_09715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09715"> The Perl interpreter always can tell from context whether a name is being used as a directory variable or as a file variable. (However, there is no real reason to do so. Your programs will be easier to read if you use different names to represent files and directories.</p>
        <p id="p_09716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09716"> The closedir Function</p>
        <p id="p_09717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09717"> To close an opened directory, call the  closedir  function.</p>
        <p id="p_09718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09718"> The syntax for the  closedir  function is</p>
        <p id="p_09719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09719"> closedir (mydir);</p>
        <p id="p_09720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09720"> closedir  expects one argument: the directory variable associated with the directory to be closed.</p>
        <p id="p_09721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09721"> The readdir Function</p>
        <p id="p_09722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09722"> After  opendir  has opened a directory, you can access the name of each file or subdirectory stored in the directory by calling the function  readdir.</p>
        <p id="p_09723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09723"> The syntax for the  readdir  function is</p>
        <p id="p_09724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09724"> readdir (mydir);</p>
        <p id="p_09725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09725"> Like  closedir, readdir  is passed the directory variable that is associated with the open directory.</p>
        <p id="p_09726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09726"> If the value returned from  readdir  is assigned to a scalar variable,  readdir  returns the name of the first file or subdirectory stored in the directory. For example:</p>
        <p id="p_09727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09727"> $filename = readdir(MYDIR);</p>
        <p id="p_09728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09728"> The first name is returned also if the return value from  readdir  is assigned to an element of an array variable. For example:</p>
        <p id="p_09729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09729"> $filearray[3] = readdir(MYDIR);</p>
        <p id="p_09730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09730"> $filearray{"foo"} = readdir(MYDIR);</p>
        <p id="p_09731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09731"> If  readdir  is called again, it returns the next name in the directory; subsequent calls return other names, continuing until the directory is exhausted. Listing 12.9 uses  readdir  to list the files and subdirectories in a directory.</p>
        <p id="p_09732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09732"> Type</p>
        <p id="p_09733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09733"> Listing 12.9. A program that lists the files and subdirectories in a directory.</p>
        <p id="p_09734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09734"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09735"> 3: opendir(HOMEDIR, "/u/jqpublic") ||</p>
        <p id="p_09736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09736"> 4: die ("Unable to open directory");</p>
        <p id="p_09737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09737"> 5: while ($filename = readdir(HOMEDIR)) {</p>
        <p id="p_09738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09738"> 6: print ("$filename\n");</p>
        <p id="p_09739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09739"> 7: }</p>
        <p id="p_09740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09740"> 8: closedir(HOMEDIR);</p>
        <p id="p_09741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09741"> Output</p>
        <p id="p_09742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09742"> $ program!2_9</p>
        <p id="p_09743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09743"> . cshrc .Xresources .xsession test bin</p>
        <p id="p_09744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09744"> letter</p>
        <p id="p_09745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09745"> filel</p>
        <p id="p_09746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09746"> $</p>
        <p id="p_09747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09747"> Analysis</p>
        <p id="p_09748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09748"> Line 3 opens the directory  /u/ jqpublic,  which is the home directory for user jqpublic.  The  opendir  function associates the directory variable  HOMEDIR  with  /u/ jqpublic.</p>
        <p id="p_09749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09749"> Lines 5-7 read the name of each file in the directory in turn. Line 6 prints each filename as it is read in.</p>
        <p id="p_09750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09750"> Note that, on a UNIX system, the list of names includes two special files:</p>
        <p id="p_09751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09751"> • The name . (a single period), which represents the current directory</p>
        <p id="p_09752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09752"> • The name . . (two periods), which represents the parent directory</p>
        <p id="p_09753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09753"> As you can see,  readdir  reads the names in the order in which they appear in the directory. Listing 12.10 shows how you can display the names in alphabetical order.</p>
        <p id="p_09754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09754"> Type</p>
        <p id="p_09755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09755"> Listing 12.10. A program that lists the files and subdirectories in a directory in alphabetical order.</p>
        <p id="p_09756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09756"> 1: #!/usr/local/bin/perl</p>
        <p id="p_09757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09757"> 2 :</p>
        <p id="p_09758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09758"> 3: opendir(HOMEDIR, "/u/jqpublic") ||</p>
        <p id="p_09759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09759"> 4: die ("Unable to open directory");</p>
        <p id="p_09760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09760"> 5: @files = readdir(HOMEDIR) ;</p>
        <p id="p_09761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09761"> 6: closedir(HOMEDIR) ;</p>
        <p id="p_09762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09762"> 7: foreach $file (sort @files) {</p>
        <p id="p_09763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09763"> 8: print ("$file\n");</p>
        <p id="p_09764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09764"> 9 : }</p>
        <p id="p_09765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09765"> Output</p>
        <p id="p_09766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09766"> $ program!2_10</p>
        <p id="p_09767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09767"> .Xresources</p>
        <p id="p_09768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09768"> . cshrc .xsession bin filel letter test $</p>
        <p id="p_09769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09769"> Analysis</p>
        <p id="p_09770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09770"> The  readdir  function behaves differently when its return value is assigned to an array; in this case, the entire list of files and subdirectories in the directory is assigned to the array variable @f iles  by line 5.</p>
        <p id="p_09771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09771"> After the entire list is stored,  sort  can be called to sort the list into alphabetical order. The  f oreach loop in lines 7-9 then prints the sorted list one name at a time.</p>
        <p id="p_09772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09772"> The telldir and seekdir Functions</p>
        <p id="p_09773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09773"> As you've seen, the library functions  tell  and  seek  enable you to skip backward and forward in a file. Similarly, the library functions  telldir  and  seekdir  enable you to skip backward and forward in a list of directories.</p>
        <p id="p_09774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09774"> To use  telldir,  pass it the directory variable defined by  opendir. telldir  returns the current directory location (where you are in the list of files).</p>
        <p id="p_09775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09775"> The syntax for the  telldir  function is</p>
        <p id="p_09776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09776"> location = telldir (mydir);</p>
        <p id="p_09777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09777"> Here,  mydir  is the directory variable corresponding to the directory whose file list you are examining, and  location  is assigned the current directory location.</p>
        <p id="p_09778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09778"> To skip to the directory location returned by  telldir,  call  seekdir.</p>
        <p id="p_09779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09779"> The syntax for the  seekdir  function is</p>
        <p id="p_09780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09780"> seekdir(mydir, location);</p>
        <p id="p_09781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09781"> This call to  seekdir  sets the current directory location to the location specified by  location.</p>
        <p id="p_09782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09782"> Warning</p>
        <p id="p_09783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09783"> seekdir  works only with directory locations returned by</p>
        <p id="p_09784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09784"> telldir</p>
        <p id="p_09785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09785"> The rewinddir Function</p>
        <p id="p_09786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09786"> Although being able to skip anywhere you like in a directory list is useful, the most common skipping operation in directory lists is  rewinding  the directory list, or starting over again. Because of this, Perl provides a special function,  rewinddir,  that handles the rewind operation.</p>
        <p id="p_09787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09787"> The syntax for the  rewinddir  function is</p>
        <p id="p_09788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09788"> rewinddir (mydir);</p>
        <p id="p_09789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09789"> rewinddir  sets the current directory location to the beginning of the list of files, which lets you read the entire list of files again. As with the other directory functions,  mydir  is the directory variable defined</p>
        <p id="p_09790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09790"> by  opendir.</p>
        <p id="p_09791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09791"> The rmdir Function</p>
        <p id="p_09792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09792"> The final directory function supplied by Perl is  rmdir,  which deletes an empty directory.</p>
        <p id="p_09793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09793"> The syntax for calling the  rmdir  function is</p>
        <p id="p_09794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09794"> rmdir (dirname);</p>
        <p id="p_09795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09795"> rmdir  returns true (nonzero) if the directory  dirname  is deleted successfully, and false if the directory is not empty or cannot be deleted.</p>
        <p id="p_09796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09796"> File-Attribute Functions</p>
        <p id="p_09797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09797"> Perl provides several library functions that modify the attributes or behavior of files. These functions can</p>
        <p id="p_09798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09798"> be divided into the following groups:</p>
        <p id="p_09799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09799"> • Functions that relocate (rename or delete) files</p>
        <p id="p_09800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09800"> • Functions that establish links or symbolic links</p>
        <p id="p_09801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09801"> • Functions that modify file permissions</p>
        <p id="p_09802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09802"> • Other file-attribute functions</p>
        <p id="p_09803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09803"> These groups of functions are described in the following sections.</p>
        <p id="p_09804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09804"> File-Relocation Functions</p>
        <p id="p_09805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09805"> Perl provides the following file-relocation functions:</p>
        <p id="p_09806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09806"> • rename,  which moves or renames a file</p>
        <p id="p_09807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09807"> • unlink, which deletes a file</p>
        <p id="p_09808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09808"> The rename Function</p>
        <p id="p_09809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09809"> The built-in function  rename  changes the name of a file.</p>
        <p id="p_09810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09810"> The syntax for the  rename  function is</p>
        <p id="p_09811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09811"> rename (oldname, newname);</p>
        <p id="p_09812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09812"> oldname  is the old filename, and  newname  is the new filename.</p>
        <p id="p_09813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09813"> The  rename  function returns true if the rename succeeds, and false if an error occurs.</p>
        <p id="p_09814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09814"> For example, to change a file named  namel  to  name2,  use the following:</p>
        <p id="p_09815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09815"> rename ("namel", "name2");</p>
        <p id="p_09816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09816"> You can use the value stored in a scalar variable as an argument to  rename,  or any variable or expression whose value is a character string, as follows:</p>
        <p id="p_09817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09817"> rename ($oldname, &amp;get_new_name);</p>
        <p id="p_09818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09818"> You can also use  rename  to move a file from one directory to another (provided both directories are in the same file system). For example:</p>
        <p id="p_09819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09819"> rename ("/u/jqpublic/namel" ,</p>
        <p id="p_09820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09820"> "/u/janedoe/name2" ) ;</p>
        <p id="p_09821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09821"> |NOTE _</p>
        <p id="p_09822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09822"> When  rename  moves a file, as in</p>
        <p id="p_09823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09823"> rename ("namel", "name2");</p>
        <p id="p_09824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09824"> it does not check whether a file named  name2  already exists. Any existing  name2  is destroyed by the  rename  operation.</p>
        <p id="p_09825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09825"> To get around this problem, use the  -e  file-test operator, which checks whether a named file exists, as follows:</p>
        <p id="p_09826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09826"> -e "name2" || rename (namel, name2);</p>
        <p id="p_09827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09827"> Here, the | | operator ensures that  rename  is called only when no file named  name2  already exists</p>
        <p id="p_09828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09828"> The unlink Function</p>
        <p id="p_09829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09829"> To delete a file, use the  unlink  function.</p>
        <p id="p_09830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09830"> The syntax for the  unlink  function is</p>
        <p id="p_09831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09831"> num = unlink (filelist);</p>
        <p id="p_09832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09832"> This function takes a list as its argument and deletes all the files named in that list, unlink  returns the number of files actually deleted.</p>
        <p id="p_09833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09833"> The following is an example of a call to  unlink:</p>
        <p id="p_09834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09834"> @deletelist = ("filel", "file2"); unlink (Qdeletelist);</p>
        <p id="p_09835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09835"> The function is called  unlink,  instead of  delete,  because what it is actually doing is removing a</p>
        <p id="p_09836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09836"> reference, or  link,  to the particular file. See the following section for more details on links in Perl.</p>
        <p id="p_09837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09837"> Link and Symbolic Link Functions</p>
        <p id="p_09838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09838"> In the UNIX environment, files can be "contained 1 ' in more than one directory at a time. Each directory contains a reference, or  link,  to the file.</p>
        <p id="p_09839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09839"> The following sections describe how to create and access links.</p>
        <p id="p_09840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09840"> |note</p>
        <p id="p_09841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09841"> If a file is referenced by multiple links,  unlink  removes only one of the links, and the file can still be referenced</p>
        <p id="p_09842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09842"> The link Function</p>
        <p id="p_09843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09843"> To create a link to an existing file, use the built-in function  link.</p>
        <p id="p_09844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09844"> The syntax for the  link  function is</p>
        <p id="p_09845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09845"> link (newlink, file);</p>
        <p id="p_09846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09846"> newlink  is the link being created, and  file  is the file being linked to. link  returns true if the link is created, and false if an error occurs.</p>
        <p id="p_09847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09847"> For example:</p>
        <p id="p_09848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09848"> link ("/u/jqpublic/file", "/u/janedoe/newfHe");</p>
        <p id="p_09849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09849"> After  link  has been called, the file  /u/jqpublic/file  also can be thought of as the file</p>
        <p id="p_09850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09850"> /u/janedoe/newf ile.  If  unlink  is called using  /u/jqpublic/file,  as in</p>
        <p id="p_09851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09851"> unlink ("/u/jqpublic/file");</p>
        <p id="p_09852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09852"> you can still reference the file by specifying the name  /u/janedoe/newfile.</p>
        <p id="p_09853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09853"> The symlink Function</p>
        <p id="p_09854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09854"> The link created by the  link  function is called a  hard link,  which means that it actually references the file itself. Many operating systems also support  symbolic links,  which are references to the filename, not to the file itself.</p>
        <p id="p_09855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09855"> To create a symbolic link, use the function  symlink. The syntax for the  symlink  function is</p>
        <p id="p_09856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09856"> symlink (newlink, file);</p>
        <p id="p_09857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09857"> newlink  is the link being created, and  file  is the file being linked to. symlink,  like  link  returns true if the link is created, and false if an error occurs.</p>
        <p id="p_09858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09858"> The following is an example of  symlink:</p>
        <p id="p_09859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09859"> symlink("/u/jqpublic/file", "/u/janedoe/newfile");</p>
        <p id="p_09860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09860"> Here,  /u/ janedoe/newf ile  is symbolically linked to  /u/ jqpublic/f ile.  Now, when the following statement is executed, the file is actually deleted:</p>
        <p id="p_09861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09861"> unlink ("/u/jqpublic/file");</p>
        <p id="p_09862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09862"> /u/ janedoe/newf ile  now references nothing at all. (In this case,  /u/ janedoe/newf ile  is an example of an  unresolved symbolic link.)  When  /u/ jqpublic/f ile  is created again, you will be able to access the new file using /  u/ janedoe/newf ile.</p>
        <p id="p_09863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09863"> The readlink Function</p>
        <p id="p_09864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09864"> If a filename, such as  /u/janedoe/newfile,is  actually a symbolic link to another filename, the function  readlink  returns the filename to which it is linked.</p>
        <p id="p_09865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09865"> The syntax for the  readlink  function is</p>
        <p id="p_09866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09866"> filename = readlink (linkname);</p>
        <p id="p_09867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09867"> linkname  is the symbolic link, and  filename  is the equivalent filename.</p>
        <p id="p_09868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09868"> readlink  returns an empty string if the filename is not a symbolic link. (In particular,  readlink  fails</p>
        <p id="p_09869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09869"> if the filename is actually a hard link.)</p>
        <p id="p_09870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09870"> For example:</p>
        <p id="p_09871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09871"> $linkname = readlink("/u/janedoe/newfile");</p>
        <p id="p_09872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09872"> # $linkname now contains "/u/jqpublic/file"</p>
        <p id="p_09873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09873"> Listing 12.11 is an example of a program that prints all the symbolic links in a particular directory.</p>
        <p id="p_09874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09874"> Type</p>
        <p id="p_09875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09875"> Listing 12.11. A program that prints symbolic links.</p>
        <p id="p_09876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09876"> Output</p>
        <p id="p_09877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09877"> $ programl2_ll</p>
        <p id="p_09878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09878"> newfile is linked to /u/jqpublic/file</p>
        <p id="p_09879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09879"> $</p>
        <p id="p_09880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09880"> Analysis</p>
        <p id="p_09881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09881"> This program uses  opendir  and  readdir  to examine each file in the directory in turn. Line 6 uses the -1 file-test operator to determine whether the filename is actually a symbolic link. If the filename is a symbolic link, the following expression becomes true, and the program executes the calls to print  in lines 7 and 8:</p>
        <p id="p_09882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09882"> -1 $dir . "/" . $name</p>
        <p id="p_09883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09883"> Line 8 calls  readlink,  passing it the directory name and the filename stored in  $name.  Because readlink  is called only if the expression in line 6 is true,  $name  is always a symbolic link.</p>
        <p id="p_09884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09884"> File-Permission Functions</p>
        <p id="p_09885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09885"> As you've seen, the built-in function  mkdir  requires you to specify the access permissions for the directory you are creating. These permissions indicate, for example, whether particular users are allowed to read files from the directory or write into the directory.</p>
        <p id="p_09886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09886"> In the UNIX environment, each individual file has its own set of access permissions. The set of possible permissions is the same as for directories. (Refer to Table 12.1 in the section titled "The  mkdir Function" earlier in today's lesson for a complete list of the possible functions.)</p>
        <p id="p_09887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09887"> In Perl, three functions are defined that deal with access permissions.</p>
        <p id="p_09888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09888"> • chmod,  which changes the access permissions for a file</p>
        <p id="p_09889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09889"> • chown,  which changes the owner of a file</p>
        <p id="p_09890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09890"> • umask, which sets the default access permissions for a file</p>
        <p id="p_09891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09891"> The chmod Function</p>
        <p id="p_09892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09892"> To change the access permissions for a list of files, call the  chmod  function.</p>
        <p id="p_09893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09893"> The syntax for the  chmod  function is</p>
        <p id="p_09894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09894"> chmod (permissions, filelist);</p>
        <p id="p_09895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09895"> permissions  is the set of access permissions you want to give, and is a standard UNIX file</p>
        <p id="p_09896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09896"> permissions mask. (For example, setting  permissions  to  07 7 7  gives read, write, and execute permission to everybody. See the section called "The  mkdir  Function" for a description of the set of permissions.)  f ilelist  is the list of files whose permissions you want to change.</p>
        <p id="p_09897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09897"> The  chmod  function returns the number of files whose permissions were successfully set.</p>
        <p id="p_09898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09898"> The following is an example of a call to  chmod:</p>
        <p id="p_09899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09899"> @ filelist = ("filel", "file2"); chmod (0777, @filelist);</p>
        <p id="p_09900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09900"> In this example, the files  filel  and  f ile2  are assigned global read, write, and execute permissions.</p>
        <p id="p_09901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09901"> |note ]</p>
        <p id="p_09902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09902"> You cannot change access permissions using  chmod  unless you have permission to do so. You need to have been granted write permission on a file before you can change its permissions</p>
        <p id="p_09903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09903"> The chown Function</p>
        <p id="p_09904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09904"> Normally, the owner of a file is the person who created it. To change the owner of a file, use the function</p>
        <p id="p_09905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09905"> chown.</p>
        <p id="p_09906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09906"> The syntax for the  chown  function is</p>
        <p id="p_09907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09907"> chown (userid, groupid, filelist);</p>
        <p id="p_09908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09908"> The  chown  function requires three arguments:</p>
        <p id="p_09909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09909"> • userid,  which is the (numerical) user ID of the new owner of the file</p>
        <p id="p_09910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09910"> • groupid,  which is the new numerical group ID to be assigned to the file (or -1 if the existing group ID is to be preserved)</p>
        <p id="p_09911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09911"> • filelist,  which is a list of files to change</p>
        <p id="p_09912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09912"> The  chown  function returns the number of files changed.</p>
        <p id="p_09913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09913"> The following is an example of a call to  chown:</p>
        <p id="p_09914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09914"> @ filelist = ("filel", "file2");</p>
        <p id="p_09915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09915"> chown (17,  -l f  @filelist);</p>
        <p id="p_09916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09916"> NOTE</p>
        <p id="p_09917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09917"> On most UNIX systems, you can retrieve a user ID or group ID from the  /etc/passwd  file. You can use the Perl function getpwnam  to retrieve information from this file. For more information on  getpwnam,  refer to  Day  15 ,  "System Functions."</p>
        <p id="p_09918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09918"> Also, the superuser (system administrator) is usually the only user allowed to change the owner of a file</p>
        <p id="p_09919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09919"> The umask Function</p>
        <p id="p_09920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09920"> As you've seen, you can change the access permissions for a file using  chmod.  To specify access permissions you cannot use when you create a file, use the  umask  function.</p>
        <p id="p_09921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09921"> The syntax for calls to  uma s k  is</p>
        <p id="p_09922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09922"> oldmaskval = umask (maskval);</p>
        <p id="p_09923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09923"> maskval  is the current  umask  value, and  umask  returns the previous (superseded)  umask  value in oldmaskval.  Each  umask  value is a file creation mask, and is used to set the default permissions for files and directories. (See the  umask  manual page for more details on file creation masks.)</p>
        <p id="p_09924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09924"> For example, the following statement disables group and world access permissions for the newly created file:</p>
        <p id="p_09925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09925"> $oldperms = umask(0022);</p>
        <p id="p_09926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09926"> NOTE</p>
        <p id="p_09927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09927"> You can determine the current  umask  value by passing no arguments to  umask,  as follows:</p>
        <p id="p_09928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09928"> $currperms = umask();</p>
        <p id="p_09929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09929"> This statement assigns the current  umask  value to  $currperms.</p>
        <p id="p_09930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09930"> Permission File-Test Operators</p>
        <p id="p_09931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09931"> Some file-test operators in Perl are designed to test for various permissions. Table 12.2 lists these file-test operators; in each case,  filename  is the name of the file being tested.</p>
        <p id="p_09932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09932"> Table 12.2. File-test operators that test for permissions.</p>
        <p id="p_09933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09933"> In this case, the real user ID is the user id specified at login, as opposed to the effective user ID, which is the user id under which you are currently running. (On some machines, a command such as /usr/local/etc/suid  enables you to change your effective user ID.)</p>
        <p id="p_09934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09934"> (See  Day 6   for more information on how to use file-test operators.)</p>
        <p id="p_09935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09935"> Miscellaneous Attribute Functions</p>
        <p id="p_09936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09936"> The following sections describe other Perl functions that manipulate files.</p>
        <p id="p_09937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09937"> The truncate Function</p>
        <p id="p_09938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09938"> The  truncate  function enables you to reduce the size of a specified file to a particular length.</p>
        <p id="p_09939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09939"> The syntax for the  truncate  function is</p>
        <p id="p_09940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09940"> truncate (filename, length);</p>
        <p id="p_09941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09941"> filename  is the name of the file to reduce, and  length  is the new length of the file.</p>
        <p id="p_09942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09942"> For example, the statement</p>
        <p id="p_09943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09943"> truncate ("/u/jqpublic/longfile",  5000);</p>
        <p id="p_09944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09944"> reduces the size of  /u/jqpublic/longf ile  to 5000 bytes in length. (If the file is already smaller than 5000 bytes,  truncate  does nothing.)</p>
        <p id="p_09945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09945"> The stat Function</p>
        <p id="p_09946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09946"> The  stat  function retrieves information about a particular file when given its name or a file variable representing its name.</p>
        <p id="p_09947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09947"> The syntax for the  stat  function is</p>
        <p id="p_09948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09948"> stat (file) ;</p>
        <p id="p_09949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09949"> Here,  file  is either a filename or a file variable.</p>
        <p id="p_09950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09950"> stat  returns a list containing the following elements, in this order:</p>
        <p id="p_09951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09951"> • The device on which the file resides</p>
        <p id="p_09952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09952"> • The internal reference number (inode number) for this file</p>
        <p id="p_09953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09953"> • The permissions for the file</p>
        <p id="p_09954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09954"> • The number of hard links to the file</p>
        <p id="p_09955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09955"> • The numerical user ID of the file owner</p>
        <p id="p_09956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09956"> • The numerical group ID of the file owner</p>
        <p id="p_09957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09957"> • The device type, if this "file" is actually a device</p>
        <p id="p_09958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09958"> • The size of the file (in bytes)</p>
        <p id="p_09959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09959"> • When the file was last accessed</p>
        <p id="p_09960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09960"> • When the file was last modified</p>
        <p id="p_09961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09961"> • When the file status last changed</p>
        <p id="p_09962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09962"> • The optimal block size for input-output operations on the file system containing the file</p>
        <p id="p_09963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09963"> • The number of blocks allocated for this file</p>
        <p id="p_09964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09964"> Some of the items returned by  stat  can be obtained using file test operators. Table 12.3 lists these items.</p>
        <p id="p_09965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09965"> Table 12.3. File-test operators that check information returned by stat.</p>
        <p id="p_09966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09966"> For more information on  stat  or the information it returns, see the  UNIX  manual page for the  stat command on your machine.</p>
        <p id="p_09967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09967"> The lstat Function</p>
        <p id="p_09968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09968"> The  lstat  function returns the same information as  stat,  but it assumes that the name being passed an argument is a symbolic link.</p>
        <p id="p_09969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09969"> The syntax for  lstat  is the same as that for  stat.</p>
        <p id="p_09970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09970"> lstat  (file);</p>
        <p id="p_09971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09971"> file is either a filename or a file variable.</p>
        <p id="p_09972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09972"> The time Function</p>
        <p id="p_09973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09973"> The access and modification times returned by  stat  and by the  -A  and  -M  file-test operators are integers representing the number of elapsed seconds from January 1, 1970, to the time the file was accessed or modified.</p>
        <p id="p_09974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09974"> To obtain the number of elapsed seconds from January 1, 1970, to the present time, call the built-in function  time.</p>
        <p id="p_09975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09975"> The syntax for calls to the  time  function is</p>
        <p id="p_09976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09976"> currtime = time();</p>
        <p id="p_09977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09977"> currtime  is the returned elapsed-seconds value.</p>
        <p id="p_09978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09978"> The gmtime and localtime Functions</p>
        <p id="p_09979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09979"> The value returned by  time  can be converted to either Greenwich Mean Time or your computer's local time.</p>
        <p id="p_09980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09980"> To convert to Greenwich Mean Time, call the  gmtime  function. To convert to local time, call the local time  function.</p>
        <p id="p_09981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09981"> The syntax for the  gmtime  and  localtime  functions is identical:</p>
        <p id="p_09982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09982"> timelist = gmtime (timeval); timelist = localtime (timeval);</p>
        <p id="p_09983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09983"> Both functions accept the time value returned by  time, stat,  or the  -A  and  -M  file-test operators. Both functions return a list consisting of the following nine elements:</p>
        <p id="p_09984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09984"> • Seconds</p>
        <p id="p_09985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09985"> • Minutes</p>
        <p id="p_09986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09986"> • The hour of the day, which is a value between 0 and 23</p>
        <p id="p_09987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09987"> • The day of the month</p>
        <p id="p_09988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09988"> • The month, which is a value between 0 (January) and 11 (December)</p>
        <p id="p_09989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09989"> • The year</p>
        <p id="p_09990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09990"> • The day of the week, which is a value between 0 (Sunday) and 6 (Saturday)</p>
        <p id="p_09991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09991"> • The day of the year, which is a value between 0 and 364</p>
        <p id="p_09992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09992"> • A flag indicating whether daylight saving time is in effect</p>
        <p id="p_09993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09993"> For more information on the list returned by  gmtime  or  local time,  refer to the UNIX manual pages for the system functions with the same names.</p>
        <p id="p_09994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09994"> The utime Function</p>
        <p id="p_09995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09995"> The time values returned by  stat, time,  and the  -A  and  -M  file-test operators can be used to set the access and modification times of other files. To do this, use the  utime  function.</p>
        <p id="p_09996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09996"> The syntax for the  utime  function is</p>
        <p id="p_09997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09997"> utime (acctime, modtime, filelist);</p>
        <p id="p_09998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09998"> acctime  is the new access time,  modtime  is the new modification time, and  filelist  is the list of files.</p>
        <p id="p_09999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_09999"> utime  returns the number of files whose access and modification times have been successfully changed. The following is an example of a call to  utime:</p>
        <p id="p_10000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10000"> $acctime = -A "filel";</p>
        <p id="p_10001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10001"> $modtime = -M "filel";</p>
        <p id="p_10002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10002"> @ filelist = ("file2", "file3"); utime ($acctime, $modtime, @filelist);</p>
        <p id="p_10003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10003"> Here, the files  file 2  and  file 3  have their access and modification times changed to those of  filel.</p>
        <p id="p_10004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10004"> The f ileno Function</p>
        <p id="p_10005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10005"> The  f ileno  function returns the internal UNIX file descriptor associated with a particular file variable. The syntax for the  f ileno  function is</p>
        <p id="p_10006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10006"> filedesc = fileno (filevar);</p>
        <p id="p_10007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10007"> Here,  filevar  is the file variable whose descriptor is to be retrieved.</p>
        <p id="p_10008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10008"> The file descriptor returned by  fileno  is used in various UNIX system calls; these calls can be accessed using the  system  function (as described on  Day  15) .</p>
        <p id="p_10009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10009"> The flock and fcntl Functions</p>
        <p id="p_10010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10010"> The  flock  and  f cntl  functions call the UNIX system commands of the same name. The syntax for the  flock  and  f cntl  functions is</p>
        <p id="p_10011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10011"> fcntl (filevar, fcntlrtn, value); flock (filevar, flockop);</p>
        <p id="p_10012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10012"> Here,  filevar  is a file variable representing an open file,  fcntlrtn  is a  fcntl  function as defined in the UNIX  fcntl  manual page, and  value  is the value passed to the function, if appropriate. Similarly,  flockop  is a file-locking operation, as defined in the UNIX  flock  manual page.</p>
        <p id="p_10013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10013"> For more information on these functions, refer to the manual pages or to a book about UNIX. (You won't really be able to use these functions effectively unless you know a fair bit about how your operating system works.)</p>
        <p id="p_10014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10014"> Using DBM Files</p>
        <p id="p_10015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10015"> Many systems on which Perl is available support files that are created using the Data Base Management (DBM) library. Perl enables you to use an associative array to access a particular DBM file.</p>
        <p id="p_10016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10016"> The following sections describe how to access DBM files from Perl programs using the  dbmopen  and dbmclose  functions. If you are running Perl 5, these functions have been superseded by the  tie  and untie  functions; see  Day 19 ,  "Object-Oriented Programming in Perl," for more details.</p>
        <p id="p_10017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10017"> For more information on DBM, refer to your system's appropriate manual pages.</p>
        <p id="p_10018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10018"> The dbmopen Function</p>
        <p id="p_10019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10019"> To associate an associative array with a DBM file, use the  dbmopen  function. The syntax for the  dbmopen  function is</p>
        <p id="p_10020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10020"> dbmopen (array, dbmfilename, permissions);</p>
        <p id="p_10021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10021"> This function requires three arguments:</p>
        <p id="p_10022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10022"> • array,  which is the associative array to use</p>
        <p id="p_10023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10023"> • dbmfilename,  which is the name of the DBM file to open</p>
        <p id="p_10024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10024"> • permissions,  which are the access permissions to use (See the section called "The  mkdir Function" for more information on access permissions.)</p>
        <p id="p_10025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10025"> After the DBM file has been opened, the subscripts for the associative array represent the DBM file keys, and the values of the array represent the values associated with the keys.</p>
        <p id="p_10026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10026"> The dbmclose Function</p>
        <p id="p_10027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10027"> To close a DBM file opened by  dbmopen,  use  dbmclose. The syntax for the  dbmclose  function is</p>
        <p id="p_10028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10028"> dbmclose (array) ;</p>
        <p id="p_10029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10029"> Here,  array  is the associative array specified in the call to  dbmopen.</p>
        <p id="p_10030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10030"> Summary</p>
        <p id="p_10031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10031"> Today, you learned how to open a pipe that directs input to the program, how to open a file for both reading and writing, and how to associate multiple file variables with a single file. You also learned how to test for the end of a particular input file or for the end of the last input file.</p>
        <p id="p_10032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10032"> You also learned how to skip backward and forward in files and how to read single characters from a file using  getc.  You can use  getc  to build hot-key applications, which act as soon as they read a single character from the keyboard.</p>
        <p id="p_10033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10033"> Perl provides several functions for manipulating directories. They enable you to create, open, read, close, delete, and skip around in directories. Other Perl functions enable you to move a file from one directory to another, create hard and symbolic links from one location to another, and delete a hard link (or a file).</p>
        <p id="p_10034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10034"> You learned about the Perl functions that enable you to change the file owner or file permissions, truncate a file, retrieve file information, set file access and modification times, retrieve the file descriptor, and call the  flock  and  f cntl  system commands.</p>
        <p id="p_10035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10035"> Finally, Perl provides an interface to the DBM library that enables you to associate DBM files with associative arrays.</p>
        <p id="p_10036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10036"> Q&amp;A</p>
        <p id="p_10037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10037"> Q: How can I determine whether a particular Perl function that manipulates the UNIX file</p>
        <p id="p_10038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10038"> system is defined on my machine?</p>
        <p id="p_10039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10039"> A: A Perl function that manipulates the UNIX file system normally has the same name as the UNIX</p>
        <p id="p_10040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10040"> command or C library function that performs the same task. If the UNIX command or C library function is defined, the Perl function is usually defined as well.</p>
        <p id="p_10041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10041"> To check whether a UNIX command or C library function is defined, enter the command  man name,  where  name  is the name of the Perl library function for which you are checking.</p>
        <p id="p_10042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10042"> Q: Why does a list of files in a directory appear in unsorted order?</p>
        <p id="p_10043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10043"> A: The list appears in the order in which the files are stored in the directory. This varies, depending</p>
        <p id="p_10044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10044"> on the machine; usually, however, newer files appear at the end of the list.</p>
        <p id="p_10045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10045"> Q: Which is better to use: the file-test operators or the built-in function stat?</p>
        <p id="p_10046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10046"> A: Whenever possible, use the file-test operators. They are easier to use and are often more</p>
        <p id="p_10047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10047"> efficient.</p>
        <p id="p_10048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10048"> Q: Why are both read and sysread defined, when they are so similar?</p>
        <p id="p_10049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10049"> A:  read,  like the UNIX function  f read,  uses the standard UNIX input-output (I/O) environment,</p>
        <p id="p_10050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10050"> sysread  and  syswrite,  on the other hand, bypass the standard I/O environment and perform low-level system calls.</p>
        <p id="p_10051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10051"> Q: Why are eof and eof () different?</p>
        <p id="p_10052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10052"> A: The short answer is: Just because. The long answer is that an empty list as an argument (as in</p>
        <p id="p_10053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10053"> eof  ()) refers to the list of files on the command line, as does the &lt;&gt; in</p>
        <p id="p_10054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10054"> while ($line = &lt;&gt;) ...</p>
        <p id="p_10055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10055"> eof , on the other hand, refers only to the file currently being read.</p>
        <p id="p_10056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10056"> Workshop</p>
        <p id="p_10057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10057"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_10058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10058"> Quiz</p>
        <p id="p_10059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10059"> 1. What do these functions do?</p>
        <p id="p_10060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10060"> a. tell</p>
        <p id="p_10061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10061"> b. mkdir</p>
        <p id="p_10062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10062"> c. link</p>
        <p id="p_10063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10063"> d. unlink</p>
        <p id="p_10064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10064"> e. truncate</p>
        <p id="p_10065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10065"> 2.  What is the difference between  stat  and  Is tat?</p>
        <p id="p_10066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10066"> 3.  What is the difference between  tell  and  telldir?</p>
        <p id="p_10067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10067"> 4. How are the following files being opened?</p>
        <p id="p_10068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10068"> d. open (MYFILE, "&gt;&amp;STDOUT");</p>
        <p id="p_10069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10069"> 5. What permissions are granted by the following values?</p>
        <p id="p_10070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10070"> Exercises</p>
        <p id="p_10071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10071"> 1. Write a program that reads the directory  /u/ jqpublic  and prints out all file and directory names that start with a period. Ignore the special files . (one period) and . . (two periods).</p>
        <p id="p_10072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10072"> 2. Write a program that lists all the files  (not  the subdirectories) in the directory  /u/  j  qpublic  and then lists the contents of any subdirectories, their subdirectories, and so on. (Hint: Use a recursive subroutine.)</p>
        <p id="p_10073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10073"> 3. Write a program that uses  readdir  and  rewinddir  to read a directory named  /u/ jqpublic and print a sorted list of the files and directories in alphabetical order. Ignore all names beginning with a period. (Of course, this is not the most efficient way to do this.)</p>
        <p id="p_10074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10074"> 4. Write a program that uses hot keys and does the following:</p>
        <p id="p_10075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10075"> o  Reads single digits and prints out their English-language equivalents (for example,  zero for 0,  one  for 1, and so on) o  Terminates if it reads the Esc (escape) character o  Ignores all other input o  Prints out one English word per line</p>
        <p id="p_10076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10076"> 5. Write a program that reads the directory  /u/ jqpublic  and grants global execute permissions for all files ending in  .pi.  Take away all other permissions, except user read, for every other file in the directory. Skip over all subdirectories.</p>
        <p id="p_10077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10077"> 6. BUG BUSTER: What is wrong with the following program?</p>
        <p id="p_10078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10078"> #!/usr/local/bin/perl</p>
        <p id="p_10079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10079"> while ($line = &lt;&gt;) {</p>
        <p id="p_10080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10080"> print ($line); if  (eof())  {</p>
        <p id="p_10081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10081"> print ("— end of current file  —\n");</p>
        <p id="p_10082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10082"> }</p>
        <p id="p_10083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10083"> }</p>
        <p id="p_10084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10084"> Chapter 13</p>
        <p id="p_10085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10085"> Process, String, and Mathematical Functions</p>
        <p id="p_10086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10086"> CONTENTS</p>
        <p id="p_10087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10087"> •  Process- and Program-Manipulation Functions</p>
        <p id="p_10088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10088"> o  Starting a Process o  Terminating a Program or Process</p>
        <p id="p_10089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10089"> o  Execution Control Functions o  Miscellaneous Control Functions</p>
        <p id="p_10090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10090"> •  Mathematical Functions</p>
        <p id="p_10091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10091"> o  The.  sin  and  cos  Functions</p>
        <p id="p_10092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10092"> o  The  at an 2  Function o  The  sqrt  Function o  The  exp  Function o  The  log  Function o  The  abs  Function o  The  rand  and  srand  Functions</p>
        <p id="p_10093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10093"> •  String-Manipulation Functions</p>
        <p id="p_10094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10094"> o  The  index  Function o  The  rindex  Function o  The  length  Function o  Retrieving String Fength Using  tr</p>
        <p id="p_10095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10095"> o  The  pos  Function o  The  substr  Function o  The  study  Function o  Case Conversion Functions</p>
        <p id="p_10096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10096"> o  The  quotemeta  Function</p>
        <p id="p_10097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10097"> O  The.  join  Function o  The  sprint f  Function</p>
        <p id="p_10098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10098"> •  Summary</p>
        <p id="p_10099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10099"> •  O&amp;A</p>
        <p id="p_10100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10100"> •  Workshop</p>
        <p id="p_10101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10101"> o  Quiz o  Exercises</p>
        <p id="p_10102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10102"> Today's lesson describes three groups of built-in Perl functions:</p>
        <p id="p_10103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10103"> • The functions that manipulate processes and programs that are currently running</p>
        <p id="p_10104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10104"> • The functions that perform mathematical operations</p>
        <p id="p_10105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10105"> • The functions that manipulate character strings</p>
        <p id="p_10106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10106"> Warning</p>
        <p id="p_10107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10107"> Many of the functions described today use features of the UNIX operating system. If you are using Perl on a machine that is not running UNIX, some of these functions might not be defined or might behave differently.</p>
        <p id="p_10108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10108"> Check the documentation supplied with your version of Perl for details on which functions are supported or emulated on your machine</p>
        <p id="p_10109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10109"> Process- and Program-Manipulation Functions</p>
        <p id="p_10110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10110"> Perl provides a wide range of functions that manipulate both the program currently being executed and other programs (also called processes) running on your machine. These functions are divided into four groups:</p>
        <p id="p_10111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10111"> • Functions that start additional processes</p>
        <p id="p_10112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10112"> • Functions that stop the current program or another process</p>
        <p id="p_10113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10113"> • Functions that control the execution of a program or process</p>
        <p id="p_10114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10114"> • Functions that manipulate processes or programs but don't fit into any of the preceding categories The following sections describe these four groups of process- and program-manipulation functions.</p>
        <p id="p_10115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10115"> Starting a Process</p>
        <p id="p_10116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10116"> Several built-in functions provide different ways of creating processes:  eval, system, fork, pipe, exec,  and  syscall.  These functions are described in the following subsections.</p>
        <p id="p_10117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10117"> The eval Function</p>
        <p id="p_10118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10118"> The  eval  function treats a character string as an executable Perl program.</p>
        <p id="p_10119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10119"> The syntax for the  eval  function is</p>
        <p id="p_10120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10120"> eval (string);</p>
        <p id="p_10121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10121"> Here,  st ring  is the character string that is to become a Perl program.</p>
        <p id="p_10122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10122"> For example, these two lines of code:</p>
        <p id="p_10123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10123"> $print = "print (V'hello, world\\n\");</p>
        <p id="p_10124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10124"> eval ($print) ;</p>
        <p id="p_10125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10125"> print the following message on your screen:</p>
        <p id="p_10126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10126"> hello, world</p>
        <p id="p_10127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10127"> The character string passed to  eval  can be a character-string constant or any expression that has a value which is a character string. In this example, the following string is assigned to  $print,  which is then passed to  eval:</p>
        <p id="p_10128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10128"> print ("hello, world\n");</p>
        <p id="p_10129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10129"> The  eval  function uses the special system variable $@ to indicate whether the Perl program contained in the character string has executed properly. If no error has occurred, $ @ contains the null string. If an error has been detected, $ @ contains the text of the message.</p>
        <p id="p_10130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10130"> The subprogram executed by  eval  affects the program that called it; for example, any variables that are changed by the subprogram remain changed in the main program. Listing 13.1 provides a simple example of this.</p>
        <p id="p_10131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10131"> Type</p>
        <p id="p_10132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10132"> Listing 13.1. A program that illustrates the behavior of eval.</p>
        <p id="p_10133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10133"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10134"> 2  :</p>
        <p id="p_10135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10135"> 3: $myvar = 1;</p>
        <p id="p_10136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10136"> 4: eval ("print  (\"hi!\\n\");  \$myvar =  2;");</p>
        <p id="p_10137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10137"> 5:  print ("the value of \$myvar is $myvar\n");</p>
        <p id="p_10138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10138"> Output</p>
        <p id="p_10139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10139"> $ programl3_l hi !</p>
        <p id="p_10140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10140"> the value of $myvar is 2 $</p>
        <p id="p_10141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10141"> Analysis</p>
        <p id="p_10142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10142"> The call to  eval  in line 4 first executes the statement</p>
        <p id="p_10143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10143"> print (  "hi!\n");</p>
        <p id="p_10144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10144"> Then it executes the following assignment, which assigns  2  to  $myvar:</p>
        <p id="p_10145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10145"> $myvar = 2;</p>
        <p id="p_10146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10146"> The value of  $myvar  remains  2  in the main program, which means that line 5 prints the value  2.  (The backslash preceding the $ in  $myvar  ensures that the Perl interpreter does not substitute the value of $myvar  for the name before passing it to  eval.)</p>
        <p id="p_10147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10147"> NOTE</p>
        <p id="p_10148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10148"> If you like, you can leave off the final semicolon in the character string passed to  eval,  as follows:</p>
        <p id="p_10149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10149"> eval ("print  (\"hi!\\n\");  \$myvar =  2");</p>
        <p id="p_10150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10150"> As before, this prints  hi  ! and assigns  2  to  $myvar</p>
        <p id="p_10151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10151"> The  eval  function has one very useful property: If the subprogram executed by  eval  encounters a fatal error, the main program does not halt. Instead, the subprogram terminates, copies the error message into the system variable $ @, and returns to the main program.</p>
        <p id="p_10152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10152"> This feature is very useful if you are moving a Perl program from one machine to another and you are not sure whether the new machine contains a built-in function you need. For example. Listing 13.2 tests whether the  tell  function is implemented.</p>
        <p id="p_10153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10153"> Type</p>
        <p id="p_10154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10154"> Listing 13.2. A program that uses eval to test whether a function is implemented.</p>
        <p id="p_10155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10155"> Output</p>
        <p id="p_10156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10156"> $ programl3_2</p>
        <p id="p_10157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10157"> The tell function is defined. $</p>
        <p id="p_10158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10158"> Analysis</p>
        <p id="p_10159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10159"> The call to  eval  in line 4 creates a subprogram that calls the function  tell.  If  tell  is defined, the subprogram assigns the location of the next line (which, in this case, is the first line) to read to the scalar variable $  start.  If  tell  is not defined, the subprogram places the error message in $@.</p>
        <p id="p_10160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10160"> Line 5 checks whether $ @ is the null string. If $ @ is empty, the subprogram in line 4 executed without generating an error, which means that the tell function is implemented. (Because assignments performed in the subprogram remain in effect in the main program, the main program can call seek using the value in $ start,  if desired.) If $ @ is not empty, the program assumes that  tell  is not defined, and it prints a message proclaiming that fact. (This program is assuming that the only reason the subprogram could fail is</p>
        <p id="p_10161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10161"> because  tell  is not defined. This is a reasonable assumption, because you know that the file referenced by MYFILE  has been successfully opened.)</p>
        <p id="p_10162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10162"> Warning</p>
        <p id="p_10163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10163"> Although  eval  is very useful, it is best to use it only for small programs. If you need to generate a larger program, it might be better to write the program to a file and call  system  to execute it. (The system  function is described in the following section.)</p>
        <p id="p_10164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10164"> Because statements executed by  eval  affect the program that calls it, the behavior of complicated programs might become difficult to track if  eval  is used to excess.</p>
        <p id="p_10165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10165"> The system Function</p>
        <p id="p_10166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10166"> You have seen examples of the  system  function in earlier lessons. The syntax for the  system  function is</p>
        <p id="p_10167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10167"> system (list);</p>
        <p id="p_10168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10168"> This function is passed a list as follows: The first element of the list contains the name of a program to execute, and the other elements are arguments to be passed to the program.</p>
        <p id="p_10169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10169"> When  system  is called, it starts a process that runs the program and waits until the process terminates. When the process terminates, the error code is shifted left eight bits, and the resulting value becomes system's  return value. Listing 13.3 is a simple example of a program that calls  system.</p>
        <p id="p_10170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10170"> Listing 13.3. A program that calls system.</p>
        <p id="p_10171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10171"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10172"> 2  :</p>
        <p id="p_10173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10173"> 3: @proglist = ("echo", "hello, world!");</p>
        <p id="p_10174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10174"> 4 :</p>
        <p id="p_10175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10175"> system(Sproglist) ;</p>
        <p id="p_10176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10176"> Output</p>
        <p id="p_10177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10177"> $ programl3_3 hello, world! $</p>
        <p id="p_10178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10178"> Analysis</p>
        <p id="p_10179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10179"> In this program, the call to  system  executes the UNIX program  echo,  which displays its arguments. The argument passed to echo is  hello, world!.</p>
        <p id="p_10180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10180"> TIP</p>
        <p id="p_10181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10181"> When you start another program using  system,  output data might be mixed, out of sequence, or duplicated.</p>
        <p id="p_10182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10182"> To get around this problem, set the system variable $ |, defined for each file, to 1. The following is an example:</p>
        <p id="p_10183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10183"> select (STDOUT);</p>
        <p id="p_10184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10184"> $1 = l;</p>
        <p id="p_10185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10185"> select (STDERR);</p>
        <p id="p_10186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10186"> $1 = l;</p>
        <p id="p_10187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10187"> When $ | is set to 1, no buffer is defined for that file, and output is written out right away. This ensures that the output behaves properly when  system  is called.</p>
        <p id="p_10188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10188"> See "Redirecting One File to Another" on  Day  12 ,  "Working with the File System," for more information on  select  and $ |</p>
        <p id="p_10189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10189"> The fork Function</p>
        <p id="p_10190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10190"> The  fork  function creates two copies of your program: the parent process and the child process. These copies execute simultaneously.</p>
        <p id="p_10191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10191"> The syntax for the  fork  function is</p>
        <p id="p_10192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10192"> procid  = fork();</p>
        <p id="p_10193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10193"> fork  returns zero to the child process and a nonzero value to the parent process. This nonzero value is the process ID  of the child process. (A process ID is an integer that enables the system to distinguish this process from the other processes currently running on the machine.)</p>
        <p id="p_10194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10194"> The return value from  fork  enables you to determine which process is the child process and which is the parent. For example:</p>
        <p id="p_10195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10195"> $retval =  fork();</p>
        <p id="p_10196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10196"> if ($retval == 0) {</p>
        <p id="p_10197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10197"> # this is the child process</p>
        <p id="p_10198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10198"> exit; # this terminates the child process</p>
        <p id="p_10199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10199"> } else {</p>
        <p id="p_10200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10200"> # this is the parent process</p>
        <p id="p_10201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10201"> If  fork  is unable to execute, the return value is a special undefined value for which you can test by using the  defined  function. (For more information on  defined,  see  Day 14 ,  "Scalar- Conversion and List-Manipulation Functions.")</p>
        <p id="p_10202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10202"> To terminate a child process created by  fork,  use the built-in function  exit,  which is described later in today's lesson.</p>
        <p id="p_10203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10203"> Warning</p>
        <p id="p_10204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10204"> Be careful when you use the  fork  function. The following are a few examples of what can go wrong:</p>
        <p id="p_10205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10205"> • If both copies of the program execute calls to  print  or any other output-generating function, the output from one copy might be mixed with the output from the other copy. There is no way to guarantee that output from one copy will appear before output from the other, unless you force one process to wait for the other.</p>
        <p id="p_10206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10206"> • If you use  f ork  in a loop, the program might wind up generating many copies of itself. This can affect the performance of your system (or crash it completely).</p>
        <p id="p_10207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10207"> • Your child process might wind up executing code that your parent process is supposed to execute, or vice versa</p>
        <p id="p_10208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10208"> The pipe Function</p>
        <p id="p_10209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10209"> The  pipe  function is designed to be used in conjunction with the  fork  function. It provides a way for the child and parent processes to communicate.</p>
        <p id="p_10210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10210"> The syntax for the  pipe  function is</p>
        <p id="p_10211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10211"> pipe (infile, outfile);</p>
        <p id="p_10212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10212"> pipe  requires two arguments, each of which is a file variable that is not currently in use-in this case, infile  and  outfile.  After  pipe  has been called, information sent via the  outfile  file variable can be read using the  infile  file variable. In effect, the output from  outfile  is piped to  infile.</p>
        <p id="p_10213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10213"> To use  pipe  with  fork,  do the following:</p>
        <p id="p_10214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10214"> 1. Call  pipe.</p>
        <p id="p_10215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10215"> 2. Call  fork  to split the program into parent and child processes.</p>
        <p id="p_10216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10216"> 3.  Have one of the processes close  infile,  and have the other close  outfile.</p>
        <p id="p_10217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10217"> The process in which  outfile  is still open can now send data to the process in which  inf ile  is still open. (The child can send data to the parent, or vice versa, depending on which process closes input and which closes output.)</p>
        <p id="p_10218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10218"> Listing  13.4  shows how  pipe  works. It uses  fork  to create a parent and child process. The parent process reads a line of input, which it passes to the child process. The child process then prints it.</p>
        <p id="p_10219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10219"> Type</p>
        <p id="p_10220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10220"> Listing 13.4.  A  program that uses fork and pipe.</p>
        <p id="p_10221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10221"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10222"> 2  :</p>
        <p id="p_10223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10223"> 3: pipe (INPUT, OUTPUT);</p>
        <p id="p_10224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10224"> 4: $retval =  fork();</p>
        <p id="p_10225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10225"> 5 :</p>
        <p id="p_10226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10226"> if ($retval != 0) {</p>
        <p id="p_10227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10227"> Output</p>
        <p id="p_10228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10228"> $ programl3_4 Enter a line of input: Here is a test line Here is a test line $</p>
        <p id="p_10229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10229"> Analysis</p>
        <p id="p_10230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10230"> I Line 3 defines the file variables  INPUT  and  OUTPUT.  Data sent to  OUTPUT  can be now read from  INPUT.</p>
        <p id="p_10231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10231"> Line 4 splits the program into a parent process and a child process. Line 5 then determines which process is which.</p>
        <p id="p_10232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10232"> The parent process executes lines 7-10. Because the parent process is sending data through  OUTPUT,  it has no need to access  INPUT;  therefore, line 7 closes  INPUT.</p>
        <p id="p_10233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10233"> Lines 8 and 9 obtain a line of data from the standard input file. Line 10 then sends this line of data to the child process via the file variable  OUTPUT.</p>
        <p id="p_10234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10234"> The child process executes lines 13-16. Because the child process is receiving data through  INPUT,  it does not need access to  OUTPUT;  therefore, line 13 closes  OUTPUT.</p>
        <p id="p_10235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10235"> Line 14 reads data from  INPUT.  Because data from  OUTPUT  is piped to  INPUT,  the program waits until the data is actually sent before continuing with line 15.</p>
        <p id="p_10236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10236"> Line 16 uses  exit  to terminate the child process. This also automatically closes  INPUT.</p>
        <p id="p_10237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10237"> Note that the  &lt;INPUT&gt;  operator behaves like any other operator that reads input (such as, for instance, &lt;STDIN&gt;).  If there is no more data to read,  INPUT  is assumed to be at the "end of file," and  &lt;INPUT&gt; returns the null string.</p>
        <p id="p_10238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10238"> Warning</p>
        <p id="p_10239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10239"> Traffic through the file variables specified by  pipe  can flow in only one direction. You cannot have a process both send and receive on the same  pipe.</p>
        <p id="p_10240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10240"> If you need to establish two-way communication, you can open two pipes, one in each direction</p>
        <p id="p_10241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10241"> The exec Function</p>
        <p id="p_10242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10242"> The  exec  function is similar to the  system  function, except that it terminates the current program before starting the new one.</p>
        <p id="p_10243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10243"> The syntax for the  exec  function is</p>
        <p id="p_10244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10244"> exec  (list);</p>
        <p id="p_10245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10245"> This function is passed a list as follows: The first element of the list contains the name of a program to execute, and the other elements are arguments to be passed to the program.</p>
        <p id="p_10246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10246"> For example, the following statement terminates the Perl program and starts the command  mail dave:</p>
        <p id="p_10247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10247"> exec ("mail  dave");</p>
        <p id="p_10248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10248"> Like  system, exec  accepts additional arguments that are assumed to be passed to the command being invoked. For example, the following statement executes the command  vi f ile 1:</p>
        <p id="p_10249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10249"> exec ("vi", "filel");</p>
        <p id="p_10250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10250"> You can specify the name that the system is to use as the program name, as follows:</p>
        <p id="p_10251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10251"> exec "maildave" ("mail dave");</p>
        <p id="p_10252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10252"> Here, the command  mail dave  is invoked, but the program name is set to  maildave.  (This affects the value of the system variable $ 0, which contains the name of the running program. It also affects the value of argv  [  0  ] if the program to be invoked was originally written in  C.)</p>
        <p id="p_10253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10253"> exec  often is used in conjunction with  fork:  when  fork  splits into two processes, the child process starts another program using  exec.</p>
        <p id="p_10254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10254"> Warning</p>
        <p id="p_10255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10255"> exec  has the same output-buffering problems as  system.  See the description of  system,  earlier in today's lesson, for a description of these problems and how to deal with them</p>
        <p id="p_10256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10256"> The syscall Function</p>
        <p id="p_10257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10257"> The  syscall  function calls a system function.</p>
        <p id="p_10258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10258"> The syntax for the  syscall  function is</p>
        <p id="p_10259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10259"> syscall (list);</p>
        <p id="p_10260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10260"> syscall  expects a list as its argument. The first element of the  list  is the name of the system call to invoke, and the remaining elements are arguments to be passed to the call.</p>
        <p id="p_10261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10261"> If an argument in the list passed to  syscall  is a numeric value, it is converted to a  C  integer (type  int). Otherwise, a pointer to the string value is passed. See the  syscall  UNIX manual page or the Perl documentation for more details.</p>
        <p id="p_10262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10262"> NOTE</p>
        <p id="p_10263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10263"> The Perl header file  syscall.ph  must be included in order to use</p>
        <p id="p_10264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10264"> syscall:</p>
        <p id="p_10265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10265"> require ("syscall.ph")</p>
        <p id="p_10266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10266"> For more information on  require,  see  Day  20 ,  "Miscellaneous Features of Perl."</p>
        <p id="p_10267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10267"> Terminating a Program or Process</p>
        <p id="p_10268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10268"> The following sections describe the functions that terminate either the currently executing program or a process running elsewhere on the system:  die, warn, exit,  and  kill.</p>
        <p id="p_10269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10269"> The die and warn Functions</p>
        <p id="p_10270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10270"> The  die  and  warn  functions provide a way for programs to pass urgent messages back to the user who is running them.</p>
        <p id="p_10271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10271"> The  die  function terminates the program and prints an error message on the standard error file.</p>
        <p id="p_10272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10272"> The syntax for the  die  function is</p>
        <p id="p_10273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10273"> die (message);</p>
        <p id="p_10274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10274"> message  is the error message to be displayed.</p>
        <p id="p_10275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10275"> For example, the call</p>
        <p id="p_10276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10276"> die ("Cannot open input file\n");</p>
        <p id="p_10277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10277"> prints the following message and then exits:</p>
        <p id="p_10278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10278"> Cannot open input file</p>
        <p id="p_10279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10279"> die  can accept a list as its argument, in which case all elements of the list are printed.</p>
        <p id="p_10280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10280"> @diemsg = ("I'm about ", "to die\n");</p>
        <p id="p_10281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10281"> die (@diemsg);</p>
        <p id="p_10282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10282"> This prints out the following message and then exits:</p>
        <p id="p_10283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10283"> I'm about to die</p>
        <p id="p_10284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10284"> If the last argument passed to  die  ends with a newline character, the error message is printed as is. If the last argument to  die  does not end with a newline character, the program filename and line number are printed, along with the line number of the input file (if applicable). For example, if line 6 of the file  myprog is</p>
        <p id="p_10285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10285"> die ("Cannot open input file");</p>
        <p id="p_10286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10286"> the message it prints is</p>
        <p id="p_10287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10287"> Cannot open input file at myprog line 6.</p>
        <p id="p_10288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10288"> The  warn  function, like  die,  prints a message on the standard error file.</p>
        <p id="p_10289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10289"> The syntax for the  warn  function is</p>
        <p id="p_10290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10290"> warn (message);</p>
        <p id="p_10291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10291"> As with  die, message  is the message to be displayed, warn,  unlike  die,  does not terminate. For example, the statement</p>
        <p id="p_10292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10292"> warn ("Input file is empty");</p>
        <p id="p_10293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10293"> sends the following message to the standard error file, and then continues executing:</p>
        <p id="p_10294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10294"> Input file is empty at myprog line 76.</p>
        <p id="p_10295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10295"> If the string passed to  warn  is terminated by a newline character, the warning message is printed as is. For example, the statement</p>
        <p id="p_10296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10296"> warn("Danger! Danger!\n") ;</p>
        <p id="p_10297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10297"> sends</p>
        <p id="p_10298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10298"> Danger! Danger!</p>
        <p id="p_10299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10299"> to the standard error file.</p>
        <p id="p_10300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10300"> |note</p>
        <p id="p_10301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10301"> If  eval  is used to invoke a program that calls  die,  the error message printed by  die  is not printed; instead, the error message is assigned to the system variable $ @</p>
        <p id="p_10302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10302"> The exit Function</p>
        <p id="p_10303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10303"> The  exit  function terminates a program.</p>
        <p id="p_10304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10304"> If you like, you can specify a return code to be passed to the system by passing  exit  an argument using the following syntax:</p>
        <p id="p_10305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10305"> exit (retcode);</p>
        <p id="p_10306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10306"> ret code  is the return code you want to pass.</p>
        <p id="p_10307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10307"> For example, the following statement terminates the program with a return code of 2:</p>
        <p id="p_10308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10308"> exit  (2);</p>
        <p id="p_10309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10309"> The kill Function</p>
        <p id="p_10310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10310"> The  kill  function enables you to send a signal to a group of processes.</p>
        <p id="p_10311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10311"> The syntax for invoking the  kill  function is</p>
        <p id="p_10312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10312"> kill (signal, proclist);</p>
        <p id="p_10313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10313"> In this case,  signal  is the numeric signal to send. (For example, a  signal  of  9  kills the listed processes.)</p>
        <p id="p_10314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10314"> proclist  is a list of process IDs (such as the child process ID returned by  fork).</p>
        <p id="p_10315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10315"> signal  also can be a signal name enclosed in quotes, as in "  INT".</p>
        <p id="p_10316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10316"> For more details on the signals you can send, refer to the kill UNIX manual page.</p>
        <p id="p_10317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10317"> Execution Control Functions</p>
        <p id="p_10318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10318"> The  sleep, wait,  and  waitpid  functions delay the execution of a particular program or process.</p>
        <p id="p_10319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10319"> The sleep Function</p>
        <p id="p_10320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10320"> The  sleep  function suspends the program for a specified number of seconds.</p>
        <p id="p_10321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10321"> The syntax for the  sleep  function is</p>
        <p id="p_10322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10322"> sleep (time);</p>
        <p id="p_10323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10323"> time  is the number of seconds to suspend program execution.</p>
        <p id="p_10324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10324"> The function returns the number of seconds that the program was actually stopped.</p>
        <p id="p_10325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10325"> For example, the following statement puts the program to sleep for five seconds:</p>
        <p id="p_10326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10326"> sleep  (5);</p>
        <p id="p_10327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10327"> The wait and waitpid Functions</p>
        <p id="p_10328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10328"> The  wait  function suspends execution and waits for a child process to terminate (such as a process created by  fork).</p>
        <p id="p_10329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10329"> The  wait  function requires no arguments:</p>
        <p id="p_10330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10330"> procid =  wait();</p>
        <p id="p_10331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10331"> When a child process terminates,  wait  returns the process ID,  procid,  of the process that has terminated. If no child processes exist,  wait  returns  -1.</p>
        <p id="p_10332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10332"> The  waitpid  function waits for a particular child process.</p>
        <p id="p_10333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10333"> The syntax for the  waitpid  function is</p>
        <p id="p_10334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10334"> waitpid (procid, waitflag);</p>
        <p id="p_10335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10335"> procid  is the process ID of the process to wait for, and  waitflag  is a special wait flag (as defined by the waitpid  or  wait  4 manual page). By default,  waitflag  is 0 (a normal wait),  waitpid  returns 1 if the process is found and has terminated, and it returns -1 if the child process does not exist.</p>
        <p id="p_10336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10336"> Listing  13.5  shows how  waitpid  can be used to control process execution.</p>
        <p id="p_10337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10337"> Type</p>
        <p id="p_10338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10338"> Listing 13.5. A program that uses waitpid.</p>
        <p id="p_10339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10339"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10340"> 2  :</p>
        <p id="p_10341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10341"> 3: $procid =  fork();</p>
        <p id="p_10342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10342"> 4:  if ($procid == 0) {</p>
        <p id="p_10343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10343"> 5: # this is the child process</p>
        <p id="p_10344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10344"> 6: print ("this line is printed  first\n");</p>
        <p id="p_10345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10345"> 7:  exit  (0);</p>
        <p id="p_10346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10346"> 8:  } else {</p>
        <p id="p_10347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10347"> 9: # this is the parent process</p>
        <p id="p_10348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10348"> 10: waitpid ($procid, 0);</p>
        <p id="p_10349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10349"> 11: print ("this line is printed  last\n");</p>
        <p id="p_10350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10350"> 12 : }</p>
        <p id="p_10351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10351"> Output</p>
        <p id="p_10352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10352"> $ programl3_5</p>
        <p id="p_10353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10353"> this line is printed first</p>
        <p id="p_10354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10354"> this line is printed last $</p>
        <p id="p_10355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10355"> Analysis</p>
        <p id="p_10356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10356"> Line 3 splits the program into a parent process and a child process. The parent process is returned the process ID of the child process, which is stored in  $procid.</p>
        <p id="p_10357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10357"> Lines 6 and 7 are executed by the child process. Line 6 prints the following line:</p>
        <p id="p_10358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10358"> this line is printed first</p>
        <p id="p_10359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10359"> Line  7  then calls  exit,  which terminates the child process.</p>
        <p id="p_10360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10360"> Lines  10  and  11  are executed by the parent process. Line  10  calls  waitpid  and passes it the  ID  of the child process; therefore, the parent process waits until the child process terminates before continuing. This means that line 11, which prints the second line, is guaranteed to be executed after the first line is printed.</p>
        <p id="p_10361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10361"> As you can see,  wait  can be used to force the order of execution of processes.</p>
        <p id="p_10362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10362"> NOTE</p>
        <p id="p_10363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10363"> For more information on the possible values that can be passed as wait flag,  examine the file  wait. ph,  which is available from the same place you retrieved your copy of Perl. (It might already be on your system.) You can find out more also by investigating the waitpid  and  wait 4  manual pages</p>
        <p id="p_10364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10364"> Miscellaneous Control Functions</p>
        <p id="p_10365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10365"> The  caller, chroot, local,  and  times  functions perform various process and program-related actions.</p>
        <p id="p_10366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10366"> The caller Function</p>
        <p id="p_10367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10367"> The  caller  function returns the name and the line number of the program that called the currently executing subroutine.</p>
        <p id="p_10368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10368"> The syntax for the  caller  function is</p>
        <p id="p_10369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10369"> subinfo</p>
        <p id="p_10370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10370"> caller();</p>
        <p id="p_10371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10371"> caller  returns a three-element list,  subinfo,  consisting of the following:</p>
        <p id="p_10372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10372"> • The name of the package from which the subroutine was called</p>
        <p id="p_10373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10373"> • The name of the file from which the subroutine was called</p>
        <p id="p_10374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10374"> • The line number of the subroutine call</p>
        <p id="p_10375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10375"> This routine is used by the Perl debugger, which you'll learn about on  Day 21 ,  "The Perl Debugger." For more information on packages, refer to  Day 20 ,  "Miscellaneous Features of Perl."</p>
        <p id="p_10376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10376"> The chroot Function</p>
        <p id="p_10377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10377"> The  chroot  function duplicates the functionality of the  chroot  function call.</p>
        <p id="p_10378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10378"> The syntax for the  chroot  function is</p>
        <p id="p_10379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10379"> chroot (dir);</p>
        <p id="p_10380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10380"> dir  is the new root directory.</p>
        <p id="p_10381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10381"> In the following example, the specified directory becomes the root directory for the program:</p>
        <p id="p_10382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10382"> chroot ("/u/jqpublic");</p>
        <p id="p_10383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10383"> For more information, refer to the  chroot  manual page.</p>
        <p id="p_10384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10384"> The local Function</p>
        <p id="p_10385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10385"> The  local  function was introduced on  Day  9 ,  "Using Subroutines." It declares that a copy of a named variable is to be defined for a subroutine. (Refer to that day for examples that use  local  inside a subroutine.)</p>
        <p id="p_10386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10386"> local  can be used also to define a copy of a variable for use inside a  statement block  (a collection of statements enclosed in brace brackets), as follows:</p>
        <p id="p_10387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10387"> if ($var == 14) {</p>
        <p id="p_10388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10388"> local ($localvar);</p>
        <p id="p_10389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10389"> # stuff goes here</p>
        <p id="p_10390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10390"> }</p>
        <p id="p_10391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10391"> This defines a local copy of the variable  $localvar  for use inside the statement block. Any other copies of  $localvar  that exist are not affected by the changes to this local copy.</p>
        <p id="p_10392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10392"> Don't</p>
        <p id="p_10393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10393"> DON'T use  local  inside a loop, as in this example:</p>
        <p id="p_10394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10394"> while ($var &lt;= 14) {</p>
        <p id="p_10395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10395"> local ($myvar);</p>
        <p id="p_10396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10396"> # stuff goes here</p>
        <p id="p_10397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10397"> }</p>
        <p id="p_10398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10398"> Here, a new copy of  $myvar  is defined each time the loop iterates. This is probably not what you want.</p>
        <p id="p_10399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10399"> The times Function</p>
        <p id="p_10400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10400"> The  times  function returns the amount of job time consumed by this program and any child processes of this program.</p>
        <p id="p_10401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10401"> The syntax for the  times  function is</p>
        <p id="p_10402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10402"> timelist = times</p>
        <p id="p_10403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10403"> As you can see,  times  accepts no arguments. It returns  timelist,  a list consisting of the following four floating-point numbers:</p>
        <p id="p_10404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10404"> • The user time consumed by this program</p>
        <p id="p_10405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10405"> • The system time consumed by this program</p>
        <p id="p_10406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10406"> • The user time consumed by the child processes, if they exist</p>
        <p id="p_10407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10407"> • The system time consumed by the child processes, if they exist</p>
        <p id="p_10408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10408"> Mathematical Functions</p>
        <p id="p_10409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10409"> Perl provides functions that perform the standard trigonometric operations, plus some other useful mathematical operations. The following sections describe these functions:  sin, cos, atan2, sqrt, exp, log, abs, rand,  and  srand.</p>
        <p id="p_10410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10410"> The sin and cos Functions</p>
        <p id="p_10411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10411"> The  sin  and  cos  functions are passed a scalar value and return the sine and cosine, respectively, of the value.</p>
        <p id="p_10412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10412"> The syntax of the  sin  and  cos  functions is</p>
        <p id="p_10413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10413"> retval = sin (value); retval = cos (value);</p>
        <p id="p_10414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10414"> value  is a placeholder here. It can be the value stored in a scalar variable or the result of an expression; it is assumed to be in radians. See the following section, "The  atan2  Function," to find out how to convert from radians to degrees.</p>
        <p id="p_10415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10415"> The at an 2 Function</p>
        <p id="p_10416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10416"> The  at an 2  function calculates and returns the arctangent of one value divided by another, in the range -p to P-</p>
        <p id="p_10417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10417"> The syntax of the  atan2  function is</p>
        <p id="p_10418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10418"> retval = atan2 (valuel, value2);</p>
        <p id="p_10419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10419"> If  valuel  and  value2  are equal,  retval  is the value of p divided by 4.</p>
        <p id="p_10420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10420"> Listing 13.6 shows how you can use this to convert from degrees to radians.</p>
        <p id="p_10421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10421"> Type</p>
        <p id="p_10422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10422"> Listing 13.6. A program that contains a subroutine that converts from degrees to radians.</p>
        <p id="p_10423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10423"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10424"> 2  :</p>
        <p id="p_10425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10425"> 3: $rad90 = &amp;degrees_to_radians(90);</p>
        <p id="p_10426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10426"> 4: $sin90 = sin($rad90);</p>
        <p id="p_10427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10427"> is $sin90\ncosine is $cos90\n");</p>
        <p id="p_10428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10428"> $degrees / 45;</p>
        <p id="p_10429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10429"> Output</p>
        <p id="p_10430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10430"> $ programl3_6 90 degrees: sine is 1</p>
        <p id="p_10431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10431"> cosine is 6.1230317691118962911e-17 $</p>
        <p id="p_10432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10432"> Analysis</p>
        <p id="p_10433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10433"> The subroutine  degrees_to_radians  converts from degrees to radians by multiplying by p divided by 180. Because  at an 2  ( 1 , 1) returns p divided by 4, all the subroutine needs to do after that is divide by 45 to obtain the number of radians.</p>
        <p id="p_10434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10434"> In the main body of the program, line 3 converts 90 degrees to the equivalent value in radians (p divided by 2). Line 4 then passes this value to  sin,  and line 5 passes it to  cos.</p>
        <p id="p_10435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10435"> NOTE</p>
        <p id="p_10436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10436"> The trigonometric operations provided here are sufficient to enable you to perform the other important trigonometric operations. For example, to obtain the tangent of a value, obtain the sine and cosine of the value by calling  sin  and  cos,  and then divide the sine by the cosine</p>
        <p id="p_10437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10437"> The sqrt Function</p>
        <p id="p_10438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10438"> The  sqrt  function returns the square root of the value it is passed. The syntax for the  sqrt  function is</p>
        <p id="p_10439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10439"> retval = sqrt (value);</p>
        <p id="p_10440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10440"> value  can be any positive number.</p>
        <p id="p_10441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10441"> The exp Function</p>
        <p id="p_10442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10442"> The  exp  function returns the number  e * * value,  where  e  is the standard mathematical constant (the base for the natural logarithm) and  value  is the argument passed to  exp.</p>
        <p id="p_10443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10443"> The syntax for the  exp  function is</p>
        <p id="p_10444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10444"> retval = exp (value);</p>
        <p id="p_10445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10445"> To retrieve  e  itself, pass  exp  the value 1.</p>
        <p id="p_10446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10446"> The log Function</p>
        <p id="p_10447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10447"> The  log  function takes a value and returns the natural (base  e)  logarithm of the value. The syntax for the  log  function is</p>
        <p id="p_10448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10448"> retval = log (value);</p>
        <p id="p_10449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10449"> The  log  function undoes  exp;  the expression</p>
        <p id="p_10450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10450"> $var = log (exp  ($var));</p>
        <p id="p_10451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10451"> always leaves  $var  with the value it started with (if you factor in round-off error).</p>
        <p id="p_10452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10452"> The abs Function</p>
        <p id="p_10453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10453"> The  abs  function returns the absolute value of a number. This is defined as follows: if a value is less than zero,  abs  negates it and returns the result.</p>
        <p id="p_10454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10454"> $result =  $abs(-3.5);  # returns 3.5</p>
        <p id="p_10455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10455"> Otherwise, the result is identical to the value:</p>
        <p id="p_10456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10456"> $result =  $abs(3.5);  # returns 3.5</p>
        <p id="p_10457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10457"> $result =  $abs(0);  # returns 0</p>
        <p id="p_10458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10458"> The syntax for the  abs  function is</p>
        <p id="p_10459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10459"> retval = abs (value);</p>
        <p id="p_10460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10460"> value  can be any number.</p>
        <p id="p_10461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10461"> The rand and srand Functions</p>
        <p id="p_10462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10462"> The  rand  and  srand  functions enable Perl programs to generate random numbers.</p>
        <p id="p_10463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10463"> The rand function is passed an integer value and generates a random floating-point number between 0 and the value.</p>
        <p id="p_10464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10464"> The syntax for the  rand  function is</p>
        <p id="p_10465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10465"> retval = rand (num);</p>
        <p id="p_10466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10466"> num  is the integer value passed to  rand,  and  retval  is a random floating-point number between 0 and the</p>
        <p id="p_10467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10467"> num.</p>
        <p id="p_10468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10468"> For example, the following statement generates a number between 0 and 10 and returns it in  $retval:</p>
        <p id="p_10469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10469"> $retval = rand (10);</p>
        <p id="p_10470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10470"> srand  initializes the random-number generator used by  rand.  This ensures that the random numbers generated are, in fact, random. (If you do not use  srand,  you'll get the same set of random numbers each time.)</p>
        <p id="p_10471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10471"> The syntax for the  srand  function is</p>
        <p id="p_10472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10472"> srand (value);</p>
        <p id="p_10473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10473"> srand  accepts an integer value as an argument; if no argument is supplied,  srand  calls the  time  function and uses its return value as the random-number seed.</p>
        <p id="p_10474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10474"> For an example that uses  rand  and  srand,  see the section titled "Returning a Value from a Subroutine" on Day 9 .</p>
        <p id="p_10475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10475"> NOTE</p>
        <p id="p_10476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10476"> The following values and functions return numbers that can make useful random-number seeds:</p>
        <p id="p_10477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10477"> • The system variable $ $ contains the process ID of the current program. (See  Day 17 ,  "System Variables," for more information on $$.)</p>
        <p id="p_10478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10478"> • t ime  returns the current time value.</p>
        <p id="p_10479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10479"> • Many of the functions described on  Day 15 ,  "System Functions," return useful values. For example,  getppid returns the process ID of the program's parent process.</p>
        <p id="p_10480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10480"> For best results, combine two or more of these using the | (bitwise OR)  operator</p>
        <p id="p_10481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10481"> String-Manipulation Functions</p>
        <p id="p_10482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10482"> This section describes the built-in Perl functions that manipulate character strings. These functions enable you to do the following:</p>
        <p id="p_10483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10483"> • Search for a substring in a character string</p>
        <p id="p_10484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10484"> • Create a string</p>
        <p id="p_10485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10485"> • Replace a substring within a string</p>
        <p id="p_10486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10486"> The index Function</p>
        <p id="p_10487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10487"> The  index  function provides a way of indicating the location of a substring in a string.</p>
        <p id="p_10488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10488"> The syntax for the  index  function is</p>
        <p id="p_10489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10489"> position = index (string, substring);</p>
        <p id="p_10490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10490"> string  is the character string to search in, and  substring  is the character string being searched for. position  returns the number of characters skipped before  substring  is located; if  substring  is not found,  position  is set to -1.</p>
        <p id="p_10491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10491"> Listing  13.7  is a program that uses  index  to locate a substring in a string.</p>
        <p id="p_10492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10492"> Type</p>
        <p id="p_10493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10493"> Listing 13.7.  A  program that uses the index function.</p>
        <p id="p_10494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10494"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10495"> 2  :</p>
        <p id="p_10496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10496"> 3: $input = &lt;STDIN&gt;;</p>
        <p id="p_10497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10497"> 4: $position = index($input, "the");</p>
        <p id="p_10498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10498"> 5: if ($position &gt;= 0) {</p>
        <p id="p_10499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10499"> 6: print ("pattern found at position $position\n");</p>
        <p id="p_10500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10500"> 7: } else {</p>
        <p id="p_10501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10501"> 8: print ("pattern not found\n");</p>
        <p id="p_10502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10502"> 9: }</p>
        <p id="p_10503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10503"> Output</p>
        <p id="p_10504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10504"> $ programl3 7</p>
        <p id="p_10505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10505"> Here is the input line I have typed, pattern found at position 8 $</p>
        <p id="p_10506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10506"> Analysis</p>
        <p id="p_10507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10507"> This program searches for the first occurrence of the word  the.  If it is found, the program prints the location of the pattern; if it is not found, the program prints  pattern not found.</p>
        <p id="p_10508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10508"> You can use the  index  function to find more than one copy of a substring in a string. To do this, pass a third argument to  index,  which tells it how many characters to skip before starting to search. For example:</p>
        <p id="p_10509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10509"> $position = index($line,  "foo", 5);</p>
        <p id="p_10510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10510"> This call to  index  skips five characters before starting to search for  foo  in the string stored in $  line.  As before, if  index  finds the substring, it returns the total number of characters skipped (including the number specified by the third argument to  index).  If  index  does not find the substring in the portion of the string that it searches, it returns -1.</p>
        <p id="p_10511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10511"> This feature of  index  enables you to find all occurrences of a substring in a string. Listing 13.8 is a modified version of Listing 13.7 that searches for all occurrences of  the  in an input line.</p>
        <p id="p_10512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10512"> Type</p>
        <p id="p_10513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10513"> Listing 13.8.  A  program that uses index to search a line repeatedly.</p>
        <p id="p_10514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10514"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10515"> 2  :</p>
        <p id="p_10516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10516"> 3: $input = &lt;STDIN&gt;;</p>
        <p id="p_10517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10517"> 4: $position = $found = 0;</p>
        <p id="p_10518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10518"> 5: while (1) {</p>
        <p id="p_10519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10519"> 6: $position = index($input, "the", $position);</p>
        <p id="p_10520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10520"> 7 :</p>
        <p id="p_10521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10521"> last if ($position == -1);</p>
        <p id="p_10522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10522"> if ($found == 0) {</p>
        <p id="p_10523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10523"> 8 :</p>
        <p id="p_10524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10524"> 9: $found = 1;</p>
        <p id="p_10525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10525"> 10: print ("pattern found - characters skipped:");</p>
        <p id="p_10526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10526"> 11 : }</p>
        <p id="p_10527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10527"> 12: print (" $position");</p>
        <p id="p_10528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10528"> 13: $position++;</p>
        <p id="p_10529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10529"> 14: }</p>
        <p id="p_10530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10530"> 15: if ($found == 0) {</p>
        <p id="p_10531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10531"> 16: print ("pattern not found\n");</p>
        <p id="p_10532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10532"> 17: } else {</p>
        <p id="p_10533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10533"> 18: print ("\n");</p>
        <p id="p_10534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10534"> 19: }</p>
        <p id="p_10535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10535"> Output</p>
        <p id="p_10536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10536"> $ programl3 8</p>
        <p id="p_10537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10537"> Here is the test line containing the words, pattern found - characters skipped: 8 33 $</p>
        <p id="p_10538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10538"> Analysis</p>
        <p id="p_10539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10539"> Line 6 of this program calls  index.  Because the initial value of  $position  is  0,  the first call to  index  starts searching from the beginning of the string. Eight charact-ers are skipped before the first occurrence of  the  is found; this means that  $position  is assigned  8.</p>
        <p id="p_10540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10540"> Line 7 tests whether a match has been found by comparing  $position  with -1, which is the value index  returns when it does not find the string for which it is looking. Because a match has been found, the loop continues to execute.</p>
        <p id="p_10541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10541"> When the loop iterates again, line 6 calls  index  again. This time,  index  skips nine characters before beginning the search again, which ensures that the previously found occurrence of  the  is skipped.  A  total of</p>
        <p id="p_10542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10542"> 33 bytes are skipped before the is found again. Once again, the loop continues, because the conditional expression in line 7 is false.</p>
        <p id="p_10543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10543"> On the final iteration of the loop, line 6 calls  index  and skips 34 characters before starting the search. This time,  the  is not found,  index  returns -1, and the conditional expression in line 7 is true. At this point, the loop terminates.</p>
        <p id="p_10544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10544"> NOTE</p>
        <p id="p_10545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10545"> To extract a substring found by  index,  use the  substr  function, which is described later in today's lesson</p>
        <p id="p_10546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10546"> The rindex Function</p>
        <p id="p_10547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10547"> The  rindex  function is similar to the  index  function. The only difference is that  rindex  starts searching from the right end of the string, not the left.</p>
        <p id="p_10548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10548"> The syntax for the  rindex  function is</p>
        <p id="p_10549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10549"> position = rindex (string, substring);</p>
        <p id="p_10550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10550"> This syntax is identical to the syntax for  index, string  is the character string to search in, and substring  is the character string being searched for.  position  returns the number of characters skipped before  substring  is located; if  substring  is not found,  position  is setto -1.</p>
        <p id="p_10551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10551"> The following is an example:</p>
        <p id="p_10552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10552"> $string = "Here is the test line containing the words.";</p>
        <p id="p_10553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10553"> $position = rindex($string, "the");</p>
        <p id="p_10554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10554"> In this example,  rindex  finds the second occurrence of  the.  As with  index, rindex  returns the number of characters between the left end of the string and the location of the found substring. In this case, 33 characters are skipped, and  $position  is assigned  33.</p>
        <p id="p_10555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10555"> You can specify a third argument to  rindex,  indicating the maximum number of characters that can be skipped. For example, if you want  rindex  to find the first occurrence of  the  in the preceding example, you can call it as follows:</p>
        <p id="p_10556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10556"> $string = "Here is the test line containing the words.";</p>
        <p id="p_10557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10557"> $position = rindex($string, "the",  32);</p>
        <p id="p_10558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10558"> Here, the second occurrence of  the  cannot be matched, because it is to the right of the specified limit of  32 skipped characters,  rindex,  therefore, finds the first occurrence of  the.  Because there are eight characters between the beginning of the string and the occurrence,  $position  is assigned  8.</p>
        <p id="p_10559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10559"> Like  index, rindex  returns  -1  if it cannot find the string it is looking for.</p>
        <p id="p_10560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10560"> The length Function</p>
        <p id="p_10561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10561"> The  length  function returns the number of characters contained in a character string.</p>
        <p id="p_10562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10562"> The syntax for the  length  function is</p>
        <p id="p_10563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10563"> num = length (string);</p>
        <p id="p_10564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10564"> string  is the character string for which you want to determine the length, and  num  is the returned length. Here is an example using  length:</p>
        <p id="p_10565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10565"> $string = "Here is a string";</p>
        <p id="p_10566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10566"> $strlen = length($string);</p>
        <p id="p_10567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10567"> In this example,  length  determines that the  string  in $  string  is 16 characters long, and it assigns  16 to $  strlen.</p>
        <p id="p_10568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10568"> Listing 13.9 is a program that calculates the average word length used in an input file. (This is sometimes used to determine the "complexity" of the text.) Numbers are skipped.</p>
        <p id="p_10569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10569"> Type</p>
        <p id="p_10570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10570"> Listing 13.9. A program that demonstrates the use of length.</p>
        <p id="p_10571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10571"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10572"> 2  :</p>
        <p id="p_10573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10573"> 3: $wordcount = $charcount = 0;</p>
        <p id="p_10574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10574"> 4: while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_10575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10575"> Output</p>
        <p id="p_10576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10576"> $ programl3 9</p>
        <p id="p_10577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10577"> Here is the test input.</p>
        <p id="p_10578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10578"> Here is the last line.</p>
        <p id="p_10579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10579"> A D</p>
        <p id="p_10580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10580"> Average word length: 3.5 $</p>
        <p id="p_10581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10581"> Analysis</p>
        <p id="p_10582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10582"> This program reads a line of input at a time from the standard input file, breaking the input line into words. Line 7 tests whether the word is a number, and skips it if it is. Line 8 strips any trailing punctuation character from the word, which ensures that the punctuation is not counted as part of the word length.</p>
        <p id="p_10583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10583"> Line 10 calls  length  to retrieve the number of characters in the word. This number is added to $ char count,  which contains the total number of characters in all of the words that have been read so far. To determine the average word length of the file, line 13 takes this value and divides it by the number of words in the file, which is stored in  $wordcount.</p>
        <p id="p_10584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10584"> Retrieving String Length Using tr</p>
        <p id="p_10585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10585"> The tr function provides another way of determining the length of a character string, in conjunction with the built-in system variable $_.</p>
        <p id="p_10586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10586"> The syntax for the  t  r function is</p>
        <p id="p_10587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10587"> tr/sourcelist/replacelist/</p>
        <p id="p_10588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10588"> sourcelist  is the list of characters to replace, and  replacelist  is the list of characters to replace with. (For details, see the following listing and the explanation provided with it.)</p>
        <p id="p_10589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10589"> Listing 13.10 shows how tr works.</p>
        <p id="p_10590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10590"> Type</p>
        <p id="p_10591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10591"> Listing 13.10.  A  program that uses tr to retrieve the length of a string.</p>
        <p id="p_10592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10592"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10593"> 2  :</p>
        <p id="p_10594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10594"> 3: $string = "here is a string";</p>
        <p id="p_10595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10595"> 4: $_ = $string;</p>
        <p id="p_10596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10596"> 5: $length = tr/a-zA-Z /a-zA-Z /;</p>
        <p id="p_10597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10597"> 6: print ("the string is $length characters long\n");</p>
        <p id="p_10598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10598"> Output</p>
        <p id="p_10599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10599"> $ programl3 10</p>
        <p id="p_10600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10600"> the string is 16 characters long $</p>
        <p id="p_10601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10601"> Analysis</p>
        <p id="p_10602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10602"> Line  3  of this program creates a string named  here is a string  and assigns it to the</p>
        <p id="p_10603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10603"> scalar variable $  string.  Line 4 copies this string into a built-in scalar variable, $_.</p>
        <p id="p_10604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10604"> Line  5  exploits two features of the  t r  operator that have not yet been discussed:</p>
        <p id="p_10605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10605"> • If the value to be translated is not explicitly specified by means of the =~ operator, tr assumes that the value is stored in $_.</p>
        <p id="p_10606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10606"> • t  r  returns the number of characters translated.</p>
        <p id="p_10607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10607"> In line 5, both the search pattern (the set of characters to look for) and the replacement pattern (the characters to replace them with) are the same. This pattern,  /a-zA-Z  /, tells  t  r  to search for all lowercase letters, uppercase letters, and blank spaces, and then replace them with themselves. This pattern matches every character in the string, which means that every character is being translated.</p>
        <p id="p_10608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10608"> Because every character is being translated, the number of characters translated is equivalent to the length of the string. This string length is assigned to the scalar variable $  length.</p>
        <p id="p_10609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10609"> tr can be used also to count the number of occurrences of a specific character, as shown in Listing 13.11.</p>
        <p id="p_10610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10610"> Listing 13.11.  A  program that uses tr to count the occurrences of specific characters.</p>
        <p id="p_10611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10611"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10612"> 2  :</p>
        <p id="p_10613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10613"> 3: $punctuation = $blanks = $total = 0;</p>
        <p id="p_10614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10614"> 4: while ($input = &lt;STDIN&gt;) {</p>
        <p id="p_10615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10615"> 5: chop ($input);</p>
        <p id="p_10616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10616"> 6: $total += length($input);</p>
        <p id="p_10617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10617"> 7: $_ = $input;</p>
        <p id="p_10618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10618"> 8: $punctuation += tr/, .-/;</p>
        <p id="p_10619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10619"> 9: $blanks += tr/ / /;</p>
        <p id="p_10620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10620"> 10 : }</p>
        <p id="p_10621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10621"> 11: print ("In this file, there are:\n") ;</p>
        <p id="p_10622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10622"> 12: print ("\t$punctuation punctuation characters,\n") ;</p>
        <p id="p_10623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10623"> 13: print 14: print 15: print</p>
        <p id="p_10624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10624"> ("\t$blanks blank characters,\n");</p>
        <p id="p_10625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10625"> ("\t",  $total - $punctuation - $blanks); (" other characters.\n");</p>
        <p id="p_10626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10626"> Output</p>
        <p id="p_10627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10627"> $ programl3 11</p>
        <p id="p_10628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10628"> Here is a line of input.</p>
        <p id="p_10629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10629"> This line, another line, contains punctuation. A D</p>
        <p id="p_10630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10630"> In this file, there are:</p>
        <p id="p_10631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10631"> 4 punctuation characters,</p>
        <p id="p_10632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10632"> 10 blank characters,</p>
        <p id="p_10633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10633"> 56 other characters.</p>
        <p id="p_10634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10634"> $</p>
        <p id="p_10635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10635"> This program uses the scalar variable  $total  and the built-in function  length  to count the total number of characters in the input file (excluding the trailing newline characters, which are removed by the call to  chop  in line 5).</p>
        <p id="p_10636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10636"> Lines 8 and 9 use tr to count the number of occurrences of particular characters. Line 8 replaces all punctuation characters with themselves; the number of replacements performed, and hence the number of punctuation characters found, is added to the total stored in  $punctuat ion.  Similarly, line 9 replaces all blanks with themselves and adds the number of blanks found to the total stored in  $blanks.  In both cases, t  r operates on the contents of the scalar variable $_, because the =~ operator has not been used to specify another value to translate.</p>
        <p id="p_10637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10637"> Line 14 uses  $total, $punctuat ion,  and  $blanks  to calculate the total number of characters that are not blank and not punctuation.</p>
        <p id="p_10638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10638"> Analysis</p>
        <p id="p_10639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10639"> NOTE</p>
        <p id="p_10640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10640"> Many other functions and operators accept $_ as the default variable on which to work. For example, lines 4-7 of this program also can be written as follows:</p>
        <p id="p_10641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10641"> while (&lt;STDIN&gt;) {</p>
        <p id="p_10642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10642"> chop () ;</p>
        <p id="p_10643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10643"> $total += length();</p>
        <p id="p_10644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10644"> For more information on $_, refer to  Day  17 ,  "System Variables.</p>
        <p id="p_10645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10645"> The pos Function</p>
        <p id="p_10646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10646"> The  pos  function, defined only in Perl 5, returns the location of the last pattern match in a string. It is ideal for use when repeated pattern matches are specified using the  g  (global) pattern-matching operator.</p>
        <p id="p_10647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10647"> The syntax for the  pos  function is</p>
        <p id="p_10648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10648"> offset = pos(string);</p>
        <p id="p_10649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10649"> string  is the string whose pattern is being matched,  offset  is the number of characters already matched or skipped.</p>
        <p id="p_10650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10650"> Listing 13.12 illustrates the use of  pos.</p>
        <p id="p_10651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10651"> Type</p>
        <p id="p_10652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10652"> Listing 13.12. A program that uses pos to display pattern match positions.</p>
        <p id="p_10653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10653"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10654"> 2  :</p>
        <p id="p_10655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10655"> 3: $string = "Mississippi";</p>
        <p id="p_10656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10656"> 4: while ($string =~ /i/g) {</p>
        <p id="p_10657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10657"> 5: $position = pos($string);</p>
        <p id="p_10658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10658"> 6:</p>
        <p id="p_10659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10659"> print("matched at position $position\n");</p>
        <p id="p_10660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10660"> 7: }</p>
        <p id="p_10661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10661"> Output</p>
        <p id="p_10662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10662"> $ programl3 12</p>
        <p id="p_10663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10663"> Analysis</p>
        <p id="p_10664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10664"> This program loops every time an  i  in  Mi s s i s s ippi  is matched. The number displayed by line 6 is the number of characters to skip to reach the point at which pattern matching resumes. For example, the first  i  is the second character in the string, so the second pattern search starts at position  2.</p>
        <p id="p_10665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10665"> NOTE</p>
        <p id="p_10666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10666"> You can also use  pos  to change the position at which pattern matching is to resume. To do this, put the call to  pos  on the left side of an assignment:</p>
        <p id="p_10667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10667"> pos($string) = 5;</p>
        <p id="p_10668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10668"> This tells the Perl interpreter to start the next pattern search with the sixth character in the string. (To restart searching from the beginning, use 0.</p>
        <p id="p_10669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10669"> The substr Function</p>
        <p id="p_10670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10670"> The  substr  function lets you assign a part of a character string to a scalar variable (or to a component of an array variable).</p>
        <p id="p_10671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10671"> The syntax for calls to the  substr  function is</p>
        <p id="p_10672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10672"> substr (expr, skipchars, length)</p>
        <p id="p_10673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10673"> expr  is the character string from which a substring is to be copied; this character string can be the value stored in a variable or the value resulting from the evaluation of an expression,  skipchars  is the number of characters to skip before starting copying,  length  is the number of characters to copy;  length  can be omitted, in which case the rest of the string is copied.</p>
        <p id="p_10674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10674"> Listing 13.13 provides a simple example of  substr.</p>
        <p id="p_10675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10675"> Type</p>
        <p id="p_10676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10676"> Listing 13.13. A program that demonstrates the use of substr.</p>
        <p id="p_10677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10677"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10678"> 2  :</p>
        <p id="p_10679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10679"> 3: $string = "This is a sample character string";</p>
        <p id="p_10680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10680"> 4: $subl = substr ($string, 10, 6);</p>
        <p id="p_10681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10681"> 5: $sub2 = substr ($string, 17);</p>
        <p id="p_10682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10682"> 6: print ("\$subl is \"$subl\"\n\$sub2 is \"$sub2\"\n");</p>
        <p id="p_10683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10683"> Output</p>
        <p id="p_10684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10684"> $ programl3 13 $subl is "sample"</p>
        <p id="p_10685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10685"> $sub2 is "character string" $</p>
        <p id="p_10686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10686"> Analysis</p>
        <p id="p_10687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10687"> Line 4 calls  substr,  which copies a portion of the string stored in $  st  r  ing.  This call specifies that ten characters are to be skipped before copying starts, and that a total of six characters are to be copied. This means that the substring  sample  is copied and stored in  $subl.</p>
        <p id="p_10688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10688"> Line 5 is another call to  substr.  Here, 17 characters are skipped. Because the length field is omitted, substr  copies the remaining characters in the string. This means that the substring  character</p>
        <p id="p_10689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10689"> string  is copied and stored in  $sub2.</p>
        <p id="p_10690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10690"> Note that lines 4 and 5 do not change the contents of $  string.</p>
        <p id="p_10691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10691"> String Insertion Using substr</p>
        <p id="p_10692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10692"> In Listing 13.13, which you've just seen, calls to substr appear to the right of the assignment operator =. This means that the return value from substr-the extracted substring-is assigned to the variable appearing to the left of the =.</p>
        <p id="p_10693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10693"> Calls to  substr  can appear also on the left of the assignment operator =. In this case, the portion of the string specified by  substr  is  replaced  by the value appearing to the right of the assignment operator.</p>
        <p id="p_10694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10694"> The syntax for these calls to  substr  is basically the same as before:</p>
        <p id="p_10695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10695"> substr (expr, skipchars, length) = newval;</p>
        <p id="p_10696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10696"> Here,  expr  must be something that can be assigned to-for example, a scalar valuable or an element of an array variable,  skipchars  represents the number of characters to skip before beginning the overwriting operation, which cannot be greater than the length of the string,  length  is the number of characters to be replaced by the overwriting operation. If  length  is not specified, the remainder of the string is replaced.</p>
        <p id="p_10697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10697"> newval  is the string that replaces the substring specified by  skipchars  and  length.  If  newval  is larger than  length,  the character string automatically grows to hold it, and the rest of the string is pushed aside (but  not  overwritten). If  newval  is smaller than  length,  the character string automatically shrinks. Basically, everything appears where it is supposed to without you having to worry about it.</p>
        <p id="p_10698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10698"> NOTE</p>
        <p id="p_10699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10699"> By the way, things that can be assigned to are sometimes known as lvalues,  because they appear to the left of assignment statements (the / in  lvalue  stands for "left"). Things that appear to the right of assignment statements are, similarly, called  r\&gt;alues.</p>
        <p id="p_10700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10700"> This book does not use the terms  lvalue  and  r\’alue,  but you might find that knowing them will prove useful when you read other books on programming languages</p>
        <p id="p_10701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10701"> Listing 13.14 is an example of a program that uses  substr  to replace portions of a string.</p>
        <p id="p_10702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10702"> Listing 13.14. A program that replaces parts of a string using substr.</p>
        <p id="p_10703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10703"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10704"> 2  :</p>
        <p id="p_10705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10705"> 9: print ("$string\n");</p>
        <p id="p_10706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10706"> string";</p>
        <p id="p_10707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10707"> Output</p>
        <p id="p_10708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10708"> $ programl3 14</p>
        <p id="p_10709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10709"> Behold! This is the sample string. $</p>
        <p id="p_10710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10710"> Analysis</p>
        <p id="p_10711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10711"> This program illustrates the many ways you can use  substr  to replace portions of a string.</p>
        <p id="p_10712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10712"> The call to  subst  r in line 4 specifies that no characters are to be skipped before overwriting, and that four characters in the original string are to be overwritten. This means that the substring  Here  is replaced by This,  and that the following is the new value of the string stored in $  string:</p>
        <p id="p_10713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10713"> This is a sample character string</p>
        <p id="p_10714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10714"> Similarly, the call to  substr  in line 5 specifies that eight characters are to be skipped and one character is to be replaced. This means that the word  a  is replaced by  the.  Now, $  string  contains the following:</p>
        <p id="p_10715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10715"> This is the sample character string</p>
        <p id="p_10716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10716"> Note that the character string is now larger than the original, because the new substring,  the,  is larger than the substring it replaced.</p>
        <p id="p_10717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10717"> Line  6  is an example of a call to  substr  that shrinks the string. Here,  19  characters are skipped, and the rest of the string is replaced by the substring  string  (because no  length  field has been specified). Now, the following is the value stored in  $string:</p>
        <p id="p_10718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10718"> This is the sample string</p>
        <p id="p_10719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10719"> In line 7, the call to  substr  is passed -1 in the  skipchars  field and is passed 1 in the  length  field. This tells  substr  to replace the last character of the string with the substring  g .  (g  followed by a period). $ string  now contains</p>
        <p id="p_10720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10720"> This is the sample string.</p>
        <p id="p_10721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10721"> NOTE</p>
        <p id="p_10722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10722"> If  substr  is passed a  skipchars  value of  -n,  where  n  is a positive integer,  substr  skips to  n  characters from the right end of the string. For example, the following call replaces the last two characters in $  string  with the string  hello:</p>
        <p id="p_10723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10723"> substr($string, -2, 2) = "hello"</p>
        <p id="p_10724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10724"> Finally, line 8 specifies that no characters are to be skipped and no characters are to be replaced. This means that the substring  "Behold ! " (including a trailing space) is added to the front of the existing string and that $  string  now contains the following:</p>
        <p id="p_10725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10725"> Behold! This is the sample string.</p>
        <p id="p_10726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10726"> Line  9  prints this final value of $  string.</p>
        <p id="p_10727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10727"> If you are a C programmer and are used to manipulating strings using pointers, note that  substr  with a length field of 1 can be used to simulate pointer-like behavior in Perl.</p>
        <p id="p_10728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10728"> For example, you can simulate the C statement</p>
        <p id="p_10729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10729"> char = *str++;</p>
        <p id="p_10730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10730"> as follows in Perl:</p>
        <p id="p_10731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10731"> $char = substr ($str, $offset++, 1);</p>
        <p id="p_10732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10732"> You'll need to define a counter valuable (such as  $of f set)  to keep track of where you are in the string. However, this is no more of a chore than remembering to initialize your C pointer variable.</p>
        <p id="p_10733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10733"> You can simulate the following C statement:</p>
        <p id="p_10734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10734"> *str++ = char;</p>
        <p id="p_10735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10735"> by assigning values using  substr  in the same way:</p>
        <p id="p_10736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10736"> substr($str, $offset++, 1) = $char;</p>
        <p id="p_10737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10737"> You shouldn't use  substr  in this way unless you really have to. Perl supplies more powerful and useful tools, such as pattern matching and substitution, to get the job done more efficiently</p>
        <p id="p_10738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10738"> The study Function</p>
        <p id="p_10739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10739"> The  study  function is a special function that tells the Perl interpreter that the specified scalar variable is about to be searched many times.</p>
        <p id="p_10740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10740"> The syntax for the  study  function is</p>
        <p id="p_10741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10741"> study (scalar);</p>
        <p id="p_10742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10742"> scalar  is the scalar variable to be "studied." The Perl interpreter takes the value stored in the specified scalar variable and represents it in an internal format that allows faster access.</p>
        <p id="p_10743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10743"> For example:</p>
        <p id="p_10744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10744"> study ($myvar) ;</p>
        <p id="p_10745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10745"> Here, the value stored in the scalar variable  $myvar  is about to be repeatedly searched.</p>
        <p id="p_10746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10746"> You can call  study  for only one scalar variable at a time. Previous calls to  study  are superseded if study  is called again.</p>
        <p id="p_10747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10747"> TIP</p>
        <p id="p_10748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10748"> To check whether  study  actually makes your program more efficient, use the function  times,  which displays the user and CPU times for a program or program fragment,  (times  is discussed earlier today.</p>
        <p id="p_10749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10749"> Case Conversion Functions</p>
        <p id="p_10750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10750"> Perl 5 provides functions that perform case conversion on strings. These are</p>
        <p id="p_10751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10751"> • The  1 c  function, which converts a string to lowercase</p>
        <p id="p_10752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10752"> • The uc function, which converts a string to uppercase</p>
        <p id="p_10753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10753"> • The  1 cf irst  function, which converts the first character of a string to lowercase</p>
        <p id="p_10754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10754"> • The  ucf irst  function, which converts the first character of a string to uppercase</p>
        <p id="p_10755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10755"> The lc and uc Functions</p>
        <p id="p_10756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10756"> The syntax for the lc and uc functions is</p>
        <p id="p_10757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10757"> retval = lc (string); retval = uc(string);</p>
        <p id="p_10758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10758"> string  is the string to be converted,  retval  is a copy of the string, converted to either lowercase or uppercase:</p>
        <p id="p_10759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10759"> The lcf irst and ucf irst Functions</p>
        <p id="p_10760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10760"> The syntax for the  lcf irst  and  ucf irst  functions is</p>
        <p id="p_10761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10761"> retval = lcfirst(string);</p>
        <p id="p_10762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10762"> retval = ucfirst(string) ;</p>
        <p id="p_10763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10763"> string  is the string whose first character is to be converted,  retval  is a copy of the string, with the first character converted to either lowercase or uppercase:</p>
        <p id="p_10764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10764"> $lower = lcfirst("HELLO"); # $lower is assigned "hELLO"</p>
        <p id="p_10765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10765"> $upper = ucfirst("hello"); # $upper is assigned "Hello"</p>
        <p id="p_10766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10766"> The quotemeta Function</p>
        <p id="p_10767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10767"> The  quotemeta  function, defined only in Perl 5, places a backslash character in front of any non-word character in a string. The following statements are equivalent:</p>
        <p id="p_10768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10768"> $string = quotemeta($string) ; $string =~  s/(\W)/\\$l/g;</p>
        <p id="p_10769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10769"> The syntax for  quotemeta  is</p>
        <p id="p_10770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10770"> newstring = quotemeta(oldstring) ;</p>
        <p id="p_10771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10771"> oldstring  is the string to be converted,  newstring  is the string with backslashes added.</p>
        <p id="p_10772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10772"> quotemeta  is useful when a string is to be used in a subsequent pattern-matching operation. It ensures that there are no characters in the string which are to be treated as special pattern-matching characters.</p>
        <p id="p_10773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10773"> The join Function</p>
        <p id="p_10774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10774"> The  join  function has been used many times in this book. It takes the elements of a list and converts them into a single character string.</p>
        <p id="p_10775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10775"> The syntax for the  join  function is</p>
        <p id="p_10776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10776"> join (joinstr,  list);</p>
        <p id="p_10777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10777"> joinstr  is the character string that is to be used to glue the elements of  list  together.</p>
        <p id="p_10778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10778"> For example:</p>
        <p id="p_10779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10779"> @list = ("Here", "is", "a", "list");</p>
        <p id="p_10780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10780"> $newstr = join @list);</p>
        <p id="p_10781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10781"> After  join  is called, the value stored in  $newstr  becomes the following string:</p>
        <p id="p_10782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10782"> Here::is::a::list</p>
        <p id="p_10783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10783"> The join string, : : in this case, appears between each pair of joined elements. The most common join string is a single blank space; however, you can use any value as the join string, including the value resulting from an expression.</p>
        <p id="p_10784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10784"> The sprint f Function</p>
        <p id="p_10785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10785"> The  sprint f  function behaves like the  print f  function defined on  Day  11 ,  "Formatting Your Output," except that the formatted string is returned by the function instead of being written to a file. This enables you to assign the string to another variable.</p>
        <p id="p_10786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10786"> The syntax for the  sprint f  function is</p>
        <p id="p_10787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10787"> sprintf (string, fields);</p>
        <p id="p_10788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10788"> string  is the character string to print, and  fields  is a list of values to substitute into the string. Listing  13.15  is an example that uses  sprintf  to build a string.</p>
        <p id="p_10789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10789"> Type</p>
        <p id="p_10790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10790"> Listing 13.15. A program that uses sprintf.</p>
        <p id="p_10791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10791"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10792"> 2  :</p>
        <p id="p_10793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10793"> 3:</p>
        <p id="p_10794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10794"> $num = 26;</p>
        <p id="p_10795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10795"> 4: $outstr = sprintf("%d = %x hexadecimal or %o octal\n",</p>
        <p id="p_10796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10796"> 5: $num, $num, $num);</p>
        <p id="p_10797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10797"> 6: print ($outstr);</p>
        <p id="p_10798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10798"> Output</p>
        <p id="p_10799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10799"> $ programl4_9</p>
        <p id="p_10800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10800"> 26 = la hexadecimal or 32 octal $</p>
        <p id="p_10801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10801"> Analysis</p>
        <p id="p_10802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10802"> I Lines 4 and 5 take three copies of the value stored in $num and include them as part of a string. The field specifiers  %d, %x,  and  %o  indicate how the values are to be formatted.</p>
        <p id="p_10803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10803"> %d  Indicates an integer displayed in the usual decimal (base-10) format %x Indicates an integer displayed in hexadecimal (base-16) format</p>
        <p id="p_10804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10804"> %o  Indicates an integer displayed in octal (base-8) format</p>
        <p id="p_10805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10805"> The created string is returned by  spr int f . Once it has been created, it behaves just like any other Perl character string; in particular, it can be assigned to a scalar variable, as in this example. Here, the string containing the three copies of  $num  is assigned to the scalar variable  $out str.  Line 6 then prints this string.</p>
        <p id="p_10806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10806"> NOTE</p>
        <p id="p_10807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10807"> For more information on field specifiers or on how  pr int f  works, refer to  Day 11 ,  which lists the field specifiers defined and provides a description of the syntax of  print f</p>
        <p id="p_10808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10808"> Summary</p>
        <p id="p_10809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10809"> Today, you learned about three types of built-in Perl functions: functions that handle process and program control, functions that perform mathematical operations, and functions that manipulate strings.</p>
        <p id="p_10810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10810"> With the process- and program-control functions, you can start new processes, stop the current program or</p>
        <p id="p_10811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10811"> other processes, or temporarily halt the current program. You also can create a pipe that sends data from one of your created processes to another.</p>
        <p id="p_10812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10812"> With the functions that perform mathematical operations, you can obtain the sine, cosine, and arctangent of a value. You also can calculate the natural logarithm and square root of a value, or use the value as an exponent of base  e.</p>
        <p id="p_10813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10813"> You also can generate random numbers and define the seed to use when generating the numbers.</p>
        <p id="p_10814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10814"> Functions that search character strings include  index,  which searches for a substring starting from the left of a string, and  rindex,  which searches for a substring starting from the right of a string. You can retrieve the length of a character string using  length.  By using the translate operator  tr  in conjunction with the system variable $_, you can count the number of occurrences of a particular character or set of characters in a string. The  pos  function enables you to determine or set the current pattern-matching location in a string.</p>
        <p id="p_10815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10815"> The function  substr  enables you to extract a substring from a string and use it in an expression or assignment statement,  substr  also can be used to replace a portion of a string or append to the front or back end of the string.</p>
        <p id="p_10816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10816"> The  lc  and  uc  functions convert strings to lowercase or uppercase. To convert the first letter of a string to lowercase  or  uppercase, use  lcfirst orucfirst.</p>
        <p id="p_10817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10817"> quotemeta  places a backslash in front of every non-word character in a string.</p>
        <p id="p_10818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10818"> You can create new character strings using  join  and  sprint f. join  creates a string by joining elements of a list, and  sprint f  builds a string using field specifiers that specify the string format.</p>
        <p id="p_10819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10819"> Q&amp;A</p>
        <p id="p_10820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10820"> Q: How does Perl generate random numbers?</p>
        <p id="p_10821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10821"> A: Basically, by performing arithmetic operations using very large numbers. If the numbers for these</p>
        <p id="p_10822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10822"> arithmetic operations are carefully chosen, a sequence of "pseudo-random" numbers can be generated by repeating the set of arithmetic operations and returning their results.</p>
        <p id="p_10823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10823"> The random-number seed provided by  srand  supplies the initial value for one of the numbers used in the set of arithmetic operations. This ensures that the sequence of pseudo-random numbers starts with a different result each time.</p>
        <p id="p_10824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10824"> Q: What programs can be called using system?</p>
        <p id="p_10825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10825"> A: Any program that you can run from your terminal can be run using  system.</p>
        <p id="p_10826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10826"> Q: How many processes can a program create using fork?</p>
        <p id="p_10827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10827"> A: Perl provides no limit on how many processes can be created at a time. However, the performance</p>
        <p id="p_10828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10828"> of your system will be adversely affected if you generate too many processes at once. In particular, programs that call  fork  and wind up in an infinite loop are sometimes called fork bombs, because they generate thousands of processes and grind your machine to an effective halt. (Your system administrator will not be pleased with you if you do this!)</p>
        <p id="p_10829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10829"> Q: How can I send signals to a process without killing it?</p>
        <p id="p_10830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10830"> A: The  kill  function actually can send any signal supported by your machine to any running process</p>
        <p id="p_10831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10831"> (that you can access).</p>
        <p id="p_10832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10832"> Refer to the UNIX system documentation for details on the signals you can send and what their names are.</p>
        <p id="p_10833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10833"> Q: What is the difference between the %d and %ld format specifiers in sprintf ?</p>
        <p id="p_10834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10834"> A:  %ld  defines a "long integer." It refers to the largest number of bits that your local machine can use</p>
        <p id="p_10835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10835"> to store an integer. (This is often  32  bits.)  %d,  on the other hand, is equivalent to your machine's standard integer format. On some machines,  %ld  and  %d  are equivalent. If you are not sure how many bits your machine uses to store integers, or you know you are going to be dealing with large numbers, it's safer to use  %ld.  (The same holds true for all other integer formats, such as  %lx  and % lo.)</p>
        <p id="p_10836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10836"> Q: What is the difference between the %c and %s format specifiers in sprintf?</p>
        <p id="p_10837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10837"> A:  %c  undoes the effect of the  ord  function. It converts a scalar value into the equivalent ASCII</p>
        <p id="p_10838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10838"> character. (Its behavior is similar to that of the  chr  function in Pascal.)  %s  treats a scalar value as a character string and inserts it into the string at the place specified.</p>
        <p id="p_10839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10839"> Workshop</p>
        <p id="p_10840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10840"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_10841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10841"> Quiz</p>
        <p id="p_10842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10842"> 1. What do these functions do?</p>
        <p id="p_10843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10843"> 2.  Explain the differences between  fork, system,  and  exec.</p>
        <p id="p_10844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10844"> 3. Explain the differences between  wait  and  waitpid.</p>
        <p id="p_10845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10845"> 4. How can you obtain the value of  pi</p>
        <p id="p_10846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10846"> 5. How can you obtain the value of the mathematical constant  e?</p>
        <p id="p_10847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10847"> 6.  What  sprintf  specifiers produce the following?</p>
        <p id="p_10848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10848"> a. A hexadecimal number</p>
        <p id="p_10849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10849"> b. An octal number</p>
        <p id="p_10850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10850"> c. A floating-point number in exponential format</p>
        <p id="p_10851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10851"> d. A floating-point number in standard (fixed) format</p>
        <p id="p_10852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10852"> 7. If the scalar variable  $string  contains  abcdefgh,  what do the following calls return?</p>
        <p id="p_10853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10853"> 8.  Assume $  string  contains the value  abcdabcd.  What value is returned by each of the following calls?</p>
        <p id="p_10854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10854"> a.</p>
        <p id="p_10855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10855"> index ($string,  "be");</p>
        <p id="p_10856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10856"> 9. Assume $  string  contains the value  abcdabcd\n  (the last character being a trailing newline character). What is returned  in$retvalby  the following?</p>
        <p id="p_10857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10857"> a. $_ = $string; $retval = tr/ab/ab/;</p>
        <p id="p_10858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10858"> b. $retval = length ($string);</p>
        <p id="p_10859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10859"> Exercises</p>
        <p id="p_10860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10860"> 1. Write a program that uses  fork  and  waitpid  to generate a total of three processes (including the program). Have each process print a line, and have the lines appear in a specified order.</p>
        <p id="p_10861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10861"> 2. Write a program that reads input from a file named  temp  and writes it to the standard output file. Write another program that reads input from the standard output file, writes it to  temp,  and uses exec  to call the first program.</p>
        <p id="p_10862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10862"> 3. Write a program that prints the natural logarithm of the integers between 1 and 100.</p>
        <p id="p_10863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10863"> 4. Write a program that computes the sum of the numbers from 1 to  10  * *  n  for values of  n  from 1 to  6.  For each computed value, use  times  to calculate the amount of time each computation takes. Print these calculation times.</p>
        <p id="p_10864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10864"> 5. Write a program that reads an integer value and prints the sine, cosine, and tangent of the value. Assume that the input value is in degrees.</p>
        <p id="p_10865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10865"> 6.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_10866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10866"> #!/usr/local/bin/perl</p>
        <p id="p_10867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10867"> print ("Here is a line of output. "); system (  "w");</p>
        <p id="p_10868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10868"> print ("Here is the rest of the line.\n");</p>
        <p id="p_10869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10869"> 7. Write a program that uses  index  to print out the locations of the letters  a, e, i, o,  and  u  in an input line.</p>
        <p id="p_10870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10870"> 8.  Write a program that uses  r index  to do the same thing as the one in Exercise 1.</p>
        <p id="p_10871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10871"> 9. Write a program that uses  subst r  to do the same thing as the one in Exercise 1. (Hint: This will require many calls to  subst r!)</p>
        <p id="p_10872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10872"> 10. Write a program that uses  t r  to count all the occurrences of  a, e, i, o,  and  u  in an input line.</p>
        <p id="p_10873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10873"> 11. Write a program that reads a number. If the number is a floating-point value, print it in exponential and fixed-point form. If the number is an integer, print it in decimal, octal, and hexadecimal form. (Hint: Recall that  print f  and  sprint f  use the same field specifiers.)</p>
        <p id="p_10874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10874"> 12.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_10875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10875"> #!/usr/local/bin/perl</p>
        <p id="p_10876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10876"> $mystring = &lt;STDIN&gt;;</p>
        <p id="p_10877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10877"> $lastfound = length ($mystring);</p>
        <p id="p_10878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10878"> while ($lastfound != -1) {</p>
        <p id="p_10879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10879"> $lastfound = index($mystring,  "xyz",  $lastfound);</p>
        <p id="p_10880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10880"> }</p>
        <p id="p_10881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10881"> Chapter 14</p>
        <p id="p_10882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10882"> Scalar-Conversion and List-Manipulation Functions</p>
        <p id="p_10883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10883"> CONTENTS</p>
        <p id="p_10884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10884"> •  The chop Function</p>
        <p id="p_10885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10885"> •  The chomp Function</p>
        <p id="p_10886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10886"> •  The crypt Function</p>
        <p id="p_10887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10887"> •  The hex Function</p>
        <p id="p_10888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10888"> •  The int Function</p>
        <p id="p_10889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10889"> •  The oct Function</p>
        <p id="p_10890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10890"> o  The oct Function and Hexadecimal Integers</p>
        <p id="p_10891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10891"> •  The ord and chr Functions</p>
        <p id="p_10892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10892"> •  The  scalar Function</p>
        <p id="p_10893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10893"> •  The pack Function</p>
        <p id="p_10894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10894"> o  The pack Function and C Data Types</p>
        <p id="p_10895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10895"> •  The unpack Function</p>
        <p id="p_10896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10896"> o  Unpacking Strings o  Skipping Characters When Unpacking</p>
        <p id="p_10897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10897"> o  The unpack Function and uuencode</p>
        <p id="p_10898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10898"> •  The  vec Function</p>
        <p id="p_10899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10899"> •  The defined Function</p>
        <p id="p_10900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10900"> •  The undef Function</p>
        <p id="p_10901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10901"> •  Array and List Functions</p>
        <p id="p_10902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10902"> o  The qrep Function o  The splice Function o  The shift Function o  The un shift Function</p>
        <p id="p_10903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10903"> o  The push Function o  The pop Function o  Creating Stacks and Queues</p>
        <p id="p_10904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10904"> o  The split Function o  The.  sort and reverse Functions</p>
        <p id="p_10905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10905"> o  The map Function o  The wantarray Function</p>
        <p id="p_10906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10906"> •  Associative Array Functions</p>
        <p id="p_10907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10907"> o  The keys Function The values Function</p>
        <p id="p_10908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10908"> o</p>
        <p id="p_10909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10909"> o</p>
        <p id="p_10910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10910"> The  each  Function o  The  delete  Function o  The  exists  Function</p>
        <p id="p_10911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10911"> •  Summary</p>
        <p id="p_10912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10912"> •  Q&amp;A</p>
        <p id="p_10913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10913"> •  Workshop</p>
        <p id="p_10914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10914"> o  Quiz o  Exercises</p>
        <p id="p_10915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10915"> Today, you learn about the built-in Perl functions that convert scalar values from one form to another, and the Perl functions that deal with variables that have not had values defined for them.</p>
        <p id="p_10916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10916"> You also learn about the built-in Perl functions that manipulate lists and array variables. These functions are divided into two groups:</p>
        <p id="p_10917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10917"> • The functions that manipulate standard array variables and their lists</p>
        <p id="p_10918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10918"> • The functions that manipulate associative arrays</p>
        <p id="p_10919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10919"> Warning</p>
        <p id="p_10920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10920"> Many of the functions described in today's lesson use features of the UNIX operating system. If you are using Perl on a machine that is not running UNIX, some of these functions might not be defined or might behave differently.</p>
        <p id="p_10921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10921"> Check the documentation supplied with your version of Perl for details on which functions are supported or emulated on your machine</p>
        <p id="p_10922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10922"> The chop Function</p>
        <p id="p_10923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10923"> The  chop  function was first discussed on  Day  3 ,  "Understanding Scalar Values." It removes the last character from a scalar value.</p>
        <p id="p_10924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10924"> The syntax for the  chop  function is</p>
        <p id="p_10925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10925"> chop  (var);</p>
        <p id="p_10926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10926"> var  can be either a scalar value or a list, as described in the following paragraphs.</p>
        <p id="p_10927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10927"> For example:</p>
        <p id="p_10928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10928"> $mystring = "This is a string"; chop ($mystring);</p>
        <p id="p_10929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10929"> # $mystring now contains "This is a strin";</p>
        <p id="p_10930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10930"> chop  is used most frequently to remove the trailing newline character from an input line, as follows:</p>
        <p id="p_10931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10931"> $input = &lt;STDIN&gt;; chop ($input);</p>
        <p id="p_10932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10932"> The argument passed to  chop  can also be a list. In this case,  chop  removes the last character from every element of the list. For example, to read an entire input file into an array variable and remove all of the trailing newline characters, use the following statements:</p>
        <p id="p_10933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10933"> @input = &lt;STDIN&gt;; chop (@input);</p>
        <p id="p_10934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10934"> chop  returns the character chopped. For example:</p>
        <p id="p_10935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10935"> $input = "12345";</p>
        <p id="p_10936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10936"> $lastchar = chop ($input);</p>
        <p id="p_10937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10937"> This call to  chop  assigns  5  to the scalar variable $  last char.</p>
        <p id="p_10938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10938"> If  chop  is passed a list, the last character from the last element of the list is returned:</p>
        <p id="p_10939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10939"> @array = ("ab", "cd",  "ef");</p>
        <p id="p_10940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10940"> $lastchar = chop(@array);</p>
        <p id="p_10941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10941"> This assigns  f , the last character of the last element of  @array,  to  $lastchar.</p>
        <p id="p_10942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10942"> The chomp Function</p>
        <p id="p_10943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10943"> The  chomp  function, defined only in Perl 5, checks whether the last characters of a string or list of strings match the input line separator defined by the $/ system variable. If they do,  chomp  removes them.</p>
        <p id="p_10944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10944"> The syntax for the  chomp  function is</p>
        <p id="p_10945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10945"> result = chomp(var)</p>
        <p id="p_10946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10946"> As in the  chop  function,  var  can be either a scalar variable or a list. If  var  is a list, each element of the list is checked for the input end-of-line string,  result  is the total number of characters removed by</p>
        <p id="p_10947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10947"> chomp.</p>
        <p id="p_10948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10948"> Listing 14.1 shows how  chomp  works.</p>
        <p id="p_10949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10949"> Output</p>
        <p id="p_10950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10950"> $ programl4_l testing 2</p>
        <p id="p_10951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10951"> testl test2 test3 4 $</p>
        <p id="p_10952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10952"> Analysis</p>
        <p id="p_10953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10953"> This program uses  chomp  to remove the input line separator from both a scalar variable and an array variable. The call to  chomp  in line 5 converts the value of  $scalar  from  testing:  : to testing.  The number of characters removed, 2, is returned by  chomp  and assigned to  $num.</p>
        <p id="p_10954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10954"> The call to  chomp  in line 8 checks each element of  @list.  The first element is converted from testl:  : to  testl,  and the last element is converted from  t e s 13  : : to  t e s 13 . (The second element is ignored, because it is not terminated by the end-of-line specifier.) The total number of characters removed, 4 (two from the first element and two from the last), is returned by  chomp  and assigned to</p>
        <p id="p_10955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10955"> $num.</p>
        <p id="p_10956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10956"> NOTE</p>
        <p id="p_10957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10957"> For more information on the $ / system variable, refer to  Day  17 , "System Variables.</p>
        <p id="p_10958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10958"> The crypt Function</p>
        <p id="p_10959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10959"> The  crypt  function encrypts a string using the NBS Data Encryption Standard (DES) algorithm. The syntax for the  crypt  function is</p>
        <p id="p_10960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10960"> result = crypt (original, salt);</p>
        <p id="p_10961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10961"> original  is the string to be encrypted, and  salt  is a character string of two characters that defines how to change the DES algorithm (to make it more difficult to decode). These two characters can be any letter or digit, or one of the . and / characters. After the algorithm is changed, the string is encrypted using the resulting key.</p>
        <p id="p_10962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10962"> result  is the encrypted string. The first two characters of  result  are the two characters specified in</p>
        <p id="p_10963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10963"> salt.</p>
        <p id="p_10964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10964"> You can use  crypt  to set up a password checker similar to those used by the UNIX login. Listing 14.2 is an example of a program that prompts the user for a password and compares it with a password stored in a special file.</p>
        <p id="p_10965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10965"> Listing 14.2. A program that asks for and compares a password.</p>
        <p id="p_10966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10966"> 1: #!/usr/local/bin/perl</p>
        <p id="p_10967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10967"> 2 :</p>
        <p id="p_10968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10968"> 3: open (PASSWD, "/u/jqpublic/passwd") ||</p>
        <p id="p_10969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10969"> 4: die ("Can't open password file");</p>
        <p id="p_10970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10970"> 5: $passwd = &lt;PASSWD&gt;;</p>
        <p id="p_10971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10971"> 6: chop ($passwd);</p>
        <p id="p_10972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10972"> 7: close (PASSWD);</p>
        <p id="p_10973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10973"> 8: print ("Enter the password for this program:\n");</p>
        <p id="p_10974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10974"> 9: system ("stty -echo");</p>
        <p id="p_10975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10975"> 10: $mypasswd = &lt;STDIN&gt;;</p>
        <p id="p_10976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10976"> 11: system ("stty echo");</p>
        <p id="p_10977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10977"> 12: chop ($mypasswd);</p>
        <p id="p_10978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10978"> 13: if (crypt ($mypasswd, substr($passwd, 0, 2)) eq $passwd) { 14: print ("Correct! Carry on!\n");</p>
        <p id="p_10979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10979"> 15: } else {</p>
        <p id="p_10980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10980"> 16 :</p>
        <p id="p_10981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10981"> die ("Incorrect password: goodbye!\n");</p>
        <p id="p_10982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10982"> Output</p>
        <p id="p_10983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10983"> $ programl4_2</p>
        <p id="p_10984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10984"> Enter the password for this program: blue jays</p>
        <p id="p_10985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10985"> Correct! Carry on!</p>
        <p id="p_10986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10986"> $</p>
        <p id="p_10987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10987"> Analysis</p>
        <p id="p_10988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10988"> Note that the password you type is not displayed on the</p>
        <p id="p_10989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10989"> screen.</p>
        <p id="p_10990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10990"> Lines 3-7 retrieve the correct password from the file  /u/ jqpublic/passwd.  This password can be created by another call to  crypt.  For example, if the correct password is  sludge,  the call that creates the string now stored in  $passwd  could be the following, where  $salt  contains some two-character string:</p>
        <p id="p_10991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10991"> $retval = crypt ("sludge", $salt);</p>
        <p id="p_10992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10992"> After the correct password has been retrieved, the next step is line 8, which asks the user to type a password. By default, anything typed in at the keyboard is immediately displayed on the screen; this behavior is called  input echoing.  Input echoing is not desirable if a password is being typed in, because someone looking over the user's shoulder can read the password and break into the program.</p>
        <p id="p_10993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10993"> To make the password-checking process more secure, line 9 calls the UNIX command  stty  -echo, which turns off input echoing; now the password is not displayed on the screen when the user types it. After the password has been entered, line 11 calls the UNIX command  stty  echo, which turns input echoing back on.</p>
        <p id="p_10994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10994"> Line 13 calls  crypt  to check the password the user has entered. Because the first two characters of the actual encrypted password contain the two-character salt used in encryption,  substr  is used to retrieve these two characters and use them as the salt when encrypting the user's password. If the value returned by  crypt  is identical to the encrypted password, the user's password is correct; otherwise, the user has gotten it wrong, and die terminates the program. (A gentler password-checking program usually gives the user two or three chances to type a password before terminating the program.)</p>
        <p id="p_10995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10995"> This password checker is secure because the actual password does not appear in the program in unencrypted form. (In fact, because the password is in a separate file, it does not appear in the program at all.) This makes it impossible to obtain the password by simply examining the text file.</p>
        <p id="p_10996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10996"> NOTE</p>
        <p id="p_10997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10997"> The behavior of  crypt  is identical to that of the UNIX library function  crypt.  See the  crypt  ( 3 ) manual page for more information on DES encryption</p>
        <p id="p_10998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10998"> The hex Function</p>
        <p id="p_10999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_10999"> The  hex  function assumes that a character string is a number written in hexadecimal format, and it converts it into a decimal number (a number in standard base-10 format).</p>
        <p id="p_11000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11000"> The syntax for the  hex  function is</p>
        <p id="p_11001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11001"> decnum = hex (hexnum);</p>
        <p id="p_11002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11002"> hexnum  is the hexadecimal character string, and  decnum  is the resulting decimal number.</p>
        <p id="p_11003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11003"> The following is an example:</p>
        <p id="p_11004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11004"> $myhexstring =  "Iff";</p>
        <p id="p_11005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11005"> $num = hex ($myhexstring);</p>
        <p id="p_11006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11006"> This call to  hex  assigns the decimal equivalent of  1 f f  to  $num,  which means that the value of  $num  is now  511.  The value stored in  $myhexstring  is not changed.</p>
        <p id="p_11007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11007"> The value passed to the string can contain either uppercase or lowercase letters (provided the letters are between  a  and  f , inclusive). This value can be the result of an expression, as follows:</p>
        <p id="p_11008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11008"> $num = hex  ("f" x 2);</p>
        <p id="p_11009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11009"> Here, the expression "  f  "  x 2  is equivalent to  f f , which is converted to  2 5 5  by  hex.</p>
        <p id="p_11010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11010"> NOTE</p>
        <p id="p_11011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11011"> To convert a string from a decimal value to a hexadecimal value, use  sprint f  and specify either  %x  (hexadecimal integer) or  %lx (long hexadecimal integer)</p>
        <p id="p_11012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11012"> Warning</p>
        <p id="p_11013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11013"> hex  does not handle hexadecimal strings that start with the characters  Ox  or  OX.  To handle these strings, either get rid of these characters using a statement such as</p>
        <p id="p_11014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11014"> $myhexstring =~  s/ A 0[xX]//;</p>
        <p id="p_11015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11015"> or call the  oct  function, which is described later in today's lesson</p>
        <p id="p_11016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11016"> The int Function</p>
        <p id="p_11017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11017"> The  int  function turns a floating-point number into an integer by getting rid of everything after the decimal point.</p>
        <p id="p_11018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11018"> The syntax for the  int  function is</p>
        <p id="p_11019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11019"> intnum = int (floatnum);</p>
        <p id="p_11020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11020"> f loatnum  is the floating-point number, and  intnum  is the resulting integer.</p>
        <p id="p_11021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11021"> The following is an example:</p>
        <p id="p_11022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11022"> $floatnum =  45.6;</p>
        <p id="p_11023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11023"> $intnum = int ($floatnum);</p>
        <p id="p_11024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11024"> This call to  int  converts  45.6   to  45   and assigns it to  $intnum.  The value stored in  $f loatnum  is not changed.</p>
        <p id="p_11025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11025"> int  can be used in expressions as well; for example:</p>
        <p id="p_11026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11026"> $intval = int  (68.3  / $divisor) + 1;</p>
        <p id="p_11027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11027"> Warning</p>
        <p id="p_11028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11028"> int  does not round up when you convert from floating point to integer. To round up when you use  int,  add  0 . 5  first, as follows:</p>
        <p id="p_11029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11029"> $intval = int ($mynum + 0.5);</p>
        <p id="p_11030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11030"> Even then, you still might need to watch out for round-off errors.</p>
        <p id="p_11031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11031"> For example, if 4.5 is actually stored in the machine as, say, 4.499999999,  adding  0 . 5  might still result in a number less than 5,  which means that  int  will truncate it to  4</p>
        <p id="p_11032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11032"> The oct Function</p>
        <p id="p_11033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11033"> The  oct  function assumes that a character string is a number written in octal format, and it converts it into a decimal number (a number in standard base-10 format).</p>
        <p id="p_11034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11034"> The syntax for the  oct  function is</p>
        <p id="p_11035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11035"> decnum = oct (octnum);</p>
        <p id="p_11036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11036"> octnum  is the octal character string, and  decnum  is the resulting decimal number.</p>
        <p id="p_11037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11037"> The following is an example:</p>
        <p id="p_11038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11038"> $myoctstring = "177";</p>
        <p id="p_11039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11039"> $num = oct ($myoctstring);</p>
        <p id="p_11040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11040"> This call to  oct  assigns the decimal equivalent of  17 7  to  $num,  which means that the value of  $num  is now 127. The value stored in  $myoctstring  is not changed.</p>
        <p id="p_11041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11041"> The value passed to  oct  can be the result of an expression, as shown in the following example:</p>
        <p id="p_11042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11042"> $num = oct  ("07" x 2);</p>
        <p id="p_11043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11043"> Here, the expression  "07"  x 2  is equivalent to  0707,  which is converted to  455  by  oct.</p>
        <p id="p_11044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11044"> NOTE</p>
        <p id="p_11045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11045"> To convert a string from a decimal value to an octal value, use sprint f  and specify either  %o  (octal integer) or  %lo  (long octal integer)</p>
        <p id="p_11046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11046"> The oct Function and Hexadecimal Integers</p>
        <p id="p_11047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11047"> The  oct  function also handles hexadecimal integers whose first two characters start with  Ox  or  OX:</p>
        <p id="p_11048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11048"> $num = oct  ("Oxff");</p>
        <p id="p_11049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11049"> This call treats  Oxff  as the hexadecimal number  f f  and converts it to 255. This feature of  oct  can be used to convert any non-standard Perl integer constant.</p>
        <p id="p_11050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11050"> Listing 14.3 is a program that reads a line of input and checks whether it is a valid Perl integer constant. If it is, it converts it into a standard (base-10) integer.</p>
        <p id="p_11051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11051"> Listing 14.3. A program that reads any kind of integer.</p>
        <p id="p_11052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11052"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11053"> 2 :</p>
        <p id="p_11054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11054"> 3: $integer = &lt;STDIN&gt;;</p>
        <p id="p_11055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11055"> 4: chop ($integer);</p>
        <p id="p_11056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11056"> 5: if ($integer !~  / A [0-9]+$| A 0[xX][0-9a-fa-F]+$/)  {</p>
        <p id="p_11057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11057"> 6:  die ("$integer is not a legal integer\n");</p>
        <p id="p_11058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11058"> 7: }</p>
        <p id="p_11059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11059"> 8: if ($integer =~  / A 0/)  {</p>
        <p id="p_11060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11060"> 9:  $integer = oct ($integer);</p>
        <p id="p_11061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11061"> 10</p>
        <p id="p_11062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11062"> }</p>
        <p id="p_11063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11063"> 11: print  ( "$integer\n");</p>
        <p id="p_11064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11064"> Output</p>
        <p id="p_11065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11065"> $ programl4_3 077 63 $</p>
        <p id="p_11066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11066"> Analysis</p>
        <p id="p_11067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11067"> The pattern in line 5</p>
        <p id="p_11068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11068"> matches one of the following:</p>
        <p id="p_11069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11069"> • One or more digits</p>
        <p id="p_11070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11070"> • A string consisting of  Ox  or  OX  followed by one or more digits or by uppercase or lowercase letters between  a  and  f , inclusive</p>
        <p id="p_11071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11071"> The first case matches any standard base-10 integer or octal integer (because octal integers start with 0 and consist of the numbers 0 to 7). The second case matches any legal hexadecimal integer. In both cases, the pattern matches only if there are no extraneous characters (blank spaces, or other words or numbers) on the line. Of course, it is easy to use the substitution operator to get rid of these first, if you like.</p>
        <p id="p_11072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11072"> Line 8 tests whether the integer is either an octal or hexadecimal integer by searching for the pattern /  A  0  / . If this pattern is found,  oct  converts the integer to decimal, placing the converted integer back in $integer.  Note that line 8 does not need to determine which type of integer is contained in  $integer because  oct  processes both octal and hexadecimal integers.</p>
        <p id="p_11073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11073"> The ord and chr Functions</p>
        <p id="p_11074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11074"> The  ord  and  chr  functions are similar to the Pascal function of the same name,  ord  converts a single character to its numeric ASCII equivalent, and  chr  converts a number to its ASCII character equivalent.</p>
        <p id="p_11075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11075"> The syntax for the  ord  function is</p>
        <p id="p_11076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11076"> asciival = ord (char);</p>
        <p id="p_11077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11077"> char  is the string whose first character is to be converted, and  asciival  is the resulting ASCII value.</p>
        <p id="p_11078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11078"> For example, the following statement assigns the ASCII value for the / character, 47, to  $ASCI  I  val:</p>
        <p id="p_11079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11079"> $ASCIIval =  ord("/");</p>
        <p id="p_11080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11080"> If the value passed to  ord  is a character string that is longer than one character in length,  ord  converts the first character in the string:</p>
        <p id="p_11081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11081"> $mystring = "/ignore the rest of this string";</p>
        <p id="p_11082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11082"> $charval = ord ($mystring);</p>
        <p id="p_11083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11083"> Here, the first character stored in  $mystring,  /, is converted and assigned to  $charval.</p>
        <p id="p_11084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11084"> The syntax for the  chr  function is</p>
        <p id="p_11085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11085"> charval = chr (asciival);</p>
        <p id="p_11086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11086"> asciival  is the value to be converted, and  charval  is the one-character string representing the character equivalent of  asciival  in the ASCII character set.</p>
        <p id="p_11087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11087"> For example, the following statement assigns / to $  slash,  because 47 is the numeric equivalent of / in the ASCII character set:</p>
        <p id="p_11088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11088"> $slash =  chr(47);</p>
        <p id="p_11089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11089"> NOTE</p>
        <p id="p_11090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11090"> The ASCII character set contains 256 characters. As a consequence, if the value passed to  chr  is greater than 256, only the bottom eight bits of the value are used.</p>
        <p id="p_11091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11091"> This means, for example, that the following statements are equivalent:</p>
        <p id="p_11092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11092"> $slash =  chr(47);</p>
        <p id="p_11093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11093"> $slash =  chr(303);</p>
        <p id="p_11094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11094"> $slash =  chr(559);</p>
        <p id="p_11095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11095"> In each case, the value of $slash is /</p>
        <p id="p_11096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11096"> The  chr  function is defined only in Perl 5. If you are using Perl 4, you will need to call  sprintfto  convert a number to a character:</p>
        <p id="p_11097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11097"> $slash = sprintf ( "%c",  47);</p>
        <p id="p_11098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11098"> This assigns / to  $slash</p>
        <p id="p_11099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11099"> The scalar Function</p>
        <p id="p_11100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11100"> In Perl, some functions or expressions behave differently when their results are assigned to arrays than they do when assigned to scalar variables. For example, the assignment</p>
        <p id="p_11101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11101"> @var = @array;</p>
        <p id="p_11102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11102"> copies the list stored in @ array  to the array variable  @var,  and the assignment</p>
        <p id="p_11103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11103"> $var = @array;</p>
        <p id="p_11104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11104"> determines the number of elements in the list stored in @ array  and assigns that number to the scalar variable  $var.</p>
        <p id="p_11105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11105"> As you can see,  @array  has two different meanings: an "array meaning" and a "scalar meaning." The Perl interpreter determines which meaning to use by examining the rest of the statement in which @array  occurs. In the first case, the array meaning is intended, because the statement is assigning to an array variable. Statements in which the array meaning is intended are called  array contexts.</p>
        <p id="p_11106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11106"> In the second case, the scalar meaning of @ array  is intended, because the statement is assigning to a scalar variable. Statements in which the scalar meaning is intended are called  scalar contexts.</p>
        <p id="p_11107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11107"> The scalar function enables you to specify the scalar meaning in an array context.</p>
        <p id="p_11108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11108"> The syntax for the  scalar  function is</p>
        <p id="p_11109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11109"> value</p>
        <p id="p_11110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11110"> scalar (list);</p>
        <p id="p_11111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11111"> list  is the list to be used in a scalar context, and  value  is the scalar meaning of the list.</p>
        <p id="p_11112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11112"> For example, to create a list consisting of the length of an array, you can use the following statement:</p>
        <p id="p_11113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11113"> @array =  ("a",  "b",  "c");</p>
        <p id="p_11114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11114"> @lengtharray = scalar (@array);</p>
        <p id="p_11115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11115"> Here, the number of elements of @  array,  3, is converted into a one-element list and assigned to</p>
        <p id="p_11116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11116"> @lengtharray.</p>
        <p id="p_11117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11117"> Another useful place to use  scalar  is in conjunction with the &lt;&gt; operator. Recall that the statement</p>
        <p id="p_11118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11118"> $myline = &lt;MYFILE&gt;;</p>
        <p id="p_11119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11119"> reads one line from the input file  MYFILE,  and</p>
        <p id="p_11120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11120"> @mylines = &lt;MYFILE&gt;;</p>
        <p id="p_11121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11121"> reads all of  MYFILE  into the array variable @  my lines.  To read one line into the array variable @my lines  (as a one-element list), use the following:</p>
        <p id="p_11122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11122"> @mylines = scalar (&lt;MYFILE&gt;);</p>
        <p id="p_11123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11123"> Specifying  scalar  with  &lt;MYFILE&gt;  ensures that only one line is read from  MYFILE.</p>
        <p id="p_11124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11124"> The pack Function</p>
        <p id="p_11125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11125"> The  pack  function enables you to take a list or the contents of an array variable and convert (pack) it into a scalar value in a format that can be stored in actual machine memory or used in programming languages such as C.</p>
        <p id="p_11126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11126"> The syntax for the  pack  function is</p>
        <p id="p_11127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11127"> formatstr = pack (packformat,  list);</p>
        <p id="p_11128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11128"> Here,  list  is a list of values; this list of values can, as always, be the contents of an array variable, f ormatstr  is the resulting string, which is in the format specified by  packf ormat.</p>
        <p id="p_11129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11129"> packf ormat  consists of one or more  pack-format characters ; these characters determine how the list to be packed. These pack formats are listed in Table 14.1.</p>
        <p id="p_11130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11130"> Table 14.1. Format characters for the pack function.</p>
        <p id="p_11131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11131"> One pack-format character must be supplied for each element in the list. If you like, you can use spaces</p>
        <p id="p_11132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11132"> or tabs to separate pack-format characters, because  pack  ignores white space.</p>
        <p id="p_11133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11133"> The following is a simple example that uses  pack:</p>
        <p id="p_11134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11134"> $integer = pack("i", 171);</p>
        <p id="p_11135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11135"> This statement takes the number 171, converts it into the format used to store integers on your machine, and returns the converted integer in $  integer.  This converted integer can now be written out to a file or passed to a program using the  system  or  exec  functions.</p>
        <p id="p_11136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11136"> To repeat a pack-format character multiple times, specify a positive integer after the character. The following is an example:</p>
        <p id="p_11137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11137"> $twoints = pack("i2", 103, 241);</p>
        <p id="p_11138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11138"> Here, the pack format  i2  is equivalent to  ii.</p>
        <p id="p_11139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11139"> To use the same pack-format character for all of the remaining elements in the list, use * in place of an integer, as follows:</p>
        <p id="p_11140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11140"> $manyints = pack("i*", 14, 26, 11, 83);</p>
        <p id="p_11141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11141"> Specifying integers or * to repeat pack-format characters works for all formats except  a, A,  and @. With the  a  and  A  formats, the integer is assumed to be the length of the string to create.</p>
        <p id="p_11142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11142"> $mystring = pack("a6",  "test");</p>
        <p id="p_11143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11143"> This creates a string of six characters (the four that are supplied, plus two null characters).</p>
        <p id="p_11144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11144"> NOTE</p>
        <p id="p_11145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11145"> The  a  and  A  formats always use exactly one element of the list, regardless of whether a positive integer is included following the character. For example:</p>
        <p id="p_11146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11146"> $mystring = pack("a6", "testl", "test2");</p>
        <p id="p_11147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11147"> Here,  test 1  is packed into a six-character string and assigned to</p>
        <p id="p_11148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11148"> $mystring. test2  is ignored.</p>
        <p id="p_11149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11149"> To get around this problem, use the  x  operator to create multiple copies of the  a  pack-format character, as follows:</p>
        <p id="p_11150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11150"> $strings = pack ("a6" x 2, "testl", "test2");</p>
        <p id="p_11151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11151"> This packs  testl  and  test2  into two six-character strings (joined together)</p>
        <p id="p_11152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11152"> The @ format is a special case. It is used only when a following integer is specified. This integer indicates the number of bytes the string must contain at this point; if the string is smaller, null characters are added. For example:</p>
        <p id="p_11153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11153"> $output = pack ("a @6 a", "test", "test2");</p>
        <p id="p_11154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11154"> Here, the string  test  is converted to ASCII format. Because this string is only four characters long, and the pack format @ 6 specifies that the packed scalar value must be six characters long at this point, two null characters are added to the string before  test 2  is packed.</p>
        <p id="p_11155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11155"> The pack Function and C Data Types</p>
        <p id="p_11156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11156"> The most frequent use of  pack  is to create data that can be used by C programs. For example, to create a string terminated by a null character, use the following call to  pack:</p>
        <p id="p_11157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11157"> $Cstring = pack ("ax", $mystring);</p>
        <p id="p_11158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11158"> Here, the  a  pack-format character converts  $mystring  into an ASCII string, and the  x  character appends a null character to the end of the string. This format-a string followed by null-is how C stores strings.</p>
        <p id="p_11159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11159"> Table 14.2 shows the pack-format characters that have equivalent data types in C.</p>
        <p id="p_11160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11160"> Table 14.2. Pack-format characters and their C equivalents.</p>
        <p id="p_11161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11161"> In each case,  pack  stores the value in your local machine's internal format.</p>
        <p id="p_11162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11162"> The unpack Function</p>
        <p id="p_11163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11163"> The  unpack  function reverses the operation performed by  pack.  It takes a value stored in machine format and converts it to a list of values understood by Perl.</p>
        <p id="p_11164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11164"> The syntax for the  unpack  function is</p>
        <p id="p_11165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11165"> list = unpack (packformat, formatstr);</p>
        <p id="p_11166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11166"> Here,  formatstr  is the value in machine format, and  list  is the created list of values.</p>
        <p id="p_11167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11167"> As in  pack, packformat  is a set of one or more pack format characters. These characters are basically the same as those understood by  pack.  Table 14.3 lists these characters.</p>
        <p id="p_11168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11168"> Table 14.3. The pack-format characters, as used by unpack.</p>
        <p id="p_11169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11169"> In almost all cases, a call to  unpack  undoes the effects of an equivalent call to  pack.  For example, consider Listing 14.4, which packs and unpacks a list of integers.</p>
        <p id="p_11170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11170"> Listing 14.4. A program that demonstrates the relationship between pack and unpack.</p>
        <p id="p_11171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11171"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11172"> (11, 26,  43);</p>
        <p id="p_11173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11173"> 3:  @list_of_integers =</p>
        <p id="p_11174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11174"> 4: $mystring = pack("i*", @list_of_integers);</p>
        <p id="p_11175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11175"> 5: @list_of_integers = unpack("i*", $mystring);</p>
        <p id="p_11176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11176"> 6: print ("@list_of_integers\n");</p>
        <p id="p_11177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11177"> Output</p>
        <p id="p_11178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11178"> $ programl4_4 11 26 43 $</p>
        <p id="p_11179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11179"> Analysis</p>
        <p id="p_11180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11180"> Line 4 calls  pack,  which takes all of the elements stored in  @list_of_integers, converts them to the machine's integer format, and stores them in  $mystring.</p>
        <p id="p_11181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11181"> Line 5 calls  unpack,  which assumes that the string stored in  $my string  is a list of values stored in the machine's integer format; it takes this string, converts each integer in the string to a Perl value, and stores the resulting list of values in  @list_of_integers.</p>
        <p id="p_11182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11182"> Unpacking Strings</p>
        <p id="p_11183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11183"> The only  unpack  operations that do not exactly mirror pack operations are those specified by the  a  and A  formats. The  a  format converts a machine-format string into a Perl value as is, whereas the  A  format converts a machine-format string into a Perl value and strips any trailing blanks or null characters.</p>
        <p id="p_11184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11184"> The  A  format is useful if you want to convert a C string into the string format understood by Perl. The following is an example:</p>
        <p id="p_11185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11185"> $perlstring = unpack("A", $Cstring);</p>
        <p id="p_11186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11186"> Here,  $Cstringis  assumed to contain a character string stored in the format used by the C programming language (a sequence of bytes terminated by a null character),  unpack  strips the trailing</p>
        <p id="p_11187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11187"> null character from the string stored in  $Cstring,  and stores the resulting string in  $perlstring.</p>
        <p id="p_11188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11188"> Skipping Characters When Unpacking</p>
        <p id="p_11189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11189"> The @ pack-format character tells  unpack  to skip to the position specified with the @. For example, the following statement skips four bytes  in$packstring,  and then unpacks a signed integer and stores it</p>
        <p id="p_11190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11190"> in  $skipnum.</p>
        <p id="p_11191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11191"> $skipnum = unpack("@4i", $packstring);</p>
        <p id="p_11192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11192"> NOTE</p>
        <p id="p_11193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11193"> If  unpack  is unpacking a single item, it can be stored in either an array variable or a scalar variable. If an array variable is used to store the result of the  unpack  operation, the resulting list consists of a single element</p>
        <p id="p_11194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11194"> If an * character appears after the @ pack-format character,  unpack  skips to the end of the value being unpacked. This can be used in conjunction with the  X  pack-format character to unpack the right end of the packed value. For example, the following statement treats the last four bytes of a packed value as a long unsigned integer and unpacks them:</p>
        <p id="p_11195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11195"> $longrightint = unpack  ("@*   X4   L",  $packstring);</p>
        <p id="p_11196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11196"> In this example, the @ * pack format specifier skips to the end of the value stored in  $packst ring. Then, the  X4  specifier backs up four bytes. Finally, the  L  specifier treats the last four bytes as a long unsigned integer, which is unpacked and stored in  $longrightint.</p>
        <p id="p_11197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11197"> Warning</p>
        <p id="p_11198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11198"> The number of bytes unpacked by the s, S, i, I, 1, and  L  formats depends on your machine. Many UNIX machines store short integers in two bytes of memory, and integer and long integer values in four bytes. However, other machines might behave differently. In general, you cannot assume that programs that use  pack  and unpack  will behave in the same way on different machines</p>
        <p id="p_11199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11199"> The unpack Function and uuencode</p>
        <p id="p_11200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11200"> The  unpack  function enables you to decode files that have been encoded by the  uuencode  encoding program. To do this, use the  u  pack-format specifier.</p>
        <p id="p_11201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11201"> NOTE</p>
        <p id="p_11202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11202"> uuencode,  a coding mechanism available on most UNIX systems, converts all characters (including unprintable characters) into printable ASCII characters. This ensures that you can safely transmit files across remote networks</p>
        <p id="p_11203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11203"> Listing 14.5 is an example of a program that uses  unpack  to decode a  uuencoded  file.</p>
        <p id="p_11204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11204"> Type</p>
        <p id="p_11205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11205"> Listing 14.5. A program that decodes a uuencoded file.</p>
        <p id="p_11206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11206"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11207"> 2 :</p>
        <p id="p_11208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11208"> 3: open (CODEDFILE, "/u/janedoe/codefile") ||</p>
        <p id="p_11209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11209"> 4: die ("Can't open input file");</p>
        <p id="p_11210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11210"> 5: open (OUTFILE, "&gt;outfile") | |</p>
        <p id="p_11211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11211"> 6: die ("Can't open output file");</p>
        <p id="p_11212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11212"> 7: while ($line - &lt;CODEDFILE&gt;) {</p>
        <p id="p_11213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11213"> 8: $decoded = unpack ("u", $line);</p>
        <p id="p_11214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11214"> 9: print OUTFILE ($decoded);</p>
        <p id="p_11215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11215"> 10 : }</p>
        <p id="p_11216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11216"> 11: close (OUTFILE);</p>
        <p id="p_11217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11217"> 12: close (CODEDFILE);</p>
        <p id="p_11218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11218"> Analysis</p>
        <p id="p_11219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11219"> The file variable  CODEDFILE  represents the file that was previously encoded by uuencode.  Lines 3 and 4 open the file (or die trying). Lines 5 and 6 open the output file, which is represented by the file variable  OUTFILE.</p>
        <p id="p_11220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11220"> Lines 7-10 read and write one line at a time. Line 7 starts off by reading a line of encoded input into the scalar variable  $line.  As with any other input file, the null string is returned if  CODEDFILE  is exhausted.</p>
        <p id="p_11221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11221"> Line 8 calls  unpack  to decode the line. If the line is a special line created by  uuencode  (for example, the first line, which lists the filename and the size, or the last line, which marks the end of the file), unpack  detects it and converts it into the null string. This means that the program does not need to contain special code to handle these lines.</p>
        <p id="p_11222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11222"> Line 9 writes the decoded line to the output file represented by  OUTFILE.</p>
        <p id="p_11223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11223"> NOTE</p>
        <p id="p_11224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11224"> You can use  pack  to  uuencode  lists of elements, as in the following:</p>
        <p id="p_11225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11225"> @encoded = pack ("u", ©decoded);</p>
        <p id="p_11226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11226"> Here, the elements in @ decoded  are encoded and stored in the array variable @  encoded.  The list in  gen coded  can then be decoded using  unpack,  as follows:</p>
        <p id="p_11227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11227"> ©decoded = unpack  ("u",  ©encoded);</p>
        <p id="p_11228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11228"> Although  pack  uses the same  uuencode  algorithm as the UNIX uuencode  utility, you cannot use the UNIX  uudecode  program on data encoded using  pack  because  pack  does not supply the header and footer (beginning and ending) lines expected by</p>
        <p id="p_11229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11229"> uudecode.</p>
        <p id="p_11230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11230"> If you really need to use  uudecode  with a file created by writing out the output from  pack,  you'll need to write out the header and footer files as well. (See the UNIX manual page for  uuencode  for more details.</p>
        <p id="p_11231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11231"> The vec Function</p>
        <p id="p_11232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11232"> The  vec  function enables you to treat a scalar value as a collection of chunks, with each chunk consisting of a specified number of bits; this collection is known as a  vector.  Each call to  vec  accesses a particular chunk of bits in the vector (known as a  bit vector).</p>
        <p id="p_11233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11233"> The syntax for the  ve c  function is</p>
        <p id="p_11234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11234"> retval = vec (vector, index, bits);</p>
        <p id="p_11235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11235"> vector  is the scalar value that is to be treated as a vector. It can be any scalar value, including the value of an expression.</p>
        <p id="p_11236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11236"> index  behaves like an array subscript. It indicates which chunk of bits to retrieve. An index of 0 retrieves the first chunk, 1 retrieves the second, and so on. Note that retrieval is from right to left. The first chunk of bits retrieved when the index 0 is specified is the chunk of bits at the right end of the vector.</p>
        <p id="p_11237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11237"> bits  specifies the number of bits in each chunk; it can be 1, 2, 4, 8, 16, or 32.</p>
        <p id="p_11238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11238"> retval  is the value of the chunk of bits. This value is an ordinary Perl scalar value, and it can be used anywhere scalar values can be used.</p>
        <p id="p_11239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11239"> Listing 14.6 shows how you can use  vec  to retrieve the value of a particular chunk of bits.</p>
        <p id="p_11240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11240"> Listing 14.6. A program that illustrates the use of vec.</p>
        <p id="p_11241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11241"> Output</p>
        <p id="p_11242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11242"> $ programl4_6 high-to-low order low-to-high order</p>
        <p id="p_11243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11243"> values: values:</p>
        <p id="p_11244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11244"> 3 and 13 11 and 12</p>
        <p id="p_11245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11245"> $</p>
        <p id="p_11246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11246"> Analysis</p>
        <p id="p_11247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11247"> The call to  pack  in line 3 assumes that each character in the string 11010011 is a bit to be packed. The bits are packed in high-to-low order (with the highest bit first), which means that the vector stored in  Svector  consists of the bits  11010011  (from left to right). Grouping these bits into chunks of four produces  1101   0011 ,  which are the binary representations of 13 and 3, respectively.</p>
        <p id="p_11248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11248"> Line 4 retrieves the first chunk of four bits from  $vector  and assigns it to  $vall.  This is the chunk 0011, because  vec  is retrieving the chunk of bits at the right end of the bit vector. Similarly, line 5 retrieves 1101, because the index 1 specifies the second chunk of bits from the right; this chunk is assigned to  $val2.  (One way to think of the index is as "the number of chunks to skip." The index  1 indicates that one chunk of bits is to be skipped.)</p>
        <p id="p_11249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11249"> Line 7 is similar to line 3, but the bits are now stored in low-to-high order, not high-to-low. This means that the string  11010011   is stored as the following (which is  11010011   reversed):</p>
        <p id="p_11250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11250"> 11001011</p>
        <p id="p_11251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11251"> When this bit vector is grouped into chunks of 4 bits, you get the following, which are the binary representations of 12 and 11, respectively:</p>
        <p id="p_11252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11252"> 1100 1011</p>
        <p id="p_11253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11253"> Lines 8 and 9, like lines 4 and 5, retrieve the first and second chunk of bits from  $vector.  This means that  $vall  is assigned 11 (the first chunk), and  $va!2  is assigned 12 (the second chunk).</p>
        <p id="p_11254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11254"> NOTE</p>
        <p id="p_11255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11255"> You can use  vec  to assign to a chunk of bits by placing the call to vec  to the left of an assignment operator. For example:</p>
        <p id="p_11256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11256"> vec (Svector, 0, 4) = 11;</p>
        <p id="p_11257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11257"> This statement assigns 11 to the first chunk of bits in $  vector. Because the binary representation of 11 is 1011, the last four bits of $ vector  become  1011</p>
        <p id="p_11258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11258"> The defined Function</p>
        <p id="p_11259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11259"> By default, all scalar variables and elements of array variables that have not been assigned to are assumed to contain the null string. This ensures that Perl programs don't crash when using uninitialized scalar variables.</p>
        <p id="p_11260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11260"> In some cases, a program might need to know whether a particular scalar variable or array element has been assigned to or not. The built-in function  defined  enables you to check for this.</p>
        <p id="p_11261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11261"> The syntax for the  defined  function is</p>
        <p id="p_11262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11262"> retval = defined (expr);</p>
        <p id="p_11263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11263"> Here, expr is anything that can appear on the left of an assignment statement, such as a scalar variable, array element, or an entire array. (An array is assumed to be defined if at least one of its elements is defined.)  retval  is true (a nonzero value) if  expr  is defined, and false (0) if it is not.</p>
        <p id="p_11264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11264"> Listing 14.7 is a simple example of a program that uses  defined.</p>
        <p id="p_11265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11265"> Tyw</p>
        <p id="p_11266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11266"> Listing 14.7. A program that illustrates the use of defined.</p>
        <p id="p_11267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11267"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11268"> 2  :</p>
        <p id="p_11269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11269"> 3: Sarray[2] = 14;</p>
        <p id="p_11270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11270"> 4: $array[4] = "hello";</p>
        <p id="p_11271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11271"> 5: for ($i = 0; $i &lt;= 5; $i++) {</p>
        <p id="p_11272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11272"> 6: if (defined ($array[$i])) {</p>
        <p id="p_11273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11273"> 7: print ("element ", $i+l, " is defined\n");</p>
        <p id="p_11274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11274"> 8 : }</p>
        <p id="p_11275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11275"> 9 : }</p>
        <p id="p_11276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11276"> Output</p>
        <p id="p_11277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11277"> $ programl4_7 element 3 is defined element 5 is defined $</p>
        <p id="p_11278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11278"> Analysis</p>
        <p id="p_11279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11279"> This program assigns values to two elements of the array variable @ array:  the element with subscript  2  (the third element), and the element with subscript  4  (the fifth element).</p>
        <p id="p_11280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11280"> The loop in lines 5-9 checks each element of @ array  to see whether it is defined. Because the third and fifth elements-$  array  [  2  ] and $  array  [  4  ], respectively-are defined,  defined  returns true when  $i is  2  and when $  i  is 4.</p>
        <p id="p_11281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11281"> NOTE</p>
        <p id="p_11282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11282"> Many functions that return the null string actually return a special "undefined" value that is treated as if it is the null string. If this undefined value is passed to  defined, defined  returns false.</p>
        <p id="p_11283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11283"> Functions that return undefined include the  read  function (discussed on  Day 12 ,  "Working with the File System") and  fork (introduced on  Day  13 , "Process, String, and Mathematical Functions"). Many functions discussed today and on  Day 15 , "System Functions," also return the special undefined value when an error occurs.</p>
        <p id="p_11284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11284"> The general rule is: A function that returns the null string when an</p>
        <p id="p_11285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11285"> error or exceptional condition occurs is usually really returning the undefined value</p>
        <p id="p_11286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11286"> The undef Function</p>
        <p id="p_11287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11287"> The  undef  function undefines a scalar variable, array element, or an entire array. The syntax of the  undef  function is</p>
        <p id="p_11288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11288"> retval = undef (expr);</p>
        <p id="p_11289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11289"> As in calls to  defined, expr  can be anything that can appear to the left of a Perl assignment statement, retval  is always the special undefined value discussed in the previous section, "The  defined Function"; this undefined value is equivalent to the null string.</p>
        <p id="p_11290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11290"> The following are some examples of  undef:</p>
        <p id="p_11291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11291"> undef ($myvar); undef  ($array[3]); undef  (@array);</p>
        <p id="p_11292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11292"> In the first case, the scalar variable  Smyvar  becomes undefined. The Perl interpreter now treats  Smyvar as if it has never been assigned to. Needless to say, any value previously stored in  Smyvar  is now lost.</p>
        <p id="p_11293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11293"> In the second example, the fourth element of @ array  is marked as undefined. Its value, if any, is lost. Other elements of @ array  are unaffected.</p>
        <p id="p_11294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11294"> In the third and final example, all the elements of @ array  are marked as undefined. This lets the Perl inteipreter free up any memory used to store the values of @  array,  which might be useful if your program is working with large arrays. For example, if you have used an array to read in an entire file, as in the following:</p>
        <p id="p_11295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11295"> @bigarray = &lt;STDIN&gt;;</p>
        <p id="p_11296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11296"> you can use the following statement to tell the Perl interpreter that you don't need the contents of the input file and that the interpreter can throw them away:</p>
        <p id="p_11297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11297"> undef (@bigarray);</p>
        <p id="p_11298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11298"> Calls to  undef  can omit  expr.  In this case,  undef  does nothing and just returns the undefined value. Listing 14.8 shows how this can be useful.</p>
        <p id="p_11299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11299"> Type</p>
        <p id="p_11300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11300"> Listing 14.8. A program that illustrates the use of undef to represent an unusual condition.</p>
        <p id="p_11301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11301"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11302"> 2 :</p>
        <p id="p_11303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11303"> 3: print ("Enter the number to divide:\n");</p>
        <p id="p_11304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11304"> 4: $valuel = &lt;STDIN&gt;;</p>
        <p id="p_11305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11305"> 5: chop ($valuel);</p>
        <p id="p_11306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11306"> 6: print ("Enter the number to divide  by:\n");</p>
        <p id="p_11307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11307"> 7:  $value2 = &lt;STDIN&gt;;</p>
        <p id="p_11308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11308"> 8: chop ($value2);</p>
        <p id="p_11309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11309"> 9: $result = &amp;safe_division($valuel, $value2);</p>
        <p id="p_11310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11310"> 10: if (defined($result)) {</p>
        <p id="p_11311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11311"> 11: print ("The result is $result. \n") ;</p>
        <p id="p_11312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11312"> 12: } else {</p>
        <p id="p_11313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11313"> 13: print  ("Can't  divide by  zero.\n");</p>
        <p id="p_11314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11314"> 14 : }</p>
        <p id="p_11315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11315"> 15 :</p>
        <p id="p_11316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11316"> 16: sub safe_division {</p>
        <p id="p_11317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11317"> 17: local ($dividend, $divisor) =</p>
        <p id="p_11318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11318"> 18 :</p>
        <p id="p_11319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11319"> local  ($result);</p>
        <p id="p_11320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11320"> $result</p>
        <p id="p_11321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11321"> ($divisor</p>
        <p id="p_11322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11322"> 0) ? undef :</p>
        <p id="p_11323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11323"> 20 :</p>
        <p id="p_11324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11324"> 21: $dividend / $divisor;</p>
        <p id="p_11325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11325"> 22  : }</p>
        <p id="p_11326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11326"> Output</p>
        <p id="p_11327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11327"> $ programl4_8</p>
        <p id="p_11328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11328"> Enter the number to divide:</p>
        <p id="p_11329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11329"> 26</p>
        <p id="p_11330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11330"> Enter the number to divide by: 0</p>
        <p id="p_11331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11331"> Can't divide by zero.</p>
        <p id="p_11332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11332"> $</p>
        <p id="p_11333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11333"> Analysis</p>
        <p id="p_11334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11334"> Lines 20 and 21 illustrate how you can use  undef.  If  $divisor  is 0, the program is attempting to divide by 0. In this case, the subroutine  saf e_division  calls  undef,  which returns the special undefined value. This value is assigned to  $result  and passed back to the main part of the program.</p>
        <p id="p_11335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11335"> Line 10 tests whether  saf e_division  has returned the undefined value by the calling  defined function. If  defined  returns false, $  re suit  contains the undefined value, and an attempted division by 0 has been detected.</p>
        <p id="p_11336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11336"> NOTE</p>
        <p id="p_11337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11337"> You can use  undef  to undefine an entire subroutine, if you like. The following example:</p>
        <p id="p_11338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11338"> undef (&amp;mysub);</p>
        <p id="p_11339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11339"> frees the memory used to store  my sub;  after this,  my sub  can no longer be called.</p>
        <p id="p_11340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11340"> You are not likely to need to use this feature of  undef,  but it might prove useful in programs that consume a lot of memory</p>
        <p id="p_11341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11341"> Array and List Functions</p>
        <p id="p_11342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11342"> The following functions manipulate standard array variables and the lists that they store:</p>
        <p id="p_11343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11343"> • grep</p>
        <p id="p_11344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11344"> • splice</p>
        <p id="p_11345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11345"> • shift</p>
        <p id="p_11346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11346"> • unshift</p>
        <p id="p_11347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11347"> • push</p>
        <p id="p_11348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11348"> • pop</p>
        <p id="p_11349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11349"> • split</p>
        <p id="p_11350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11350"> • sort</p>
        <p id="p_11351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11351"> • reverse</p>
        <p id="p_11352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11352"> • map</p>
        <p id="p_11353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11353"> • wantarray</p>
        <p id="p_11354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11354"> The grep Function</p>
        <p id="p_11355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11355"> The  grep  function provides a convenient way of extracting the elements of a list that match a specified pattern. (It is named after the UNIX search utility of the same name.)</p>
        <p id="p_11356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11356"> The syntax for the  grep  function is</p>
        <p id="p_11357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11357"> foundlist = grep (pattern, searchlist);</p>
        <p id="p_11358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11358"> pattern  is the pattern to search for.  searchlist  is the list of elements to search in.  foundlist  is the list of elements matched.</p>
        <p id="p_11359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11359"> Here is an example:</p>
        <p id="p_11360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11360"> @list = ("This", "is", "a", "test");</p>
        <p id="p_11361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11361"> @foundlist = grep(/ A [tT]/, @list);</p>
        <p id="p_11362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11362"> Here,  grep  examines all the elements of the list stored in  @list.  If a list element contains the letter  t (in either uppercase or lowercase), the element is included as part of @  f oundlist.  As a result, @foundlist  consists of two elements:  This  and  test.</p>
        <p id="p_11363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11363"> Listing 14.9 is an example of a program that uses  grep.  It searches for all integers on an input line and adds them together.</p>
        <p id="p_11364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11364"> Type</p>
        <p id="p_11365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11365"> Listing 14.9. A program that demonstrates the use of grep.</p>
        <p id="p_11366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11366"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11367"> 2 :</p>
        <p id="p_11368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11368"> 3: $total = 0;</p>
        <p id="p_11369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11369"> 4: $line = &lt;STDIN&gt;;</p>
        <p id="p_11370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11370"> 5: @words = split(/\s+/, $line);</p>
        <p id="p_11371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11371"> 6: @numbers =  grep(/ A \d+?$/,  @words);</p>
        <p id="p_11372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11372"> 7:  foreach $number (@numbers) {</p>
        <p id="p_11373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11373"> 8 : $total += $number;</p>
        <p id="p_11374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11374"> 9 : }</p>
        <p id="p_11375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11375"> 10: print ("The total is $total. \n");</p>
        <p id="p_11376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11376"> Output</p>
        <p id="p_11377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11377"> $ programl4_9</p>
        <p id="p_11378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11378"> This line of input contains 8, 11 and 26.</p>
        <p id="p_11379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11379"> The total is 45.</p>
        <p id="p_11380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11380"> $</p>
        <p id="p_11381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11381"> Analysis</p>
        <p id="p_11382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11382"> Line  5  splits the input line into words, using the standard pattern / \  s  + /, which matches one or more tabs or blanks. Some of these words are actually numbers, and some are not.</p>
        <p id="p_11383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11383"> Line 6 uses  grep   to match the words that are actually numbers. The pattern / A \d+[.,; : ] ? $ / matches if a word consists of one or more digits followed by an optional punctuation character. The words that match this pattern are returned by  grep  and stored in  @number s.  After line 6 has been executed, @ numbers  contains the following list:</p>
        <p id="p_11384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11384"> " 11 ",</p>
        <p id="p_11385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11385"> " 26 .")</p>
        <p id="p_11386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11386"> Lines  7-9  use a  f oreach  loop to total the numbers. Note that the totaling operation works properly even if a number being added contains a closing punctuation character: when the Perl interpreter converts a string to an integer, it reads from left to right until it sees a character that is not a digit. This means that the final word,  2   6   ., is converted to  2   6 ,  which is the expected number.</p>
        <p id="p_11387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11387"> Because  split  and  grep  each return a list and  f oreach  expects a list, you can combine lines  5-9  into a single loop if you want to get fancy.</p>
        <p id="p_11388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11388"> foreach $number (grep  (/ A \d+?$/,  split  (/\s  +  /,  $line))) {</p>
        <p id="p_11389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11389"> $total  += $number;</p>
        <p id="p_11390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11390"> }</p>
        <p id="p_11391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11391"> As always, there is a trade-off of speed versus readability: this code is more concise, but the code in Listing 14.9 is more readable.</p>
        <p id="p_11392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11392"> Using grep with the File-Test Operators</p>
        <p id="p_11393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11393"> A useful feature of  grep  is that it can be used to search for any expression, not just patterns. For example,  grep  can be used in conjunction with  readdir  and the file-test operators to search a directory.</p>
        <p id="p_11394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11394"> Listing 14.10 is an example of a program that searches all the readable files of the current directory for a particular word (which is supplied on the command line). Files whose names begin with a period are ignored.</p>
        <p id="p_11395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11395"> Type</p>
        <p id="p_11396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11396"> Listing 14.10. A program that uses grep with the file-test operators.</p>
        <p id="p_11397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11397"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11398"> 2 :</p>
        <p id="p_11399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11399"> 3: opendir(CURRDIR,  ".")   ||</p>
        <p id="p_11400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11400"> 4:  die("Can't open current directory");</p>
        <p id="p_11401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11401"> 5: @filelist = grep  (!/ A \./,  grep(-r, readdir(CURRDIR)));</p>
        <p id="p_11402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11402"> 6: closedir(CURRDIR);</p>
        <p id="p_11403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11403"> Output</p>
        <p id="p_11404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11404"> $ program!4_10 pattern</p>
        <p id="p_11405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11405"> filel:This line of this file contains the word "pattern".</p>
        <p id="p_11406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11406"> myfile:This file also contains abcpatterndef.</p>
        <p id="p_11407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11407"> $</p>
        <p id="p_11408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11408"> Analysis</p>
        <p id="p_11409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11409"> Line 3 of this program opens the current directory. If it cannot be opened, line 4 calls  die, which terminates the program.</p>
        <p id="p_11410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11410"> Line 5 is actually three function calls in one, as follows:</p>
        <p id="p_11411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11411"> 1. readdir  retrieves a list of all of the files in the directory.</p>
        <p id="p_11412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11412"> 2. This list of files is passed to  grep,  which uses the  -r  file test operator to search for all files that the user has permission to read.</p>
        <p id="p_11413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11413"> 3.  This list of readable files is passed to another call to  grep,  which uses the expression ! /  A  \ . / to match all the files whose names do not begin with a period.</p>
        <p id="p_11414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11414"> The resulting list-all the files in the current directory that are readable and whose names do not start with a period-is assigned to  @filelist.</p>
        <p id="p_11415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11415"> The rest of the program contains nothing new. Line 6 closes the open directory, and lines</p>
        <p id="p_11416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11416"> 7-16 read each file in turn, searching for the word specified on the command line. (Recall that the built-in</p>
        <p id="p_11417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11417"> array  @ARGV  lists all the arguments supplied on the command line and that the first word specified on the</p>
        <p id="p_11418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11418"> command line is stored in $  ARGV  [  0  ].) Line 11 prints any lines containing the word to search for, using</p>
        <p id="p_11419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11419"> the format employed by the UNIX  grep  command (the filename, followed by :, followed by the line</p>
        <p id="p_11420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11420"> itself).</p>
        <p id="p_11421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11421"> The splice Function</p>
        <p id="p_11422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11422"> The  splice  function enables you to modify the list stored in an array variable. By passing the appropriate arguments to  splice,  you can add elements to the middle of a list, delete a portion of a list, or replace a portion of a list.</p>
        <p id="p_11423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11423"> The syntax for the  splice  function is</p>
        <p id="p_11424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11424"> retval = splice (array, skipelements, length, newlist)</p>
        <p id="p_11425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11425"> array  is the array variable containing the list to be spliced,  skipelements  is the number of elements to skip before splicing,  length  is the number of elements to be replaced,  newlist  is the list to be spliced in; this list can be stored in an array variable or specified explicitly.</p>
        <p id="p_11426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11426"> If  length  is greater than 0,  retval  is the list of elements replaced by  splice.</p>
        <p id="p_11427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11427"> The following sections provide examples of what you can do with  splice.</p>
        <p id="p_11428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11428"> Replacing List Elements</p>
        <p id="p_11429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11429"> You can use  splice  to replace a sublist (a set of elements in a list) with another sublist. The following is an example:</p>
        <p id="p_11430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11430"> @array = ("1", "2", "3", "4");</p>
        <p id="p_11431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11431"> splice (@array, 1, 2, ("two", "three"));</p>
        <p id="p_11432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11432"> This call to  splice  takes the list stored in  @array,  skips over the first element, and replaces the next two elements with the list ( "two" , "three"  ) . The new value of @ array  is the list</p>
        <p id="p_11433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11433"> ("1", "two", "three", "4")</p>
        <p id="p_11434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11434"> If the replacement list is longer than the original list, the elements to the right of the replaced list are pushed to the right. For example:</p>
        <p id="p_11435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11435"> @array = ("1", "2", "3", "4");</p>
        <p id="p_11436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11436"> splice (@array, 1, 2, ("two", "2.5", "three"));</p>
        <p id="p_11437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11437"> After this call, the new value of @ array  is the following:</p>
        <p id="p_11438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11438"> ("1", "two", "2.5", "three", "4")</p>
        <p id="p_11439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11439"> Similarly, if the replacement list is shorter than the original list, the elements to the right of the original list are moved left to fill the resulting gap. For example:</p>
        <p id="p_11440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11440"> @array = ("1", "2", "3",  "4");</p>
        <p id="p_11441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11441"> splice  (@array, 1, 2, "twothree");</p>
        <p id="p_11442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11442"> After this call to  splice,  @array  contains the following list:</p>
        <p id="p_11443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11443"> ("1", "twothree",  "4")</p>
        <p id="p_11444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11444"> NOTE</p>
        <p id="p_11445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11445"> You do not need to put parentheses around the list you pass to splice.  For example, the following two statements are equivalent:</p>
        <p id="p_11446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11446"> splice (@array, 1, 2, ("two", "three"));</p>
        <p id="p_11447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11447"> splice (@array, 1, 2, "two", "three")</p>
        <p id="p_11448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11448"> When the Perl interpreter sees the second form of  splice,  it assumes that the fourth and subsequent arguments are the replacement list.</p>
        <p id="p_11449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11449"> Listing 14.11 is an example of a program that uses  splice  to replace list elements. It reads a file containing a form letter, and replaces the string  &lt;name&gt;  with a name read from the standard input file. It then writes out the new letter.</p>
        <p id="p_11450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11450"> The output shown assumes that the file  form  contains</p>
        <p id="p_11451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11451"> Hello &lt;name&gt;!</p>
        <p id="p_11452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11452"> This is your lucky day, &lt;name&gt;!</p>
        <p id="p_11453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11453"> Type</p>
        <p id="p_11454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11454"> Listing  14.11. A  program that uses  splice  to replace list elements.</p>
        <p id="p_11455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11455"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11456"> 2 :</p>
        <p id="p_11457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11457"> 3: open (FORM, "form") || die ("Can't open form letter");</p>
        <p id="p_11458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11458"> 4: @form = &lt;FORM&gt;;</p>
        <p id="p_11459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11459"> 5: close (FORM);</p>
        <p id="p_11460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11460"> 6: $name = &lt;STDIN&gt;;</p>
        <p id="p_11461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11461"> 7: Qnameparts = split  (/\s  +  /,  $name);</p>
        <p id="p_11462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11462"> 8: foreach $line (@form) {</p>
        <p id="p_11463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11463"> 35 : 36:</p>
        <p id="p_11464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11464"> 37 :</p>
        <p id="p_11465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11465"> 38 : 39: 40 : 41:</p>
        <p id="p_11466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11466"> 42 :</p>
        <p id="p_11467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11467"> 43 :</p>
        <p id="p_11468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11468"> 44 :</p>
        <p id="p_11469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11469"> 45 :</p>
        <p id="p_11470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11470"> 46 :</p>
        <p id="p_11471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11471"> 47 :</p>
        <p id="p_11472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11472"> 48 :</p>
        <p id="p_11473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11473"> 49 :</p>
        <p id="p_11474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11474"> 50 : 51:</p>
        <p id="p_11475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11475"> 52 :</p>
        <p id="p_11476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11476"> 53 :</p>
        <p id="p_11477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11477"> 54 :</p>
        <p id="p_11478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11478"> 55 :</p>
        <p id="p_11479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11479"> 56 :</p>
        <p id="p_11480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11480"> 57 :</p>
        <p id="p_11481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11481"> 58 :</p>
        <p id="p_11482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11482"> 59 :</p>
        <p id="p_11483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11483"> 60 :</p>
        <p id="p_11484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11484"> }</p>
        <p id="p_11485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11485"> if ($form[$i] =~ / A \s*$/) {</p>
        <p id="p_11486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11486"> $~ = "FLUSH"; write;</p>
        <p id="p_11487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11487"> $~ = "BLANK"; write;</p>
        <p id="p_11488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11488"> $i++; next;</p>
        <p id="p_11489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11489"> }</p>
        <p id="p_11490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11490"> if ($writeline ne "" &amp;&amp;</p>
        <p id="p_11491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11491"> $writeline !~ / $/) { $writeline .= " ";</p>
        <p id="p_11492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11492"> }</p>
        <p id="p_11493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11493"> $writeline .= $form[$i];</p>
        <p id="p_11494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11494"> if (length ($writeline) &lt; 60) {</p>
        <p id="p_11495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11495"> $i++; next;</p>
        <p id="p_11496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11496"> }</p>
        <p id="p_11497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11497"> $~ = "WRITELINE";</p>
        <p id="p_11498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11498"> write;</p>
        <p id="p_11499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11499"> $i++;</p>
        <p id="p_11500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11500"> }</p>
        <p id="p_11501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11501"> format WRITELINE =</p>
        <p id="p_11502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11502"> $writeline</p>
        <p id="p_11503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11503"> format FLUSH</p>
        <p id="p_11504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11504"> 61 :</p>
        <p id="p_11505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11505"> 62:  A &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;--63: $writeline</p>
        <p id="p_11506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11506"> 64 : .</p>
        <p id="p_11507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11507"> 65: format BLANK =</p>
        <p id="p_11508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11508"> 6   6  :</p>
        <p id="p_11509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11509"> 67 : .</p>
        <p id="p_11510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11510"> Output</p>
        <p id="p_11511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11511"> $ programl4_ll Fred</p>
        <p id="p_11512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11512"> Hello Fred! This is your lucky day, Fred! $</p>
        <p id="p_11513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11513"> Analysis</p>
        <p id="p_11514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11514"> This program starts off by reading the entire form letter from the file named  form  into the array variable @  f orm.  This makes it possible to format the form letter output later on.</p>
        <p id="p_11515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11515"> Lines 6 and 7 read the name from the standard input file and break into individual words. This list of words is stored in the array variable  @namepart s.</p>
        <p id="p_11516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11516"> The loop in lines 8-28 reads each line in the form letter and looks for occurrences of the string  &lt;name&gt;. First, line 9 breaks the line into individual words. This list of words is stored in the array variable</p>
        <p id="p_11517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11517"> @words.</p>
        <p id="p_11518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11518"> The  while  loop starting in line 11 then examines each word of  @words  in turn. Line 12 checks whether the loop has reached the end of the list by calling  defined;  if the loop is past the end of the list, defined  will return false, indicating that the array element is not defined.</p>
        <p id="p_11519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11519"> Lines 13-15 check whether a word consists entirely of the string  &lt;name&gt;.  If it does, line 14 calls splice;  this call replaces the word  &lt;name&gt;  with the words in the name list  gnameparts.</p>
        <p id="p_11520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11520"> If a word is not equal to the string  &lt;name&gt;,  it might still contain  &lt;name&gt;  followed by a punctuation character. To test for this, line 16 tries to match the pattern /  A &lt;name&gt;/.  If it matches, lines 17 and 18 isolate the punctuation in a single word. This punctuation is stored in the scalar variable  $punc.</p>
        <p id="p_11521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11521"> Lines 19 and 20 create a copy of the name array  @nameparts  and append the punctuation to the last element of the array. This ensures that the punctuation will appear in the form letter where it is supposed to-right after the last character of the substituted name. Line 21 then calls  splice  as in line 14.</p>
        <p id="p_11522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11522"> After the words in  @words  have been searched and the name substituted for  &lt;name&gt;,  line  27  joins the words back into a single line. As an additional benefit, the multiple spaces and tabs in the original line have now been replaced by a single space, which will make the eventual formatted output look nicer.</p>
        <p id="p_11523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11523"> Lines 30-56 write out the output. The string to be written is stored in the scalar variable  $writeline. The program ensures that the form-letter output is formatted by doing the following:</p>
        <p id="p_11524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11524"> 1. First, the print format  WRITE LINE  is defined to use the  A &lt;&lt;&lt;&lt; value-field format. This format fits as much of the contents of  $writeline  into the line as possible and then deletes the part of $writeline  that has been written out.</p>
        <p id="p_11525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11525"> 2. Lines 36-43 enable you to add paragraphs to your form letter. Line 36 tests whether an input line is blank. If it is, the FLUSH print format is used to write out any output from previous lines that has not yet been printed. (Because the output line specified by  FLUSH  starts with the line is printed only if it is not blank-in other words, if  $writeline  actually contains some leftover text.) Then, the  BLANK  print format writes a blank line.</p>
        <p id="p_11526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11526"> 3. Lines 44-47 check whether a space needs to be placed between the end of one input line and the beginning of the next when formatting.</p>
        <p id="p_11527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11527"> 4.  Lines  49-52  ensure that  $writeline  is always long enough to fill the value field specified by WRITELINE.  This guarantees that there will be no unnecessary space in any of the output lines.</p>
        <p id="p_11528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11528"> 5. When @ f orm has been completely read, lines 32-34 ensure that all of the output from previous lines has been written by using the FLUSH print format.</p>
        <p id="p_11529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11529"> (For more information on the print formats used in this example, refer to  Day 11 ,  "Formatting Your Output.")</p>
        <p id="p_11530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11530"> NOTE</p>
        <p id="p_11531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11531"> You can use  splice  to splice the contents of a scalar variable into an array. For example:</p>
        <p id="p_11532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11532"> splice (@array, 8, 1, $name);</p>
        <p id="p_11533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11533"> This creates a one-element list consisting of the contents of  $name and adds it to the list stored in @ array  (as the eighth element)</p>
        <p id="p_11534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11534"> Appending List Elements</p>
        <p id="p_11535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11535"> You can use  splice  to add a sublist anywhere in a list. To do this, specify a length field of 0. For example:</p>
        <p id="p_11536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11536"> splice (@array, 5, 0, "Hello", "there");</p>
        <p id="p_11537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11537"> This call to  splice  adds the list  ("Hello", "there")  to the list stored in  @array. Hello becomes the new sixth element of  $list,  and  there  becomes the new seventh element; the existing sixth and seventh elements, if they exist, become the new eighth and ninth elements, and every other element is also pushed to the right.</p>
        <p id="p_11538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11538"> To add a new element to the end of an existing array, specify a  skipelement s  value of  -1,  as shown in the following:</p>
        <p id="p_11539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11539"> splice (@array, -1, 0, "Hello");</p>
        <p id="p_11540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11540"> This adds  Hello  as the last element of the list stored in @ array.</p>
        <p id="p_11541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11541"> Listing 14.12 is an example of a program that uses  splice  to insert an element into a list. This program inserts a word count after every tenth word in a file.</p>
        <p id="p_11542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11542"> ( Type</p>
        <p id="p_11543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11543"> Listing 14.12. A program that uses splice to insert array elements.</p>
        <p id="p_11544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11544"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11545"> 2 :</p>
        <p id="p_11546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11546"> 3: $count = 0;</p>
        <p id="p_11547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11547"> 4: while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_11548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11548"> 5 : chop ($line);</p>
        <p id="p_11549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11549"> 6: @words = split  (/\s  +  /,  $line);</p>
        <p id="p_11550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11550"> 7: $added = 0;</p>
        <p id="p_11551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11551"> 8: for ($i = 0; $i+$added &lt; @words; $i++) {</p>
        <p id="p_11552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11552"> o o</p>
        <p id="p_11553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11553"> 9:</p>
        <p id="p_11554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11554"> if ($count &gt; 0 &amp;&amp; ($count + $i)</p>
        <p id="p_11555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11555"> 10</p>
        <p id="p_11556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11556"> 0 ) {</p>
        <p id="p_11557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11557"> Output</p>
        <p id="p_11558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11558"> $ programl4_12</p>
        <p id="p_11559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11559"> Here is a line with some words on it.</p>
        <p id="p_11560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11560"> Here are some more test words to count. ABCDEFGHIJKLMNOP</p>
        <p id="p_11561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11561"> Here is a line with some words on it.</p>
        <p id="p_11562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11562"> Here 10 are some more test words to count. ABC20DEFGHI JKLM30NOP $</p>
        <p id="p_11563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11563"> Analysis</p>
        <p id="p_11564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11564"> This program, like many of the others you have seen, reads one line at a time and breaks the line into words; the array variable @  words  contains the list of words for a particular line.</p>
        <p id="p_11565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11565"> The scalar variable $  count  contains the number of words in the lines previously read. Lines  8  through</p>
        <p id="p_11566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11566"> 14 read each word in the current input line in turn; at any given point, the counting variable  $i  lists the number of words read in the line, and the sum of $  count  and  $i  lists the total number of words read in all input lines.</p>
        <p id="p_11567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11567"> Line 9 adds the value stored in $  count  to the value stored in  $i;  if this value, the current word number, is a multiple of ten, lines 10 and 11 call  splice  and insert the current word number into the list. As a result, every tenth word is followed by its word number.</p>
        <p id="p_11568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11568"> The scalar variable $  added  counts the number of elements added to the list; this ensures that the word numbers added by lines 10 and 11 are not included as part of the word count.</p>
        <p id="p_11569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11569"> After the word numbers have been inserted into the list, line 16 rebuilds the input line by joining the elements of @ words;  this new input line includes the word numbers. Line 17 then prints the rebuilt line.</p>
        <p id="p_11570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11570"> Deleting List Elements</p>
        <p id="p_11571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11571"> You can use  splice  to delete list elements without replacing them. To do this, call  splice  and omit the  newlist  argument. For example:</p>
        <p id="p_11572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11572"> ©deleted = splice (©array, 8, 2);</p>
        <p id="p_11573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11573"> This call to  splice  deletes the ninth and tenth elements of the list stored in  ©array.  If  ©array contains subsequent elements, these elements are shifted left to fill the gap. The list of deleted elements is returned and stored in  ©deleted.</p>
        <p id="p_11574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11574"> Listing 14.13 reads an input file, uses  splice  to delete all words greater than five characters long, and writes out the result.</p>
        <p id="p_11575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11575"> Listing  14.13. A  program that uses  splice  to delete words.</p>
        <p id="p_11576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11576"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11577"> 2 :</p>
        <p id="p_11578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11578"> 3: while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_11579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11579"> 4: ©words = split(/\s+/, $line);</p>
        <p id="p_11580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11580"> 5 :</p>
        <p id="p_11581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11581"> $1</p>
        <p id="p_11582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11582"> 0 ;</p>
        <p id="p_11583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11583"> 6:</p>
        <p id="p_11584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11584"> while  (defined(Swords[$i])) {</p>
        <p id="p_11585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11585"> 7: if (length($words[$i]) &gt; 5) {</p>
        <p id="p_11586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11586"> 8: splice(@words, $i, 1);</p>
        <p id="p_11587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11587"> Output</p>
        <p id="p_11588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11588"> $ programl4_13</p>
        <p id="p_11589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11589"> this is a test of the program which removes long words A D</p>
        <p id="p_11590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11590"> this is a test of the which long words</p>
        <p id="p_11591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11591"> $</p>
        <p id="p_11592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11592"> Analysis</p>
        <p id="p_11593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11593"> This program reads one line of input at a time and breaks each input line into words. Line 7 calls  length  to determine the length of a particular word. If the word is greater than five characters in length, line 8 calls  splice  to remove the word from the list.</p>
        <p id="p_11594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11594"> NOTE</p>
        <p id="p_11595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11595"> You also can omit the  length  argument when you call  splice.  If you do,  splice  deletes everything after the element specified by</p>
        <p id="p_11596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11596"> skipelements:</p>
        <p id="p_11597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11597"> splice (@array, 7);</p>
        <p id="p_11598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11598"> This deletes the seventh and all subsequent elements of the list stored in  @array.</p>
        <p id="p_11599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11599"> To delete the last element of a list, specify -1 as the</p>
        <p id="p_11600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11600"> skipelements  argument.</p>
        <p id="p_11601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11601"> splice (@array, -1);</p>
        <p id="p_11602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11602"> In all cases,  splice  returns the list of deleted elements</p>
        <p id="p_11603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11603"> The shift Function</p>
        <p id="p_11604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11604"> One list operation that is frequently needed in a program is to remove an element from the front of a list. Because this operation is often performed, Perl provides a special function,  shift,  that handles it.</p>
        <p id="p_11605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11605"> shift  removes the first element of the list and moves (or "shifts") every remaining element of the list to the left to cover the gap.  shift  then returns the removed element.</p>
        <p id="p_11606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11606"> The syntax for the  shift  function is</p>
        <p id="p_11607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11607"> element = shift (arrayvar);</p>
        <p id="p_11608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11608"> shift  is passed one argument: an array variable that contains a list,  element  is the returned element.</p>
        <p id="p_11609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11609"> Here is a simple example using  shift:</p>
        <p id="p_11610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11610"> @mylist -  ("1", "2", "3");</p>
        <p id="p_11611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11611"> $firstval = shift(@mylist);</p>
        <p id="p_11612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11612"> This call to  shift  removes the first element, 1, from the list stored in  @mylist.  This element is assigned to $  first val. @ my list  now contains the list  ("2", "3").</p>
        <p id="p_11613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11613"> If you do not specify an array variable when you call  shift,  the Perl interpreter assumes that  shift  is to remove the first element from the system array variable  @ARGV.  This variable lists the arguments supplied on the command line when the program is started up. For example, if you call a Perl program named  f oo  with the following command:</p>
        <p id="p_11614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11614"> foo argl arg2 arg3</p>
        <p id="p_11615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11615"> @ARGV  contains the list  ("argl", "arg2",  "arg3").</p>
        <p id="p_11616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11616"> This default feature of  shift  makes it handy for processing command-line arguments. Listing 14.14 is a simple program that prints out its arguments.</p>
        <p id="p_11617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11617"> Type</p>
        <p id="p_11618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11618"> Listing 14.14.  A  program that uses shift to process the command-line arguments.</p>
        <p id="p_11619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11619"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11620"> 2 :</p>
        <p id="p_11621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11621"> 3: while (1) {</p>
        <p id="p_11622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11622"> 4: $currarg = shift;</p>
        <p id="p_11623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11623"> 5: last if (!defined($currarg));</p>
        <p id="p_11624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11624"> 6: print ("$currarg\n");</p>
        <p id="p_11625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11625"> 7: }</p>
        <p id="p_11626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11626"> Output</p>
        <p id="p_11627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11627"> $ program!4_14 argl arg2 arg3</p>
        <p id="p_11628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11628"> argl</p>
        <p id="p_11629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11629"> arg2</p>
        <p id="p_11630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11630"> arg3</p>
        <p id="p_11631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11631"> $</p>
        <p id="p_11632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11632"> Analysis</p>
        <p id="p_11633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11633"> I When this program is called, the array variable  @ARGV  contains a list of the values supplied as arguments to the program. Line 4 calls  shift  to remove the first argument from the list and assign it</p>
        <p id="p_11634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11634"> to  Scurrarg.</p>
        <p id="p_11635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11635"> If there are no elements (or none remaining),  shift  returns the undefined value, and the call to defined  in line 5 returns false. This ensures that the loop terminates when there are no more arguments to read.</p>
        <p id="p_11636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11636"> The unshift Function</p>
        <p id="p_11637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11637"> To undo the effect of a  shift  function, call  un shift. The syntax for the  unshift  function is</p>
        <p id="p_11638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11638"> count = unshift (arrayvar, elements);</p>
        <p id="p_11639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11639"> arrayvar  is the list (usually stored in an array variable) to add to, and  elements  is the element or list of elements to add.  count  is the number of elements in the resulting list.</p>
        <p id="p_11640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11640"> The following is an example of a call to  un shift:</p>
        <p id="p_11641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11641"> unshift (@array, "newitem");</p>
        <p id="p_11642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11642"> This adds the element  newitem  to the front of the list stored in  @array.  The other elements of the list are moved to the right to accommodate the new item.</p>
        <p id="p_11643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11643"> You can use  unshift  to add more than one element to the front of an array. For example:</p>
        <p id="p_11644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11644"> unshift (@array, @sublistl, "newitem", @sublist2);</p>
        <p id="p_11645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11645"> This adds a list consisting of the list stored in  @sublist 1,  the element  newitem,  and the list stored in @ sublist 2  to the front of the list stored in  @array.</p>
        <p id="p_11646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11646"> un shift  returns the number of elements in the new list, as shown in the following:</p>
        <p id="p_11647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11647"> @array = (1, 2, 3) ;</p>
        <p id="p_11648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11648"> $num = unshift (@array, "newitem");</p>
        <p id="p_11649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11649"> This assigns  4  to  $num.</p>
        <p id="p_11650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11650"> NOTE</p>
        <p id="p_11651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11651"> The  un shift  function is equivalent to calling  splice  with a skipelements  value of 0 and a  length  value of 0. For example, the following statements are equivalent:</p>
        <p id="p_11652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11652"> unshift (@array, "iteml", "item2"); splice (@array, 0, 0, "iteml", "item2")</p>
        <p id="p_11653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11653"> The push Function</p>
        <p id="p_11654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11654"> As you have seen, the  un shift  function adds an element to the front of a list. To add an element to the end of a list, call the  push  function.</p>
        <p id="p_11655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11655"> The syntax for the  push  function is</p>
        <p id="p_11656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11656"> push (arrayvar, elements);</p>
        <p id="p_11657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11657"> arrayvar  is the list (usually stored in an array variable) to add to, and  elements  is the element or list of elements to add.</p>
        <p id="p_11658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11658"> The following is an example that uses  push:</p>
        <p id="p_11659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11659"> push (@array, "newitem");</p>
        <p id="p_11660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11660"> This adds the element  newitem  to the end of the list.</p>
        <p id="p_11661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11661"> The end of the list is always assumed to be the last defined element. For example, consider the following statements:</p>
        <p id="p_11662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11662"> @array = ("one", "two");</p>
        <p id="p_11663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11663"> $array[3] = "four"; push (@array, "five");</p>
        <p id="p_11664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11664"> Here, the first statement creates a two-element list and assigns it to @ array.  The second statement assigns  four  to the fourth element of @ array.  Because the fourth element is now the last element of @array,  the call to  push  creates a fifth element, even though the third element is undefined.  @array now contains the list</p>
        <p id="p_11665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11665"> ("one", "two", "four", "five");</p>
        <p id="p_11666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11666"> The undefined third element is, as always, equivalent to the null string.</p>
        <p id="p_11667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11667"> As with  un shift,  you can use  push  to add multiple elements to the end of a list, as in this example:</p>
        <p id="p_11668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11668"> push (@array, @sublistl, "newitem", @sublist2);</p>
        <p id="p_11669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11669"> Here, the list consisting of the contents of @  sublist 1,  the element  newitem,  and the contents of @sublist2  is added to the end of the list stored in @  array.</p>
        <p id="p_11670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11670"> NOTE</p>
        <p id="p_11671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11671"> push  is equivalent to a call to  splice  with the skipargument s  argument set to the length of the array. This means that the following statements are equivalent:</p>
        <p id="p_11672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11672"> push (@array, "newitem");</p>
        <p id="p_11673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11673"> splice (@array, @array, 0, "newitem")</p>
        <p id="p_11674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11674"> The pop Function</p>
        <p id="p_11675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11675"> The  pop  function undoes the effect of  push.  It removes the last element from the end of a list. The removed element is returned.</p>
        <p id="p_11676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11676"> The syntax for the  pop  function is</p>
        <p id="p_11677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11677"> element = pop (arrayvar);</p>
        <p id="p_11678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11678"> arrayvar  is the array element from which an element is to be removed,  element  is the returned element.</p>
        <p id="p_11679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11679"> For example, the following statement removes the last element from the list stored in @ array  and assigns it to the scalar variable  $popped:</p>
        <p id="p_11680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11680"> $popped = pop (@array);</p>
        <p id="p_11681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11681"> If the list passed to  pop  is empty,  pop  returns the undefined value.</p>
        <p id="p_11682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11682"> NOTE</p>
        <p id="p_11683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11683"> pop  is equivalent to a call to  splice  with a  skipelement s value of -1 (indicating the last element of the array). This means that the following statements behave in the same way:</p>
        <p id="p_11684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11684"> $popped = pop (@array);</p>
        <p id="p_11685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11685"> $popped = splice (@array, -1)</p>
        <p id="p_11686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11686"> Creating Stacks and Queues</p>
        <p id="p_11687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11687"> The functions you have just seen are handy for constructing two commonly used data structures: stacks and queues. The following sections provide examples that use a stack and a queue.</p>
        <p id="p_11688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11688"> Creating a Stack</p>
        <p id="p_11689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11689"> A  stack  is a data structure that behaves like a stack of plates in a cupboard: the last item added to the stack is always the first item removed. Data items that are added to the stack are said to be pushed onto the stack; items which are removed from the stack are popped off the stack.</p>
        <p id="p_11690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11690"> As you might have guessed, the functions  push  and  pop  enable you to create a stack in a Perl program. Listing 14.15 is an example of a program that uses a stack to perform arithmetic operations. It works as follows:</p>
        <p id="p_11691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11691"> 1. Two numbers are pushed onto the stack.</p>
        <p id="p_11692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11692"> 2. The program reads an arithmetic operator, such as + or The two numbers are popped off the stack, and the operation is performed.</p>
        <p id="p_11693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11693"> 3. The result of the operation is pushed onto the stack, enabling it to be used in further arithmetic operations.</p>
        <p id="p_11694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11694"> After all the arithmetic operations have been performed, the stack should consist of a single element, which is the final result.</p>
        <p id="p_11695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11695"> The numbers and operators are read from the standard input file.</p>
        <p id="p_11696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11696"> Note that Listing 14.15 is the "inverse" of Listing 9.12. In the latter program, the arithmetic operators appear first, followed by the values.</p>
        <p id="p_11697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11697"> Listing  14.15. A  program that uses a stack to perform arithmetic.</p>
        <p id="p_11698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11698"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11699"> 2 :</p>
        <p id="p_11700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11700"> 3: while (defined ($value = &amp;read_value)) {</p>
        <p id="p_11701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11701"> 4: if ($value =~ / A \d+$/) {</p>
        <p id="p_11702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11702"> 5: push (@stack, Svalue);</p>
        <p id="p_11703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11703"> 6: } else {</p>
        <p id="p_11704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11704"> 7: $firstpop = pop (@stack);</p>
        <p id="p_11705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11705"> 8: $secondpop = pop (@stack);</p>
        <p id="p_11706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11706"> 9: push (@stack,</p>
        <p id="p_11707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11707"> 10: &amp;do_math ($firstpop, $secondpop, $value));</p>
        <p id="p_11708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11708"> 11 : }</p>
        <p id="p_11709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11709"> 12  : }</p>
        <p id="p_11710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11710"> 13: $result = pop (@stack);</p>
        <p id="p_11711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11711"> 14: if (defined ($result)) {</p>
        <p id="p_11712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11712"> print ("The result is $result.\n") ;</p>
        <p id="p_11713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11713"> 15 :</p>
        <p id="p_11714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11714"> 16 :</p>
        <p id="p_11715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11715"> 17 :</p>
        <p id="p_11716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11716"> 18 :</p>
        <p id="p_11717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11717"> 19 :</p>
        <p id="p_11718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11718"> 20  : 21 : 22  :</p>
        <p id="p_11719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11719"> 23 :</p>
        <p id="p_11720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11720"> 24 :</p>
        <p id="p_11721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11721"> 25 : 26:</p>
        <p id="p_11722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11722"> 27 :</p>
        <p id="p_11723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11723"> 28 : 29: 30 : 31:</p>
        <p id="p_11724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11724"> 32 :</p>
        <p id="p_11725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11725"> 33 :</p>
        <p id="p_11726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11726"> 34 :</p>
        <p id="p_11727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11727"> 35 : 36:</p>
        <p id="p_11728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11728"> 37 :</p>
        <p id="p_11729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11729"> 38 : 39: 40 :</p>
        <p id="p_11730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11730"> } else {</p>
        <p id="p_11731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11731"> die ("Stack empty when printing result.\n");</p>
        <p id="p_11732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11732"> }</p>
        <p id="p_11733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11733"> sub read_value {</p>
        <p id="p_11734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11734"> local ($retval);</p>
        <p id="p_11735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11735"> $input =~  s/ A \s+//;</p>
        <p id="p_11736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11736"> while ($input eq "") {</p>
        <p id="p_11737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11737"> $input = &lt;STDIN&gt;; return if ($input eq $input =~  s/ A \s+//;</p>
        <p id="p_11738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11738"> }</p>
        <p id="p_11739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11739"> $input =~  s/ A \S+//;</p>
        <p id="p_11740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11740"> $retval = $&amp;;</p>
        <p id="p_11741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11741"> }</p>
        <p id="p_11742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11742"> sub do_math {</p>
        <p id="p_11743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11743"> local ($val2, $vall, $operator) = @_;</p>
        <p id="p_11744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11744"> local ($result);</p>
        <p id="p_11745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11745"> if (!defined($vall) | | !defined($val2)) {</p>
        <p id="p_11746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11746"> die ("Missing operand");</p>
        <p id="p_11747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11747"> }</p>
        <p id="p_11748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11748"> if ($operator =~  m. A [H—/*]$.  ) {</p>
        <p id="p_11749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11749"> eval ("\$result = \$val2 $operator  \$vall");</p>
        <p id="p_11750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11750"> 41</p>
        <p id="p_11751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11751"> } else {</p>
        <p id="p_11752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11752"> 42 :</p>
        <p id="p_11753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11753"> 43 :</p>
        <p id="p_11754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11754"> 44 :</p>
        <p id="p_11755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11755"> 45 :</p>
        <p id="p_11756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11756"> die ("Soperator is not an operator");</p>
        <p id="p_11757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11757"> }</p>
        <p id="p_11758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11758"> $result; # ensure the proper return value</p>
        <p id="p_11759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11759"> Output</p>
        <p id="p_11760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11760"> $ programl4_15 11 4 + 26 -A D</p>
        <p id="p_11761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11761"> The result is 11.</p>
        <p id="p_11762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11762"> $</p>
        <p id="p_11763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11763"> Analysis</p>
        <p id="p_11764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11764"> Before going into details, let's first take a look at how the program produces the final result,</p>
        <p id="p_11765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11765"> which is 11:</p>
        <p id="p_11766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11766"> 1. The program starts off by reading the numbers 11 and 4 and pushing them onto the stack. If the stack is listed from the top down, it now looks like this:</p>
        <p id="p_11767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11767"> 4</p>
        <p id="p_11768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11768"> 11</p>
        <p id="p_11769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11769"> Another way to look at the stack is this: At present, the list stored  in@stackis(ll,   4).</p>
        <p id="p_11770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11770"> 2. The program then reads the + operator, pops the 4 and 11 off the stack, and performs the addition, pushing the result onto the stack. The stack now contains a single value:</p>
        <p id="p_11771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11771"> 15</p>
        <p id="p_11772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11772"> 3. The next value, 2 6, is pushed onto the stack, which now looks like this:</p>
        <p id="p_11773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11773"> 26</p>
        <p id="p_11774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11774"> 15</p>
        <p id="p_11775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11775"> 4. The program then reads the - operator, pops 15 and 2 6 off the stack, and subtracts 15 from 2 6. The result, 11, is pushed onto the stack.</p>
        <p id="p_11776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11776"> 5. Because there are no more operations to perform, 11 becomes the final result.</p>
        <p id="p_11777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11777"> This program delegates to the subroutine  read_value  the task of reading values and operators. This</p>
        <p id="p_11778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11778"> subroutine reads a line of the standard input file and extracts the non-blank items on the line. Each call to read_value  extracts one item from an input line; when an input line is exhausted,  read_value reads the next one. When the input file is exhausted and there are no more items to return, $  input becomes the undefined value, which is equivalent to the null string; the call to  defined  in line 3 tests for this condition.</p>
        <p id="p_11779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11779"> If an item returned by  read_value  is a number, line 5 calls  push,  which pushes the number onto the stack. If an item is not a number, the program assumes it is an operator. At this point,  pop  is called twice to remove the last two numbers from the stack, and  do_math  is called to perform the arithmetic operation.</p>
        <p id="p_11780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11780"> The  do_math  subroutine uses a couple of tricks. First,  defined  is called to see whether there are, in fact, two numbers to add. If one or both of the numbers does not exist, the program terminates.</p>
        <p id="p_11781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11781"> Next, the subroutine uses the pattern  m.  A   [+-*/]$.  to check whether the character string stored in $ operator  is, in fact, a legal arithmetic operator. (Recall that you can use a pattern delimiter other than / by specifying m followed by the character you want to use as the delimiter. In this case, the period character is the pattern delimiter.)</p>
        <p id="p_11782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11782"> Finally, the subroutine calls  eval  to perform the arithmetic operation,  eval  replaces the name $ operator  with its current value, and then treats the resulting character string as an executable statement; this performs the arithmetic operation specified by  Soperator.  Using  eval  here saves space; the only alternative is to use a complicated  if-elseif  structure.</p>
        <p id="p_11783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11783"> The result of the operation is returned in  $result.  Lines 9 and 10 then pass this value to  push,  which pushes the result onto the stack. This enables you to use the result in subsequent operations.</p>
        <p id="p_11784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11784"> When the last arithmetic operation has been performed, the final result is stored as the top element of the stack. Line 13 pops this element, and line 15 prints it.</p>
        <p id="p_11785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11785"> Note that this program always assumes that the last element pushed onto the stack is to be on the left of the arithmetic operation. To reverse this, all you need to do is change the order of  $vall  and  $val2  in line 33. (Some programs that manipulate stacks also provide an operation which reverses the order of the top two elements of a stack.)</p>
        <p id="p_11786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11786"> Warning</p>
        <p id="p_11787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11787"> The  pop  function returns the undefined value if the stack is empty. Because the undefined value is equivalent to the null string, and the null string is treated as 0 in arithmetic operations, your program will not complain if you try to pop a number from an empty stack.</p>
        <p id="p_11788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11788"> To ensure that you get the result you want, always call  defined after you call  pop  to ensure that a value has actually been popped from the stack</p>
        <p id="p_11789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11789"> Creating a Queue</p>
        <p id="p_11790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11790"> A  queue  is a data structure that processes data in the order in which it is entered; such data structures are known as first-in, first-out (or FIFO) structures. (A stack, on the other hand, is an example of a last-in, first-out, or LIFO, structure.)</p>
        <p id="p_11791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11791"> To create a queue, use the function  push  to add items to the queue, and call  shift  to remove elements from it. Because  push  adds to the right of the list and  shift  removes from the left, elements are processed in the order in which they appear.</p>
        <p id="p_11792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11792"> Listing 14.16 is an example of a program that uses a queue to add a set of numbers retrieved via a pipe. Each input line can consist of more than one number, and the numbers are added in the order listed.</p>
        <p id="p_11793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11793"> The input/output example shown for this listing assumes that the numbers retrieved via the pipe are 11, 12, and 13.</p>
        <p id="p_11794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11794"> Type</p>
        <p id="p_11795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11795"> Listing 14.16. A program that illustrates the use of a queue.</p>
        <p id="p_11796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11796"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11797"> 2 :</p>
        <p id="p_11798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11798"> 3: open (PIPE, "numbers|") I |</p>
        <p id="p_11799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11799"> 4: die ("Can't open pipe");</p>
        <p id="p_11800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11800"> 5: $result = 0;</p>
        <p id="p_11801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11801"> 6: while (defined ($value = Sreadnum)) {</p>
        <p id="p_11802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11802"> 7: $result += Svalue;</p>
        <p id="p_11803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11803"> }</p>
        <p id="p_11804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11804"> Output</p>
        <p id="p_11805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11805"> $ programl4_16 The result is 36. $</p>
        <p id="p_11806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11806"> This program assumes that a program named  numbers  exists, and that its out-put is a stream of numbers. Multiple numbers can appear on a single line of this output. Lines 3 and 4 associate the file variable  PIPE  with the output from the  numbers  command.</p>
        <p id="p_11807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11807"> Lines  6-8  call the subroutine  readnum  to obtain a number and then add it to the result stored in $result.  This subroutine reads input from the pipe, breaks it into individual numbers, and then calls</p>
        <p id="p_11808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11808"> Analysis</p>
        <p id="p_11809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11809"> push  to add the numbers to the queue stored in @ queue.  Line 20 then calls  shift  to retrieve the first element in the queue, which is returned to the main program.</p>
        <p id="p_11810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11810"> If an input line is blank, the call to  split  in line 17 produces the empty list, which means that nothing is added to @ queue.  This ensures that input is read from the pipe until a non-blank line is read or until the input is exhausted.</p>
        <p id="p_11811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11811"> The split Function</p>
        <p id="p_11812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11812"> The  split  function was first discussed on  Day  5 , "Lists and Array Variables." It splits a character string into a list of elements.</p>
        <p id="p_11813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11813"> The usual syntax for the  split  function is</p>
        <p id="p_11814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11814"> list = split (pattern, value);</p>
        <p id="p_11815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11815"> Here,  value  is the character string to be split,  pattern  is a pattern to be searched for.  A  new element is started every time  pattern  is matched,  (pattern  is not included as part of any element.) The resulting list of elements is returned in  list.</p>
        <p id="p_11816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11816"> For example, the following statement breaks the character string stored in  $line  into elements, which are stored in @ list:</p>
        <p id="p_11817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11817"> @list = split  (/:/,  $line);</p>
        <p id="p_11818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11818"> A new element is started every time the pattern / : / is matched. If  $line  contains This : is : a : string,  the resulting list is  ("This", "is", "a", "string").</p>
        <p id="p_11819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11819"> If you like, you can specify the maximum number of elements of the list produced by  split  by specifying the maximum as the third argument. For example:</p>
        <p id="p_11820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11820"> $line = "This:is:a:string"; @list = split  (/:/,  $line, 3);</p>
        <p id="p_11821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11821"> As before, this breaks the string stored in  $line  into elements. After three elements have been created, no more new elements are created. Any subsequent matches of the pattern are ignored. In this case, the list assigned to @  list  is  ("This", "is", "a: string").</p>
        <p id="p_11822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11822"> TIP</p>
        <p id="p_11823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11823"> If you use  split  with a limit, you can assign to several scalar variables at once:</p>
        <p id="p_11824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11824"> $line = "11 12 13 14 15";</p>
        <p id="p_11825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11825"> ($varl, $var2, $line) = split (/\s+/, $line,</p>
        <p id="p_11826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11826"> 3) ;</p>
        <p id="p_11827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11827"> This splits $  line  into the list  ("11", "12", "13 14 15"). $varl  is assigned  11, $var2  is assigned  12,  and  $line  is assigned "13 14  15".  This enables you to assign the "leftovers" to a single variable, which can then be split again at a later time</p>
        <p id="p_11828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11828"> The sort and reverse Functions</p>
        <p id="p_11829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11829"> The  sort  function sorts a list in alphabetical order, as follows:</p>
        <p id="p_11830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11830"> @sorted = sort (@list);</p>
        <p id="p_11831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11831"> The sorted list is returned.</p>
        <p id="p_11832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11832"> The  reverse  function reverses the order of a list:</p>
        <p id="p_11833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11833"> @reversed = reverse (@list);</p>
        <p id="p_11834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11834"> For more information on the  sort  and  reverse  functions, see  Day  5 .  For information on how you can specify the sort order that  sort  is to use, see  Day  9 ,  "Using Subroutines."</p>
        <p id="p_11835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11835"> The map Function</p>
        <p id="p_11836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11836"> The  map  function, defined only in Perl 5, enables you to use each of the elements of a list, in turn, as an operand in an expression.</p>
        <p id="p_11837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11837"> The syntax for the  map  function is</p>
        <p id="p_11838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11838"> resultlist = map(expr, list);</p>
        <p id="p_11839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11839"> list  is the list of elements to be used as operands or arguments; this list is copied by  map,  but is not itself changed, expr is the expression to be repeated. The results of the repeated evaluation of the</p>
        <p id="p_11840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11840"> expression are stored in a list, which is returned in  resultlist.</p>
        <p id="p_11841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11841"> expr  assumes that the system variable $_ contains the element of the list currently being used as an operand. For example:</p>
        <p id="p_11842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11842"> @list = (100, 200, 300) ;</p>
        <p id="p_11843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11843"> @results = map($_+l, @list);</p>
        <p id="p_11844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11844"> This evaluates the expression  $_+l  for each of  100, 2 00,  and  300  in turn. The results,  101, 2 01,  and 301,  respectively, are formed into the list  (101, 201, 301).  This list is then assigned to</p>
        <p id="p_11845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11845"> @results.</p>
        <p id="p_11846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11846"> To use  map  with a subroutine, just pass $_ to the subroutine, as in the following:</p>
        <p id="p_11847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11847"> @results = map(&amp;mysub($_), @list);</p>
        <p id="p_11848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11848"> This calls the subroutine  my sub  once for each element of the list stored in  @list.  The values returned by  my sub  are stored in a list, which is assigned to  @results.</p>
        <p id="p_11849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11849"> This also works with built-in functions:</p>
        <p id="p_11850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11850"> @results = map(chr($_), @list);</p>
        <p id="p_11851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11851"> @results = map(chr, @list); # same as above,</p>
        <p id="p_11852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11852"> Asince $_ is the default argument for chr</p>
        <p id="p_11853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11853"> This converts each element of the list in @  list  to its ASCII character equivalent. The resulting list of characters is stored  in@results.</p>
        <p id="p_11854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11854"> NOTE</p>
        <p id="p_11855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11855"> For more information on the $_ system variable, refer to  Day 17</p>
        <p id="p_11856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11856"> The wantarray Function</p>
        <p id="p_11857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11857"> In Perl, the behavior of some built-in functions depends on whether they are dealing with scalar values or lists. For example, the  chop  function either chops the last character of a single string or chops the last character of every element of a list:</p>
        <p id="p_11858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11858"> chop($scalar) ;</p>
        <p id="p_11859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11859"> # chop a single string</p>
        <p id="p_11860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11860"> chop (@array); # chop every element of an array</p>
        <p id="p_11861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11861"> Perl 5 enables you to define similar two-way behavior for your subroutines using the  wantarray function. (This function is not defined in Perl 4.)</p>
        <p id="p_11862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11862"> The syntax for the  wantarray  function is</p>
        <p id="p_11863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11863"> result = wantarray();</p>
        <p id="p_11864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11864"> result  is a non-zero value if the subroutine is expected to return a list, and is zero if the subroutine is expected to return a scalar value.</p>
        <p id="p_11865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11865"> Listing 14.17 illustrates how  wantarray  works.</p>
        <p id="p_11866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11866"> Listing 14.17. A program that uses the wantarray function.</p>
        <p id="p_11867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11867"> 1: #!/usr/local/bin/perl</p>
        <p id="p_11868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11868"> 2 :</p>
        <p id="p_11869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11869"> 3: @array = &amp;mysub();</p>
        <p id="p_11870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11870"> 4: $scalar = &amp;mysub();</p>
        <p id="p_11871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11871"> 5 :</p>
        <p id="p_11872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11872"> 6: sub mysub {</p>
        <p id="p_11873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11873"> 7: if (wantarray()) {</p>
        <p id="p_11874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11874"> 8: print ("true\n");</p>
        <p id="p_11875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11875"> 9: } else {</p>
        <p id="p_11876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11876"> 10: print ("false\n");</p>
        <p id="p_11877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11877"> 11:</p>
        <p id="p_11878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11878"> }</p>
        <p id="p_11879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11879"> 12  : }</p>
        <p id="p_11880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11880"> Output</p>
        <p id="p_11881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11881"> $ programl4_17</p>
        <p id="p_11882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11882"> true</p>
        <p id="p_11883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11883"> false</p>
        <p id="p_11884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11884"> $</p>
        <p id="p_11885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11885"> Analysis</p>
        <p id="p_11886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11886"> I When  my sub  is first called in line 3, the return value is expected to be a list, which means that  wantarray  returns a non-zero (true) value in line 7. The second call to  mysub  in line 4 expects a scalar return value, which means that  wantarray  returns zero (false).</p>
        <p id="p_11887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11887"> Associative Array Functions</p>
        <p id="p_11888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11888"> Perl provides a variety of functions that operate on associative arrays. Most of these functions are described in detail on  Day  10 ,  "Associative Arrays"; a brief description of each function is presented here.</p>
        <p id="p_11889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11889"> The keys Function</p>
        <p id="p_11890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11890"> The  keys  function returns a list of the subscripts of the elements of an associative array. The syntax for  keys  is straightforward:</p>
        <p id="p_11891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11891"> list = keys (assoc_array);</p>
        <p id="p_11892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11892"> assoc_array  is the associative array from which subscripts are to be extracted, and  list  is the returned list of subscripts.</p>
        <p id="p_11893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11893"> For example:</p>
        <p id="p_11894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11894"> %array =</p>
        <p id="p_11895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11895"> ("foo", 26, "bar", 17);</p>
        <p id="p_11896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11896"> @list = keys(%array);</p>
        <p id="p_11897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11897"> This call to  keys  assigns ( "  f oo" , "bar")  to @  list.  (The elements of the list might be in a different order. To specify a particular order, sort the list using the  sort  function.)</p>
        <p id="p_11898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11898"> keys  often is used with  f oreach,  as in the following example:</p>
        <p id="p_11899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11899"> foreach $subscript (keys (%array)) {</p>
        <p id="p_11900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11900"> # stuff goes here</p>
        <p id="p_11901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11901"> }</p>
        <p id="p_11902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11902"> This loops once for each subscript of the array.</p>
        <p id="p_11903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11903"> The values Function</p>
        <p id="p_11904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11904"> The  values  function returns a list consisting of all the values in an associative array.</p>
        <p id="p_11905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11905"> The syntax for the  values  function is</p>
        <p id="p_11906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11906"> list = values (assoc_array);</p>
        <p id="p_11907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11907"> assoc_array  is the associative array from which values are to be extracted, and  list  is the returned list of values.</p>
        <p id="p_11908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11908"> The following is an example that uses  values:</p>
        <p id="p_11909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11909"> %array = ("foo", 26, "bar", 17);</p>
        <p id="p_11910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11910"> @list = values(%array);</p>
        <p id="p_11911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11911"> This assigns the list  (26, 17)to@list  (not necessarily in this order).</p>
        <p id="p_11912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11912"> The each Function</p>
        <p id="p_11913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11913"> The  each  function returns an associative array element as a two-element list. The list consists of the associative array subscript and its associated value. Successive calls to  each  return another associative array element.</p>
        <p id="p_11914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11914"> The syntax for the  each  function is</p>
        <p id="p_11915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11915"> pair = each (assoc_array);</p>
        <p id="p_11916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11916"> assoc_array  is the associative array from which pairs are to be returned, and  pair  is the subscript-element pair returned.</p>
        <p id="p_11917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11917"> The following is an example:</p>
        <p id="p_11918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11918"> %array = ("foo", 26, "bar", 17);</p>
        <p id="p_11919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11919"> @list = each(%array);</p>
        <p id="p_11920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11920"> The first call to  each  assigns either  ("foo", 26)  or  ("bar", 17)  to @  list. A  subsequent call returns the other element, and a third call returns an empty list. (The order in which the elements are returned depends on how the list is stored; no particular order is guaranteed.)</p>
        <p id="p_11921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11921"> The delete Function</p>
        <p id="p_11922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11922"> The  delete  function deletes an associative array element.</p>
        <p id="p_11923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11923"> The syntax for the  delete  function is</p>
        <p id="p_11924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11924"> element = delete (assoc_array_item);</p>
        <p id="p_11925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11925"> assoc_array_item  is the associative array element to be deleted, and  element  is the value of the deleted element.</p>
        <p id="p_11926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11926"> The following is an example:</p>
        <p id="p_11927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11927"> %array = ("foo", 26, "bar", 17); $retval = delete ($array{"foo"});</p>
        <p id="p_11928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11928"> After  delete  is called, the associative array  %array  contains only one element: the element with the subscript  bar. $retval  is assigned the value of the deleted element  foo,  which in this case is 26.</p>
        <p id="p_11929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11929"> The exists Function</p>
        <p id="p_11930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11930"> The  exists  function, defined only in Perl 5, enables you to determine whether a particular element of an associative array exists.</p>
        <p id="p_11931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11931"> The syntax for the  exists  function is</p>
        <p id="p_11932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11932"> result = exists(element) ;</p>
        <p id="p_11933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11933"> element  is the element of the associative array that is being tested for existence,  result  is non-zero if the element exists, and zero if it does not.</p>
        <p id="p_11934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11934"> The following is an example:</p>
        <p id="p_11935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11935"> $result = exists ($myarray{$mykey});</p>
        <p id="p_11936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11936"> $result  is nonzero if  $myarray { $mykey } exists.</p>
        <p id="p_11937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11937"> Summary</p>
        <p id="p_11938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11938"> Today, you learned about functions that manipulate scalar values and convert them from one form to another, and about functions that manipulate lists.</p>
        <p id="p_11939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11939"> The  chop  function removes the last character from a scalar value or from each element of a list.</p>
        <p id="p_11940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11940"> The  crypt  function encrypts a scalar value, using the same method that the UNIX password encryptor uses.</p>
        <p id="p_11941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11941"> The  int  function takes a floating-point number and gets rid of everything after the decimal point.</p>
        <p id="p_11942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11942"> The  defined  function checks whether a scalar variable, array element, or array has been assigned to. The  undef  function enables you to treat a previously defined scalar variable, array element, or array as if it is undefined,  scalar  enables you to treat an array or list as if it is a scalar value.</p>
        <p id="p_11943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11943"> The other functions described in today's lesson convert values from one form into another. The  hex  and oct  functions read hexadecimal and octal constants and convert them into decimal form. The  ord function converts a character into its ASCII decimal equivalent,  pack  and  unpack  convert a scalar value into a format that can be stored in machine memory, and vice versa,  vec  enables you to treat a value as an array of numeric values, each of which is a certain number of bits long.</p>
        <p id="p_11944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11944"> The  grep  function enables you to extract the elements of a list that match a particular pattern. This function can be used in conjunction with the file-test operators.</p>
        <p id="p_11945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11945"> The  splice  function enables you to extract a portion of a list or insert a sub list into a list. The  shift</p>
        <p id="p_11946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11946"> and  pop  functions remove an element from the left and right ends of a list, and the  unshift  and  push functions add one or more elements to the left and right ends of a list. You can use  push, pop,  and shift  to create stacks and queues.</p>
        <p id="p_11947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11947"> The  split  function enables you to break a character string into list elements. You can impose an upper limit on the number of list elements to be created.</p>
        <p id="p_11948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11948"> The  sort  function sorts a list in a specified order. The  reverse  function reverses the order of the elements in a list.</p>
        <p id="p_11949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11949"> The  map  function copies a list and then performs an operation on every element of the list.</p>
        <p id="p_11950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11950"> The  wantarray  function enables you to determine whether the statement that called a subroutine is expecting a scalar return value or a list.</p>
        <p id="p_11951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11951"> Five functions are defined that manipulate associative arrays:</p>
        <p id="p_11952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11952"> • keys,  which returns a list of the array subscripts</p>
        <p id="p_11953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11953"> • values,  which returns a list of the array values</p>
        <p id="p_11954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11954"> • each,  which returns a two-element list consisting of an array subscript and its value</p>
        <p id="p_11955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11955"> • delete,  which deletes an element</p>
        <p id="p_11956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11956"> •  exists,  which checks whether a particular element exists</p>
        <p id="p_11957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11957"> Q&amp;A</p>
        <p id="p_11958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11958"> Q: Why is the undefined value equivalent to the null string?</p>
        <p id="p_11959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11959"> A: Basically, to keep Perl programs from blowing up if they try to access a variable that has not yet</p>
        <p id="p_11960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11960"> been assigned to.</p>
        <p id="p_11961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11961"> Q: Why does oct handle hexadecimal constants that start with Ox or OX?</p>
        <p id="p_11962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11962"> A: There is no particular reason, except that it's a little more convenient. If you find that it bothers</p>
        <p id="p_11963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11963"> you to use  oct  to convert a hexadecimal constant, get rid of the leading  Ox  or  OX  (using the substitute operator) and call  hex  instead.</p>
        <p id="p_11964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11964"> Q: I want to put a password check in my program. How can I ensure that it is secure?</p>
        <p id="p_11965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11965"> A: Do two things:</p>
        <p id="p_11966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11966"> • Don't include the unencrypted text of your password in your program source. People can then find out the password just by reading the file.</p>
        <p id="p_11967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11967"> • Use a password that is not a real English-language word or proper name. Include at least one digit. This makes your password harder to "crack."</p>
        <p id="p_11968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11968"> Q: Why does int truncate instead of rounding?</p>
        <p id="p_11969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11969"> A: Some programs might find it useful to just retrieve the integer part of a floating-point number.</p>
        <p id="p_11970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11970"> (For example, in earlier chapters, you have seen  int  used in conjunction with  rand  to return a random integer.)</p>
        <p id="p_11971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11971"> You can always add  0 . 5  to your number before calling  int,  which will effectively round it up when necessary.</p>
        <p id="p_11972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11972"> Q: When I pack integers using the s or i pack-format characters, the bits don't appear in the</p>
        <p id="p_11973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11973"> order I was expecting. What is happening?</p>
        <p id="p_11974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11974"> A: Most machines enable you to store integers that are more than one byte long (two- and four-byte</p>
        <p id="p_11975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11975"> integers usually are supported). However, each machine does not store a multibyte integer in the same way. Some machines store the most significant byte of a word at a lower address; these machines are called  big-endian  machines because the big end of a word is first. Other machines, called  little-endian  machines, store the least significant byte of a word at a lower byte address.</p>
        <p id="p_11976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11976"> If you are not getting the result you expect, you might be expecting big-endian and getting little-endian, or vice versa.</p>
        <p id="p_11977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11977"> Q: The splice function works by shifting elements to the right or left to make room or fill</p>
        <p id="p_11978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11978"> gaps. Is this inefficient?</p>
        <p id="p_11979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11979"> A: No. The Perl interpreter actually stores a list as a sequence of pointers (memory addresses). All</p>
        <p id="p_11980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11980"> splice  has to do is rearrange the pointers. This holds true also for  sort  and  reverse.</p>
        <p id="p_11981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11981"> Q: Can I use each to work through an associative array in a specified order?</p>
        <p id="p_11982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11982"> A: No. If you need to access the elements of an associative array in a specified order, use  keys  and</p>
        <p id="p_11983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11983"> sort  to sort the subscripts, and then retrieve the value associated with each element.</p>
        <p id="p_11984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11984"> Q: If I am using values with f oreach, can I retrieve the subscript associated with a</p>
        <p id="p_11985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11985"> particular value if I need it?</p>
        <p id="p_11986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11986"> A: No. If you are likely to need the subscripts as well as their values, use  each  or  keys.</p>
        <p id="p_11987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11987"> Workshop</p>
        <p id="p_11988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11988"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_11989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11989"> Quiz</p>
        <p id="p_11990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11990"> 1. What format does each of the following pack-format characters specify?</p>
        <p id="p_11991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11991"> a. A</p>
        <p id="p_11992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11992"> b. A</p>
        <p id="p_11993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11993"> c. d</p>
        <p id="p_11994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11994"> d . p</p>
        <p id="p_11995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11995"> e. @</p>
        <p id="p_11996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11996"> 2. What do these unpack-format specifiers do?</p>
        <p id="p_11997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11997"> II n If</p>
        <p id="p_11998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11998"> d  •  d</p>
        <p id="p_11999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_11999"> b. "@4Al0i*"</p>
        <p id="p_12000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12000"> c. "@ *X4C*"</p>
        <p id="p_12001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12001"> d. "ix4iX8i"</p>
        <p id="p_12002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12002"> e. "b*X*B*"</p>
        <p id="p_12003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12003"> 3. What value is stored in  $value  by the following?</p>
        <p id="p_12004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12004"> a. The statements</p>
        <p id="p_12005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12005"> $vector = pack ("b*", "10110110");</p>
        <p id="p_12006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12006"> $value = vec ($vector, 3, 1);</p>
        <p id="p_12007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12007"> b . The statements</p>
        <p id="p_12008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12008"> $vector = pack ("b*", "10110110");</p>
        <p id="p_12009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12009"> Svalue = vec ($vector, 1, 2);</p>
        <p id="p_12010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12010"> 4. What's the difference between  defined  and  undef  ?</p>
        <p id="p_12011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12011"> 5. Assume @  list  contains  ("1",   "2", "3", "4",  "  5  ").  What are the contents of @  list after the following statement?</p>
        <p id="p_12012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12012"> a. splice (@list, 0, 1, "new");</p>
        <p id="p_12013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12013"> b. splice (@list,  2 ,  0, "testl", "test2");</p>
        <p id="p_12014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12014"> c. splice (@list,  -1 ,  1 ,  "testl", "test2");</p>
        <p id="p_12015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12015"> d. splice (@list, 2, 1);</p>
        <p id="p_12016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12016"> e. splice (@list, 3);</p>
        <p id="p_12017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12017"> 6. What do the following statements return?</p>
        <p id="p_12018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12018"> a. grep  (!/ A !/,  @array);</p>
        <p id="p_12019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12019"> b. grep (/\b\d+\b/, @array);</p>
        <p id="p_12020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12020"> c. grep  (/./,  @array);</p>
        <p id="p_12021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12021"> d. grep  (//,  @array);</p>
        <p id="p_12022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12022"> 7. What is the difference between  shift  and  unshift?</p>
        <p id="p_12023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12023"> 8.  What arguments to  splice  are equivalent to the following function calls?</p>
        <p id="p_12024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12024"> a. shift (@array);</p>
        <p id="p_12025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12025"> b. pop (@array);</p>
        <p id="p_12026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12026"> c. push (@array, @sublist);</p>
        <p id="p_12027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12027"> d. unshift (@array, @sublist);&gt;</p>
        <p id="p_12028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12028"> 9. How can you create a stack using  shift, pop, push,  or  unshift?</p>
        <p id="p_12029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12029"> 10. How can you create a queue using  shift, pop, push,  or  unshift?</p>
        <p id="p_12030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12030"> Exercises</p>
        <p id="p_12031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12031"> 1. Write a program that reads two binary strings of  any  length, adds them together, and writes out the binary output. (Hint: This is a really nasty problem. To get this to work, you will need to ensure that your bit strings are a multiple of eight bits by adding zeros at the front.)</p>
        <p id="p_12032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12032"> 2. Write a program that reads two hexadecimal strings of any length, adds them together, and writes out the hexadecimal output. (Hint: This is a straightforward modification of Exercise 1.)</p>
        <p id="p_12033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12033"> 3. Write a program that uses int to round a value to two decimal places. (Hint: This is trickier than it seems.)</p>
        <p id="p_12034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12034"> 4. Write a program that encrypts a password and then asks the user to guess it. Give the user three chances to get it right.</p>
        <p id="p_12035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12035"> 5.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_12036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12036"> #!/usr/local/bin/perl $bitstring = "00000011";</p>
        <p id="p_12037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12037"> $packed = pack("b*", $bitstring);</p>
        <p id="p_12038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12038"> $highbit = vec($packed, 0, 1);</p>
        <p id="p_12039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12039"> print ("The high-order bit is $highbit\n");</p>
        <p id="p_12040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12040"> 6. Write a program that uses  splice  to sort a list in numeric order.</p>
        <p id="p_12041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12041"> 7. Write a program that "flips" an associative array; that is, the subscripts of the old array become the values of the new, and vice versa. Print an error message if the old array has two subscripts with identical values.</p>
        <p id="p_12042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12042"> 8.  Write a program that reads a file from standard input, breaks each line into words, uses  grep  to get rid of all words longer than five characters, and prints the file.</p>
        <p id="p_12043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12043"> 9.  Write a program that reads an input line and uses  split  to read and print one word of the line at a time.</p>
        <p id="p_12044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12044"> 10. BUG BUSTER: What is wrong with the following subroutine?</p>
        <p id="p_12045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12045"> sub retrieve_first_element { local ($retval);</p>
        <p id="p_12046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12046"> $retval = unshift(@array);</p>
        <p id="p_12047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12047"> }</p>
        <p id="p_12048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12048"> Week 2 in Review</p>
        <p id="p_12049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12049"> By now, you know enough about programming in Perl to write some quite powerful programs. The program in Listing R2.1 illustrates some of the concepts you've learned this week. It prompts you for a directory name, lists the subdirectories for that directory, and stores them in an associative array for later access. It also enables you to move about in the directory hierarchy and print the names of the files in any directory.</p>
        <p id="p_12050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12050"> Type</p>
        <p id="p_12051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12051"> Listing R2.1. Browsing directories and printing their contents.</p>
        <p id="p_12052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12052"> # if we don't have a current directory, get one if ($curdir eq "") {</p>
        <p id="p_12053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12053"> print ("Enter directory to list:\n"); $curdir = &lt;STDIN&gt;;</p>
        <p id="p_12054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12054"> $curdir =~  s/ A \s+|\s+$//g;</p>
        <p id="p_12055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12055"> $curdir = &amp;followlink($curdir); &amp;readsubdirs($curdir);</p>
        <p id="p_12056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12056"> 13:</p>
        <p id="p_12057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12057"> }</p>
        <p id="p_12058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12058"> 14 :</p>
        <p id="p_12059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12059"> 15 :</p>
        <p id="p_12060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12060"> 16 :</p>
        <p id="p_12061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12061"> 17 :</p>
        <p id="p_12062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12062"> 18 :</p>
        <p id="p_12063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12063"> 19 :</p>
        <p id="p_12064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12064"> 20  :</p>
        <p id="p_12065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12065"> 21 :</p>
        <p id="p_12066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12066"> 22  :</p>
        <p id="p_12067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12067"> 23 :</p>
        <p id="p_12068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12068"> 24 :</p>
        <p id="p_12069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12069"> 25 : 26:</p>
        <p id="p_12070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12070"> 27 :</p>
        <p id="p_12071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12071"> 28 :</p>
        <p id="p_12072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12072"> 29 :</p>
        <p id="p_12073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12073"> 30 :</p>
        <p id="p_12074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12074"> 31:</p>
        <p id="p_12075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12075"> 32 : 33:</p>
        <p id="p_12076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12076"> 34 :</p>
        <p id="p_12077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12077"> 35 :</p>
        <p id="p_12078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12078"> 36 :</p>
        <p id="p_12079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12079"> 37 :</p>
        <p id="p_12080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12080"> 38 : array</p>
        <p id="p_12081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12081"> $curdir = Smenudir($curdir);</p>
        <p id="p_12082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12082"> }</p>
        <p id="p_12083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12083"> # Find all subdirectories of the given directory,</p>
        <p id="p_12084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12084"> # and store them in an associative array.</p>
        <p id="p_12085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12085"> #</p>
        <p id="p_12086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12086"> # The associative array subscripts and values are:</p>
        <p id="p_12087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12087"> # &lt;directory name&gt;: 1</p>
        <p id="p_12088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12088"> # (indicates that directory has been read)</p>
        <p id="p_12089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12089"> # &lt;directory name&gt;.&lt;num&gt; the &lt;num&gt;th subdirectory</p>
        <p id="p_12090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12090"> sub readsubdirs {</p>
        <p id="p_12091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12091"> local ($dirname) =</p>
        <p id="p_12092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12092"> local ($dirvar, $subdircount, $name, $index);</p>
        <p id="p_12093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12093"> # open the current directory;</p>
        <p id="p_12094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12094"> # $dircount ensures that each file variable is unique $dirvar =  "DIR"  . ++$dircount;</p>
        <p id="p_12095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12095"> if (lopendir ($dirvar, $dirname)) {</p>
        <p id="p_12096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12096"> warn ("Can't open $dirname\n"); return;</p>
        <p id="p_12097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12097"> }</p>
        <p id="p_12098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12098"> # read all the subdirectories; store in a standard</p>
        <p id="p_12099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12099"> chdir ($dirname);</p>
        <p id="p_12100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12100"> $subdircount = 0;</p>
        <p id="p_12101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12101"> while ($name = readdir ($dirvar] next if ($name eq if ($dirname eq  "/") {</p>
        <p id="p_12102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12102"> } else</p>
        <p id="p_12103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12103"> $name = $dirname . $name;</p>
        <p id="p_12104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12104"> $name = $dirname . "/" . $name;</p>
        <p id="p_12105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12105"> if (-d $name]</p>
        <p id="p_12106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12106"> $dirarray[$subdircount++] = $name;</p>
        <p id="p_12107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12107"> closedir ($dirvar);</p>
        <p id="p_12108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12108"> # sort the standard array; assign the sorted array to</p>
        <p id="p_12109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12109"> # associative array @dirarray = sort (@dirarray);</p>
        <p id="p_12110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12110"> for ($index = 0; $index &lt; $subdircount; $index++]</p>
        <p id="p_12111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12111"> $dirarray {$dirname . $index} =</p>
        <p id="p_12112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12112"> $dirarray[$index];</p>
        <p id="p_12113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12113"> undef (@dirarray);</p>
        <p id="p_12114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12114"> 62: }</p>
        <p id="p_12115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12115"> $dirarray{$dirname}</p>
        <p id="p_12116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12116"> i;</p>
        <p id="p_12117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12117"> print ("enter &lt; to move up in the</p>
        <p id="p_12118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12118"> 91:</p>
        <p id="p_12119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12119"> list,\n");</p>
        <p id="p_12120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12120"> 92 :</p>
        <p id="p_12121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12121"> 93:</p>
        <p id="p_12122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12122"> 94 :</p>
        <p id="p_12123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12123"> list,\n");</p>
        <p id="p_12124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12124"> 95 :</p>
        <p id="p_12125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12125"> 9 6 :</p>
        <p id="p_12126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12126"> 97 :</p>
        <p id="p_12127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12127"> directory, \n" )  ;</p>
        <p id="p_12128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12128"> 98 :</p>
        <p id="p_12129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12129"> 9 9 :</p>
        <p id="p_12130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12130"> 100  :</p>
        <p id="p_12131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12131"> 101  :</p>
        <p id="p_12132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12132"> 102  :</p>
        <p id="p_12133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12133"> 103 :</p>
        <p id="p_12134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12134"> 104 :</p>
        <p id="p_12135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12135"> 105 :</p>
        <p id="p_12136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12136"> " ") {</p>
        <p id="p_12137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12137"> }</p>
        <p id="p_12138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12138"> if ($dirarray{$curdir.($base+10)} ne "") {</p>
        <p id="p_12139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12139"> print ("enter &gt; to move down in the</p>
        <p id="p_12140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12140"> }</p>
        <p id="p_12141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12141"> print ("enter d to display the files,\n");</p>
        <p id="p_12142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12142"> print ("enter e to specify a new</p>
        <p id="p_12143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12143"> print ("or enter q to quit entirely.\n");</p>
        <p id="p_12144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12144"> print ("&gt; ");</p>
        <p id="p_12145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12145"> Scommand = &lt;STDIN&gt;;</p>
        <p id="p_12146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12146"> Scommand =~  s/ /v \s+|\s + $//g;</p>
        <p id="p_12147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12147"> if ($command eq  "q")  {</p>
        <p id="p_12148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12148"> exit (0) ;</p>
        <p id="p_12149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12149"> } elsif ($command eq  "&gt;")  {</p>
        <p id="p_12150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12150"> if  ($dirarray{$curdir.($base+10)} ne</p>
        <p id="p_12151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12151"> 106 :</p>
        <p id="p_12152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12152"> 107 :</p>
        <p id="p_12153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12153"> 108 :</p>
        <p id="p_12154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12154"> 109 :</p>
        <p id="p_12155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12155"> 110  : 111  : 112  :</p>
        <p id="p_12156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12156"> 113 : force</p>
        <p id="p_12157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12157"> 114 :</p>
        <p id="p_12158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12158"> }</p>
        <p id="p_12159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12159"> $base += 10;</p>
        <p id="p_12160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12160"> } elsif ($command eq "&lt;") {</p>
        <p id="p_12161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12161"> $base -= 10 if $base &gt; 0;</p>
        <p id="p_12162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12162"> } elsif (Scommand eq "d") {</p>
        <p id="p_12163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12163"> &amp;display ($curdir);</p>
        <p id="p_12164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12164"> } elsif ($command eq  "e")  {</p>
        <p id="p_12165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12165"> # set the current directory to "" to</p>
        <p id="p_12166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12166"> # the main program to prompt for a</p>
        <p id="p_12167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12167"> name</p>
        <p id="p_12168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12168"> 115: return ( " " ) ;</p>
        <p id="p_12169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12169"> 116: } elsif ($command =~  / /v \d+$/)  {</p>
        <p id="p_12170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12170"> 117: $subdir =</p>
        <p id="p_12171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12171"> $dirarray{$curdir.($command+$base)};</p>
        <p id="p_12172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12172"> 118: # if subdirectory is the parent</p>
        <p id="p_12173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12173"> directory,</p>
        <p id="p_12174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12174"> 119: # remove .. and the last directory</p>
        <p id="p_12175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12175"> name</p>
        <p id="p_12176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12176"> 120  :</p>
        <p id="p_12177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12177"> 121  :</p>
        <p id="p_12178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12178"> 122  :</p>
        <p id="p_12179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12179"> 123 :</p>
        <p id="p_12180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12180"> 124 : becomes</p>
        <p id="p_12181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12181"> 125 :</p>
        <p id="p_12182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12182"> 126 :</p>
        <p id="p_12183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12183"> 127 :</p>
        <p id="p_12184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12184"> 128 :</p>
        <p id="p_12185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12185"> &amp;followlink($subdir)</p>
        <p id="p_12186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12186"> 129 :</p>
        <p id="p_12187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12187"> 130 :</p>
        <p id="p_12188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12188"> 131 :</p>
        <p id="p_12189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12189"> 132 :</p>
        <p id="p_12190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12190"> 133 :</p>
        <p id="p_12191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12191"> 134 :</p>
        <p id="p_12192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12192"> 135 :</p>
        <p id="p_12193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12193"> 136: }</p>
        <p id="p_12194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12194"> # from the path</p>
        <p id="p_12195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12195"> if ($subdir =~ /\ .  \.$/)  {</p>
        <p id="p_12196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12196"> $subdir =~ s#  (.*)/.*/..#$1#;</p>
        <p id="p_12197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12197"> }</p>
        <p id="p_12198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12198"> #  if  subdirectory is defined, it</p>
        <p id="p_12199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12199"> # the new current directory</p>
        <p id="p_12200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12200"> if ($subdir ne "") {</p>
        <p id="p_12201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12201"> if ($dirarray{$subdir} != 1) {</p>
        <p id="p_12202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12202"> $subdir =</p>
        <p id="p_12203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12203"> &amp;readsubdirs ($subdir);</p>
        <p id="p_12204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12204"> }</p>
        <p id="p_12205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12205"> return ($subdir);</p>
        <p id="p_12206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12206"> }</p>
        <p id="p_12207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12207"> } else {</p>
        <p id="p_12208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12208"> warn ("Invalid command $command\n");</p>
        <p id="p_12209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12209"> }</p>
        <p id="p_12210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12210"> 137 : }</p>
        <p id="p_12211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12211"> 138 :</p>
        <p id="p_12212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12212"> 191 :</p>
        <p id="p_12213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12213"> }</p>
        <p id="p_12214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12214"> 192: $dirname; # return value</p>
        <p id="p_12215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12215"> 193: }</p>
        <p id="p_12216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12216"> Output</p>
        <p id="p_12217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12217"> $ programR2_l</p>
        <p id="p_12218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12218"> Enter directory to list:</p>
        <p id="p_12219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12219"> /agl/dave</p>
        <p id="p_12220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12220"> Current directory is: /agl/dave</p>
        <p id="p_12221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12221"> Subdirectories :</p>
        <p id="p_12222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12222"> 0: /agl/dave/. .</p>
        <p id="p_12223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12223"> 1: /agl/dave/.elm 2: /agl/dave/.mosaic 3: /agl/dave/.nn 4: /agl/dave/Mail 5: /agl/dave/News 6: /agl/dave/bin 7: /agl/dave/dave 8: /agl/dave/ems &lt;more down&gt;</p>
        <p id="p_12224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12224"> Enter a number to move to the specified directory.</p>
        <p id="p_12225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12225"> enter &gt; to move down in the list,</p>
        <p id="p_12226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12226"> enter d to display the files.</p>
        <p id="p_12227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12227"> enter e to specify a new directory, or enter q to quit entirely.</p>
        <p id="p_12228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12228"> &gt; d</p>
        <p id="p_12229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12229"> Files in directory /agl/dave:</p>
        <p id="p_12230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12230"> Subdirectories:</p>
        <p id="p_12231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12231"> 0: /agl/dave/..</p>
        <p id="p_12232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12232"> 1: /agl/dave/.elm 2: /agl/dave/.mosaic 3: /agl/dave/.nn 4: /agl/dave/Mail 5: /agl/dave/News 6: /agl/dave/bin 7: /agl/dave/dave 8: /agl/dave/ems &lt;more down&gt;</p>
        <p id="p_12233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12233"> Enter a number to move to the specified directory, enter &gt; to move down in the list,</p>
        <p id="p_12234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12234"> enter d to display the files, enter e to specify a new directory, or enter q to quit entirely.</p>
        <p id="p_12235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12235"> &gt;  6</p>
        <p id="p_12236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12236"> Current directory is: /agl/dave/bin</p>
        <p id="p_12237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12237"> Subdirectories:</p>
        <p id="p_12238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12238"> 0: /agl/dave/bin/. .</p>
        <p id="p_12239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12239"> Enter a number to move to the specified directory, enter d to display the files, enter e to specify a new directory, or enter q to quit entirely.</p>
        <p id="p_12240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12240"> &gt; q</p>
        <p id="p_12241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12241"> $</p>
        <p id="p_12242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12242"> Analysis</p>
        <p id="p_12243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12243"> The program in Listing R2.1 consists of five parts:</p>
        <p id="p_12244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12244"> • A very simple main program</p>
        <p id="p_12245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12245"> • The subroutine  &amp;readsubdirs,  which reads and stores the subdirectories of a directory</p>
        <p id="p_12246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12246"> • The subroutine  smenudir,  which displays the subdirectories of the current directory, lists the menu options, and processes the menu choices</p>
        <p id="p_12247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12247"> • The subroutine &amp;  display,  which lists the files in the current directory</p>
        <p id="p_12248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12248"> • The subroutine &amp;  foil owl ink,  which checks whether a directory name is really a symbolic link</p>
        <p id="p_12249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12249"> The main program is quite simple: all it does is prompt for a directory name and call the subroutines &amp;readsubdirs  and  &amp;menudir.  (Many complicated programs are like this: the main portion of the program just calls a few subroutines.)</p>
        <p id="p_12250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12250"> The subroutine  &amp;readsubdirs  is passed the name of a directory to examine. Line  33  opens the directory using  opendir,  and lines  38-51  store the subdirectories in a (standard) array named  @dirarray.  After this, line  56  sorts the array, and lines  57-59  load the sorted elements into an associative array named %dirarray.  (Recall that Perl programs can use the same name for an associative array and for a standard array because the program always can tell them apart.)</p>
        <p id="p_12251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12251"> The subscripts for the associative array use a simple scheme:</p>
        <p id="p_12252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12252"> • When a directory is read, line 61 defines an associative array element whose subscript is the directory name, and sets its value to 1. (For example, if the directory  /agl /dave  is being read, the array element $  dir array!  " /  agl/dave"  } is set to 1.) This is the way the program indicates that a particular directory has been read.</p>
        <p id="p_12253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12253"> • Line 49 stores the subdirectory names in associative array elements whose subscripts consist of the name of the directory joined with a unique integer. For example, if the first subdirectory of /agl/dave  is named  /agl/dave/f oo,  the associative array element</p>
        <p id="p_12254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12254"> $ dir array { " /agl/ daveO "  } is assigned the value  agl/ dave/f oo.  Similarly, the second subdirectory of  /agl/dave  has its name stored in  $dirarray{ "/agl/davel"  }, and so on.</p>
        <p id="p_12255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12255"> Line 60 introduces a function you have not yet seen:  undef . This function basically just throws away the contents of  @dirarray  because the program no longer needs them. (For more details on  undef,  see  Day 14 ,  "Scalar-Conversion and List-Manipulation Functions.")</p>
        <p id="p_12256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12256"> The subroutine  &amp;menudir  uses this associative array to display the subdirectories of the current directory. Line 74 prints the name of the current directory, and lines 79-84 print the names of the subdirectories of the directory. If there are more than ten subdirectories,  &amp;menudir  displays only a "window" of ten subdirectories, and it prints  &lt;more down&gt;  or  &lt;more up&gt;  to show that there are more subdirectories available. Each subdirectory is printed with a corresponding number that you can use to select the subdirectory and set it to be the current directory.</p>
        <p id="p_12257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12257"> After  &amp;menudir  prints the subdirectory names, lines 88-99 print a list of the available menu commands. These commands are</p>
        <p id="p_12258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12258"> • d,  which displays the files stored in the current directory</p>
        <p id="p_12259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12259"> •  e,  which enables you to enter the name of a directory to display</p>
        <p id="p_12260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12260"> •  q,  which enables you to quit the program</p>
        <p id="p_12261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12261"> • a number between 0 and 9, which changes the current directory to the specified subdirectory</p>
        <p id="p_12262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12262"> • &lt;, which moves up in the list of subdirectories (if possible)</p>
        <p id="p_12263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12263"> • &gt;, which moves down in the list of subdirectories (again, if possible)</p>
        <p id="p_12264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12264"> Line 100 reads a command from the standard input file, and line 101 gets rid of any leading or trailing white space. Lines 102-135 determine which command has been entered.</p>
        <p id="p_12265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12265"> If  q  has been entered, line 103 calls exit, which terminates the program.</p>
        <p id="p_12266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12266"> If either &gt; or &lt; has been entered, lines 104-109 move up or down in the directory list. They do this by modifying the value of a variable named  $base,  which determines how many subdirectory names to skip before lines 79-84 start printing.</p>
        <p id="p_12267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12267"> If  d  has been entered, line 111 calls &amp;  display,  which prints the list of files.</p>
        <p id="p_12268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12268"> If  e  has been entered, line 115 exits the subroutine with a return value of the null string. This forces the main program to execute lines 7-13 again, which prompt you for a directory name.</p>
        <p id="p_12269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12269"> If a number has been entered, line 117 takes the number, joins it to the current directory name, and uses the</p>
        <p id="p_12270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12270"> resulting string as the subscript into the associative array  %dirarray.  (For example, if the current directory is  /agl/dave  and the number  6  has been entered, line 117 accesses the associative array element  %dirarray { " /agl/dave6  " }). This is one of the array elements that line 49 of &amp;readsubdirs  created; its value is the name of a subdirectory.</p>
        <p id="p_12271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12271"> Line 127 takes the name of this subdirectory and uses it, in turn, as an associative array subscript. (For example, if the value of  %dirarray { "/agl/dave6"  } is " /agl/dave/bin",  line 127 checks the associative array element  %dir array! " / agl/dave/bin"  }.) If the value of this element is 1, &amp;readsubdirs  has already read this directory and stored its subdirectory names in the associative array, so the program does not need to do it again. If this element is not defined, the program calls &amp;readsubdirs,  which reads and stores the names of the subdirectories of this directory.</p>
        <p id="p_12272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12272"> The subroutine &amp;  display  prints the names of the files stored in a particular directory. To save space, it prints the filenames three per line. &amp;  display  prints only ten lines at a time. If there are more than ten lines (in other words, 30 filenames), line 168 pauses and waits for you to press Enter before continuing to print. This gives you time to read all of the currently displayed names.</p>
        <p id="p_12273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12273"> The final subroutine is  sfollowlink,  which always is called immediately before the subroutine &amp;readsubdirs  is called. Its job is to check whether a directory name is really a symbolic link. If it is, line 190 calls  readlink,  which retrieves the real directory name. This directory name is returned to the calling subroutine or main program and then is passed to &amp;  readsubdirs.</p>
        <p id="p_12274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12274"> As you can see, you now know enough about Perl to write programs that manipulate the file system and use complex data structures. In Week 3, you'll learn about the remainder of Perl's built-in functions and the rest of the features of Perl.</p>
        <p id="p_12275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12275"> Week 3 at a Glance</p>
        <p id="p_12276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12276"> CONTENTS</p>
        <p id="p_12277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12277"> •  Where You’re Going</p>
        <p id="p_12278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12278"> You've finished your second week of learning how to program with Perl. By now, you know enough about Perl to consider yourself an accomplished Perl programmer.</p>
        <p id="p_12279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12279"> Where You're Going</p>
        <p id="p_12280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12280"> The third week covers the rest of the Perl library functions, describes some of the more esoteric concepts of the language, and introduces some features unique to version 5 of Perl. Here's a summary of what you'll learn.</p>
        <p id="p_12281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12281"> Day  15 ,  "System Functions," describes the functions that work with lists and array variables.</p>
        <p id="p_12282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12282"> Day  16 ,  "Command-Line Options," describes the options you can supply with Perl to control how your program runs.</p>
        <p id="p_12283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12283"> Day  17 , "System Variables," describes the built-in variables that are included automatically as part of every Perl program.</p>
        <p id="p_12284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12284"> Day  18 ,  "References in Perl 5," describes the use of Perl references and the concept of pointers.</p>
        <p id="p_12285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12285"> Day  19 ,  "Object-Oriented Programming in Perl," covers how to construct objects in Perl and how to use OOP features offered by Perl.</p>
        <p id="p_12286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12286"> Day  20 ,  "Miscellaneous Features of Perl," covers some of the more exotic or obscure features of the</p>
        <p id="p_12287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12287"> language.</p>
        <p id="p_12288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12288"> Finally,  Day 21 ,  "The Perl Debugger," shows you how to use the Perl debugger to quickly discover errors.</p>
        <p id="p_12289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12289"> By the end of the third week, you'll know all the features and capabilities of Perl.</p>
        <p id="p_12290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12290"> Chapter 15</p>
        <p id="p_12291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12291"> System Functions</p>
        <p id="p_12292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12292"> CONTENTS</p>
        <p id="p_12293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12293"> •  System Library Emulation Functions</p>
        <p id="p_12294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12294"> o  The  cretgrent  Function o  The  setgrent  and  endgrent  Functions</p>
        <p id="p_12295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12295"> o  The  qetqrnam  Function</p>
        <p id="p_12296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12296"> o  The  get grid  Function o  The  getnetent  Function o  The  getnetbyaddr  Function</p>
        <p id="p_12297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12297"> o  The  getnetbyname  Function O  The  setnetent  and  endnetent  Functions</p>
        <p id="p_12298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12298"> o  The  gethostbyaddr  Function</p>
        <p id="p_12299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12299"> o  The  gethostbyname  Function</p>
        <p id="p_12300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12300"> o  The  gethostent, sethostent,  and  endhostent  Functions</p>
        <p id="p_12301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12301"> o  The  qetloqin  Function o  The  getpgrp  and  setpgrp  Functions</p>
        <p id="p_12302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12302"> o  The  getppid  Function o  The  getpwnam  Function</p>
        <p id="p_12303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12303"> o  The  getpwuid  Function</p>
        <p id="p_12304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12304"> o  The  getpwent  Function</p>
        <p id="p_12305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12305"> o  The  setpwent  and  endpwent  Functions</p>
        <p id="p_12306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12306"> o  The  getpriority  and  setpriority  Functions</p>
        <p id="p_12307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12307"> o  The  getprotoent  Function</p>
        <p id="p_12308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12308"> o  The  getprotobyname  and  getprotobynumber  Functions</p>
        <p id="p_12309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12309"> o  The  setprotoent  and  endprotoent  Functions</p>
        <p id="p_12310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12310"> o  The  getservent  Function</p>
        <p id="p_12311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12311"> o  The  get servbyname  and  get servbyport  Functions</p>
        <p id="p_12312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12312"> O  The  setservent  and  endservent  Functions</p>
        <p id="p_12313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12313"> o  The  chroot  Function o  The  ioctl  Function o  The  alarm Function o  Calling the System  select  Function</p>
        <p id="p_12314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12314"> o  The  dump  Function</p>
        <p id="p_12315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12315"> •  Socket-Manipulation Functions</p>
        <p id="p_12316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12316"> o  The  socket  Function</p>
        <p id="p_12317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12317"> o  The  bind  Function</p>
        <p id="p_12318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12318"> O  The  listen  Function o  The  accept  Function o  The  connect  Function o  The  shutdown  Function o  The  socketpair  Function o  The  getsockopt  and  setsockopt  Functions</p>
        <p id="p_12319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12319"> o  The  getsockname  and  getpeername  Functions</p>
        <p id="p_12320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12320"> •  The UNIX System V IPC Functions</p>
        <p id="p_12321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12321"> o  IPC Functions and the  require  Statement</p>
        <p id="p_12322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12322"> o  The  msgcret  Function o  The  msgsnd  Function o  The  msgrcv  Function o  The  m scroti  Function o  The  shmget  Function o  The  shmwrite  Function</p>
        <p id="p_12323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12323"> o  The  shmread  Function</p>
        <p id="p_12324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12324"> o  The  shmctl  Function o  The  semget  Function o  The  semop  Function o  The  semctl  Function</p>
        <p id="p_12325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12325"> •  Summary</p>
        <p id="p_12326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12326"> •  Q&amp;A</p>
        <p id="p_12327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12327"> •  Workshop</p>
        <p id="p_12328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12328"> o  Quiz o  Exercises</p>
        <p id="p_12329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12329"> Today's lesson describes the built-in Peri functions that perform various system-level operations. These functions are divided into three groups:</p>
        <p id="p_12330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12330"> • The functions that emulate system library functions</p>
        <p id="p_12331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12331"> • The functions that work with Berkeley UNIX sockets</p>
        <p id="p_12332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12332"> • The functions that perform UNIX System V IPC operations</p>
        <p id="p_12333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12333"> Warning</p>
        <p id="p_12334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12334"> Many of the functions described in today's lesson use features of the UNIX operating system. If you are using Perl on a machine that is not running UNIX, some of these functions might not be defined or might behave differently.</p>
        <p id="p_12335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12335"> Check the documentation supplied with your version of Perl for details on which functions are supported or emulated on your machine</p>
        <p id="p_12336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12336"> System Library Emulation Functions</p>
        <p id="p_12337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12337"> Several built-in Perl functions enable you to execute various system library calls from within your Perl program. Each one corresponds to a UNIX system library function.</p>
        <p id="p_12338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12338"> The following sections briefly describe these system library functions. For more information on a particular system library function, refer to the on-line manual page for that function. For example, to find out more about the  getnetent  function, refer to your UNIX system's  getnetent  manual page.</p>
        <p id="p_12339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12339"> The getgrent Function</p>
        <p id="p_12340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12340"> In the UNIX environment, each user belongs to a user group. Being in a user group enables you to define files that only certain users-the people in your user group-can read from or write to.</p>
        <p id="p_12341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12341"> On UNIX systems, the file /  etc/group  lists the user groups defined for your machine. Each entry in the user group file consists of four components:</p>
        <p id="p_12342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12342"> • The user group name</p>
        <p id="p_12343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12343"> • The user group password, if one exists</p>
        <p id="p_12344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12344"> • The group ID, which is a unique integer that the system uses to identify this particular user group</p>
        <p id="p_12345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12345"> • A list of the user IDs that belong to this group</p>
        <p id="p_12346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12346"> The Perl function  getgrent  enables you to retrieve an item from the user group file.</p>
        <p id="p_12347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12347"> The syntax for the  getgrent  function is</p>
        <p id="p_12348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12348"> (gname, gpasswd, gid, gmembers) = getgrent;</p>
        <p id="p_12349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12349"> This function returns a four-element list consisting of the four components of a group line entry, as just described,  gname  contains the user group name,  gpasswd  contains the user group password,  gid  is the group ID, and  gmembers  is a character string consisting of a list of the user IDs belonging to this group. The user IDs listed in  gmembers  are separated by spaces.</p>
        <p id="p_12350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12350"> Each call to  getgrent  returns another line from the  /etc/group  file. Therefore, you can put</p>
        <p id="p_12351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12351"> getgrent  inside a  while  loop.</p>
        <p id="p_12352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12352"> while (($gname, $gpasswd, $gid, $gmembers) = getgrent) {</p>
        <p id="p_12353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12353"> # do stuff here</p>
        <p id="p_12354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12354"> }</p>
        <p id="p_12355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12355"> When the  / etc/group  file is exhausted,  getgrent  returns the empty list.</p>
        <p id="p_12356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12356"> Listing 15.1 is an example of a program that uses  getgrent  to list all the user IDs associated with each group on your system.</p>
        <p id="p_12357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12357"> Type</p>
        <p id="p_12358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12358"> Listing 15.1. A program that uses getgrent.</p>
        <p id="p_12359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12359"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12360"> 2  :</p>
        <p id="p_12361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12361"> 3: while ((Sgname, $gpasswd, $gid, $gmembers) = getgrent) {</p>
        <p id="p_12362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12362"> 4: $garray{$gname} = $gmembers;</p>
        <p id="p_12363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12363"> 5: }</p>
        <p id="p_12364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12364"> 6: foreach Sgname (sort keys (%garray)) {</p>
        <p id="p_12365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12365"> 7: print ("Userids belonging to group $gname:\n");</p>
        <p id="p_12366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12366"> 8: Sgmembers = Sgarray{Sgname};</p>
        <p id="p_12367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12367"> 9: Suserids = 0;</p>
        <p id="p_12368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12368"> 10: while (1) {</p>
        <p id="p_12369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12369"> 11: last if (Sgmembers eq  "");</p>
        <p id="p_12370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12370"> 12: ($userid, Sgmembers) =</p>
        <p id="p_12371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12371"> 13: split  (/\s+/,  Sgmembers, 2);</p>
        <p id="p_12372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12372"> 14 :</p>
        <p id="p_12373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12373"> printf ("</p>
        <p id="p_12374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12374"> % - 2 0 s " , $userid);</p>
        <p id="p_12375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12375"> 3</p>
        <p id="p_12376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12376"> 0) {</p>
        <p id="p_12377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12377"> Output</p>
        <p id="p_12378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12378"> $ programl5_l Userids belonging to a dm</p>
        <p id="p_12379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12379"> Userids belonging to dave mpython</p>
        <p id="p_12380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12380"> Userids belonging to root</p>
        <p id="p_12381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12381"> group adm: daemon</p>
        <p id="p_12382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12382"> group develop: jqpublic ralomar group root:</p>
        <p id="p_12383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12383"> $</p>
        <p id="p_12384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12384"> kilroy xy z zy</p>
        <p id="p_12385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12385"> Line  3  of this program calls  getgrent.  This function returns a four-element list whose elements are the components of a group entry stored in the  /etc/group  file. If  /etc/group  is exhausted,  getgrent  returns the empty list.</p>
        <p id="p_12386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12386"> Line 4 takes the list of group members in  $gmembers  and stores it in an associative array named %garray.  The subscript for this array element is the name of the group, which is contained in  $gname.</p>
        <p id="p_12387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12387"> Analysis</p>
        <p id="p_12388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12388"> Lines 6-23 print the list of user IDs for each group. The loop iterates once for each group name, and the call to  sort  in line 6 ensures that the group names appear in alphabetical order. First, line 7 prints the name of the group. Then, line 8 retrieves the list of user IDs in the group by accessing the associative array %garray.  This list is stored, once again, in  $gmembers.</p>
        <p id="p_12389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12389"> Lines 12 and 13 call  split  to extract the next user ID from the list,  split  breaks the string into two parts when it sees the first white space. The first part, the substring before the first space, contains one user ID and is assigned to  $userid;  the rest of the string is reassigned to  $gmembers.</p>
        <p id="p_12390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12390"> The rest of the loop prints the extracted user ID. User IDs are printed three per line to save space.</p>
        <p id="p_12391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12391"> The setgrent and endgrent Functions</p>
        <p id="p_12392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12392"> The  setgrent  function affects the behavior of  getgrent:  it tells the Perl interpreter to rewind the /etc/group  file. After  setgrent  is called, the next call to  getgrent  retrieves the first element of the / etc/group  file.</p>
        <p id="p_12393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12393"> The  endgrent  function tells the Perl interpreter that you no longer need to access the  /etc/group  file. It frees the memory used to store group information.</p>
        <p id="p_12394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12394"> Neither  setgrent  nor  endgrent  accepts any arguments or returns any values.</p>
        <p id="p_12395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12395"> The syntax for these functions is</p>
        <p id="p_12396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12396"> setgrent() ; endgrent();</p>
        <p id="p_12397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12397"> The getgrnam Function</p>
        <p id="p_12398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12398"> The  getgrnam  function enables you to retrieve the group file entry corresponding to a particular group name.</p>
        <p id="p_12399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12399"> The syntax for the  getgrnam  function is</p>
        <p id="p_12400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12400"> (gname, gpasswd, gid, gmembers) = getgrnam (name);</p>
        <p id="p_12401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12401"> Here,  name  is the group name to search for.  getgrnam  returns the same four-element list that getgrent  returns:  gname  is the group name (which is the same as  name), gpasswd  is the group password,  gid  is the group ID, and  gmembers  is the list of user IDs in the group. If  getgrnam  does not find a group entry matching  name,  it returns the empty list.</p>
        <p id="p_12402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12402"> Listing 15.2 is a modification of Listing 15.1. It asks you for a group name and then prints the user IDs in that group.</p>
        <p id="p_12403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12403"> Listing 15.2. A program that uses getgrnam.</p>
        <p id="p_12404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12404"> 21: }</p>
        <p id="p_12405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12405"> Output</p>
        <p id="p_12406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12406"> $ programl5_2</p>
        <p id="p_12407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12407"> Enter the group name to list: develop</p>
        <p id="p_12408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12408"> dave jqpublic kilroy</p>
        <p id="p_12409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12409"> mpython ralomar  xyzzy</p>
        <p id="p_12410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12410"> $</p>
        <p id="p_12411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12411"> Analysis</p>
        <p id="p_12412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12412"> Line 6 takes the group name stored in  $name  and passes it to  getgrnam.  If a group corresponding to that name exists,  getgrnam  returns the name, password, group ID, and members. If no such group exists,  getgrnam  returns the empty list, the conditional expression in line 6 fails, and line 7 calls  die  to terminate the program.</p>
        <p id="p_12413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12413"> The rest of the program is taken verbatim from Listing 15.1: the  while  loop in lines 10-18 extracts a user ID from the list of user IDs in  $gmembers  and prints it, continuing until the list is exhausted.</p>
        <p id="p_12414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12414"> The getgrid Function</p>
        <p id="p_12415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12415"> The  getgrid  function is similar to  getgrnam,  except that it retrieves the group file entry corresponding to a given group ID.</p>
        <p id="p_12416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12416"> The syntax for the  getgrid  function is</p>
        <p id="p_12417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12417"> (gname, gpasswd, gid, gmembers) = getgrid  (id);</p>
        <p id="p_12418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12418"> Like  getgrname, getgrid  returns a four-element list consisting of the group name, password, ID, and member list. If the group specified by  id  does not exist,  getgrid  returns the empty list.</p>
        <p id="p_12419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12419"> This function often is used to retrieve the associated group name:</p>
        <p id="p_12420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12420"> ($gname) = getgrid (11) ;</p>
        <p id="p_12421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12421"> This line retrieves the group name associated with group ID 11. (The other elements of the list are thrown away.)</p>
        <p id="p_12422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12422"> Warning</p>
        <p id="p_12423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12423"> You must place parentheses around  Sgname  to denote that getgrid  is assigning to a list. The statement</p>
        <p id="p_12424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12424"> Sgname = getgrid (11);</p>
        <p id="p_12425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12425"> assigns the list returned by  getgrid  to the scalar variable  Sgname. In Perl, assigning a list to a scalar variable actually assigns the length of the list to the variable, so this statement assigns 4 to  Sgname because there are four elements in the list returned by  getgrid</p>
        <p id="p_12426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12426"> The getnetent Function</p>
        <p id="p_12427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12427"> The  getnetent  function enables you to step through the file  /etc/networks,  which lists the names and addresses of the networks your machine is on.</p>
        <p id="p_12428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12428"> The syntax for the  getnetent  function is</p>
        <p id="p_12429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12429"> (name, altnames, addrtype, net) = getnetent();</p>
        <p id="p_12430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12430"> name  is the name of a network,  altnames  is a list of alternative names for the network; this list of names is returned as a character string, with spaces separating the individual names,  addrtype  is the address type; at present, this is always whatever value is defined for the system constant  AF_INET,  which indicates that the address is an Internet address.</p>
        <p id="p_12431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12431"> NOTE</p>
        <p id="p_12432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12432"> To get the value of  AF_INET  on your machine, refer to the header file</p>
        <p id="p_12433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12433"> /usr/include/netdb.hor/usr/include/bsd/netdb.h,</p>
        <p id="p_12434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12434"> and look for a statement similar to the following:</p>
        <p id="p_12435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12435"> #define AF_INET 2</p>
        <p id="p_12436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12436"> The number that appears after  AF_INET  is the one you want</p>
        <p id="p_12437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12437"> net  is the Internet address of this network. This address is represented as a string of four bytes, which can be unpacked into Perl scalar values using the  unpack  function.</p>
        <p id="p_12438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12438"> Listing 15.3 shows how you can use  getnetent  to list the machine names and addresses at your site.</p>
        <p id="p_12439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12439"> Type</p>
        <p id="p_12440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12440"> Listing 15,3. A program that uses getnetent.</p>
        <p id="p_12441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12441"> 1 : 2  : 3:</p>
        <p id="p_12442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12442"> 4 :</p>
        <p id="p_12443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12443"> 5 :</p>
        <p id="p_12444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12444"> 6  :</p>
        <p id="p_12445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12445"> 7 :</p>
        <p id="p_12446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12446"> 8  :</p>
        <p id="p_12447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12447"> #!/usr/local/bin/perl</p>
        <p id="p_12448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12448"> print ("Networks this machine is connected to:\n") ; while (($name, $altnames, $addrtype, $rawaddr) = getnetent()) {</p>
        <p id="p_12449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12449"> @addrbytes = unpack ("C4", $rawaddr);</p>
        <p id="p_12450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12450"> $address = join @addrbytes);</p>
        <p id="p_12451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12451"> print ("$name, at address $address\n");</p>
        <p id="p_12452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12452"> }</p>
        <p id="p_12453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12453"> Output</p>
        <p id="p_12454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12454"> $ programl5_3</p>
        <p id="p_12455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12455"> Networks this machine is connected to: silver, at address 192.75.236.168 $</p>
        <p id="p_12456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12456"> Analysis</p>
        <p id="p_12457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12457"> Line 4 calls  getnetent,  which reads from the file  /etc/networks.  If the file has been exhausted,  getnetent  returns the empty list, and the  while  loop terminates. If  /etc/networks  still contains an unread entry,  getnetent  retrieves it and assigns its components to  $name, $altnames, $addrtype,  and  $rawaddr.</p>
        <p id="p_12458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12458"> $rawaddr  contains the Internet address for a particular network. This address is stored as a four-byte integer; each byte contains one component of the address. (This method works because each number in an Internet address has a maximum value of 255, which is the largest value that can fit in a byte.) Line 5 converts this four-byte integer into a list of integers by calling  unpack,  and it stores the list in</p>
        <p id="p_12459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12459"> @addrbytes.</p>
        <p id="p_12460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12460"> Line  6  calls  join  to convert the list of integers into a character string that contains the readable address. Line 7 then prints the network name and the readable address of the network.</p>
        <p id="p_12461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12461"> The getnetbyaddr Function</p>
        <p id="p_12462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12462"> The  getnetbyaddr  function enablesyou to retrieve the line of input from  /etc/networks  that matches a particular network number.</p>
        <p id="p_12463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12463"> The syntax for the  getnetbyaddr  function is</p>
        <p id="p_12464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12464"> (name, altnames, addrtype, addr) = getnetbyaddr (inaddr, inaddrtype) ;</p>
        <p id="p_12465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12465"> Here,  inaddr  is the network number or address for which you want to search. This address must be a packed four-byte integer whose four bytes are the four components of the address. (An example of a network address is  192.75.236.168,  which is the machine on which I work.) To build a packed address, use the  pack  command:</p>
        <p id="p_12466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12466"> @addrbytes = (192, 75, 236,  168);</p>
        <p id="p_12467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12467"> $packedaddr = pack  ("C4",  @addrbytes);</p>
        <p id="p_12468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12468"> The packed address in  $packedaddr  can now be passed to  getnetbyaddr.</p>
        <p id="p_12469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12469"> inaddrtype  is the address type, which is always  AF_INET  (whose value is located in the file /us r/include/net db. h  or  /us r/include/bsd/net db. h).</p>
        <p id="p_12470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12470"> The  getnetbyaddr  function returns the same four-element list as  getnetent:  the name of the network, the list of alternative names, the address type, and the packed address.</p>
        <p id="p_12471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12471"> The getnetbyname Function</p>
        <p id="p_12472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12472"> The  getnetbyname  function is similar to  getnetbyaddr,  except that it enables you to search in the /etc/networks  file for a network of a particular name.</p>
        <p id="p_12473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12473"> The syntax for the  getnetbyname  function is</p>
        <p id="p_12474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12474"> (name, altnames, addrtype, net) = getnetbyname (inname);</p>
        <p id="p_12475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12475"> Here,  inname  is the machine name to search for. Like  getnetbyaddr  and  getnetent, getnetbyname  returns a four-element list consisting of the network name, alternative name list, address type, and packed address.</p>
        <p id="p_12476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12476"> NOTE</p>
        <p id="p_12477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12477"> You can pass  getnetbyname  either the principal network name or one of its aliases</p>
        <p id="p_12478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12478"> The setnetent and endnetent Functions</p>
        <p id="p_12479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12479"> The  setnetent  function rewinds the  /etc/networks  file; after  setnetent  has been called, a call to  getnetent  returns the first entry in the  /etc/networks  file.</p>
        <p id="p_12480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12480"> The syntax for the  setnetent  function is</p>
        <p id="p_12481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12481"> setnetent (keepopen) ;</p>
        <p id="p_12482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12482"> keepopen  is a scalar value. If  keepopen  is not zero, the  /etc/networks  file is not closed after getnetbyname  or  getnetbyaddr  is called; therefore, you can efficiently call these functions repeatedly. If  keepopen  is zero, the file is closed.</p>
        <p id="p_12483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12483"> The  endnetent  function tells the Perl interpreter that your program is finished with the /etc/networks  file. It closes the file and frees any memory used by your program to store related information.</p>
        <p id="p_12484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12484"> The syntax for the  endnetent  function is</p>
        <p id="p_12485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12485"> endnetent;</p>
        <p id="p_12486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12486"> It accepts no arguments and returns no values.</p>
        <p id="p_12487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12487"> The gethostbyaddr Function</p>
        <p id="p_12488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12488"> The  gethostbyaddr  function searches the file  /etc/hosts  (or the equivalent name server) for the host name corresponding to a particular Internet address.</p>
        <p id="p_12489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12489"> The syntax for the  gethostbyaddr  function is</p>
        <p id="p_12490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12490"> (name, altnames, addrtype, len, addrs) = gethostbyaddr (inaddr, inaddrtype) ;</p>
        <p id="p_12491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12491"> This function requires two arguments. The first,  inaddr,  is the Internet address to search for, stored in packed four-byte format (identical to that used by  getnetbyaddr).  The second argument, inaddrtype,  is the address type; at present, only Internet address types are understood, and inaddrtype  is always  AF_INET.  (The value of  AF_INET  can be found in /us r / include/net db. h  or  /us r/include/sys/net db. h.)</p>
        <p id="p_12492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12492"> gethostbyaddr  returns a five-element list. The first element,  name,  is the host name corresponding to the Internet address specified by  inaddr. altnames  is the list of aliases or alternative names by which the host can be referred,  addrtype,  like  inaddrtype,  is always  AF_INET.</p>
        <p id="p_12493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12493"> addrs  is a list of addresses (main address and alternatives) corresponding to the host node named  name. Each address is stored as a four-byte integer,  len  is the length of the  addrs  field; this length is always four multiplied by the number of addresses returned in  addrs.</p>
        <p id="p_12494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12494"> Listing 15.4 shows how you can use  gethostbyaddr  to retrieve the Internet address corresponding to a particular machine name.</p>
        <p id="p_12495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12495"> Type</p>
        <p id="p_12496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12496"> Listing 15.4. A program that uses gethostbyaddr.</p>
        <p id="p_12497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12497"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12498"> 2  :</p>
        <p id="p_12499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12499"> 3: print ("Enter an Internet address:\n");</p>
        <p id="p_12500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12500"> 4: $machine = &lt;STDIN&gt;;</p>
        <p id="p_12501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12501"> 5: $machine =~  s/^Xst|\s+$//g;</p>
        <p id="p_12502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12502"> 6:  @bytes = split  (/\./,  $machine) ;</p>
        <p id="p_12503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12503"> 7: $packaddr = pack  ("C4",  @bytes) ;</p>
        <p id="p_12504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12504"> 8: if  (!(($name,  $altnames, $addrtype, $len, @addrlist) =</p>
        <p id="p_12505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12505"> 9: gethostbyaddr ($packaddr,  2))) {</p>
        <p id="p_12506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12506"> die ("Address $machine not found.\n");</p>
        <p id="p_12507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12507"> 10  :</p>
        <p id="p_12508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12508"> 11 : }</p>
        <p id="p_12509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12509"> 12: print ("Principal name: $name\n");</p>
        <p id="p_12510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12510"> 13: if ($altnames ne "") {</p>
        <p id="p_12511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12511"> 14: print ("Alternative names:\n");</p>
        <p id="p_12512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12512"> 15: @altlist = split (/\s+/, $altnames);</p>
        <p id="p_12513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12513"> 16: for ($i =0; $i &lt; @altlist; $i++) {</p>
        <p id="p_12514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12514"> 17: print ("\t$altlist[$i]\n");</p>
        <p id="p_12515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12515"> 18: }</p>
        <p id="p_12516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12516"> 19: }</p>
        <p id="p_12517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12517"> $ programl5_4</p>
        <p id="p_12518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12518"> Enter an Internet address:</p>
        <p id="p_12519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12519"> 128.174.5.59</p>
        <p id="p_12520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12520"> Principal name: uxl.cso.uiuc.edu</p>
        <p id="p_12521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12521"> $</p>
        <p id="p_12522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12522"> Analysis</p>
        <p id="p_12523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12523"> The program starts by prompting you for an Internet address. (In this example, the Internet address specified is  128.174.5.59,  which is the location of a popular public access Gopher site.) Lines 5-7  then convert the address into a four-byte packed integer, which is stored in  $packaddr.</p>
        <p id="p_12524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12524"> Lines 8 and 9 call  gethostbyaddr.  This function searches the  /etc/hosts  file for an entry matching the specified machine name. If the entry is not found, the conditional expression becomes false, and line 10 calls  die  to terminate the program.</p>
        <p id="p_12525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12525"> NOTE</p>
        <p id="p_12526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12526"> Line 9 uses the value 2 as the address type to pass to gethostbyaddr.  If your machine defines a different value of AF_INET,  as defined in the files  /usr/include/netdb . h  or /usr/include/bsd/netdb . h,  replace  2  with that value</p>
        <p id="p_12527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12527"> If the entry is found, line 12 prints the principal machine name, which was returned by  gethostbyaddr and is now stored in the scalar variable  $name.  Line 13 then checks whether the returned entry lists any alternative machine names corresponding to this Internet address.</p>
        <p id="p_12528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12528"> If alternative machine names exist, lines 14-18 split the alternative name list into individual names and print each name on a separate line.</p>
        <p id="p_12529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12529"> NOTE</p>
        <p id="p_12530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12530"> gethostbyaddr  and the other functions that access /etc/hosts  expect the following format for a host entry:</p>
        <p id="p_12531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12531"> address mainname altnamel altname2 ...</p>
        <p id="p_12532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12532"> Here,  address  is an Internet address;  mainname  is the name associated with the address; and  altnamel, altname2,  and so on are the (optional) alternative names for the host.</p>
        <p id="p_12533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12533"> If your  /etc/hosts  file is in a different format, gethostbyaddr  might not work properly</p>
        <p id="p_12534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12534"> The get host by name Function</p>
        <p id="p_12535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12535"> The  gethostbyname  function is similar to  gethostbyaddr,  except that it searches for an /etc/hosts  entry that matches a specified machine name or Internet site name.</p>
        <p id="p_12536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12536"> The syntax for the  gethostbyname  function is</p>
        <p id="p_12537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12537"> (name, altnames, addrtype, len, addrs) = gethostbyname (inname);</p>
        <p id="p_12538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12538"> Here,  inname  is the machine name or Internet site name to search for.  gethostbyname,  like gethostbyaddr,  returns a five-element list consisting of the machine name, a character string containing a list of alternative names, the address type, the length of the address list, and the address list.</p>
        <p id="p_12539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12539"> Listing 15.5 is a simple program that searches for an Internet address when given the name of a site.</p>
        <p id="p_12540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12540"> Type</p>
        <p id="p_12541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12541"> Listing 15.5. A program that uses gethostbyname.</p>
        <p id="p_12542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12542"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12543"> 2  :</p>
        <p id="p_12544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12544"> 3: print ("Enter a machine name or Internet site name:\n");</p>
        <p id="p_12545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12545"> 4: $machine = &lt;STDIN&gt;;</p>
        <p id="p_12546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12546"> 5: $machine =~ s/^Xsi|\s+$//g;</p>
        <p id="p_12547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12547"> 6: if (!(($name, $altnames, $addrtype, $len, @addrlist) =</p>
        <p id="p_12548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12548"> 7: gethostbyname ($machine))) {</p>
        <p id="p_12549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12549"> 8: die ("Machine name $machine not found.\n");</p>
        <p id="p_12550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12550"> 9: }</p>
        <p id="p_12551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12551"> 10: print ("Equivalent addresses:\n");</p>
        <p id="p_12552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12552"> 11: for ($i =0; $i &lt; @addrlist; $i++) {</p>
        <p id="p_12553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12553"> 12: @addrbytes = unpack("C4", $addrlist[$i]);</p>
        <p id="p_12554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12554"> 13: $realaddr = join  (".",  @addrbytes);</p>
        <p id="p_12555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12555"> 14: print ("\t$realaddr\n" ) ;</p>
        <p id="p_12556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12556"> 15: }</p>
        <p id="p_12557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12557"> Output</p>
        <p id="p_12558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12558"> $ programl5_5</p>
        <p id="p_12559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12559"> Enter a machine name or Internet site name: uxl.cso.uiuc.edu</p>
        <p id="p_12560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12560"> Equivalent addresses:</p>
        <p id="p_12561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12561"> 128.174.5.59</p>
        <p id="p_12562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12562"> $</p>
        <p id="p_12563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12563"> Analysis</p>
        <p id="p_12564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12564"> This program prompts for a machine name and then removes the leading and trailing white space from it. After the machine name has been prepared, lines 6 and 7 call  gethostbyname,  which searches for the  /etc/hosts  entry matching the specified machine name. If  gethostbyname  does not find the entry, it returns the null string, the conditional expression becomes false, and line 8 calls  die  to terminate the program.</p>
        <p id="p_12565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12565"> If  gethostbyname  finds the entry, the loop in lines 11-15 examines the list of addresses in @addrlist,  assembling and printing one address at a time. Line 12 assembles an address by unpacking one element of  @addrlist  and storing the individual bytes in  @addrbytes.  Line 13 joins the bytes into a character string, placing a period between each pair of bytes. The resulting string is a readable Internet address, which line 14 prints.</p>
        <p id="p_12566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12566"> NOTE</p>
        <p id="p_12567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12567"> The machine name passed to  gethostbyname  can be either the principal machine name (as specified in the first element of the returned list) or one of the alternative names (aliases)</p>
        <p id="p_12568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12568"> The gethostent, sethostent, and endhostent Functions</p>
        <p id="p_12569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12569"> The  gethostent  function enables you to read each item of the  /etc/hosts  file in turn. The syntax for the  gethostent  function is</p>
        <p id="p_12570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12570"> (name, altnames, addrtype, len, addrs) = gethostent();</p>
        <p id="p_12571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12571"> The first call to  gethostent  returns the first element in the  /etc/hosts  file; subsequent calls to gethostent  return successive elements. Each call to  gethostent  returns a five-element list identical to the list returned by  gethostbyaddr  or  gethostbyname.  This list consists of a machine name, a character string listing the alternative machine names, the address type (always  AF_INET),  the length of the address field, and the address field itself.</p>
        <p id="p_12572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12572"> Warning</p>
        <p id="p_12573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12573"> Many machines simulate an /  etc/hosts  file using a name server. When a program that is running on a machine using a name server attempts to access  /etc/host s,  the server queries various Internet sites for machine names, addresses, and other information.</p>
        <p id="p_12574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12574"> If a Perl program running on such a machine calls  gethostent repeatedly, the program might try to access many Internet sites to obtain machine information. This takes a lot of time and is a strain on Internet resources; do not do it unless you absolutely must, and do it during off-peak hours if possible</p>
        <p id="p_12575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12575"> The  sethostent  function rewinds the  /etc/hosts  file, which means that the next call to gethostent  will return the first entry in the file.</p>
        <p id="p_12576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12576"> The syntax for the  sethostent  function is</p>
        <p id="p_12577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12577"> sethostent (keepopen);</p>
        <p id="p_12578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12578"> keepopen  is a scalar value. If  keepopen  is nonzero, the Perl program keeps  /etc/hosts  information in memory, which ensures that subsequent calls to  gethostent  are performed as efficiently as possible. If  keepopen  is zero, no information is retained after  sethostent  finishes executing.</p>
        <p id="p_12579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12579"> The  endhostent  function closes the  /etc/hosts  file and indicates that the program is to free any internal memory retaining host-related information.</p>
        <p id="p_12580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12580"> The  endhostent  function expects no arguments and returns no values:</p>
        <p id="p_12581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12581"> endhostent() ;</p>
        <p id="p_12582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12582"> The get login Function</p>
        <p id="p_12583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12583"> The  get login  function returns the user ID under which you are logged in. The user ID is retrieved from the file /  etc/utmp.</p>
        <p id="p_12584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12584"> The syntax for the  get login  function is</p>
        <p id="p_12585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12585"> logname = getlogin();</p>
        <p id="p_12586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12586"> logname  is the returned user ID.</p>
        <p id="p_12587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12587"> The following is a simple example using  getlogin:</p>
        <p id="p_12588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12588"> $logname = getlogin(); if ($logname == "dave") {</p>
        <p id="p_12589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12589"> print ("Hello, dave! How are you?\n");</p>
        <p id="p_12590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12590"> }</p>
        <p id="p_12591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12591"> The getpgrp and setpgrp Functions</p>
        <p id="p_12592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12592"> In the UNIX environment, processes are organized into collections of processes known as process groups. Each process group is identified by a unique integer known as a process group ID.</p>
        <p id="p_12593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12593"> The  getpgrp  function retrieves the process group ID for a particular process.</p>
        <p id="p_12594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12594"> The syntax of the  getpgrp  function is</p>
        <p id="p_12595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12595"> pgroup = getpgrp (pid);</p>
        <p id="p_12596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12596"> pid  is the process ID whose group you want to retrieve, and  pgroup  is the returned process group ID, which is a scalar value.</p>
        <p id="p_12597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12597"> If  pid  is not specified or is zero,  getpgrp  assumes that you want the process group ID for the current process (the program you are running).</p>
        <p id="p_12598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12598"> Listing 15.6 is an example of a program that retrieves its own process group ID.</p>
        <p id="p_12599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12599"> Type</p>
        <p id="p_12600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12600"> Listing 15,6.  A  program that uses getpgrp.</p>
        <p id="p_12601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12601"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12602"> 2  :</p>
        <p id="p_12603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12603"> 3: $pgroup = getpgrp  (0);</p>
        <p id="p_12604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12604"> 4 :</p>
        <p id="p_12605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12605"> print ("The process group for this program is $pgroup.\n");</p>
        <p id="p_12606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12606"> Output</p>
        <p id="p_12607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12607"> $ programl5_6</p>
        <p id="p_12608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12608"> The process group for this program is 3313. $</p>
        <p id="p_12609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12609"> Analysis</p>
        <p id="p_12610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12610"> Line  3  calls  getpgrp  with the argument  0,  which indicates the current process (the current program). The process group ID for this process is returned in  $pgroup  and then printed.</p>
        <p id="p_12611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12611"> The  setpgrp  function enables you to set the process group ID for a process.</p>
        <p id="p_12612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12612"> The syntax of the  setpgrp  function is</p>
        <p id="p_12613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12613"> setpgrp (pid, groupid);</p>
        <p id="p_12614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12614"> pid  is the ID of the process whose group you want to change, and  groupid  is the process group ID you want your process to be part of. (This group ID is usually returned by a call to  getpgrp.)</p>
        <p id="p_12615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12615"> Warning</p>
        <p id="p_12616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12616"> Not all machines support  setpgrp,  and some machines impose limitations on how you can use it. If your program uses  setpgrp, you should call  getpgrp  immediately afterward to ensure that the process group ID has been set properly</p>
        <p id="p_12617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12617"> The getppid Function</p>
        <p id="p_12618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12618"> On UNIX machines, as you have seen, every running program or other executing process has its own unique process ID. Each program and process also is associated with a parent process, which is the process that started it. For example, when you execute a command that starts a Perl program, the parent process of the Perl program is the shell program from which you entered the command.</p>
        <p id="p_12619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12619"> To retrieve the process ID for the parent process for your program, call the function  getppid.</p>
        <p id="p_12620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12620"> The syntax of the  getppid  function is</p>
        <p id="p_12621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12621"> parentid = getppid();</p>
        <p id="p_12622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12622"> Here,  parentid  is the process ID of your program.</p>
        <p id="p_12623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12623"> You can use  getppid  with  fork  to ensure that each of the two processes produced by  fork  knows the process ID of the other.</p>
        <p id="p_12624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12624"> Listing 15.7 shows how to do this.</p>
        <p id="p_12625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12625"> Type</p>
        <p id="p_12626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12626"> Listing 15.7,  A  program that calls fork and getppid.</p>
        <p id="p_12627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12627"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12628"> 2  :</p>
        <p id="p_12629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12629"> 3: $otherid =  fork();</p>
        <p id="p_12630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12630"> 4:   if  ($otherid == 0) {</p>
        <p id="p_12631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12631"> 5: # this is the child; retrieve parent ID</p>
        <p id="p_12632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12632"> 6: $otherid = getppid();</p>
        <p id="p_12633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12633"> 7: } else {</p>
        <p id="p_12634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12634"> 8: # this is the parent</p>
        <p id="p_12635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12635"> 9: }</p>
        <p id="p_12636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12636"> This program requires no input and generates no output.</p>
        <p id="p_12637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12637"> Analysis</p>
        <p id="p_12638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12638"> When line 3 calls  fork,  the program splits into two separate processes (or running programs, if you want to think of them that way),  fork  returns 0 to the child process and returns the process ID of</p>
        <p id="p_12639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12639"> the child process to the parent process. At this point, the parent process knows the process ID of the child, but the child does not know the process ID of the parent.</p>
        <p id="p_12640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12640"> Line 6, which is executed only by the child process, fixes this imbalance by calling  getppid  and returning the process ID of the parent (the other process created by fork). After the child process executes line 6, both the parent and the child process have stored the process ID of the other process in the scalar variable  $otherid.</p>
        <p id="p_12641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12641"> After each process has the ID of the other, the processes can send signals to one another using the kill function (which is discussed on  Day 13 ,  "Process, String, and Mathematical Functions").</p>
        <p id="p_12642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12642"> The getpwnam Function</p>
        <p id="p_12643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12643"> On UNIX machines, the  /etc/passwd  file (also known as the password file) contains information on each of the users who are authorized to use the machine. The  getpwnam  function enables you to retrieve the password file entry for a particular user.</p>
        <p id="p_12644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12644"> The syntax of the  getpwnam  function is</p>
        <p id="p_12645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12645"> (username, A_homedir,</p>
        <p id="p_12646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12646"> password, userid, groupid, quota, comment, infofield, shell) = getpwnam (name);</p>
        <p id="p_12647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12647"> name  is the login user ID of the user whose information you want to retrieve. If an entry in the /etc/passwd  file corresponds to this name,  getpwnam  returns a nine-element list containing the contents of the entry. These contents are</p>
        <p id="p_12648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12648"> • username,  which is identical to  name</p>
        <p id="p_12649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12649"> • password,  which is the user's encrypted password</p>
        <p id="p_12650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12650"> • userid,  which is the unique numerical ID that represents this user</p>
        <p id="p_12651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12651"> • groupid,  which is the ID of the group to which this user belongs</p>
        <p id="p_12652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12652"> • quota  and  comment,  which mean different things on different machines (check your local getpwnam  manual page for details)</p>
        <p id="p_12653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12653"> • infofield,  which is a character string containing personal information about the user (such as the room number of the user's office, or the user's phone number)</p>
        <p id="p_12654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12654"> • homedir,  which is the user's home directory (the directory that becomes the current directory when the user logs in)</p>
        <p id="p_12655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12655"> • shell,  which is the command shell that is stalled when the user logs in getpwnam  returns the empty list if no password file entry for  name  exists.</p>
        <p id="p_12656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12656"> You can use  getpwnam  in various ways. The most common way is to retrieve the user ID or group ID corresponding to a particular user name. Listing 15.8 is a program that retrieves and prints the user ID for a particular user.</p>
        <p id="p_12657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12657"> Type</p>
        <p id="p_12658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12658"> Listing 15.8. A program that retrieves the user ID for a user.</p>
        <p id="p_12659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12659"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12660"> 2  :</p>
        <p id="p_12661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12661"> 3: print ("Enter a username:\n");</p>
        <p id="p_12662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12662"> 4: $username = &lt;STDIN&gt;;</p>
        <p id="p_12663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12663"> 5: $username =~ s/ A \s+|\s+$//g;</p>
        <p id="p_12664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12664"> 6: if (($username, $passwd, $userid) = getpwnam ($username)) {</p>
        <p id="p_12665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12665"> 7: print ("Username $username has user id $userid.\n");</p>
        <p id="p_12666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12666"> 8: } else {</p>
        <p id="p_12667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12667"> 9: print ("Username not found.\n");</p>
        <p id="p_12668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12668"> 10 : }</p>
        <p id="p_12669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12669"> Output</p>
        <p id="p_12670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12670"> $ programl5_8 Enter a username: dave</p>
        <p id="p_12671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12671"> Username dave has userid 127.</p>
        <p id="p_12672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12672"> $</p>
        <p id="p_12673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12673"> Analysis</p>
        <p id="p_12674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12674"> After lines 4 and 5 have retrieved the user name and removed any extraneous white space, line 6 passes the user name to  getpwnam.  If a password file entry exists for this user name, the nine-element entry is returned, and the first three elements are assigned to  $username, $password,  and  $userid.</p>
        <p id="p_12675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12675"> (The remaining elements are thrown away.) The third element, the user ID, is stored in  $userid  and is printed by line 7.</p>
        <p id="p_12676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12676"> The getpwuid Function</p>
        <p id="p_12677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12677"> The  getpwuid  function is similar to the  getpwnam  function because it also accesses the /etc/passwd  file,  getpwuid,  however, searches for the password file entry that matches a particular user ID.</p>
        <p id="p_12678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12678"> The syntax of the function is</p>
        <p id="p_12679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12679"> (username, password, userid, groupid, quota, comment, infofield, A_homedir, shell) = getpwuid (inputuid);</p>
        <p id="p_12680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12680"> inputuid  is the user ID that is to be searched for; it must be a nonzero integer. The nine-element list returned by  getpwuid  is identical to that returned by  getpwnam.</p>
        <p id="p_12681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12681"> NOTE</p>
        <p id="p_12682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12682"> The  userid  field in the nine-element list returned by  getpwuid  is always identical to the  inputuid  field that is passed as an argument</p>
        <p id="p_12683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12683"> The getpwent Function</p>
        <p id="p_12684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12684"> The  getpwnam  and  getpwuid  functions enable you to retrieve a single entry from the password file. To access each entry of the password file in turn, call  getpwent.</p>
        <p id="p_12685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12685"> The syntax for the  getpwent  function is</p>
        <p id="p_12686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12686"> (username, password, userid, groupid, quota, comment, infofield, A_homedir, shell) = getpwent();</p>
        <p id="p_12687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12687"> When a program calls  getpwent  for the first time, it retrieves the first entry in the  /etc/passwd  file. Subsequent calls retrieve further entries; if no more entries remain, the empty list is returned.</p>
        <p id="p_12688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12688"> The components of the nine-element list returned by  getpwent  are the same as those in the lists returned</p>
        <p id="p_12689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12689"> by  getpwnam  and  getpwuid.</p>
        <p id="p_12690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12690"> Listing 15.9 is an example of a program that uses  getpwent.  It lists the user names known by the machine as well as their user IDs.</p>
        <p id="p_12691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12691"> Type</p>
        <p id="p_12692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12692"> Listing 15.9. A program that uses getpwent.</p>
        <p id="p_12693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12693"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12694"> 2  :</p>
        <p id="p_12695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12695"> 3: while (1) {</p>
        <p id="p_12696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12696"> 4: last unless (($username, $password, $userid)</p>
        <p id="p_12697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12697"> 5: = getpwent ()) ;</p>
        <p id="p_12698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12698"> 6: $userlist{$username} = $userid;</p>
        <p id="p_12699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12699"> 7: }</p>
        <p id="p_12700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12700"> 8: print ("Users known to this machine:\n");</p>
        <p id="p_12701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12701"> 9: foreach $user (sort keys (%userlist)) {</p>
        <p id="p_12702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12702"> 10: printf ("%-20s %d\n", $user, $userlist{$user});</p>
        <p id="p_12703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12703"> 11 : }</p>
        <p id="p_12704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12704"> Output</p>
        <p id="p_12705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12705"> $ programl5_9</p>
        <p id="p_12706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12706"> Users known to this machine:</p>
        <p id="p_12707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12707"> adm 4</p>
        <p id="p_12708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12708"> daemon 1</p>
        <p id="p_12709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12709"> dave 127</p>
        <p id="p_12710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12710"> ftp 8</p>
        <p id="p_12711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12711"> jimmy</p>
        <p id="p_12712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12712"> 711</p>
        <p id="p_12713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12713"> root</p>
        <p id="p_12714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12714"> 0</p>
        <p id="p_12715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12715"> $</p>
        <p id="p_12716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12716"> The  while  loop in lines 3-7 uses  getpwent  to read every entry in the password file. Only the first three elements of the returned list are saved-in the scalar variables  $username, $pas  sword, and $userid-the rest are thrown away. After the  /etc/passwd  file has been completely read, line 4 terminates the  while  loop.</p>
        <p id="p_12717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12717"> Line 6 creates an associative array element for each user. The subscript for the array element is the user name, and the value of the element is the user ID.</p>
        <p id="p_12718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12718"> Lines 9-11 print the list of users, sorting them in order by user name and printing the name and user ID for each.</p>
        <p id="p_12719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12719"> The setpwent and endpwent Functions</p>
        <p id="p_12720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12720"> Analyse</p>
        <p id="p_12721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12721"> Like  getpwent, setpwent  and  endpwent  manipulate the  /etc/passwd  file. The  setpwent  function rewinds the /  etc/passwd  file.</p>
        <p id="p_12722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12722"> The syntax of the  setpwent  function is</p>
        <p id="p_12723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12723"> setpwent (keepopen);</p>
        <p id="p_12724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12724"> If  keepopen  is nonzero, the Perl interpreter assumes that the  /etc/passwd  file is to be accessed again, and it keeps information about the password file stored in internal memory. If  keepopen  is zero, any information the program has related to the password file is thrown away.</p>
        <p id="p_12725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12725"> The  endpwent  function closes the password file and tells the program to throw away any internal memory related to it.</p>
        <p id="p_12726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12726"> The  endpwent  function accepts no arguments and returns no values.</p>
        <p id="p_12727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12727"> endpwent();</p>
        <p id="p_12728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12728"> The getpriority and setpriority Functions</p>
        <p id="p_12729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12729"> In the UNIX environment, each process has a priority, which tells the system which processes are important and which are not. Priorities are integer values that vary from system to system: a typical range is from -20 (most important) to 20 (least important), with a default value of 0.</p>
        <p id="p_12730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12730"> NOTE</p>
        <p id="p_12731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12731"> Although priority ranges might vary from system to system, the general rule under UNIX is always this: the higher the priority number associated with a process, the less important the process is</p>
        <p id="p_12732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12732"> To change the priority for your program, process, process group, or user ID, call the  setpriority function.</p>
        <p id="p_12733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12733"> The syntax of the  setpriority  function is</p>
        <p id="p_12734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12734"> setpriority (category, id, priority);</p>
        <p id="p_12735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12735"> category  is a scalar value that indicates what processes are to have their priorities altered. To find the value to use, take the following actions:</p>
        <p id="p_12736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12736"> • Examine the header file  /usr/include/sys/resource . h</p>
        <p id="p_12737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12737"> • In this file, look up and note the values of the constants  PRIO_PROCESS, PRIO_PGRP,  and PRIO_USER</p>
        <p id="p_12738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12738"> • Pick the appropriate value to use, as described in the remainder of this section</p>
        <p id="p_12739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12739"> NOTE</p>
        <p id="p_12740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12740"> If you are not familiar with the C programming language, the value of a constant is specified by a statement of the following form:</p>
        <p id="p_12741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12741"> #define constant value</p>
        <p id="p_12742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12742"> Here,  constant  is a constant such as  PRIO_PROCESS,  and value  is its defined value</p>
        <p id="p_12743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12743"> If  category  is the value associated with  PRIO_PROCESS,  only one process has its priority altered. If category  is the value of  PRIO_PGRP,  every process in a process group has its priority altered. If category  is the value of  PRIO_USER,  every process belonging to a particular user has its priority altered.</p>
        <p id="p_12744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12744"> The value of  id  depends on  category.  If  category  is the value of  PRIO_PROCESS, id  is the process ID for the process whose priority is to be altered. If  category  is the value of  PRIO_PGRP, id  is the process group ID for the group whose priority is to be altered. If  category  is  PRIO_USER, id  is the user ID for the group whose priority is to be altered.</p>
        <p id="p_12745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12745"> NOTE</p>
        <p id="p_12746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12746"> If  category  is the value of  PRIO_PROCESS  or  PRIO_PGRP  and</p>
        <p id="p_12747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12747"> id is 0, id is assumed to be the ID of the current process or process group</p>
        <p id="p_12748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12748"> priority  is the new priority for the process, group, or user. You can specify a lower priority value for your process or processes (in other words, specify that your processes are "more important") only if you are a privileged user (the superuser).</p>
        <p id="p_12749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12749"> The function  getpriority  retrieves the current priority for a process, process group, or user.</p>
        <p id="p_12750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12750"> The syntax of the  getpriority  function is</p>
        <p id="p_12751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12751"> priority = getpriority (category, id) ;</p>
        <p id="p_12752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12752"> Here,  category  and  id  are identical to the equivalent arguments in  setpriority. priority  is the returned current priority.</p>
        <p id="p_12753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12753"> Listing 15.10 is a program that lowers the priority of every process you are currently running. It uses several of the functions you have seen in today's lesson.</p>
        <p id="p_12754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12754"> Listing 15,10.  A  program that  uses  setpriority and getpriority.</p>
        <p id="p_12755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12755"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12756"> 2  :</p>
        <p id="p_12757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12757"> 3: print ("You're not in a hurry today, are you?\n") ;</p>
        <p id="p_12758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12758"> 4: $username = getlogin();</p>
        <p id="p_12759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12759"> 5: ($username, $password, $userid) = getpwnam ($username);</p>
        <p id="p_12760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12760"> 6: $oldpriority = getpriority (2, $userid);</p>
        <p id="p_12761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12761"> 7: setpriority (2, $userid, $oldpriority + 1);</p>
        <p id="p_12762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12762"> Output</p>
        <p id="p_12763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12763"> $ programl5_10</p>
        <p id="p_12764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12764"> You're not in a hurry today, are you? $</p>
        <p id="p_12765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12765"> Analysis</p>
        <p id="p_12766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12766"> Line 4 of this program calls  get login,  which retrieves the user's login name. Then, line 5 passes this name to  getpwnam,  which retrieves the user ID from the  /etc/pas swd  file.</p>
        <p id="p_12767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12767"> Line  6  calls  getpriority.  Because the first argument to  getpriority  is  2  (the value of the system constant  PRIO_USER),  the current priority for all processes owned by the user specified by the user ID stored in  $useridis  returned.</p>
        <p id="p_12768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12768"> Line 7 calls  setpriority,  adding one to the current priority for the user to obtain the new priority. As in line 6, the first argument to  setpriority  is  2 (PRIO_USER),  which indicates that the current priority for all processes belonging to this user is to be changed.</p>
        <p id="p_12769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12769"> The getprotoent Function</p>
        <p id="p_12770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12770"> The  getprotoent  function enables you to search through the system protocol database, which is stored in the file  /etc/protocols.</p>
        <p id="p_12771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12771"> The syntax of the  getprotoent  function is</p>
        <p id="p_12772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12772"> (name, aliases, number) = getprotoent();</p>
        <p id="p_12773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12773"> name  is the name associated with a particular system protocol,  aliases  is a scalar value consisting of a list of alternative names for this system protocol, with names being separated by a space,  number  is the number associated with this particular system protocol.</p>
        <p id="p_12774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12774"> The first call to  getprotoent  returns the first element in  /etc/protocols.  Further calls return subsequent entries; when  /etc/protocols  is exhausted,  getprotoent  returns the empty list.</p>
        <p id="p_12775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12775"> The get protobyname and getprotoby number Functions</p>
        <p id="p_12776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12776"> The  getprotobyname  and  getprotobynumber  functions provide ways of searching in the /etc/protocols  file.</p>
        <p id="p_12777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12777"> The  getprotobyname  function enables you to search for a particular protocol entry in the</p>
        <p id="p_12778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12778"> /etc/protocols file.</p>
        <p id="p_12779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12779"> The syntax of the  getprotobyname  function is</p>
        <p id="p_12780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12780"> (name, aliases, number) = getprotobyname (searchname);</p>
        <p id="p_12781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12781"> Here,  searchname  is the protocol name you are looking for.  name, aliases,  and  number  are the same as in  getprotoent.</p>
        <p id="p_12782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12782"> Similarly,  getprotobynumber  searches for a protocol entry in / etc/protocols  that matches a particular protocol number.</p>
        <p id="p_12783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12783"> The syntax of the  getprotobynumber  function is</p>
        <p id="p_12784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12784"> (name, aliases, number) = getprotobynumber (searchnum);</p>
        <p id="p_12785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12785"> searchnum  is the protocol number to search for.  name, aliases,  and  number  are the same as in getprotoent.</p>
        <p id="p_12786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12786"> Both functions return the empty list if no matching protocol database entry is found.</p>
        <p id="p_12787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12787"> The setprotoent and endprotoent Functions</p>
        <p id="p_12788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12788"> The  setprotoent  and  endprotoent  functions provide other ways of manipulating the</p>
        <p id="p_12789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12789"> /etc/protocols  file.</p>
        <p id="p_12790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12790"> The  setprotoent  function rewinds the  /etc/protocols  file.</p>
        <p id="p_12791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12791"> The syntax of the  setprotoent  function is</p>
        <p id="p_12792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12792"> setprotoent (keepopen);</p>
        <p id="p_12793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12793"> If  keepopen  is a nonzero value, the value indicates that the program should keep  /etc/protocols open, because it intends to continue accessing the system protocol database. After  setprotoent  has been called, the next call to  getprotoent  reads (or rereads) the first element of the database.</p>
        <p id="p_12794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12794"> The  endprotoent  function closes the  /etc/protocols  file and indicates that the program no longer wants to read any system protocols from the database.</p>
        <p id="p_12795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12795"> The  endprotoent  function requires no arguments and returns no values:</p>
        <p id="p_12796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12796"> endprotoent() ;</p>
        <p id="p_12797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12797"> NOTE</p>
        <p id="p_12798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12798"> For more information on system protocols, refer to the getprotoent  manual page on your system</p>
        <p id="p_12799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12799"> The getservent Function</p>
        <p id="p_12800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12800"> The  getservent  function enables you to search through the system services database, which is stored in the file /  etc/services.</p>
        <p id="p_12801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12801"> The syntax of the  getservent  function is</p>
        <p id="p_12802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12802"> (name, aliases, portnum, protoname) = getservent();</p>
        <p id="p_12803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12803"> name  is the name associated with a particular system service,  aliases  is a scalar value consisting of a list of alternative names for this system service; the names are separated by a space.</p>
        <p id="p_12804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12804"> portnum  is the port number associated with this particular system protocol, which indicates the location of the port at which the service is residing. This port number is returned as a packed array of integers, which can be unpacked using  unpack  (with aC* format specifier).</p>
        <p id="p_12805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12805"> protoname  is a protocol name (such as  tcp).</p>
        <p id="p_12806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12806"> The first call to  getservent  returns the first element in  /etc/ services.  Further calls return subsequent entries; when  /etc/services  is exhausted,  getservent  returns the empty list.</p>
        <p id="p_12807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12807"> The getservbyname and getservbyport Functions</p>
        <p id="p_12808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12808"> The  getservbyname  and  getservbyport  functions provide ways of searching in the /etc/services  file.</p>
        <p id="p_12809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12809"> The  getservbyname  function enables you to search the  /etc/ services  file for a particular service name.</p>
        <p id="p_12810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12810"> The syntax of the  getservbyname  function is</p>
        <p id="p_12811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12811"> (name, aliases, portnum, protoname) = getservbyname (searchname, searchproto) ;</p>
        <p id="p_12812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12812"> Here,  searchname  and  searchproto  are the service name and service protocol type to be matched. If the name and type are matched,  getservbyname  returns the system service database entry corresponding to this name and type. This entry is the same four-element list as is returned by getservent.  (The empty list is returned if the name and type are not matched.)</p>
        <p id="p_12813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12813"> Similarly, the  get servbyport  function searches for a service name that matches a particular service port number.</p>
        <p id="p_12814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12814"> The syntax of the  getservbyname  function is</p>
        <p id="p_12815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12815"> (name, aliases, portnum, protoname) = getservbyname (searchportnum, Asearchproto);</p>
        <p id="p_12816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12816"> searchportnum  and  searchproto  are the port number and protocol type to search for.  name, aliases, portnum,  and  protoname  are the same as in  getservbyname  and  getservent.</p>
        <p id="p_12817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12817"> The setservent and endservent Functions</p>
        <p id="p_12818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12818"> The  setservent  and  endservent  functions provide other ways of manipulating the</p>
        <p id="p_12819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12819"> /etc/services  file.</p>
        <p id="p_12820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12820"> The  setservent  function rewinds the  /etc/services  file.</p>
        <p id="p_12821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12821"> The syntax of the  setservent  function is</p>
        <p id="p_12822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12822"> setservent (keepopen);</p>
        <p id="p_12823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12823"> After  setservent  has been called, the next call to  getservent  retrieves the first element of the /etc/services  file,  keepopen,  if nonzero, specifies that the  /etc/services  file is still in use and is to remain open.</p>
        <p id="p_12824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12824"> The function  endservent  indicates that the  /etc/services  file can be closed, because it is no longer needed.</p>
        <p id="p_12825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12825"> The  endservent  function requires no arguments and returns no values:</p>
        <p id="p_12826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12826"> endservent() ;</p>
        <p id="p_12827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12827"> The chroot Function</p>
        <p id="p_12828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12828"> The  chroot  function enables you to specify the root directory for your program and any subprocesses that it creates.</p>
        <p id="p_12829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12829"> The syntax of the  chroot  function is</p>
        <p id="p_12830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12830"> chroot (dirname);</p>
        <p id="p_12831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12831"> dirname  is the name of the directory to serve as the root. After  chroot  has been called, the directory name specified by  dirname  is appended to every pathname specified by your program and its subprocesses. For example, the statement</p>
        <p id="p_12832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12832"> chroot ( "/pub");</p>
        <p id="p_12833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12833"> adds  /pub  to the front of every directory name. For example, when your program or a subprocess tries to access the directory  /u/ jqpublic,  the directory it accesses is actually  /pub/u/ jqpublic.</p>
        <p id="p_12834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12834"> chroot  often is used to restrict access to a particular portion of a file system. It can be called only if you have superuser privileges on your machine and execute permission on the specified root directory.</p>
        <p id="p_12835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12835"> The ioctl Function</p>
        <p id="p_12836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12836"> The  i octl  function enables you to set system-dependent file attributes (such as the special character definitions for your keyboard).</p>
        <p id="p_12837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12837"> The syntax of the  ioctl  function is</p>
        <p id="p_12838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12838"> ioctl (filevar, attribute, value);</p>
        <p id="p_12839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12839"> f ilevar  is a file variable representing a previously opened file,  attribute  is a value representing the operation to be performed. Incorporated as part of the  attribute  value is a number indicating whether the operation is retrieving the value of an attribute or setting the value of an attribute.</p>
        <p id="p_12840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12840"> value  holds the attribute value associated with the operation specified by  attribute.  If the operation is setting an attribute,  value  contains the new value of the attribute. If the operation is retrieving the current value of the attribute,  value  is assigned this current value.</p>
        <p id="p_12841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12841"> ioctl  returns a nonzero value if the operation is performed successfully, or zero if the operation fails.</p>
        <p id="p_12842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12842"> NOTE</p>
        <p id="p_12843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12843"> For details on what operations can be performed on your machine, refer to the file  /usr/include/ sys/ioctl. h  on your machine. This file is a header file written in the C programming language that contains information on the available  ioctl  operations</p>
        <p id="p_12844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12844"> Warning</p>
        <p id="p_12845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12845"> Different machines (and devices) support different  ioctl operations. Thus, a program that requests an  ioctl  operation is not portable if you move it from one machine to another. You therefore should use  ioctl  operations only when you must</p>
        <p id="p_12846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12846"> The alarm Function</p>
        <p id="p_12847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12847"> The  alarm  function sends a special "alarm" signal,  SIGALARM,  to your program. The syntax of the  alarm  function is</p>
        <p id="p_12848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12848"> alarm (value);</p>
        <p id="p_12849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12849"> value  is an expression indicating how many seconds are to pass before the alarm goes off.</p>
        <p id="p_12850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12850"> For more information on signals and their relationship to processes, refer to the description of the  kill function on  Day  13 .</p>
        <p id="p_12851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12851"> Calling the System select Function</p>
        <p id="p_12852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12852"> Perl enables you to call the UNIX  select  function from within your Perl program.</p>
        <p id="p_12853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12853"> The syntax for a call to the UNIX  select  function is</p>
        <p id="p_12854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12854"> select (mask, wmask, emask, timeout) ;</p>
        <p id="p_12855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12855"> rmask, wmask  and  emask  are bit masks, and  timeout  is a timeout value in seconds.</p>
        <p id="p_12856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12856"> For more information on  select,  refer to the UNIX manual page.</p>
        <p id="p_12857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12857"> NOTE</p>
        <p id="p_12858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12858"> The UNIX  select  function is different from the Perl  select function that you've seen in earlier lessons.</p>
        <p id="p_12859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12859"> The Perl interpreter determines whether a program is calling the Perl select  function or the UNIX  select  function by counting the number of arguments: the Perl function expects only one, and the UNIX function expects four</p>
        <p id="p_12860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12860"> The dump Function</p>
        <p id="p_12861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12861"> The  dump  function, which is defined only in Perl 5, enables you to generate a UNIX core dump from within your Perl program.</p>
        <p id="p_12862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12862"> The syntax for the  dump  function is</p>
        <p id="p_12863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12863"> dump(label) ;</p>
        <p id="p_12864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12864"> label  is an optional label, specifying where execution is to restart if the UNIX  undump  command is executed.</p>
        <p id="p_12865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12865"> Warning</p>
        <p id="p_12866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12866"> If a core dump file created by  dump  is restarted by the UNIX undump  command, files that were open when the program was executing will no longer be open. This means they cannot be read from or written to</p>
        <p id="p_12867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12867"> Socket-Manipulation Functions</p>
        <p id="p_12868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12868"> In Berkeley UNIX environments (version 4.3BSD) and some other environments, processes can communicate with one another using a connection device known as a  socket.  When a socket has been created, one process can write data which can then be read by another process.</p>
        <p id="p_12869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12869"> Perl supports various functions that create sockets and set up connections with them. The following sections describe these functions.</p>
        <p id="p_12870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12870"> The socket Function</p>
        <p id="p_12871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12871"> To create a socket, call the  socket  function. This function defines a socket and associates it with a Perl file variable.</p>
        <p id="p_12872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12872"> The syntax of the  socket  function is</p>
        <p id="p_12873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12873"> socket (socket, domain, type, format);</p>
        <p id="p_12874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12874"> socket  is a file variable that is to be associated with the new socket.</p>
        <p id="p_12875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12875"> domain  is the protocol family to use. The legal values for  domain  are listed in the system header file /usr/include/sys/socket. h;  these values are represented by the constants  PF_UNIX, PF_INET, PF_IMPLINK,  and  PF_NS.</p>
        <p id="p_12876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12876"> type  is the type of socket to create. The legal values for  type  are also listed in the file /usr/include/sys/socket. h.  These legal values are represented by the five constants SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET,  and  SOCK_RDM.</p>
        <p id="p_12877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12877"> format  is the number of the protocol to be used with the socket. This protocol is normally retrieved by calling  getprotobyname.  (See the manual page for  getprotobyname  for details on what protocols are supported on your machine.)</p>
        <p id="p_12878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12878"> The  socket  function returns a nonzero value if the socket has been created and zero if an error occurs.</p>
        <p id="p_12879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12879"> The bind Function</p>
        <p id="p_12880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12880"> After you create a socket using  socket,  the next step is to bind the socket to a particular network address. To do this, use the  bind  function.</p>
        <p id="p_12881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12881"> The syntax of the  bind  function is</p>
        <p id="p_12882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12882"> bind (socket, address) ;</p>
        <p id="p_12883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12883"> Here,  socket  is the file variable corresponding to the socket created by  socket.</p>
        <p id="p_12884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12884"> address  is the network address to be associated with the socket. This address consists of the following elements:</p>
        <p id="p_12885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12885"> • The address type, which is an unsigned short integer and is always  AF_INET  (defined in /us r/include/net db . h  or  /us r/include/bsd/netdb . h).</p>
        <p id="p_12886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12886"> • The number of the port to use when connecting, which is a short integer in network order</p>
        <p id="p_12887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12887"> • The packed four-byte representation of the Internet address of the machine to which the socket is to be bound</p>
        <p id="p_12888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12888"> This function returns a nonzero value if the bind operation succeeds and zero if an error occurs. To create an address suitable for passing to  bind,  call  pack.</p>
        <p id="p_12889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12889"> $address = pack ("Sna4x8", 2, $portnum, $intaddress);</p>
        <p id="p_12890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12890"> Here, the pack format specifier  Sna4x8  indicates an unsigned short integer, followed by a short integer in network order (the port number), a four-byte ASCII string (which is the packed address), and eight null bytes. This is the format that  bind  expects when binding an address to a socket.</p>
        <p id="p_12891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12891"> The listen Function</p>
        <p id="p_12892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12892"> After an address has been bound to the socket associated with each of the machines that are to communicate, the next step is to define a process that is to be the "listening" process. This process waits for connections to be established with it. (In a client-server architecture, this process corresponds to the server.) To define this listening process, call the  listen  function.</p>
        <p id="p_12893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12893"> The syntax of the  listen  function is</p>
        <p id="p_12894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12894"> listen (socket, number);</p>
        <p id="p_12895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12895"> socket  is the socket created using the  socket  function,  number  is the maximum number of processes that can be queued up to connect to this process.</p>
        <p id="p_12896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12896"> listen  returns a nonzero value if it executes successfully, zero if it does not.</p>
        <p id="p_12897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12897"> Warning</p>
        <p id="p_12898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12898"> The maximum number of processes that can be queued using listen  is 5. This limitation is imposed by the Berkeley UNIX operating system</p>
        <p id="p_12899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12899"> The accept Function</p>
        <p id="p_12900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12900"> After a process that has been established as the listening process calls  listen,  the next step is to have this process call the  accept  function,  accept  waits until a process wants to connect with it, and then it returns the address of the connecting process.</p>
        <p id="p_12901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12901"> The syntax of the  accept  function is</p>
        <p id="p_12902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12902"> accept (procsocket, socket);</p>
        <p id="p_12903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12903"> procsocket  is a previously undefined file variable that is to represent the newly created connection. The listening process can then send to or receive from the other process using the file variable specified in procsocket.  This file variable can be treated like any other file variable: the program can send data through the socket by calling  write  or  print,  or can read data using the &lt;&gt; operator.</p>
        <p id="p_12904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12904"> socket  is the socket created by  socket  and bound to an address by  bind.</p>
        <p id="p_12905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12905"> Listing 15.11 is an example of a program that uses  listen  and  accept  to create a simple server. This server just sends the message  Hello, world ! to any process that connects to it. (A client program that receives this message is listed in the next section, "The  connect  Function.")</p>
        <p id="p_12906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12906"> Type</p>
        <p id="p_12907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12907"> Listing 15.11. A simple server program.</p>
        <p id="p_12908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12908"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12909"> 2  :</p>
        <p id="p_12910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12910"> 3: $line = "Hello, world!\n";</p>
        <p id="p_12911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12911"> 4 :</p>
        <p id="p_12912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12912"> 5: $port = 2000;</p>
        <p id="p_12913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12913"> 6: while (getservbyport ($port, "tcp")) {</p>
        <p id="p_12914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12914"> 7: $port++;</p>
        <p id="p_12915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12915"> 8 : }</p>
        <p id="p_12916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12916"> 9: ($dl, $d2, $prototype) = getprotobyname ("tcp");</p>
        <p id="p_12917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12917"> 10: ($dl, $d2, $d3, $d4, $rawserver) = gethostbyname ("silver");</p>
        <p id="p_12918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12918"> 11: $serveraddr = pack ("Sna4x8", 2, $port, $rawserver);</p>
        <p id="p_12919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12919"> 12: socket (SSOCKET, 2, 1, $prototype) || die ("No socket");</p>
        <p id="p_12920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12920"> 13: bind (SSOCKET, Sserveraddr) || die ("Can't bind");</p>
        <p id="p_12921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12921"> 14: listen (SSOCKET, 1) || die ("Can't listen");</p>
        <p id="p_12922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12922"> ($clientaddr = accept (SOCKET, SSOCKET)) ||</p>
        <p id="p_12923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12923"> 15 :</p>
        <p id="p_12924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12924"> 16: die ("Can't accept");</p>
        <p id="p_12925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12925"> 17: select (SOCKET);</p>
        <p id="p_12926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12926"> 18: $| = 1;</p>
        <p id="p_12927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12927"> 19: print SOCKET ("$line\n");</p>
        <p id="p_12928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12928"> 20: close (SOCKET);</p>
        <p id="p_12929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12929"> 21: close (SSOCKET);</p>
        <p id="p_12930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12930"> Output</p>
        <p id="p_12931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12931"> (This program requires no input and generates no output.</p>
        <p id="p_12932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12932"> Analysis</p>
        <p id="p_12933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12933"> The first task this server program performs is to search for a port to use when establishing a socket connection. To be on the safe side, the program first checks that the port it is going to use, port 2000, is not reserved for use by another program. If it is reserved, the program checks port 2001, then port 2002, and so on until it finds an unused port.</p>
        <p id="p_12934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12934"> To do this checking, line 6 calls  getservbyport.  If  get servbyport  returns a non-empty list, the port being checked is listed in the  /etc / services  file, which means that it is being used by some other program. In this case, the port number is increased by one, and  getservbyport  is called again. This process continues until  getservbyport  returns an empty list, which indicates that the port being checked is unused. When lines 5-8 are no longer executing, the scalar variable  $port  contains the number of the port to be used.</p>
        <p id="p_12935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12935"> Line 9 calls  getprotobyname  to retrieve the  /etc/protocols  entry associated with the TCP protocol. The protocol number associated with the TCP protocol is retrieved from this /etc/protocols  entry and is stored in the scalar variable  $prototype.  (The other elements of the list are ignored; the convention used by this program is to store element entries that are not going to be used in variables named  $dl, $d2,  and so on; the  d  stands for dummy.)</p>
        <p id="p_12936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12936"> Line 10 calls  gethostbyname  to retrieve the network address of the machine on which this server is running. This program assumes that the server is running on a local machine named  silver.  To run this program on your own machine, replace  silver  with your machine name.</p>
        <p id="p_12937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12937"> You can modify this program to run on any machine. To do so, modify line 10 as shown here:</p>
        <p id="p_12938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12938"> ($dl, $d2, $d3, $d4, $rawserver) = gethostbyname ('hostname');</p>
        <p id="p_12939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12939"> The string in backquotes, '  hostname  ', tells the Perl interpreter to call the  hostname  program and return its output as a scalar value. The  hostname  program returns the name of the machine on which it is running. Therefore, the call to  gethostbyname  retrieves the address</p>
        <p id="p_12940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12940"> of the machine on which you are running regardless of what the machine is.</p>
        <p id="p_12941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12941"> This capability enables you to move this program from one machine to another without having to modify it.</p>
        <p id="p_12942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12942"> Note that enclosing a command in backquotes works for any UNIX command that returns output. For example, the statement</p>
        <p id="p_12943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12943"> $userid = 'whoami';</p>
        <p id="p_12944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12944"> assigns the current login user ID to the scalar variable  $userid (because the UNIX command  whoami  displays the current login user ID)</p>
        <p id="p_12945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12945"> After  gethostbyname  has been called, the scalar variable  $rawserver  contains the Internet address of your machine. Line 11 calls  pack  to convert the address type, the port number, and this address into the form understood by the operating system. (The address type parameter, 2, is the local value of  AF_INET, which is the only address type supported.) This information is stored in the scalar variable</p>
        <p id="p_12946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12946"> $ serveraddr.</p>
        <p id="p_12947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12947"> After  pack  is called to build the server address, the program is ready to create a socket. Line 12 does this by calling  socket.  This call to  socket  passes it the file variable  SSOCKET,  the socket domain, the socket type, and the protocol number. After  socket  is called, the file variable  SSOCKET  represents the "master socket" that is to listen for connections. (Note that the values  2  and  1  passed to  socket  are, respectively, the local values of the constants  PF_INET  and  SOCK_STREAM. PF_INET  indicates Internet-style protocol, and  SOCK_STREAM  indicates that transmission will be in the form of a stream of bytes.</p>
        <p id="p_12948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12948"> You likely will not need to use any other values for these arguments.)</p>
        <p id="p_12949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12949"> After the socket has been created, the next step is line 13, which associates the socket with your machine by calling  bind.  This call to  bind  is passed the file variable  SSOCKET  associated with the socket and the server address created by the call to  pack  in line 11.</p>
        <p id="p_12950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12950"> After the socket is bound to your machine address, you are ready to listen for clients that want to connect</p>
        <p id="p_12951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12951"> to your server. Line 14 does this by calling  listen.  This call to  listen  is passed the file variable SSOCKET  and the value  1;  the latter indicates that only one client is listened for at any particular time.</p>
        <p id="p_12952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12952"> Line 15 calls  accept,  which waits until a client process wants to connect to this server. When a connection is established,  accept  creates a new socket associated with this connection and uses the file variable  SOCKET  to represent it. (The address of the client connection is returned in  $clientaddr;  if you want to, you can use  unpack  to obtain the address, and then call  gethostbyaddr  to retrieve the name of the machine on which the client process is running.)</p>
        <p id="p_12953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12953"> When the connection has been established and the file variable  SOCKET  has been associated with it, you can treat  SOCKET  like any other file variable: you can read data from it or write data to it. Lines 17 and 18 turn off buffering for  SOCKET,  which ensures that data sent through the socket is sent right away. (If buffering is left on, the program won't send data until the special internal buffer is full, which means that the client process won't receive the data right away.) After buffering is turned off, line 19 writes the line of data to  SOCKET,  which sends it to the client process. (For more information on buffering and how it works, refer to "Redirecting One File to Another" on  Day  12 ,  "Working with the File System.")</p>
        <p id="p_12954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12954"> Warning</p>
        <p id="p_12955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12955"> Although you can both send and receive data through the same socket, doing so is dangerous, because you run the risk of deadlock. Deadlock  occurs when the client and server processes think that the other is going to send data. Neither can proceed until the other does.</p>
        <p id="p_12956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12956"> The only way to get out of a deadlock is to send signals to the processes (such as  KILL).</p>
        <p id="p_12957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12957"> To avoid a deadlock, make sure that you understand how data flows between the processes you are running</p>
        <p id="p_12958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12958"> The connect Function</p>
        <p id="p_12959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12959"> As you have seen, when two processes communicate using a socket, one process is designated as the listening process. This process calls  listen  to indicate that it is the listening process, and then it calls accept  to wait for a connection from another process. (Listening processes are called  servers,  because they provide service to the processes that connect to them. The processes that connect to servers are called clients.)</p>
        <p id="p_12960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12960"> To connect to a process that has called  accept  and is now waiting for a connection, use the  connect function.</p>
        <p id="p_12961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12961"> The syntax of the  connect  function is</p>
        <p id="p_12962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12962"> connect (socket, address);</p>
        <p id="p_12963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12963"> socket  is a file variable representing a socket created using  socket  and bound using  bind, address is the internal representation of the Internet address to which you want to connect. In the process to which this process is connecting, this address must have been passed to  bind  to bind it to a socket, and the socket, in turn, must have been specified in calls to  listen  and  accept.</p>
        <p id="p_12964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12964"> After  connect  has been called, the program that calls it can send data to or receive data from the other process by means of the file variable specified in  socket.</p>
        <p id="p_12965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12965"> Listing 15.12 is an example of a program that uses  connect  to obtain data from another process. (The process that sends the data is displayed in Listing 15.11.)</p>
        <p id="p_12966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12966"> Type</p>
        <p id="p_12967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12967"> Listing 15.12. A simple client program.</p>
        <p id="p_12968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12968"> 1: #!/usr/local/bin/perl</p>
        <p id="p_12969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12969"> 2  :</p>
        <p id="p_12970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12970"> 3: $port = 2000;</p>
        <p id="p_12971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12971"> 4: while (getservbyport ($port, "tcp")) {</p>
        <p id="p_12972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12972"> 5: $port++;</p>
        <p id="p_12973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12973"> 6 : }</p>
        <p id="p_12974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12974"> 7: ($dl, $d2, $prototype) = getprotobyname ("tcp");</p>
        <p id="p_12975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12975"> 8: ($dl, $d2, $d3, $d4, $rawclient) = gethostbyname ("mercury");</p>
        <p id="p_12976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12976"> 9: ($dl, $d2, $d3, $d4, $rawserver) = gethostbyname ("silver");</p>
        <p id="p_12977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12977"> 10: $clientaddr = pack ("Sna4x8", 2, 0, $rawclient);</p>
        <p id="p_12978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12978"> 11: $serveraddr = pack ("Sna4x8", 2, $port, $rawserver);</p>
        <p id="p_12979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12979"> 12: socket (SOCKET, 2, 1, $prototype) || die ("No socket");</p>
        <p id="p_12980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12980"> 13: bind (SOCKET, $clientaddr) || die ("Can't bind");</p>
        <p id="p_12981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12981"> 14: connect (SOCKET, $serveraddr);</p>
        <p id="p_12982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12982"> 15 :</p>
        <p id="p_12983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12983"> &lt;SOCKET&gt;;</p>
        <p id="p_12984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12984"> ( "$line\n") ;</p>
        <p id="p_12985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12985"> (SOCKET);</p>
        <p id="p_12986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12986"> $ programl5_12 Hello, world!</p>
        <p id="p_12987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12987"> $</p>
        <p id="p_12988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12988"> Lines 3-6 obtain the port to use when receiving data by means of a socket connection. As in Listing  15.11,  the port number is compared with the list of ports stored in  /etc/ services  by calling getservbyport.  The first unused port number greater than or equal to  2000  becomes the number of the port to use. (This program and Listing  15.11  assume that the same  /etc / services  file is being examined in both cases. If the  /etc/services  files are different, you will need to choose a port number yourself and specify this port number in both your client program and your server program-in other words, assign a prespecified value to the variable  $port.)</p>
        <p id="p_12989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12989"> Line 7 calls  getprotobyname  to retrieve the protocol number associated with the TCP protocol. This protocol number is eventually passed to  socket.</p>
        <p id="p_12990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12990"> Lines 8 and 9 retrieve the Internet addresses of the client (this program) and the server (the process to connect to).  $rawclient  is assigned the Internet address of the client, and  $rawserver  is assigned the Internet address of the server; each of these addresses is a four-byte scalar value.</p>
        <p id="p_12991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12991"> Lines  10  and  11  take the addresses stored in  $rawclient  and  $rawserver  and convert them to the form used by the socket processing functions. In both cases, the  2  passed to  pack  is the local value for AF_INET  (the only type of address supported in the UNIX environment). Note that line  10  doesn't bother specifying a port value to pass to  pack;  this is because the connection uses the port specified in the server address in line 11.</p>
        <p id="p_12992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12992"> Line  12  now calls  socket  to create a socket for the current program (the client). As in the call to  socket in Listing  15.11,  the values  2  and  1  passed to  socket  are the local values of the constants  PF_INIT  and S OCK_S TREAM;  if these values are different on your machine, you need to replace the values shown here with the ones defined for your machine. The call to  socket  in line  12  associates the file variable  SOCKET with the newly created socket.</p>
        <p id="p_12993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12993"> Analyse</p>
        <p id="p_12994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12994"> After the socket has been created, line 13 calls  bind  to associate the socket with the client program,  bind requires two arguments: the file variable associated with the socket that has just been created, and the address of the client machine as packed by line 10.</p>
        <p id="p_12995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12995"> Line 14 now tries to connect to the server process by calling  connect  and passing it the server address created by line 11. If the connection is successful, you can send and receive data through the socket using the  SOCKET  file variable.</p>
        <p id="p_12996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12996"> The  SOCKET  file variable behaves just like any other file variable. This means that line 16 reads a line of data from the server process. Because the server process is sending the character string  Hello, world! (followed by a newline character), this is the string that is assigned to  $line.  Line 17 then prints  $line, which means that the following appears on your screen:</p>
        <p id="p_12997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12997"> Hello, world!</p>
        <p id="p_12998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12998"> After the client process is finished with the  socket,  line 18 calls  close.  This call indicates that the program is finished with the socket. (After the socket is closed by both the server and the client programs, the server program can accept a connection from another client process, if desired.)</p>
        <p id="p_12999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_12999"> The shutdown Function</p>
        <p id="p_13000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13000"> When two processes are communicating using a socket, data can be sent in either direction: the client can receive data from the server, or vice versa. The  shutdown  function enables you to indicate that traffic in one or both directions is no longer needed.</p>
        <p id="p_13001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13001"> The syntax for the  shutdown  function is</p>
        <p id="p_13002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13002"> shutdown (socket, direction);</p>
        <p id="p_13003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13003"> Here,  socket  is the file variable associated with the socket whose traffic is to be restricted,  direction is one of the following values:</p>
        <p id="p_13004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13004"> • 0 indicates that the program can send through the socket but can no longer receive data.</p>
        <p id="p_13005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13005"> • 1 indicates that the program can receive data from the socket but can no longer send.</p>
        <p id="p_13006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13006"> • 2 indicates that both sending and receiving are disallowed.</p>
        <p id="p_13007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13007"> NOTE</p>
        <p id="p_13008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13008"> To terminate communication through a socket, call  close  and pass it the file variable associated with the socket:</p>
        <p id="p_13009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13009"> close (SOCKET);</p>
        <p id="p_13010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13010"> This line closes the socket represented by  SOCKET</p>
        <p id="p_13011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13011"> The socketpair Function</p>
        <p id="p_13012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13012"> The  socketpair  function is similar to  socket,  but it creates a pair of sockets rather than just one socket.</p>
        <p id="p_13013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13013"> The syntax of the  socketpair  function is</p>
        <p id="p_13014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13014"> socketpair (socketl, socket2, domain, type, format);</p>
        <p id="p_13015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13015"> socket  1 is the file variable to be associated with the first newly created socket, and  socket2  is the file variable to be associated with the second socket.</p>
        <p id="p_13016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13016"> As in  socket, domain  is the protocol family to use,  type  is the type of socket to create, and  format is the number of the protocol to be used with the socket.</p>
        <p id="p_13017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13017"> socketpair  often is used to create a bidirectional communication channel between a parent and a child process.</p>
        <p id="p_13018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13018"> Warning</p>
        <p id="p_13019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13019"> Some machines that support sockets do not support  socketpair</p>
        <p id="p_13020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13020"> The getsockopt and setsockopt Functions</p>
        <p id="p_13021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13021"> The  getsockopt  and  setsockopt  functions enable you to obtain and set socket options.</p>
        <p id="p_13022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13022"> To obtain the current value of a socket option in your environment, call the  getsockopt  function. The syntax of the  getsockopt  function is</p>
        <p id="p_13023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13023"> retval = getsockopt (socket, opttype, optname);</p>
        <p id="p_13024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13024"> socket  is the file variable associated with the socket whose option you want to retrieve.</p>
        <p id="p_13025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13025"> opttype  is the type of option (or option level). The value of the system constant  SOL_SOCKET  specifies a "socket level" option. To find out the other possible values for  opttype,  refer to the system header file</p>
        <p id="p_13026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13026"> /usr/include/sys/socket.h.</p>
        <p id="p_13027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13027"> optname  is the name of the option whose value is to be retrieved;  retval  is the value of this option.</p>
        <p id="p_13028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13028"> To set a socket option, call  setsockopt.</p>
        <p id="p_13029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13029"> The syntax of the  setsockopt  function is</p>
        <p id="p_13030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13030"> setsockopt (socket, opttype, optname, value);</p>
        <p id="p_13031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13031"> Here,  socket, opttype,  and  optname  are the same as in  getsockopt,  and  value  is the new value of the  optname  option.</p>
        <p id="p_13032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13032"> NOTE</p>
        <p id="p_13033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13033"> Socket options are system dependent (and a full treatment of them is beyond the scope of this book). For more information on socket options, refer to the  getsockopt  and  setsockopt  manual pages on your machine or to the / usr/include/sys/socket. h header file</p>
        <p id="p_13034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13034"> The getsockname and getpeername Functions</p>
        <p id="p_13035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13035"> The  getsockname  and  getpeername  functions enable you to retrieve the addresses of the two ends of a socket connection.</p>
        <p id="p_13036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13036"> The  getsockname  function returns the address of this end of a socket connection (the end created by the currently running program).</p>
        <p id="p_13037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13037"> The syntax of the  getsockname  function is</p>
        <p id="p_13038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13038"> retval = getsockname (socket);</p>
        <p id="p_13039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13039"> As in the other socket functions,  socket  is the file variable associated with a particular socket,  retval is the returned address.</p>
        <p id="p_13040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13040"> The returned address is in packed format as built by the calls to  pack  in Listing 15.11 and Listing 15.12.</p>
        <p id="p_13041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13041"> The following code retrieves a socket address and converts it into readable form:</p>
        <p id="p_13042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13042"> $rawaddr = getsockname ($dl, $d2, @addrbytes) $readable = join</p>
        <p id="p_13043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13043"> (SOCKET);</p>
        <p id="p_13044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13044"> = unpack ("SnC4x8", @addrbytes);</p>
        <p id="p_13045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13045"> $rawaddr);</p>
        <p id="p_13046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13046"> NOTE</p>
        <p id="p_13047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13047"> Normally, you already have the address returned by  getsockname because you need to pass it to  bind  to associate the socket with your machine</p>
        <p id="p_13048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13048"> To retrieve the address of the other end of the socket connection, call  getpeername. The syntax of the  getpeername  function is</p>
        <p id="p_13049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13049"> retval = getpeername (socket);</p>
        <p id="p_13050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13050"> As in  getsockname, socket  is the file variable associated with the socket, and  retval  is the returned address.</p>
        <p id="p_13051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13051"> NOTE</p>
        <p id="p_13052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13052"> The address returned by  getpeername  is normally identical to the address returned by  accept</p>
        <p id="p_13053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13053"> The UNIX System V IPC Functions</p>
        <p id="p_13054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13054"> The functions you've just seen describe interprocess communication using sockets. Sockets are supported on machines running the 4.3BSD (Berkeley UNIX) operating system and on some other UNIX operating systems as well.</p>
        <p id="p_13055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13055"> Some machines that do not support sockets support a set of UNIX System V interprocess communication (IPC) functions. These functions consist of the following:</p>
        <p id="p_13056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13056"> • Functions that send messages from one process to another by means of a message queue</p>
        <p id="p_13057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13057"> • Functions that create and manipulate shared memory</p>
        <p id="p_13058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13058"> • Functions that create and manipulate semaphores</p>
        <p id="p_13059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13059"> Perl enables you to use these IPC functions by defining Perl functions with the same names as the IPC functions. The following sections provide a brief description of these functions.</p>
        <p id="p_13060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13060"> For more information on any IPC function, refer to the manual page for that function.</p>
        <p id="p_13061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13061"> IPC Functions and the require Statement</p>
        <p id="p_13062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13062"> Before you can use any System V IPC functions, you first must give the program the information it needs to use them.</p>
        <p id="p_13063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13063"> To do this, add the following statements to your program, immediately following the</p>
        <p id="p_13064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13064"> # ! /usr/local/bin/perl  header line:</p>
        <p id="p_13065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13065"> require "ipc.ph"; require "msg.ph"; require "sem.ph"; require "shm.ph";</p>
        <p id="p_13066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13066"> The  require  statement is like the  #include  statement in the C preprocessor: it takes the contents of the specified file and includes them as part of your program.</p>
        <p id="p_13067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13067"> The syntax for the require statement is</p>
        <p id="p_13068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13068"> require "name";</p>
        <p id="p_13069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13069"> Here,  name  is the name of the file to be added to your program.</p>
        <p id="p_13070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13070"> For example, the following statement includes the file  ipc . ph  as part of your program:</p>
        <p id="p_13071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13071"> require "ipc.ph";</p>
        <p id="p_13072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13072"> NOTE</p>
        <p id="p_13073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13073"> If the Perl interpreter complains that it cannot find a file that you are trying to include using  require,  one of two things is wrong:</p>
        <p id="p_13074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13074"> • The built-in array variable @  INC  is not defined properly.</p>
        <p id="p_13075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13075"> • The file does not exist.</p>
        <p id="p_13076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13076"> See the description of @  INC  on  Day  17 , "System Variables," for more details</p>
        <p id="p_13077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13077"> The msgget Function</p>
        <p id="p_13078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13078"> To use the System V message-passing facility, the first step is to create a message queue ID to represent a particular message queue. To do this, call the  msgget  function.</p>
        <p id="p_13079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13079"> The syntax of the  msgget  function is</p>
        <p id="p_13080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13080"> msgid = msgget (key, flag);</p>
        <p id="p_13081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13081"> Here,  key  is either  IPC_PRIVATE  or an arbitrary constant. If  key  is  IPC_PRIVATE  or  flag  has IPC_CREAT  set, the message queue is created, and its queue ID is returned in  msgid.</p>
        <p id="p_13082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13082"> If  msgget  is unable to create the message queue,  msgid  is set to the null string.</p>
        <p id="p_13083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13083"> The msgsnd Function</p>
        <p id="p_13084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13084"> To send a message to a message queue, call the  msgsnd  function.</p>
        <p id="p_13085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13085"> The syntax of the  msgsnd  function is</p>
        <p id="p_13086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13086"> msgsnd (msgid, message,  flags);</p>
        <p id="p_13087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13087"> msgid  is the message queue ID returned by  msgget. message  is the text of the message, and  flags specifies options that affect the message.</p>
        <p id="p_13088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13088"> msgsnd  returns a nonzero value if the send operation succeeds, zero if an error occurs.</p>
        <p id="p_13089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13089"> For more information on the format of the message sent by  msgsnd,  refer to your  msgsnd  manual page.</p>
        <p id="p_13090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13090"> The msgrcv Function</p>
        <p id="p_13091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13091"> To obtain a message from a message queue, call the  msgrcv  function.</p>
        <p id="p_13092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13092"> The syntax of the  msgrcv  function is</p>
        <p id="p_13093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13093"> msgrcv (msgid, message, size, mesgtype, flags);</p>
        <p id="p_13094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13094"> Here,  msgid  is the ID of the message queue, as returned by  msgget. message  is a scalar variable (or array element) in which the message is to be stored,  size  is the size of the message, plus the size of the message type; this message type is specified in  mesgtype. flags  specifies options that affect the message.</p>
        <p id="p_13095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13095"> msgrcv  returns a nonzero value if the send operation succeeds, zero if an error occurs.</p>
        <p id="p_13096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13096"> The ms get 1 Function</p>
        <p id="p_13097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13097"> The  ms get 1  function enables you to set options for message queues and send commands that affect them. The syntax of the  ms get 1  function is</p>
        <p id="p_13098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13098"> msgctl (msgid, msgemd, msgarg) ;</p>
        <p id="p_13099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13099"> msgid  is the message queue ID.  msgemd  is the command to be sent to the message queue; the list of available commands is defined in the file  /usr/include/sys/ipc.h.</p>
        <p id="p_13100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13100"> Some of the commands that can be specified by  msgemd  set the values of message queue options. If one of these commands is specified, the new value of the option is specified in  msgarg.</p>
        <p id="p_13101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13101"> If an error occurs,  msgctl  returns the undefined value,  msgctl  also can return zero or a nonzero value.</p>
        <p id="p_13102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13102"> The shmget Function</p>
        <p id="p_13103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13103"> To use the System V shared memory capability, you must first create the shared memory. To do this, call the  shmget  function.</p>
        <p id="p_13104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13104"> The syntax of the shmget function is</p>
        <p id="p_13105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13105"> shmid = shmget (key, size, flag);</p>
        <p id="p_13106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13106"> Here,  key  is either  IPC_PRIVATE  or an arbitrary constant. If  key  is  IPC_PRIVATE  or  flag  has IPC_CREAT  set, the shared memory segment is created, and its ID is returned in  shmid. size  is the size of the created shared memory (in bytes). If  shmget  is unable to create the message queue,  shmid  is set to</p>
        <p id="p_13107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13107"> the null string.</p>
        <p id="p_13108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13108"> The shmwrite Function</p>
        <p id="p_13109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13109"> To send data to a particular segment of shared memory, call the  shmwrite  function. The syntax of the  shmwrite  function is</p>
        <p id="p_13110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13110"> shmwrite (shmid, text, pos,  size);</p>
        <p id="p_13111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13111"> shmid  is the shared memory ID returned by  shmget. text  is the character string to write to the shared memory,  pos  is the number of bytes to skip over in the shared memory before writing to it, and  si ze  is the number of bytes to write.</p>
        <p id="p_13112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13112"> This function returns a nonzero value if the write operation succeeds; it returns zero if an error occurs.</p>
        <p id="p_13113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13113"> NOTE</p>
        <p id="p_13114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13114"> If the character string specified by  text  is longer than the value specified by  size,  only the first  size  bytes of text are written to the shared memory.</p>
        <p id="p_13115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13115"> If the character string specified by  text  is shorter than the value specified by  size, shmwrite  fills the leftover space with null characters</p>
        <p id="p_13116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13116"> The shmread Function</p>
        <p id="p_13117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13117"> To obtain data from a segment of shared memory, call the  shmread  function. The syntax of the  shmread  function is</p>
        <p id="p_13118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13118"> shmread (shmid, retval, pos,  size);</p>
        <p id="p_13119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13119"> Here,  shmid  is the shared memory ID returned by  shmget. retval  is a scalar variable (or array element) in which the returned data is to be stored,  pos  is the number of bytes to skip in the shared memory segment before copying to  retval,  and  size  is the number of bytes to copy.</p>
        <p id="p_13120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13120"> This function returns a nonzero value if the read operation succeeds, and it returns zero if an error occurs.</p>
        <p id="p_13121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13121"> The shmctl Function</p>
        <p id="p_13122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13122"> The  shmctl  function enables you to set options for shared memory segments and send commands that affect them.</p>
        <p id="p_13123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13123"> The syntax of the  shmctl  function is</p>
        <p id="p_13124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13124"> shmctl (shmid, shmcmd, shmarg);</p>
        <p id="p_13125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13125"> shmid  is the shared memory ID returned by  shmget. shmcmd  is the command that affects the shared memory; the list of available commands is defined in the header file named</p>
        <p id="p_13126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13126"> /usr/include/sys/ipc.h.</p>
        <p id="p_13127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13127"> Some of the commands that can be specified by  shmcmd  set the values of shared memory options. If one of these commands is specified, the new value of the option is specified in  shmarg.</p>
        <p id="p_13128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13128"> If an error occurs,  shmctl  returns the undefined value,  shmctl  also can return zero or a nonzero value.</p>
        <p id="p_13129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13129"> The  semget Function</p>
        <p id="p_13130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13130"> To use the System V semaphore facility, you must first create the semaphore. To do this, call the  semget function.</p>
        <p id="p_13131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13131"> The syntax of the  semget  function is</p>
        <p id="p_13132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13132"> semid = semget (key, num, flag) ;</p>
        <p id="p_13133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13133"> Here,  key  is either  IPC_PRIVATE  or an arbitrary constant. If  key  is  IPC_PRIVATE  or  flag  has IPC_CREAT  set, the shared memory segment is created, and its ID is returned in  semid. num  is the number of semaphores created. If  semget  is unable to create the semaphore,  semid  is set to the null string.</p>
        <p id="p_13134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13134"> The  semop Function</p>
        <p id="p_13135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13135"> To perform a semaphore operation, call the  semop  function. The syntax of the  semop  function is</p>
        <p id="p_13136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13136"> semop (semid, semstructs);</p>
        <p id="p_13137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13137"> Here,  semid  is the semaphore ID returned by  semget,  and  semstructs  is a character string consisting of an array of semaphore structures. Each semaphore structure consists of the following components, each</p>
        <p id="p_13138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13138"> of which is a short integer (as created by the  s  format character in  pack):</p>
        <p id="p_13139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13139"> • The number of semaphores</p>
        <p id="p_13140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13140"> • The semaphore operation</p>
        <p id="p_13141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13141"> • The semaphore flags, if any</p>
        <p id="p_13142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13142"> This function returns a nonzero value if the semaphore operation is successful, zero if an error occurs.</p>
        <p id="p_13143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13143"> NOTE</p>
        <p id="p_13144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13144"> For more information on semaphore operations and the semaphore structure, refer to the  semop  manual page</p>
        <p id="p_13145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13145"> The semctl Function</p>
        <p id="p_13146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13146"> The  semctl  function enables you to set options for semaphores and send commands that affect them. The syntax of the  semctl  function is</p>
        <p id="p_13147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13147"> semctl (semid, semcmd, semarg);</p>
        <p id="p_13148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13148"> semid  is the semaphore ID returned by  semget. semcmd  is the command that affects the semaphore; the list of available commands is defined in the file  /usr/include/sys/ipc . h.</p>
        <p id="p_13149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13149"> Some of the commands that can be specified by  semcmd  set the values of semaphore options. If one of these commands is specified, the new value of the option is specified in  semarg.</p>
        <p id="p_13150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13150"> If an error occurs,  semctl  returns the undefined value,  semctl  also can return zero or a nonzero value.</p>
        <p id="p_13151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13151"> Summary</p>
        <p id="p_13152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13152"> Today you learned about Perl functions that emulate system library functions, perform Berkeley UNIX socket operations, and perform System V IPC operations.</p>
        <p id="p_13153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13153"> Perl functions that emulate system library functions perform the following tasks, among others:</p>
        <p id="p_13154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13154"> • Read the  /etc/group  file, which lists the user groups for your machine</p>
        <p id="p_13155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13155"> • Read the  /etc/networks  file, which lists networks to which your machine is connected</p>
        <p id="p_13156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13156"> • Read from the  /etc/hosts  file, which lists the remote machines accessible from your local network</p>
        <p id="p_13157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13157"> • Obtain the current login user ID</p>
        <p id="p_13158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13158"> • Retrieve the current process group and parent process ID</p>
        <p id="p_13159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13159"> • Read the  /etc/passwd  file, which lists information about the users who have access to your</p>
        <p id="p_13160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13160"> machine</p>
        <p id="p_13161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13161"> • Obtain the current priority for your program and set it to another value</p>
        <p id="p_13162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13162"> • Read the  /etc/protocols  file, which lists the types of protocols available for interprocess communication</p>
        <p id="p_13163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13163"> • Read the  /etc/services  file, which lists the port numbers associated with system services on your machine</p>
        <p id="p_13164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13164"> Today you also learned about the Berkeley UNIX socket mechanism, which provides interprocess</p>
        <p id="p_13165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13165"> communication using a client-server model. The System V IPC message queue, shared memory, and</p>
        <p id="p_13166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13166"> semaphore capabilities are also briefly covered.</p>
        <p id="p_13167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13167"> Q&amp;A</p>
        <p id="p_13168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13168"> Q: What is the difference between getnetent and gethostent, and which one accesses</p>
        <p id="p_13169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13169"> /etc/networks?</p>
        <p id="p_13170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13170"> A: On most systems,  getnetent  accesses the contents of  /etc/networks,  which lists the</p>
        <p id="p_13171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13171"> names and numbers of the networks for your machine,  gethostent,  on the other hand, accesses the contents of  /etc/hosts,  which lists the names and addresses of other machines on local and remote networks.</p>
        <p id="p_13172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13172"> Q: What will happen if I establish a socket connection using a port number listed in</p>
        <p id="p_13173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13173"> /etc/services?</p>
        <p id="p_13174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13174"> A: If the system service is always active, the system likely will not enable you to establish a socket</p>
        <p id="p_13175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13175"> connection using this port. If the system service runs intermittently, you run the risk of disrupting it.</p>
        <p id="p_13176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13176"> In your programs, it is always best to use a port never used by any other system service.</p>
        <p id="p_13177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13177"> Q: How did sockets get their name?</p>
        <p id="p_13178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13178"> A: A server process that is listening for clients is like an electrical socket on your wall: any client</p>
        <p id="p_13179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13179"> process with the appropriate protocol can "plug into" it.</p>
        <p id="p_13180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13180"> Q: What is the purpose of a semaphore?</p>
        <p id="p_13181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13181"> A: A semaphore is a method of ensuring that only one process can run a particular segment of code or</p>
        <p id="p_13182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13182"> access a particular chunk of shared memory storage at any given time.</p>
        <p id="p_13183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13183"> A full description of how semaphores work is beyond the scope of this book. Many books on operating systems can give you an introduction to the concepts used in semaphores. Also, the UNIX System V manual pages for the semaphore functions listed in today's lesson provide a brief description of how semaphores work.</p>
        <p id="p_13184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13184"> Q: The machine name I retrieved using gethostbyaddr has a lot of funny characters in it.</p>
        <p id="p_13185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13185"> Why?</p>
        <p id="p_13186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13186"> A: The address you've retrieved is an Internet domain address, which is a list of names separated by</p>
        <p id="p_13187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13187"> periods  (.).  These domain names ensure that each Internet user and machine can be distinguished from the millions of other users and machines around the world.</p>
        <p id="p_13188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13188"> For more details on the Internet and how to use it, refer to a book on the subject (many are available).</p>
        <p id="p_13189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13189"> Workshop</p>
        <p id="p_13190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13190"> The Workshop provides quiz questions to help you solidify your understanding of the material covered, and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_13191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13191"> Quiz</p>
        <p id="p_13192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13192"> 1. Which functions manipulate the following files?</p>
        <p id="p_13193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13193"> a. /etc/passwd</p>
        <p id="p_13194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13194"> b. /etc/hosts</p>
        <p id="p_13195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13195"> c. /etc/networks</p>
        <p id="p_13196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13196"> d. /etc/services</p>
        <p id="p_13197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13197"> 2. Which of the following functions are called by client processes and which by server processes when performing socket operations? In what order should these functions be called?</p>
        <p id="p_13198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13198"> a. Bind</p>
        <p id="p_13199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13199"> b. listen</p>
        <p id="p_13200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13200"> c. socket</p>
        <p id="p_13201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13201"> d. accept</p>
        <p id="p_13202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13202"> e. connect</p>
        <p id="p_13203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13203"> 3. What do the following functions do?</p>
        <p id="p_13204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13204"> a. getpwuid</p>
        <p id="p_13205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13205"> b. setprotoent</p>
        <p id="p_13206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13206"> c. gethostbyaddr</p>
        <p id="p_13207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13207"> d. getgrent</p>
        <p id="p_13208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13208"> e. getservbyport</p>
        <p id="p_13209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13209"> 4. How do you send information using a socket?</p>
        <p id="p_13210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13210"> 5. Describe how to list all the (numeric) user IDs on your machine.</p>
        <p id="p_13211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13211"> Exercises</p>
        <p id="p_13212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13212"> 1. Write a program that lists (by name) all the groups into which user IDs are sorted on your machine. List all the user names in each group. Sort the groups, and the user names in each group, in alphabetical order.</p>
        <p id="p_13213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13213"> 2. Write a program that lists every user name on your machine and prints the home directory for each.</p>
        <p id="p_13214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13214"> 3. Write a program that lists the shells used by users on your machine. List the number of users of each shell, and sort the list in descending order of use.</p>
        <p id="p_13215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13215"> 4. Write a program that splits into two identical processes, and have each process print the process ID of the other.</p>
        <p id="p_13216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13216"> 5. Write a program that sends a specific file,  /u/jqpublic/testfile,  to clients who request it. The program should send the file by creating a copy of itself using  fork,  and it should be able to send to five clients at once.</p>
        <p id="p_13217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13217"> 6.  BUG BUSTER:  What is wrong with the following program?</p>
        <p id="p_13218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13218"> #!/usr/local/bin/perl</p>
        <p id="p_13219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13219"> print ("Network names and numbers at your site:\n"); while ( ($name, $dl, $d2, $address) = getnetentO) { print ("$name, at address $address\n");</p>
        <p id="p_13220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13220"> }</p>
        <p id="p_13221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13221"> Chapter 16</p>
        <p id="p_13222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13222"> Command-Line Options</p>
        <p id="p_13223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13223"> CONTENTS</p>
        <p id="p_13224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13224"> •  Specifying Options</p>
        <p id="p_13225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13225"> o  Specifying Options on the Command Line</p>
        <p id="p_13226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13226"> o  Specifying an Option in the Program</p>
        <p id="p_13227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13227"> •  The  -v  Option: Printing the Perl Version Number</p>
        <p id="p_13228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13228"> •  The  -c  Option: Checking Your Syntax</p>
        <p id="p_13229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13229"> •  The  -w  Option: Printing Warnings</p>
        <p id="p_13230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13230"> o  Checking for Possible Typos o  Checking for Redefined Subroutines o  Checking for Incorrect Comparison Operators</p>
        <p id="p_13231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13231"> •  The -e Option: Executing a Single-Line Program</p>
        <p id="p_13232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13232"> •  The -s Option: Supplying Your Own Command-Line Options</p>
        <p id="p_13233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13233"> o  The - s Option and Other Command-Line Arguments</p>
        <p id="p_13234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13234"> •  The  -P  Option: Using the C Preprocessor</p>
        <p id="p_13235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13235"> o  The C Preprocessor: A Quick Overview</p>
        <p id="p_13236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13236"> •  The - I Option: Searching for C Include Files</p>
        <p id="p_13237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13237"> •  The  -n  Option: Operating on Multiple Files</p>
        <p id="p_13238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13238"> •  The  -p  Option: Operating on Files and Printing</p>
        <p id="p_13239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13239"> •  The  -i  Option: Editing Files</p>
        <p id="p_13240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13240"> o  Backing Up Input Files Using the  -i  Option</p>
        <p id="p_13241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13241"> •  The  -a  Option: Splitting Lines</p>
        <p id="p_13242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13242"> •  The  -F  Option: Specifying the Split Pattern</p>
        <p id="p_13243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13243"> •  The -0 Option: Specifying Input End-of-Line</p>
        <p id="p_13244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13244"> •  The -1 Option: Specifying Output End-of-Line</p>
        <p id="p_13245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13245"> •  The  -x Option: Extracting a Program from a Message</p>
        <p id="p_13246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13246"> •  Miscellaneous Options</p>
        <p id="p_13247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13247"> o  The  -u  Option o  The  -U  Option o  The -  S  Option o  The  -D  Option</p>
        <p id="p_13248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13248"> o  The  -T  Option: Writing Secure Programs</p>
        <p id="p_13249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13249"> •  The -d Option: Using the Perl Debugger</p>
        <p id="p_13250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13250"> •  Summary</p>
        <p id="p_13251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13251"> •  Q&amp;A</p>
        <p id="p_13252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13252"> •  Workshop</p>
        <p id="p_13253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13253"> o  Quiz o  Exercises</p>
        <p id="p_13254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13254"> Today's lesson describes the options you can specify to control how your Perl program operates. These options provide many features, including those that perform the following tasks:</p>
        <p id="p_13255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13255"> • Check syntax</p>
        <p id="p_13256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13256"> • Print warnings</p>
        <p id="p_13257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13257"> • Use preprocessor commands</p>
        <p id="p_13258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13258"> • File editing</p>
        <p id="p_13259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13259"> • Change the "end of input line" marker</p>
        <p id="p_13260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13260"> Today's lesson begins with a description of how to supply options to your Perl program.</p>
        <p id="p_13261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13261"> Specifying Options</p>
        <p id="p_13262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13262"> There are two ways to supply options to a Perl program:</p>
        <p id="p_13263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13263"> • On the command line, when you enter the command that starts your Perl program</p>
        <p id="p_13264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13264"> • On the first line of your Perl program</p>
        <p id="p_13265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13265"> The following sections describe these methods of supplying options.</p>
        <p id="p_13266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13266"> Specifying Options on the Command Line</p>
        <p id="p_13267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13267"> One way to specify options for a Perl program is to enter them on the command line when you enter the command that starts your program.</p>
        <p id="p_13268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13268"> The syntax for specifying options on the command line is</p>
        <p id="p_13269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13269"> perl options program</p>
        <p id="p_13270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13270"> Here,  program  is the name of the Perl program you want to run, and  options  is the list of options you want to supply to the program.</p>
        <p id="p_13271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13271"> For example, the following command runs the Perl program named  testl  and passes it the options  -s and  -w.  (You'll learn about these and other options later today.)</p>
        <p id="p_13272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13272"> $ perl -s -w testl</p>
        <p id="p_13273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13273"> Some options need to be specified along with a value. For example, the -0 option requires an integer to be passed with it:</p>
        <p id="p_13274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13274"> $ perl -0 26 testl</p>
        <p id="p_13275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13275"> Here, the integer 2 6 is associated with the option - 0.</p>
        <p id="p_13276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13276"> If you want, you can omit the space between the option and its associated value, as in the following:</p>
        <p id="p_13277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13277"> $ perl -026 testl</p>
        <p id="p_13278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13278"> As before, this command associates  2 6  with the  -0  option. In either case, the value associated with an option must always immediately follow the option.</p>
        <p id="p_13279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13279"> NOTE</p>
        <p id="p_13280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13280"> If an option does not require an associated value, you can put another option immediately after it without specifying an additional -character or space. For example, the following commands are equivalent:</p>
        <p id="p_13281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13281"> $ perl -s -w testl $ perl -sw testl</p>
        <p id="p_13282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13282"> You can put an option that requires a value as part of a group of options, provided that it is last in the group. For example, the following commands are equivalent:</p>
        <p id="p_13283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13283"> $ perl -s -w -0 26 testl $ perl -sw026 test</p>
        <p id="p_13284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13284"> Specifying an Option in the Program</p>
        <p id="p_13285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13285"> Another way to specify a command option is to include it as part of the header comment for the program. For example, suppose that the first line of your Perl program is this:</p>
        <p id="p_13286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13286"> #!/usr/local/bin/perl -w</p>
        <p id="p_13287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13287"> In this case, the  -w  option is automatically specified when you start the program.</p>
        <p id="p_13288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13288"> Warning</p>
        <p id="p_13289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13289"> Perl 4 enables you to specify only one option (or group of options) on the header comment line. This means that the following line generates an "unrecognized switch" error message:</p>
        <p id="p_13290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13290"> #!/usr/local/bin/perl -w -s</p>
        <p id="p_13291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13291"> Perl 5 enables as many switches as you like on the command line. However, some operating systems chop the header line after 32 characters, so be careful if you are planning to use a large number of switches</p>
        <p id="p_13292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13292"> [NOTE</p>
        <p id="p_13293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13293"> Options specified on the command line override options specified in the header comment. For example, if your header comment is</p>
        <p id="p_13294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13294"> #!/usr/local/bin/perl -w</p>
        <p id="p_13295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13295"> and you start your program with the command</p>
        <p id="p_13296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13296"> $ perl -s testl</p>
        <p id="p_13297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13297"> the program will run with the  -s  option specified but not the  -w option</p>
        <p id="p_13298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13298"> The -v Option: Printing the Perl Version Number</p>
        <p id="p_13299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13299"> The  -v  option enables you to find out what version of Perl is running on your machine. When the Perl inteipreter sees this option, it prints information on itself and then exits without running your program.</p>
        <p id="p_13300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13300"> This means that if you supply a command such as the following, the file  testl  is not executed:</p>
        <p id="p_13301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13301"> $ perl -v testl</p>
        <p id="p_13302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13302"> Here is sample output from the  -v  command:</p>
        <p id="p_13303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13303"> This is perl, version  5.001</p>
        <p id="p_13304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13304"> Unofficial patch level lm</p>
        <p id="p_13305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13305"> Copyright (c) 1987-1994, Larry Wall</p>
        <p id="p_13306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13306"> Perl may be copied only under the terms of either the Artistic License</p>
        <p id="p_13307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13307"> or the GNU General Public License, which may be found in the Perl 5.0</p>
        <p id="p_13308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13308"> source kit.</p>
        <p id="p_13309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13309"> The only really useful things here, besides the copyright notice, are the version number of the Perl you are running-in this case, 4.0-and the patch level, which indicates how many repairs, or patches, have been made to this version. Here, the patch level is  3 6  (which, at this writing, is the latest release of Perl version 4.0).</p>
        <p id="p_13310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13310"> No other options should be specified if you specify the  -v  option, because none of them would do anything in this case anyway.</p>
        <p id="p_13311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13311"> The -c Option: Checking Your Syntax</p>
        <p id="p_13312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13312"> The  -c  option tells the Perl interpreter to check whether your Perl program is correct without actually running it. If it is correct, the Perl interpreter prints the following message (in which  filename  is the name of your program) and then exits without executing your program:</p>
        <p id="p_13313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13313"> filename syntax OK</p>
        <p id="p_13314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13314"> If the Perl interpreter detects errors, it displays them just as it normally does. After printing the error messages, it prints the following message, in which  filename  is the name of your program:</p>
        <p id="p_13315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13315"> filename had compilation errors</p>
        <p id="p_13316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13316"> Again, there is no point in supplying other options if you specify the  -c  option because the Perl interpreter isn't actually running the program; the only exception is the  -w  option, which prints warnings. This option is described in the following section.</p>
        <p id="p_13317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13317"> The -w Option: Printing Warnings</p>
        <p id="p_13318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13318"> As you have seen on the preceding days, some mistakes are easy to make when you are writing a Perl program, such as accidentally typing the wrong variable name, or using == when you really mean to use eq. Because certain mistakes crop up frequently, the Perl interpreter provides an option that checks for them.</p>
        <p id="p_13319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13319"> This option, the  -w  option, prints a warning every time the Perl interpreter sees something that might cause a problem. For example, if the interpreter sees the statement</p>
        <p id="p_13320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13320"> $y = $x;</p>
        <p id="p_13321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13321"> and hasn't seen  $x  before (which means that  $x  is undefined), it prints a warning message in the following form if you are running Perl 4:</p>
        <p id="p_13322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13322"> Possible typo: "x" at filename line linenum.</p>
        <p id="p_13323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13323"> Here,  filename  is the name of your Perl program, and  linenum  is the number of the line on which the interpreter has detected a potential problem.</p>
        <p id="p_13324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13324"> If you are running Perl 5, the message is similar, but also includes the name of the current package:</p>
        <p id="p_13325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13325"> Identifier "main::x" used only once: possible typo at filename line linenum.</p>
        <p id="p_13326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13326"> For more information on packages, see  Day 19 ,  "Object-Oriented Programming in Perl."</p>
        <p id="p_13327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13327"> The following sections provide a partial list of the potential problems detected by the  -w  option. (If you are running Perl 5, the  -w  option provides dozens of useful warnings. Consult the Perl manual pages for a complete list.)</p>
        <p id="p_13328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13328"> NOTE</p>
        <p id="p_13329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13329"> The  -w  option can be combined with the  -c  option to provide a means of checking your syntax for errors and problems before you actually ran the program</p>
        <p id="p_13330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13330"> Checking for Possible Typos</p>
        <p id="p_13331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13331"> As you have seen, a statement such as the following one leads to a warning message if $  x  has not been</p>
        <p id="p_13332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13332"> previously defined:</p>
        <p id="p_13333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13333"> $y = $x;</p>
        <p id="p_13334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13334"> The "possible typo" error message also appears in the following circumstances, among others:</p>
        <p id="p_13335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13335"> • If a variable is assigned to but is never used again</p>
        <p id="p_13336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13336"> • If a file variable is referred to without being specified in an open statement</p>
        <p id="p_13337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13337"> Of course, the possible-typo message might flag lines that don't actually contain typos. Following are two of the most common situations in which a possible typo actually is correct code:</p>
        <p id="p_13338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13338"> • The Perl 4 interpreter sometimes confuses a print format specifier with a file variable and claims that the name of the print format specifier is a possible typo. For example, the statement</p>
        <p id="p_13339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13339"> format BLANK =</p>
        <p id="p_13340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13340"> • (which enables you to print a blank line on a formatted page) might generate the warning message</p>
        <p id="p_13341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13341"> Possible typo: "BLANK" at filel line 26.</p>
        <p id="p_13342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13342"> • This warning message might appear even if the print format is actually used in the program, because it is specified by a statement such as</p>
        <p id="p_13343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13343"> $~ = "BLANK";</p>
        <p id="p_13344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13344"> • and the Perl interpreter doesn't realize that the string BLANK refers to the BLANK print format.</p>
        <p id="p_13345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13345"> • The Perl 5 interpreter does not generate this warning message.</p>
        <p id="p_13346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13346"> • If you call a function that returns a list, and you need only an element of the list, one way to extract that single element is to assign the other elements to dummy variables. For example, if you want to retrieve just the group ID when you call  getgrnam,  you can do so as shown here:</p>
        <p id="p_13347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13347"> ($dl, $d2, $groupid) = getgrnam (Sgroupname);</p>
        <p id="p_13348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13348"> • Here, the scalar variables  $dl  and  $d2  are dummy variables that hold the elements of the group file entry that you do not need. If (as is likely)  $dl  and  $d2  are not referred to again, the  -w  option treats  $dl  and  $d2  as possible typos.</p>
        <p id="p_13349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13349"> Checking for Redefined Subroutines</p>
        <p id="p_13350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13350"> One useful feature of the  -w  option is that it checks whether two subroutines of the same name have been defined in the program. (Normally, if the Perl interpreter sees two subroutines of the same name, it quietly replaces the first subroutine with the second one and carries on.)</p>
        <p id="p_13351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13351"> If, for example, two subroutines named  x  are defined in a program, the  -w  option prints a message similar to the following one:</p>
        <p id="p_13352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13352"> Subroutine x redefined at filel line 46.</p>
        <p id="p_13353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13353"> The line number specified is the line that starts the second subroutine.</p>
        <p id="p_13354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13354"> When the  -w  option has detected this problem, you can decide which subroutine to rename or throw away.</p>
        <p id="p_13355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13355"> Checking for Incorrect Comparison Operators</p>
        <p id="p_13356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13356"> Another really helpful feature of the  -w  option is that it checks whether you are trying to compare a string using the == operator.</p>
        <p id="p_13357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13357"> In a statement such as the following:</p>
        <p id="p_13358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13358"> if ($x == "humbug") {</p>
        <p id="p_13359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13359"> }</p>
        <p id="p_13360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13360"> the conditional expression</p>
        <p id="p_13361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13361"> $x == "humbug"</p>
        <p id="p_13362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13362"> is equivalent to the expression</p>
        <p id="p_13363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13363"> $x == 0</p>
        <p id="p_13364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13364"> because all character strings are converted to 0 when used in a numeric context (a place where a number is expected). This is correct in Perl, but it is not likely to be what you want.</p>
        <p id="p_13365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13365"> If the  -w  option is specified and the Perl interpreter sees a statement such as this one, it prints a message similar to the following if you are running Perl 4:</p>
        <p id="p_13366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13366"> Possible use of == on string value at filel line 26.</p>
        <p id="p_13367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13367"> In Perl 5, the following warning is printed:</p>
        <p id="p_13368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13368"> Argument "humbug"  isn't  numeric for numeric eq at filel line 26.</p>
        <p id="p_13369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13369"> In either case, this warning enables you detect these incorrect == operators and replace them with  eq operators, which compare strings.</p>
        <p id="p_13370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13370"> Warning</p>
        <p id="p_13371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13371"> The  -w  operator doesn't detect the opposite problem, namely:</p>
        <p id="p_13372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13372"> if ($x eq  46) {</p>
        <p id="p_13373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13373"> }</p>
        <p id="p_13374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13374"> In this case, the Perl interpreter converts 4 6 to the string 4 6 and performs a string comparison.</p>
        <p id="p_13375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13375"> Because a number and its string equivalent usually mean the same thing, this normally doesn't cause a problem. Watch out, though, for octal numbers in string comparisons, as in the following example:</p>
        <p id="p_13376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13376"> if ($x eq  046)  {</p>
        <p id="p_13377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13377"> }</p>
        <p id="p_13378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13378"> Here, the octal value 0 4 6 is converted to the number 38 before being converted to a string. If you really want to compare  $x  to 0 4  6,  this code will not produce the results you expect.</p>
        <p id="p_13379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13379"> Another thing to watch out for is this: In Perl 4, the  -w  option does not check for conditional expressions such as the following:</p>
        <p id="p_13380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13380"> if ($x = 0) {</p>
        <p id="p_13381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13381"> }</p>
        <p id="p_13382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13382"> because there are many cases in Perl in which the = assignment operator belongs inside a conditional expression. You will have to manually check that you are not specifying = (assignment) when you really mean to use == (equality comparison).</p>
        <p id="p_13383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13383"> Perl 5 flags this with the following message:</p>
        <p id="p_13384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13384"> Found = in conditional, should be == at filename line filenum</p>
        <p id="p_13385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13385"> The -e Option: Executing a Single-Line Program</p>
        <p id="p_13386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13386"> The  -e  option enables you to execute a Perl program from your shell command line. For example, the command</p>
        <p id="p_13387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13387"> $ perl -e "print  ('Hello');"</p>
        <p id="p_13388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13388"> prints the following string on your screen:</p>
        <p id="p_13389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13389"> Hello</p>
        <p id="p_13390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13390"> You can also specify multiple  -e  options. In this case, the Perl statements are executed left to right. For example, the command</p>
        <p id="p_13391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13391"> $ perl -e "print  ('Hello');"  -e "print ('  there');"</p>
        <p id="p_13392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13392"> prints the following string on your screen:</p>
        <p id="p_13393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13393"> Hello there</p>
        <p id="p_13394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13394"> By itself, the  -e  option is not all that useful. It becomes useful, however, when you use it in conjunction with some of the other options you'll see in today's lesson.</p>
        <p id="p_13395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13395"> Warning</p>
        <p id="p_13396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13396"> You can leave off the closing semicolon in a Perl statement passed via the  -e  option, if you want to:</p>
        <p id="p_13397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13397"> $ perl -e "print ('Hello')"</p>
        <p id="p_13398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13398"> If you are supplying two or more  -e  options, however, the Perl interpreter strings them together and treats them as though they are a single Perl program. This means that the following command generates an error because there must be a semicolon after the statement specified with the first  -e  option:</p>
        <p id="p_13399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13399"> $ perl -e "print ('Hello')" -e "print (' there')</p>
        <p id="p_13400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13400"> The -s Option: Supplying Your Own Command-Line Options</p>
        <p id="p_13401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13401"> As you can see from this chapter, you can control the behavior of Perl by specifying various command-line options. You can control the behavior of your own Perl programs by spec-ifying command-line options for them too. To do this, specify the  -s  option when you call the program.</p>
        <p id="p_13402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13402"> Here's an example of a command that passes an option to a Perl program:</p>
        <p id="p_13403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13403"> $ perl -s testfile -q</p>
        <p id="p_13404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13404"> This command starts the Perl program  testfile  and passes it the  -q  option.</p>
        <p id="p_13405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13405"> Warning</p>
        <p id="p_13406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13406"> To be able to pass options to your program, you must specify the Perl-s  option. The following command does not pass  -q  as an option:</p>
        <p id="p_13407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13407"> $ perl testfile -q</p>
        <p id="p_13408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13408"> In this case,  -q  is just an ordinary argument that is passed to your program and stored in the built-in array variable  0ARGV.</p>
        <p id="p_13409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13409"> The easiest way to remember to include -  s  is to specify it as part of your header comment:</p>
        <p id="p_13410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13410"> #!/usr/local/bin/perl -s</p>
        <p id="p_13411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13411"> This ensures that your program always will check for options.</p>
        <p id="p_13412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13412"> (Unless, of course, you override the option check by providing other Perl options on the command line when you invoke the program.</p>
        <p id="p_13413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13413"> If an option is specified when you invoke your Perl program, the scalar variable whose name is the same as the option is automatically set to 1 before program execution begins. For example, if a Perl program named testfileis  called with the  -q  option, as in the following, the scalar variable  $q  is automatically set to 1:</p>
        <p id="p_13414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13414"> $ perl -s testfile -q</p>
        <p id="p_13415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13415"> You then can use this variable in a conditional expression to test whether the option has been set.</p>
        <p id="p_13416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13416"> NOTE</p>
        <p id="p_13417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13417"> If  -q  is treated as an option, it does not appear in the system variable 0ARGV.  A command-line argument either sets an option or is added</p>
        <p id="p_13418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13418"> to  0ARGV</p>
        <p id="p_13419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13419"> Options can be longer than a single character. For example, the following command sets the value of the scalar variable  $potatotol:</p>
        <p id="p_13420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13420"> $ perl -s testfile -potato</p>
        <p id="p_13421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13421"> You also can set an option to a value other than 1 by specifying = and the desired value on the command line:</p>
        <p id="p_13422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13422"> $ perl -s testfile -potato="hot"</p>
        <p id="p_13423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13423"> This line sets the value of  $potato  to  hot.</p>
        <p id="p_13424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13424"> Listing 16.1 is a simple example of a program that uses command-line options to control its behavior. This program prints information about the user currently logged in.</p>
        <p id="p_13425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13425"> Listing 16.1. An example of a program that uses command-line options.</p>
        <p id="p_13426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13426"> 1: #!/usr/local/bin/perl -s</p>
        <p id="p_13427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13427"> 2  :</p>
        <p id="p_13428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13428"> 3: # This program prints information as specified by</p>
        <p id="p_13429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13429"> 4: # the following options:</p>
        <p id="p_13430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13430"> 5: # -u: print numeric user ID</p>
        <p id="p_13431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13431"> 6: # -U: print user ID (name)</p>
        <p id="p_13432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13432"> 7: # -g: print group ID</p>
        <p id="p_13433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13433"> 8: # -G: print group name</p>
        <p id="p_13434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13434"> 9: # -d: print home directory</p>
        <p id="p_13435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13435"> 10: # -s: print login shell</p>
        <p id="p_13436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13436"> 11: # -all: print everything (overrides other options) 12  :</p>
        <p id="p_13437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13437"> 13: $u = $U = $g = $G = $d = $s = 1 if ($all);</p>
        <p id="p_13438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13438"> 14: $whoami = "whoami";</p>
        <p id="p_13439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13439"> 15: chop ($whoami);</p>
        <p id="p_13440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13440"> 16: ($name, $dl, $userid, $groupid, $d2, $d3, $d4,</p>
        <p id="p_13441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13441"> 17: $homedir, $shell) = getpwnam ($whoami);</p>
        <p id="p_13442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13442"> 18: print ("user id: $userid\n") if ($u);</p>
        <p id="p_13443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13443"> 19: print ("user name: $name\n") if ($U);</p>
        <p id="p_13444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13444"> 20: print ("group id: $groupid\n") if  ($g);</p>
        <p id="p_13445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13445"> 21:  if  ($G) {</p>
        <p id="p_13446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13446"> 22: ($groupname) = getgrgid ($groupid);</p>
        <p id="p_13447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13447"> 23: print ("group name: $groupname\n");</p>
        <p id="p_13448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13448"> 24: }</p>
        <p id="p_13449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13449"> 25: print ("home directory: $homedir\n") if  ($d);</p>
        <p id="p_13450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13450"> 26:  print ("login shell: $shell\n") if  ($s);</p>
        <p id="p_13451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13451"> Output</p>
        <p id="p_13452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13452"> $ programl6_l -U -d user name: dave home directory: /agl/dave $</p>
        <p id="p_13453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13453"> The header comment in line 1 specifies that the -  s  option is to be automatically specified when this Perl program is invoked. This ensures that options can always be passed to this program (unless, of course, you override the  -s  option on the command line, as described earlier).</p>
        <p id="p_13454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13454"> The comments in lines 3-11 provide information on what options the program supports. This information is useful when someone is reading or modifying the program because there is no other way to tell which scalar variables are used to test options.</p>
        <p id="p_13455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13455"> The option  -all  indicates that the program is to print everything; if this option is specified, the scalar variable $  all  is set to 1. To cut down on the number of comparisons later, line 13 checks whether  $all is 1; if it is, the other scalar variables corresponding to command-line options are set to 1. This technique ensures that the following commands are equivalent (assuming that your program is named</p>
        <p id="p_13456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13456"> programl 6_1):</p>
        <p id="p_13457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13457"> Analysis</p>
        <p id="p_13458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13458"> $ programl6_l -all $ programl6_l -u -U -g -G -d -s</p>
        <p id="p_13459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13459"> The scalar variables listed in line 13 can be assigned to, even though they correspond to possible command¬ line options, because they behave just like other Perl scalar variables.</p>
        <p id="p_13460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13460"> Lines 14-17 provide the raw material for the various print operations in this program. To start, when the Perl interpreter sees the string '  whoami  ', it calls the system command  whoami,  which returns the name of the user running the program. This name is then passed to  getpwnam,  which searches the password file /etc/passwd  and retrieves the entry for this particular user.</p>
        <p id="p_13461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13461"> Line 18 checks whether the -u option has been specified. To do this, it checks whether $u has a nonzero value. If it does, the user ID is printed. (The user ID is also printed if  -all  has been specified because line 13 sets $u to a nonzero value in this case.)</p>
        <p id="p_13462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13462"> Similarly, line 19 prints the user name if  -U  has been specified, line 20 prints the group ID if  -g  has been specified, line 25 prints the home directory if -d has been specified, and line 26 prints the filename of the login shell if -  s  has been specified.</p>
        <p id="p_13463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13463"> Lines 21-24 check whether to print the group name. If  -g  has been specified,  $g  is nonzero, and line 22 calls  get grid  to retrieve the group name.</p>
        <p id="p_13464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13464"> NOTE</p>
        <p id="p_13465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13465"> Because command-line options can change the initial values of scalar variables, it is a good idea to always assign a value to a scalar variable before you use it. Consider the following example:</p>
        <p id="p_13466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13466"> #!/usr/local/bin/perl while ($count &lt; 10) {</p>
        <p id="p_13467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13467"> print ( "$count\n");</p>
        <p id="p_13468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13468"> $count++;</p>
        <p id="p_13469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13469"> }</p>
        <p id="p_13470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13470"> This program normally prints the numbers from 0 to 9 because $ count  is assumed to have an initial value of 0. However, if this program is called with the -  count  option, the initial value of $ count  becomes something other than 0, and the program behaves differently.</p>
        <p id="p_13471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13471"> If you add the following statement before the  while  loop, the program always prints the numbers 0 to 9 regardless of what options are specified on the command line:</p>
        <p id="p_13472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13472"> Scount = 0</p>
        <p id="p_13473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13473"> The -s Option and Other Command-Line Arguments</p>
        <p id="p_13474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13474"> You can supply both options and command-line arguments to your program (provided that you supply the s  option to Perl). These are the rules that the Perl interpreter follows:</p>
        <p id="p_13475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13475"> • Any arguments immediately following the program name that start with a - are assumed to be options.</p>
        <p id="p_13476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13476"> • Any argument that does not start with a - is assumed to be an ordinary argument and not an option.</p>
        <p id="p_13477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13477"> • When the Perl interpreter sees an argument that is not an option, all subsequent arguments are also treated as ordinary arguments, not options, even if they start with a -.</p>
        <p id="p_13478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13478"> This means, for example, that the following command treats  -w  as an option  totestfile,  and  f oo  and e  as ordinary arguments:</p>
        <p id="p_13479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13479"> $ perl -s testfile -w foo -e</p>
        <p id="p_13480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13480"> The special argument — also indicates "end of options." For example, the following command treats  -w  as an option and  -e  as an ordinary argument. The — is thrown away.</p>
        <p id="p_13481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13481"> $ perl -s testfile -w - -e</p>
        <p id="p_13482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13482"> The -P Option: Using the C Preprocessor</p>
        <p id="p_13483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13483"> The C preprocessor is a program that takes code written in the C programming language and searches for special preprocessor statements. In Perl, the  -P  option enables you to use this preprocessor with your Perl program:</p>
        <p id="p_13484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13484"> $ perl -P myprog</p>
        <p id="p_13485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13485"> Here, the Perl program  myprog  is first run through the C preprocessor. The resulting output is then passed to the Perl inteipreter for execution.</p>
        <p id="p_13486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13486"> NOTE</p>
        <p id="p_13487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13487"> Perl provides no way to just run the C preprocessor on a Perl program. To do this, you'll need a C compiler that provides an option which specifies "preprocessor only."</p>
        <p id="p_13488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13488"> Refer to the documentation for your C compiler for details about how to do this</p>
        <p id="p_13489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13489"> The following sections describe some of the most commonly used C preprocessor commands.</p>
        <p id="p_13490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13490"> The C Preprocessor: A Quick Overview</p>
        <p id="p_13491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13491"> C preprocessor statements always employ the following syntax:</p>
        <p id="p_13492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13492"> tcommand value</p>
        <p id="p_13493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13493"> Each C preprocessor statement starts with a # character,  command  is the preprocessor operation to perform, and  value  is the (optional) value associated with this operation.</p>
        <p id="p_13494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13494"> Macro Substitution: The #def ine Operator</p>
        <p id="p_13495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13495"> The most common preprocessor statement is  #def ine.  This statement tells the preprocessor to replace every occurrence of a particular character string with a specified value.</p>
        <p id="p_13496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13496"> The syntax for  #def ine  is</p>
        <p id="p_13497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13497"> #define macro value</p>
        <p id="p_13498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13498"> This statement replaces all occurrences of the character string  macro  with the value specified by  value. This operation is known as  macro substitution,   macro  can contain letters, digits, or underscores.</p>
        <p id="p_13499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13499"> The value specified in a #  define  statement can be any character string or number. For example, the following statement replaces all occurrences of  USERNAME  with the string  "dave"  (including the quotation marks):</p>
        <p id="p_13500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13500"> #define USERNAME "dave"</p>
        <p id="p_13501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13501"> This statement replaces  EXPRESSION  with the string ( 14  +  6 ), including the parentheses:</p>
        <p id="p_13502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13502"> #define EXPRESSION (14+6)</p>
        <p id="p_13503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13503"> NOTE</p>
        <p id="p_13504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13504"> When you are using  #def ine  with a value that is an expression, it is usually a good idea to enclose the value in parentheses. For example, consider the following Perl statement:</p>
        <p id="p_13505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13505"> $result = EXPRESSION * 5;</p>
        <p id="p_13506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13506"> If your preprocessor command is #define EXPRESSION 14+6 the resulting Perl statement becomes</p>
        <p id="p_13507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13507"> $result = 14 + 6 * 5;</p>
        <p id="p_13508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13508"> which assigns  44to$result  (because the multiplication is performed first). If you enclose the preprocessor expression in parentheses, as in</p>
        <p id="p_13509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13509"> #define EXPRESSION (14+6)</p>
        <p id="p_13510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13510"> the statement becomes</p>
        <p id="p_13511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13511"> $result = (14 +6) * 5;</p>
        <p id="p_13512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13512"> which yields the result 100, which is likely what you want.</p>
        <p id="p_13513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13513"> Also, you always should enclose any parameters (described in the following section) in parentheses, for the same reason</p>
        <p id="p_13514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13514"> Passing Arguments Using #def ine</p>
        <p id="p_13515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13515"> You can specify one or more parameters with your #  define  statement. This capability enables you to treat the preprocessor command like a simple function that accepts arguments. For example, the following preprocessor statement takes a specified value and uses it as an exponent:</p>
        <p id="p_13516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13516"> #define POWEROFTWO(val) (2 ** (val))</p>
        <p id="p_13517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13517"> In the Perl statement</p>
        <p id="p_13518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13518"> $result = POWEROFTWO(1.3  +2.6)   +4;</p>
        <p id="p_13519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13519"> the preprocessor substitutes the expression  1.3  +  2.6  for  val  and produces this:</p>
        <p id="p_13520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13520"> $result = (2 ** (1.3 +  2.6))  + 4;</p>
        <p id="p_13521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13521"> You can supply more than one parameter with a #  define  statement. For example, consider the following statement:</p>
        <p id="p_13522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13522"> #define EXPONENT (base, exp) ((base)  **  (exp))</p>
        <p id="p_13523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13523"> Now, the statement</p>
        <p id="p_13524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13524"> $result = EXPONENT(4, 11);</p>
        <p id="p_13525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13525"> yields the following result after preprocessing:</p>
        <p id="p_13526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13526"> $result =  ((4)   **  (11));</p>
        <p id="p_13527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13527"> The Perl interpreter ignores the extra parentheses.</p>
        <p id="p_13528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13528"> TIP</p>
        <p id="p_13529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13529"> By convention, macros defined using  #def ine  normally use all uppercase letters (plus occasional digits and underscores). This makes it easier to distinguish macros from other variable names or character strings</p>
        <p id="p_13530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13530"> Listing 16.2 is an example of a Perl program that uses a  #def ine  statement to perform macro substitution. This listing is just Listing 15.4 with the preprocessor statement added.</p>
        <p id="p_13531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13531"> Listing 16.2. A program that uses a #def ine statement.</p>
        <p id="p_13532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13532"> 13: print ("Principal name: $name\n");</p>
        <p id="p_13533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13533"> Output</p>
        <p id="p_13534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13534"> $ programl6_2</p>
        <p id="p_13535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13535"> Enter an Internet address:</p>
        <p id="p_13536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13536"> 128.174.5.59</p>
        <p id="p_13537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13537"> Principal name: uxl.cso.uiuc.edu</p>
        <p id="p_13538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13538"> $</p>
        <p id="p_13539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13539"> Analysis</p>
        <p id="p_13540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13540"> Line 3 defines the macro  AF_INET  and assigns it the value  2.  When the C preprocessor sees AF_INET  in line 10, it replaces it with  2,  which is the value of  AF_INET  on the current machine (as specified in the header file  /usr/include/netdb .hor/usr/include/bsd/netdb . h).</p>
        <p id="p_13541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13541"> If this program is moved to a machine that defines a different value for  AF_INET,  all you need to do to get this program to work is change line 3 to use the value on that machine.</p>
        <p id="p_13542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13542"> Using Macros in #def ine Statements</p>
        <p id="p_13543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13543"> You can use a previously defined macro as the value in another #  define  statement. The following is an example:</p>
        <p id="p_13544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13544"> #define FIRST</p>
        <p id="p_13545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13545"> 1</p>
        <p id="p_13546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13546"> #define SECOND</p>
        <p id="p_13547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13547"> FIRST</p>
        <p id="p_13548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13548"> $result = 43 + SECOND;</p>
        <p id="p_13549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13549"> Here, the macro  FIRST  is defined to be equivalent to the value 1, and  SECOND  is defined to be equivalent to  FIRST.  This means that the statement following the macro definitions is equivalent to the following statement:</p>
        <p id="p_13550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13550"> $result  =43+1;</p>
        <p id="p_13551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13551"> Conditional Execution Using #ifdef and #endif</p>
        <p id="p_13552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13552"> The  #ifdef  and  #endif  statements control whether a given group of statements is to be included as part of your program.</p>
        <p id="p_13553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13553"> The syntax for the  #if def  and  #endif  statements is</p>
        <p id="p_13554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13554"> #ifdef macro</p>
        <p id="p_13555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13555"> code</p>
        <p id="p_13556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13556"> #endif</p>
        <p id="p_13557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13557"> Here,  macro  is any character string that can appear in a #  def ine  statement,  code  is one or more lines of your Perl program.</p>
        <p id="p_13558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13558"> When the C preprocessor sees an  #if def  statement, it checks whether the macro has been defined using the #  def ine  statement. If it has, the code specified by  code  is included as part of the program. If it has not, the code specified by  code  is skipped.</p>
        <p id="p_13559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13559"> NOTE</p>
        <p id="p_13560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13560"> The code enclosed by  #if def  and  #endif  does not have to be a complete Perl statement. For example, the following code is legal:</p>
        <p id="p_13561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13561"> $result = 14 * 2 #ifdef PLUSONE +  1</p>
        <p id="p_13562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13562"> #endif</p>
        <p id="p_13563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13563"> Here,  $result  is assigned  17  if  PLUSONE  is defined,  16  if it's not. Be careful, though: If you abuse  #if def,  the resulting program</p>
        <p id="p_13564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13564"> might become difficult to read</p>
        <p id="p_13565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13565"> The  #ifndef  and  #else  Statements</p>
        <p id="p_13566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13566"> The  #ifndef  and  #else  statements provide additional control over when parts of your program are to be executed.</p>
        <p id="p_13567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13567"> The  #ifndef  statement enables you to define code that is to be executed when a particular macro is not defined.</p>
        <p id="p_13568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13568"> The syntax for  #ifndef  is the same as for  #ifdef:</p>
        <p id="p_13569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13569"> #ifndef macro</p>
        <p id="p_13570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13570"> code</p>
        <p id="p_13571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13571"> #endif</p>
        <p id="p_13572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13572"> For example:</p>
        <p id="p_13573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13573"> #ifndef  MYMACRO $result = 26;</p>
        <p id="p_13574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13574"> #endif</p>
        <p id="p_13575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13575"> The assignment is performed only if  MYMACRO   has not appeared in a #  define  statement.</p>
        <p id="p_13576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13576"> The  #else  statement enables you to specify code to be executed if a macro is defined and an alternative to choose if the macro is not defined. For example:</p>
        <p id="p_13577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13577"> #ifdef  MYMACRO $result = 47;</p>
        <p id="p_13578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13578"> #else</p>
        <p id="p_13579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13579"> print ("Hello, world!\n");</p>
        <p id="p_13580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13580"> #endif</p>
        <p id="p_13581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13581"> Here, if  MYMACRO   has been defined by a #  define  statement, the following statement is exe-cuted:</p>
        <p id="p_13582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13582"> $result</p>
        <p id="p_13583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13583"> 47;</p>
        <p id="p_13584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13584"> If  MYMACRO  has not been defined, the following statement is executed:</p>
        <p id="p_13585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13585"> print ("Hello, world!\n");</p>
        <p id="p_13586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13586"> You can use #  e 1 s e  with #  i f n de f , as in the following:</p>
        <p id="p_13587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13587"> #ifndef MYMACRO</p>
        <p id="p_13588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13588"> print ("Hello, world!\n");</p>
        <p id="p_13589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13589"> #else</p>
        <p id="p_13590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13590"> $result = 47;</p>
        <p id="p_13591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13591"> #endif</p>
        <p id="p_13592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13592"> This code is identical to the  #if def-#else-#endif  sequence shown earlier in this section.</p>
        <p id="p_13593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13593"> The #if Statement</p>
        <p id="p_13594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13594"> The #  i f  statement enables you to specify that certain lines of your program are to be included only if the expression included with the statement is nonzero.</p>
        <p id="p_13595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13595"> The syntax for the #  i f  statement is</p>
        <p id="p_13596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13596"> #if expr</p>
        <p id="p_13597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13597"> code</p>
        <p id="p_13598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13598"> #endif</p>
        <p id="p_13599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13599"> Here,  expr  is the expression to be evaluated, and  code  is the code to be executed if  expr  is nonzero.</p>
        <p id="p_13600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13600"> For example, the following statement is executed only if the expression  14  +  3  is nonzero (which it always is, of course):</p>
        <p id="p_13601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13601"> #if  14+3</p>
        <p id="p_13602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13602"> $result = 26;</p>
        <p id="p_13603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13603"> #endif</p>
        <p id="p_13604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13604"> You can use a macro definition as part of an  #if  statement. If the macro is defined, it has a nonzero value in an  #if  expression; if it is not defined, it has the value zero. Consider the following example:</p>
        <p id="p_13605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13605"> #if MACRO1 || MACRO2</p>
        <p id="p_13606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13606"> $result = 47;</p>
        <p id="p_13607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13607"> #endif</p>
        <p id="p_13608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13608"> When the preprocessor sees the  #if  statement, it evaluates the expression  MACROl | | MACRO2.  This expression has a nonzero value if either  MACROl  or  MACRO2  is nonzero. Therefore, the following statement is executed if either  MACROl  or  MACRO2  is defined:</p>
        <p id="p_13609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13609"> $result = 47;</p>
        <p id="p_13610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13610"> The  #i f  statement provides a quick way to remove lines of code from your program temporarily:</p>
        <p id="p_13611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13611"> #if 0</p>
        <p id="p_13612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13612"> $result = 46;</p>
        <p id="p_13613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13613"> print ("This line is not printed right now.\n");</p>
        <p id="p_13614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13614"> #endif</p>
        <p id="p_13615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13615"> Here, the expression included with the #if statement is always zero, which means that the statements between  #if  and  #endif  are always skipped.</p>
        <p id="p_13616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13616"> You can use  #else  with # if,  as in the following example:</p>
        <p id="p_13617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13617"> #if MACROl || MACRO2</p>
        <p id="p_13618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13618"> print ("MACROl or MACR02 is defined.\n");</p>
        <p id="p_13619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13619"> #else</p>
        <p id="p_13620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13620"> print ("MACROl and MACR02 are not defined.\n");</p>
        <p id="p_13621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13621"> #endif</p>
        <p id="p_13622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13622"> This code includes the first print statement if  MACROl  or  MACRO2  has been defined using  #def ine,  and it includes the second print statement if neither has been defined.</p>
        <p id="p_13623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13623"> Warning</p>
        <p id="p_13624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13624"> You cannot use the * * (exponentiation) operator in an #if statement because * * is not supported in the C programming language</p>
        <p id="p_13625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13625"> Nesting Conditional Execution Statements</p>
        <p id="p_13626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13626"> You can put one  #if def-#else-#endif  construct inside another. For example:</p>
        <p id="p_13627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13627"> #ifdef MACROl #ifdef MACRO2</p>
        <p id="p_13628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13628"> print ("MACROl yes, MACR02 yes\n"); #else</p>
        <p id="p_13629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13629"> print ("MACROl yes, MACR02 no\n");</p>
        <p id="p_13630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13630"> #endif</p>
        <p id="p_13631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13631"> #else</p>
        <p id="p_13632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13632"> #ifdef MACRO2</p>
        <p id="p_13633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13633"> print ("MACROl no, MACR02  yes\n"); #else</p>
        <p id="p_13634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13634"> print ("MACROl no, MACR02  no\n");</p>
        <p id="p_13635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13635"> #endif</p>
        <p id="p_13636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13636"> #endif</p>
        <p id="p_13637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13637"> You also can put an  #if-#else-#endif  construct or an  #if ndef-#else-#endif  construct inside an #if def-#else-#endif  construct, or vice versa. The only restriction is that the inner construct must be completely contained in one part of the outer construct.</p>
        <p id="p_13638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13638"> Including Other Files Using #include</p>
        <p id="p_13639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13639"> Another preprocessor command that is quite useful is the  #include  command. This command tells the C preprocessor to include the contents of the specified file as part of the program.</p>
        <p id="p_13640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13640"> The syntax for the # include  command is</p>
        <p id="p_13641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13641"> #include filename</p>
        <p id="p_13642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13642"> filename  is the name of the file to be included.</p>
        <p id="p_13643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13643"> For example, the following command includes the contents of  myincf ile . h  as part of the program:</p>
        <p id="p_13644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13644"> #include &lt;myincfile.h&gt;</p>
        <p id="p_13645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13645"> When an  #include  statement is found in a Perl program, the C preprocessor searches for the file in the current directory and the  /us r/ local /lib/perl  directory. (The -  I  option, described in the following section, enables you to search in other directories.) To instruct the C preprocessor to search only the current directory, enclose the filename in double quotation marks rather than angle brackets.</p>
        <p id="p_13646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13646"> ((include "myincf ile .h"</p>
        <p id="p_13647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13647"> This command limits the search for  myincf ile . h  to the current directory.</p>
        <p id="p_13648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13648"> You can specify an entire pathname in an # include  statement, as in the following example:</p>
        <p id="p_13649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13649"> ((include "/u/dave/myincf ile . h"</p>
        <p id="p_13650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13650"> This command retrieves the contents of  /u/dave/myincf ile . h  and adds them to the program.</p>
        <p id="p_13651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13651"> NOTE</p>
        <p id="p_13652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13652"> Perl also enables you to include other files as part of a program using the  require  statement. For more information on  require,  refer to</p>
        <p id="p_13653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13653"> Day 19 ,  "Object-Oriented Programming in Perl.</p>
        <p id="p_13654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13654"> The -I Option: Searching for C Include Files</p>
        <p id="p_13655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13655"> You use the -  I  option with the  -P  option. It enables you to specify where to look for include files to be processed by the C preprocessor. For example:</p>
        <p id="p_13656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13656"> perl -P -I /u/dave/myincdir testfile</p>
        <p id="p_13657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13657"> This command tells the Perl interpreter to search the directory  /u/dave/myincdir  for include files (as well as the default directories).</p>
        <p id="p_13658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13658"> To specify multiple directories to search, repeat the - I option:</p>
        <p id="p_13659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13659"> perl -P -I /u/dave/dirl -I /u/dave/dir2 testfile</p>
        <p id="p_13660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13660"> This command searches in both  /u/dave/dirl  and  /u/dave/dir2.</p>
        <p id="p_13661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13661"> NOTE</p>
        <p id="p_13662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13662"> The directories specified in the -1 option also are added to the system variable @  INC.  This technique ensures that the  require function can search in the same directories as the C preprocessor.</p>
        <p id="p_13663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13663"> For more information on @  INC,  refer to  Day  17 ,  "System Variables." For more information on  require,  refer to  Day  19</p>
        <p id="p_13664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13664"> The -n Option: Operating on Multiple Files</p>
        <p id="p_13665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13665"> One of the most common tasks in Perl programs and in UNIX commands is to read the contents of several input files one line at a time and process each input line as it is read. In these programs and commands, the names of the input files are supplied on the command line. A simple example is the UNIX command  cat:</p>
        <p id="p_13666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13666"> $ cat filel file2 file3 ...</p>
        <p id="p_13667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13667"> This command reads one line of input at a time and writes it to the standard output file.</p>
        <p id="p_13668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13668"> In Perl, one way to read the contents of several input files, one line at a time, is to enclose the &lt;&gt; operator in a  while  loop:</p>
        <p id="p_13669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13669"> while ($line = &lt;&gt;) {</p>
        <p id="p_13670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13670"> # process $line in here</p>
        <p id="p_13671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13671"> }</p>
        <p id="p_13672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13672"> Another method is to specify the  -n  option. This option takes your program and executes it once for each line of input in each of the files specified on the command line.</p>
        <p id="p_13673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13673"> Listing 16.3 is a simple example of a program that uses the  -n  option. It puts asterisks around each input line and then prints it.</p>
        <p id="p_13674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13674"> Type</p>
        <p id="p_13675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13675"> Listing 16.3. A simple program that uses the -n option.</p>
        <p id="p_13676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13676"> $ programl6_3</p>
        <p id="p_13677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13677"> * This test file has only one line in it.</p>
        <p id="p_13678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13678"> $</p>
        <p id="p_13679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13679"> The  -n  option encloses the program shown here in an invisible  while  loop. Each time the program is executed, the next line of input from one of the input files is read and is stored in the system variable $_. Line 4 takes this line and copies it into another scalar variable,  $line;  line 5 then removes the last character-the trailing newline character-from this line.</p>
        <p id="p_13680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13680"> Analyse</p>
        <p id="p_13681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13681"> Line 6 uses  printf  to write the input line to the standard output file. Because  print f  is formatting the input, the asterisks all appeal- in the same columns (column 1 and column 56) on your screen.</p>
        <p id="p_13682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13682"> NOTE</p>
        <p id="p_13683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13683"> The previous program is equivalent to the following Perl program (which does not use the  -n  option):</p>
        <p id="p_13684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13684"> #!/usr/local/bin/perl while (&lt; &gt;) {</p>
        <p id="p_13685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13685"> # input line is stored in the system variable</p>
        <p id="p_13686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13686"> $_</p>
        <p id="p_13687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13687"> $line = $_; chop ($line);</p>
        <p id="p_13688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13688"> printf ("* %-72s *\n", $line);</p>
        <p id="p_13689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13689"> }</p>
        <p id="p_13690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13690"> The  -n  and  -e  options work well together. For example, the following command is equivalent to the  cat command:</p>
        <p id="p_13691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13691"> $ perl -n -e "print $_; " filel file2 file3</p>
        <p id="p_13692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13692"> The  print  $_; argument supplied with the  -e  option is a one-line Perl program. Because the  -n  option executes the program once for each input line and reads each input line into the system variable $_, the statement</p>
        <p id="p_13693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13693"> print $_;</p>
        <p id="p_13694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13694"> prints each input line in turn, which is exactly what the  cat  command does. (Note that the parentheses that normally enclose the argument passed to  print  have been omitted in this case.)</p>
        <p id="p_13695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13695"> The previous command can be made even simpler:</p>
        <p id="p_13696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13696"> $ perl -n -e "print" filel file2 file3</p>
        <p id="p_13697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13697"> By default, if no argument is supplied,  print  assumes that it is to print the contents of $_. And, if the program consists of a single statement, there is no need to include the closing semicolon.</p>
        <p id="p_13698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13698"> The pattern matching and substitution operators also operate on $_ by default. For example, the following statement examines the contents of $_ and searches for a digit:</p>
        <p id="p_13699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13699"> $found  = / [0-9]/;</p>
        <p id="p_13700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13700"> This default behavior makes it easy to include a search or a substitution in a single-line command. For example:</p>
        <p id="p_13701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13701"> $ perl -n -e "print if / [0 —  9]/"  filel file2 file3</p>
        <p id="p_13702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13702"> This command reads each line of the files  filel, file2,  and  file 3.  If an input line contains a digit, it is printed.</p>
        <p id="p_13703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13703"> NOTE</p>
        <p id="p_13704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13704"> Several other functions use $_ as the default scalar variable to operate on, which makes those functions ideal for use with the  -n and  -e  options. A full list of these functions is provided in the description of the $_ system variable, which is contained in  Day 17</p>
        <p id="p_13705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13705"> The -p Option: Operating on Files and Printing</p>
        <p id="p_13706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13706"> The  -p  option is similar to the  -n  option: it reads each line of its input files in turn. However, the  -p option also prints each line it reads.</p>
        <p id="p_13707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13707"> This means, for example, that you can simulate the behavior of the UNIX  cat  command with the following command:</p>
        <p id="p_13708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13708"> $ perl -p -e filel file2 file3</p>
        <p id="p_13709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13709"> Here, the ; is a Perl program consisting of one statement that does nothing.</p>
        <p id="p_13710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13710"> The  -p  option is designed for use with the -  i  option, described in the following section.</p>
        <p id="p_13711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13711"> NOTE</p>
        <p id="p_13712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13712"> If both the  -p  and the  -n  options are specified, the  -n  option is ignored</p>
        <p id="p_13713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13713"> The -i Option: Editing Files</p>
        <p id="p_13714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13714"> As you have seen, the  -n  and  -p  options read lines from the files specified on the command line. The  -i</p>
        <p id="p_13715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13715"> option, when used with the -p option, takes the input lines being read and writes them back out to the files from which they came. This process enables you to edit files using commands similar to those used in the UNIX sed command.</p>
        <p id="p_13716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13716"> For example, consider the following command:</p>
        <p id="p_13717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13717"> $ perl -p -i -e "s/abc/def/g;" filel file2 file3</p>
        <p id="p_13718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13718"> This command contains a one-line Perl program that examines the scalar variable $_ and changes all occurrences of  abc  into  def . (Recall that the substitution operator operates on $_ if the =~ operator is not specified.) The  -p  option ensures that $_ is assigned each line of each input file in turn and that the program is executed once for each input line. Thus, this command changes all occurrences of  abc  in the files  filel, file2,  and  file3  to  def.</p>
        <p id="p_13719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13719"> Warning</p>
        <p id="p_13720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13720"> Do not use the  -i  option with the  -n  option unless you know what you're doing. The following command also changes all occurrences of  abc  to  def,  but it doesn't write out the input lines after it changes them:</p>
        <p id="p_13721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13721"> $ perl -n -i -e "s/abc/def/g;" filel file2 f ile3</p>
        <p id="p_13722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13722"> Because the  -i  option specifies that the input files are to be edited, the result is that the contents of  filel, f ile2,  and  f ile3  are completely destroyed</p>
        <p id="p_13723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13723"> The  -i  option also works on programs that do not use the  -p  option but do contain the &lt;&gt; operator inside a loop. For example, consider the following command:</p>
        <p id="p_13724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13724"> $ perl -i filel file2 file3</p>
        <p id="p_13725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13725"> In this case, the Perl interpreter copies the first file, filel, to a temporary file and opens the temporary file for reading. Then, it opens  filel  for writing and sets the default output file (the file used by calls to</p>
        <p id="p_13726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13726"> print, write,  and  printf)  to be  filel.</p>
        <p id="p_13727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13727"> After the program finishes reading the temporary file to which  filel  was copied, it then copies  file2  to a temporary file, opens it for reading, opens  file 2  for writing, and sets the default output file to be f ile2.  This process continues until the program runs out of input files.</p>
        <p id="p_13728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13728"> Listing 16.4 is a simple example of a program that edits using the  -i  option and the &lt; &gt; operator. This program evaluates any arithmetic expressions (containing integers) it sees on a single line and replaces them with their results.</p>
        <p id="p_13729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13729"> Type</p>
        <p id="p_13730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13730"> Listing 16.4. A program that edits files using the -i option.</p>
        <p id="p_13731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13731"> 1: #!/usr/local/bin/perl -i</p>
        <p id="p_13732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13732"> 2  :</p>
        <p id="p_13733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13733"> s*[*+-/]\s*\d+)*#&lt;x&gt;#) {</p>
        <p id="p_13734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13734"> ) ;</p>
        <p id="p_13735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13735"> /;</p>
        <p id="p_13736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13736"> 8 : }</p>
        <p id="p_13737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13737"> 9 : print ($line);</p>
        <p id="p_13738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13738"> 10 : }</p>
        <p id="p_13739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13739"> 3: while ($line = &lt;&gt;;</p>
        <p id="p_13740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13740"> 4 :</p>
        <p id="p_13741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13741"> 5 :</p>
        <p id="p_13742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13742"> 6  : 7 :</p>
        <p id="p_13743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13743"> while ($line ='</p>
        <p id="p_13744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13744"> s#\d+\s*[*+-/]\s*\d+(\</p>
        <p id="p_13745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13745"> eval ("\$result = $&amp;;"</p>
        <p id="p_13746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13746"> $line =~ s/&lt;x&gt;/$result</p>
        <p id="p_13747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13747"> Output</p>
        <p id="p_13748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13748"> This program produces no output because output is written to the files specified on the command line.</p>
        <p id="p_13749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13749"> The &lt;&gt; operator at the beginning of the  while  loop (line  3)  reads a line at a time from the input file or files. Each line is searched using the pattern shown in line 5. This pattern matches any substring containing the following elements (in the order given):</p>
        <p id="p_13750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13750"> 1. One or more digits</p>
        <p id="p_13751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13751"> 2. Zero or more spaces</p>
        <p id="p_13752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13752"> 3.  An or / character</p>
        <p id="p_13753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13753"> 4. Zero or more spaces</p>
        <p id="p_13754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13754"> 5. One or more digits</p>
        <p id="p_13755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13755"> Analyse</p>
        <p id="p_13756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13756"> 6. Zero or more of the preceding four subpatterns (which matches the last part of expressions such as</p>
        <p id="p_13757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13757"> 4 + 7-3)</p>
        <p id="p_13758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13758"> This pattern is replaced by a placeholder substring,  &lt;x&gt;.</p>
        <p id="p_13759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13759"> Lines 6 and 7 are executed once for each pattern matched in the input line. The matched pattern, an arithmetic expression, is automatically stored in the system variable $&amp;; line 6 substitutes this expression into a character string and passes this character string to the function  eval.  The call to  eval  creates a subprogram that evaluates the expression and returns the result in the scalar variable $  result.  Line 7 replaces the placeholder,  &lt;x&gt;,  with the result returned in  $result.</p>
        <p id="p_13760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13760"> When all the arithmetic expressions have been evaluated and substituted for, the inner  while  loop terminates, and line 9 calls  print.  Because the  -i  option has been set, the line is written back to the original input file from which it came.</p>
        <p id="p_13761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13761"> NOTE</p>
        <p id="p_13762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13762"> Even though you do not know the name of the file variable that represents the file being edited, you can still set the default output file variable to some other file and change it back later.</p>
        <p id="p_13763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13763"> To perform this task, recall that the  select  function returns the file variable associated with the current default file:</p>
        <p id="p_13764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13764"> $editfile = select (MYFILE); # change default</p>
        <p id="p_13765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13765"> file</p>
        <p id="p_13766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13766"> # do your write operations here</p>
        <p id="p_13767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13767"> select ($editfile); # change default file back</p>
        <p id="p_13768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13768"> After the second  select  call has been performed, the default output file is, once again, the file being edited</p>
        <p id="p_13769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13769"> Backing Up Input Files Using the -i Option</p>
        <p id="p_13770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13770"> By default, the  -i  option overwrites the existing input files. If you wish, you can save a copy of the original input file or files before overwriting them. To do this, specify a file extension with the  -i  option:</p>
        <p id="p_13771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13771"> $ perl -i .old filel file2 file3</p>
        <p id="p_13772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13772"> Here, the  .old  file extension specified with the  -i  option tells the Perl interpreter to copy  filel  to filel. old  before overwriting it. Similarly, the interpreter copies  file 2  to  file2 .old,  and  file 3 to  file3.old.</p>
        <p id="p_13773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13773"> The file extension specified with the  -i  option can be any character string. By convention, file extensions</p>
        <p id="p_13774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13774"> usually begin with a period; this convention makes it easier for you to spot them when you list the files in your directory.</p>
        <p id="p_13775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13775"> TIP</p>
        <p id="p_13776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13776"> If you are using the  -i  option with a program you are not familiar with, it is a good idea to specify a file extension. Doing so ensures that your files are not damaged if the program does not work the way you expect</p>
        <p id="p_13777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13777"> The -a Option: Splitting Lines</p>
        <p id="p_13778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13778"> The  -a  option is used with the  -n  or  -p  option. If the  -a  option is set, each input line that is read is automatically split into a list of "words" (sequences of characters that are not white space); this list of words is stored in a special system array variable named  @F.</p>
        <p id="p_13779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13779"> For example, if your input file contains the line</p>
        <p id="p_13780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13780"> This is a test.</p>
        <p id="p_13781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13781"> and if a program that is called with the  -a  option reads this line, the array  @F  contains the list</p>
        <p id="p_13782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13782"> ("This", "is", "a", "test.")</p>
        <p id="p_13783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13783"> The  -a  option is useful for extracting information from files. Suppose that your input files contain records of the form</p>
        <p id="p_13784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13784"> company_name quantity_ordered total_cost</p>
        <p id="p_13785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13785"> such as, for example,</p>
        <p id="p_13786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13786"> JOHN H. SMITH 10 47.32</p>
        <p id="p_13787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13787"> Listing 16.5 shows how you can use the  -a  option to easily produce a program that extracts the quantity and total cost fields from these files.</p>
        <p id="p_13788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13788"> Listing 16.5. An example of the -a option.</p>
        <p id="p_13789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13789"> 1: #!/usr/local/bin/perl</p>
        <p id="p_13790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13790"> 2  :</p>
        <p id="p_13791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13791"> 3: # This program is called with the -a and -n options</p>
        <p id="p_13792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13792"> 4: while  ($F[0 ] =~ /[ A \d.]/) {</p>
        <p id="p_13793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13793"> 5 :</p>
        <p id="p_13794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13794"> 6  :</p>
        <p id="p_13795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13795"> shift  (@F);</p>
        <p id="p_13796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13796"> next if  (!defined($F[0]));</p>
        <p id="p_13797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13797"> 7:  }</p>
        <p id="p_13798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13798"> 8:  print  ("$F[0] $F[l]\n");</p>
        <p id="p_13799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13799"> Output</p>
        <p id="p_13800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13800"> $ perl -a -n programl6_5 10  47.32 106 11.54 $</p>
        <p id="p_13801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13801"> Because the program is called with the  -a  option, the array variable  @F  contains a list, each element of which is a word from the current input line.</p>
        <p id="p_13802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13802"> Because the company name in the input file might consist of more than one word (such as  JOHN H . SMITH),  the  while  loop in lines 4-7 is needed to get rid of everything that isn't a quantity field or a total cost field. After these fields have been eliminated, line 8 can print the useful fields.</p>
        <p id="p_13803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13803"> Analyse</p>
        <p id="p_13804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13804"> Note that this program just skips over any nonstandard input lines.</p>
        <p id="p_13805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13805"> The  -F  Option: Specifying the Split Pattern</p>
        <p id="p_13806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13806"> The  -F  option, defined only in Perl 5, is designed to be used in conjunction with the  -a  option, and specifies the pattern to use when you split input lines into words. For example, suppose Listing 16.5 is called as follows:</p>
        <p id="p_13807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13807"> $ perl -a -n -F:: programl6_5</p>
        <p id="p_13808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13808"> In this case, the words in the input file are assumed to be separated by a pair of colons, which means that the program is expecting to read lines such as the following:</p>
        <p id="p_13809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13809"> JOHN H. SMITH: :10:  :47.32</p>
        <p id="p_13810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13810"> NOTE</p>
        <p id="p_13811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13811"> The  -F  option ignores opening and closing slashes if they are present because it interprets them as pattern delimiters. This means that the following program invocations are identical:</p>
        <p id="p_13812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13812"> $ perl -a -n  -F::  programl6_5 $ perl -a -n  -F /::/  programl6_</p>
        <p id="p_13813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13813"> The -0 Option: Specifying Input End-of-Line</p>
        <p id="p_13814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13814"> In all the programs you have seen so far, when the Perl inteipreter reads a line from an input file or from the keyboard, it reads until it sees a newline character. You can tell Perl that you want the "end-of-line" input character to be something other than the newline character by specifying the -  0  option. (The  0  here is the digit zero, not the letter 0.)</p>
        <p id="p_13815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13815"> With the  -0  option, you specify which character is to be the end-of-line character for your input file by providing its ASCII representation in base 8 (octal). For example, the command</p>
        <p id="p_13816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13816"> $ perl -0 040 progl infile</p>
        <p id="p_13817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13817"> calls the Perl program named  progl  and specifies that it is to use the space character (ASCII 32, or 40 octal) as the end-of-line character when it reads the input file  inf ile  (or any other input file).</p>
        <p id="p_13818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13818"> This means, for example, that if this program reads an input file containing the following:</p>
        <p id="p_13819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13819"> Test input.</p>
        <p id="p_13820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13820"> Here's  another line.</p>
        <p id="p_13821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13821"> it will read a total of four input lines:</p>
        <p id="p_13822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13822"> • The first input line consists of the word  Test.</p>
        <p id="p_13823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13823"> • The second input line consists of  input ., followed by a newline character, followed by  Here ' s.</p>
        <p id="p_13824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13824"> • The third input line consists of the word  another.</p>
        <p id="p_13825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13825"> • The fourth input line consists of the word  line  ., followed by a newline character.</p>
        <p id="p_13826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13826"> The  -0  option provides a quick way to read an input file one word at a time, assuming that each line ends with at least one blank character. (If it doesn't, you can quickly write a Perl program that uses the  -i  and -p options to add a space to the end of each line in each file.) Listing 16.6 is an example of a program that uses - 0 to read an input file one word at a time.</p>
        <p id="p_13827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13827"> Type</p>
        <p id="p_13828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13828"> Listing 16.6.  A  program that uses the -0 option.</p>
        <p id="p_13829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13829"> Output</p>
        <p id="p_13830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13830"> $ programl6_6 filel This</p>
        <p id="p_13831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13831"> line</p>
        <p id="p_13832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13832"> contains</p>
        <p id="p_13833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13833"> five words. $</p>
        <p id="p_13834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13834"> The header comment (line 1) specifies that the  -0  option is to be used and that the space character is to become the end-of-line character. (Recall that you do not need a space between an option and the value associated with an option.) This means that line 3 reads from the input file until it sees a blank space.</p>
        <p id="p_13835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13835"> Not everything read by line 3 is a word, of course. There are two types of lines that are not particularly useful that the program must check for:</p>
        <p id="p_13836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13836"> • Empty lines, which are generated when the input file contains two consecutive spaces</p>
        <p id="p_13837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13837"> • Lines containing the newline character (remember, the newline character is no longer an end-of-line character, so now it actually appears in input lines)</p>
        <p id="p_13838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13838"> Line 4 checks whether any newline characters are contained in the current input line. The substitution in this line is a global substitution, because an input line can contain two or more newline characters. (This occurs when an input file contains a blank line.)</p>
        <p id="p_13839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13839"> After all the newline characters have been eliminated, line 5 checks whether the resulting input line is empty. If it is, the program continues with the next input line. If the resulting input line is not empty, the input line must be a useful word, and line 6 prints it.</p>
        <p id="p_13840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13840"> Analyse</p>
        <p id="p_13841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13841"> NOTE</p>
        <p id="p_13842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13842"> If you specify the value  0 0  (octal zero) with the -  0  option, the Perl interpreter reads until it sees two newline characters. This enables you to read an entire paragraph at a time.</p>
        <p id="p_13843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13843"> If you specify no value with the -0 option, the null character (ASCII 0) is assumed</p>
        <p id="p_13844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13844"> The -1 Option: Specifying Output End-of-Line</p>
        <p id="p_13845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13845"> The -1 option enables you to specify an output end-of-line character for use in  print  statements.</p>
        <p id="p_13846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13846"> Like the -0 option, the -1 option accepts a base-8 (octal) integer that indicates the ASCII representation of the character you want to use.</p>
        <p id="p_13847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13847"> When the -1 option is specified, the Perl interpreter does two things:</p>
        <p id="p_13848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13848"> • If the  -n  or  -p  option is specified, each input line read in from the standard input file has its last character (the line terminator) removed. (The Perl interpreter takes this action because it assumes that you want to replace the old end-of-line character with the one specified by the -1 option.)</p>
        <p id="p_13849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13849"> • When you call the  print  function, the output written by  print  will be immediately followed by the character specified by the -1 option.</p>
        <p id="p_13850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13850"> If you do not specify a value with the -1 option, the Perl interpreter uses the character specified by the -0 option, if it is defined. If - 0 has not been specified, the end-of-line character is defined to be the newline character.</p>
        <p id="p_13851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13851"> Warning</p>
        <p id="p_13852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13852"> If you are using both the -1 and the -0 option and you do not provide a value with the -1 option, the order of the options becomes significant because the options are processed from left to right.</p>
        <p id="p_13853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13853"> If the -1 option appears first, the output end-of-line character is set to the newline character. If the -  0  option appears first, the output end-of-line character (set by -1) becomes the same as the input end-of-line character (set by -0)</p>
        <p id="p_13854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13854"> Listing 16.7 is a simple example of a program that uses -1.</p>
        <p id="p_13855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13855"> Type</p>
        <p id="p_13856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13856"> Listing 16.7,  A  program that uses the -1 option.</p>
        <p id="p_13857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13857"> $ programl6_7 Hello!</p>
        <p id="p_13858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13858"> This is a very simple test program!</p>
        <p id="p_13859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13859"> $</p>
        <p id="p_13860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13860"> The -1014 option in the header comment in line 1 sets the output line character to the newline character. This means that every  print  statement in the program will have a newline character added to it. As a consequence, the output from lines 3 and 4 appear on separate lines.</p>
        <p id="p_13861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13861"> Analyse</p>
        <p id="p_13862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13862"> NOTE</p>
        <p id="p_13863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13863"> You can control the input and output end-of-line characters also by using the system variables $ / and $ \. For a description of these system variables, refer to  Day 17</p>
        <p id="p_13864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13864"> The -x Option: Extracting a Program from a Message</p>
        <p id="p_13865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13865"> The  -x  option enables you to process a Perl program that appears in the middle of a file (such as a file containing an electronic mail message, which usually contains some mail routing information). When the -x  option is specified, the Perl interpreter ignores every line in the program until it sees a header comment (a comment beginning with the # ! characters).</p>
        <p id="p_13866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13866"> If you are using Perl 5, the header comment must also contain the word "perl.</p>
        <p id="p_13867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13867"> After the Perl interpreter sees the header comment, it then processes the program as usual until one of the following three conditions occurs:</p>
        <p id="p_13868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13868"> • The bottom of the program file is reached.</p>
        <p id="p_13869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13869"> • The program file contains a line consisting of just the Ctrl+D or Ctrl+Z character.</p>
        <p id="p_13870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13870"> • The program file contains a line consisting of the following statement (by itself):</p>
        <p id="p_13871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13871"> END</p>
        <p id="p_13872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13872"> If the Perl interpreter reads one of the end-of-program lines (the second and third conditions listed previously), it ignores everything appearing after that line in the file.</p>
        <p id="p_13873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13873"> Listing 16.8 is a simple example of a program that works if run with the  -x  option.</p>
        <p id="p_13874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13874"> Type</p>
        <p id="p_13875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13875"> Listing 16.8, A Perl program contained in a file.</p>
        <p id="p_13876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13876"> Output</p>
        <p id="p_13877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13877"> $ programl6_8 Hello, world!</p>
        <p id="p_13878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13878"> $</p>
        <p id="p_13879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13879"> Analyse</p>
        <p id="p_13880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13880"> If this program is started with the  -x  option, the Peri interpreter skips over everything until it sees line 6. (Needless to say, if you try to run this program without specifying the  -x  option, the Perl interpreter will complain.) Line 8 then prints the message  Hello, world.</p>
        <p id="p_13881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13881"> Line 9 is the special end-of-program line. When the Perl interpreter sees this line, it skips the rest of the program.</p>
        <p id="p_13882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13882"> NOTE</p>
        <p id="p_13883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13883"> Of course, you can't specify the  -x  option in the header comment itself because the Perl interpreter has to know in advance that the program contains lines that must be skipped</p>
        <p id="p_13884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13884"> Miscellaneous Options</p>
        <p id="p_13885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13885"> The following sections describe some of the more exotic options you can pass to the Perl interpreter. You are not likely to need any of these options unless you are doing something unusual (and you really know what you are doing).</p>
        <p id="p_13886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13886"> The -u Option</p>
        <p id="p_13887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13887"> The -u option tells the Perl interpreter to generate a core dump file. This file can then be examined and manipulated.</p>
        <p id="p_13888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13888"> The -U Option</p>
        <p id="p_13889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13889"> The  -U  option tells the Perl interpreter to enable you to perform "unsafe" operations in your program. (Basically, you'll know that an operation is considered unsafe when the Perl interpreter doesn't let you perform it without specifying the  -U  option!)</p>
        <p id="p_13890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13890"> The -S Option</p>
        <p id="p_13891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13891"> The  -S  option tells the Perl interpreter that your program might be contained in any of the directories specified by your  PATH  environment variable. The Perl interpreter checks each of these directories in turn, in the order in which they are specified, to see whether your program is located there. (This is the normal behavior of the shell for commands in the UNIX environment.)</p>
        <p id="p_13892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13892"> NOTE</p>
        <p id="p_13893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13893"> You need to use  -S  only if you are running your Perl program using the  perl  command, as in</p>
        <p id="p_13894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13894"> $ perl myprog</p>
        <p id="p_13895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13895"> If you are running the program using a command such as</p>
        <p id="p_13896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13896"> $ myprog</p>
        <p id="p_13897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13897"> your shell (normally) treats it like any other command and searches the directories specified in your PATH environment variable even if you don't specify the -S option</p>
        <p id="p_13898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13898"> The -D Option</p>
        <p id="p_13899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13899"> The  -D  option sets the Perl interpreter's internal debugging flags. This option is specified with an integer value (for example,  -D 2 5 6).</p>
        <p id="p_13900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13900"> For details on this option, refer to the online manual page for Perl.</p>
        <p id="p_13901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13901"> NOTE</p>
        <p id="p_13902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13902"> The internal debugging flags specified by  -D  have nothing to do with the Perl debugger, which is specified by the -d option.</p>
        <p id="p_13903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13903"> The debugging flags specified by  -D  provide information on how Perl itself works, not on how your program works</p>
        <p id="p_13904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13904"> The -T Option: Writing Secure Programs</p>
        <p id="p_13905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13905"> The  -T  option specifies that data obtained from the outside world cannot be used in any command that modifies your file system. This feature enables you to write secure programs for system administration tasks.</p>
        <p id="p_13906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13906"> This option is only available in Perl 5. If you are running Perl 4, use a special version of Perl named taintperl.  For details on  taintperl,  see the online documentation supplied with your Perl distribution.</p>
        <p id="p_13907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13907"> The -d Option: Using the Perl Debugger</p>
        <p id="p_13908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13908"> One final option that is quite useful is  -d.  This option tells the Perl interpreter to run your program using the Perl debugger. For a complete description of the Perl debugger and how to use it, refer to  Day  21 ,  "The Perl Debugger."</p>
        <p id="p_13909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13909"> NOTE</p>
        <p id="p_13910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13910"> If you are specifying the -d option, you still can use other options</p>
        <p id="p_13911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13911"> Summary</p>
        <p id="p_13912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13912"> Today you learned how to specify options when you run your Perl programs. An option is a dash followed by a single letter, and optionally followed by a value to be associated with the option. Options lacking associated values can be grouped together.</p>
        <p id="p_13913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13913"> You can specify options in two ways: on the command line and in the header comment. Only one option or group of options can be supplied in the header comment.</p>
        <p id="p_13914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13914"> Available options include those that list the Perl version number, check your syntax, display warnings, allow single-line programs on the command line, invoke the C preprocessor, automatically read from the input files, and edit files in place.</p>
        <p id="p_13915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13915"> Q&amp;A</p>
        <p id="p_13916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13916"> Q: Why can you specify only one option in the header comment?</p>
        <p id="p_13917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13917"> A: This is a restriction imposed by the UNIX operating system.</p>
        <p id="p_13918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13918"> Q: Why does v display the Perl version number without running the program?</p>
        <p id="p_13919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13919"> A: This option enables you to check whether the version of Perl you are running is capable of running</p>
        <p id="p_13920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13920"> your program. If an old copy of Perl is running on your machine, your program might not work properly.</p>
        <p id="p_13921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13921"> Q: What options enable me to write a program that edits every line of a file?</p>
        <p id="p_13922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13922"> A: Use the  -i  (edit in place) and  -p  (print each line) options. (These options are often used with the -</p>
        <p id="p_13923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13923"> e  option to perform an editing command similar to those used by the UNIX  sed  command.)</p>
        <p id="p_13924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13924"> Q: I have a program that needs to run on two or more different machines. Is there a way of</p>
        <p id="p_13925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13925"> writing the program that ensures that I don't have to change the program each time I change machines?</p>
        <p id="p_13926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13926"> A: Here's how to carry out this task:</p>
        <p id="p_13927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13927"> 1. On each machine, define a file that is to be used to store system-dependent constants. Give the file the same name on each machine. For example, you could call the file  perldef . h. The location of the file doesn't matter as long as it's a different directory name on each type of machine.</p>
        <p id="p_13928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13928"> 2.  In each  perldef . h,  use #  define  to define one constant for each type of machine you run. For example, if you are running this program on UNIX 4.3BSD and System V machines, you could define constants named  M_BSD  and  M_SYSV.</p>
        <p id="p_13929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13929"> 3. After you have defined the constants, set the value of each constant to 0, except for the one corresponding to the machine on which you are running. For example, on your 4.3BSD machines, set  M_BSD  to 1, and set all the other constants to 0.</p>
        <p id="p_13930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13930"> 4. Add the following statement to your program:</p>
        <p id="p_13931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13931"> #include &lt;perldef.h&gt;</p>
        <p id="p_13932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13932"> 5. In your program, use  #if  and  #endif  to enclose any system-dependent information. For example, if a group of statements is to be executed only on 4.3BSD machines, enclose the statements with the statements</p>
        <p id="p_13933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13933"> #if BSD #endif</p>
        <p id="p_13934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13934"> 6. When you run your program, use the  -P  option to specify C preprocessing, and use the -  I option to tell the Perl interpreter to search for the directory corresponding to the perldef . h  file for this machine. For example, if you are running your program on a 4.3BSD machine and the  perldef . h  file for 4.3BSD machines is in the /usr/local/include/bsdperl  directory, include the following option when you start your program:</p>
        <p id="p_13935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13935"> -I /usr/local/include/bsdperl</p>
        <p id="p_13936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13936"> Q: Why does the -p option override the -n option?</p>
        <p id="p_13937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13937"> A: The -p option tells the Perl interpreter that you want to print each input line that you read, and the -</p>
        <p id="p_13938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13938"> n  option tells it that you don't want to do so. These options basically contradict one another,  -p overrides  -n  because  -p  is safer; if you really want  -n,  you can throw away the output from  -p.</p>
        <p id="p_13939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13939"> If you really want  -p  and get  -n,  you won't get the output you want.</p>
        <p id="p_13940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13940"> Workshop</p>
        <p id="p_13941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13941"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_13942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13942"> Quiz</p>
        <p id="p_13943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13943"> 1. What do the following options do?</p>
        <p id="p_13944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13944"> 2. What happens when -1 and -0 are both specified, and</p>
        <p id="p_13945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13945"> a. -1 appears first?</p>
        <p id="p_13946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13946"> b. - 0 appears first?</p>
        <p id="p_13947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13947"> 3. Why do the -i and -n options destroy input files when included together?</p>
        <p id="p_13948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13948"> 4. How does the C preprocessor distinguish between preprocessor commands and Perl comments?</p>
        <p id="p_13949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13949"> 5. How does the Perl interpreter distinguish options for the inteipreter from options for the program itself?</p>
        <p id="p_13950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13950"> Exercises</p>
        <p id="p_13951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13951"> 1. Write a program that replaces all the newline characters in the file  test file  with colons. Use</p>
        <p id="p_13952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13952"> only command-line options to do this.</p>
        <p id="p_13953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13953"> 2. Write a one-line program that prints only the lines containing the word the.</p>
        <p id="p_13954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13954"> 3. Write a one-line program that prints the second word of each input line.</p>
        <p id="p_13955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13955"> 4. Write a program that prints  Hello  ! if you pass the  -H  switch to it and that prints  Goodbye  ! if you pass the  -G  switch.</p>
        <p id="p_13956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13956"> 5. Write a one-line program that converts all lowercase letters to uppercase.</p>
        <p id="p_13957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13957"> 6. BUG BUSTER: What is wrong with this command line?</p>
        <p id="p_13958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13958"> $ perl -i -n -e  "s/abc/def/g";</p>
        <p id="p_13959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13959"> 7. BUG BUSTER: What is wrong with this command line?</p>
        <p id="p_13960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13960"> $ perl -ipe  "s/abc/def/g";</p>
        <p id="p_13961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13961"> Chapter 17</p>
        <p id="p_13962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13962"> System Variables</p>
        <p id="p_13963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13963"> CONTENTS</p>
        <p id="p_13964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13964"> •  Global Scalar Variables</p>
        <p id="p_13965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13965"> o  The Default Scalar Variable: $</p>
        <p id="p_13966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13966"> o  The Program Name: $  0 o  The User ID: $&lt; and $&gt; o  The Group ID: $ ( and $)</p>
        <p id="p_13967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13967"> o  The Version Number: $ 1 o  The Input Line Separator: $ /</p>
        <p id="p_13968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13968"> o  The Output Line Separator: $</p>
        <p id="p_13969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13969"> o  The Output Field Separator: $, o  The Array Element Separator: $ "</p>
        <p id="p_13970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13970"> o  The Number Output Format: $ #</p>
        <p id="p_13971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13971"> o  The  eval  Error Message: $@</p>
        <p id="p_13972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13972"> o  The System Error Code: $ ? o  The System Error Message: $ !</p>
        <p id="p_13973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13973"> o  The Current Line Number: $ .</p>
        <p id="p_13974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13974"> o  Multiline Matching: $* o  The First Array Subscript: $ [</p>
        <p id="p_13975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13975"> o  Multidimensional Associative Arrays and the $; Variable</p>
        <p id="p_13976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13976"> o  The Word-Break Specifier: $ :</p>
        <p id="p_13977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13977"> o  The Perl Process ID: $ $ o  The Current Filename:  $ARGV</p>
        <p id="p_13978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13978"> o  The Write Accumulator:  $ A A o  The Internal Debugging Value: $  A D</p>
        <p id="p_13979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13979"> o  The System File Flag:  $  A F o  Controlling File Editing Using $  A I o  The Format Form-Feed Character: $  A L</p>
        <p id="p_13980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13980"> o  Controlling Debugging:  $ A P o  The Program Start Time:  $  A T o  Suppressing Warning Messages:  $^W</p>
        <p id="p_13981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13981"> o  The $  A X  Variable</p>
        <p id="p_13982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13982"> •  Pattern System Variables</p>
        <p id="p_13983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13983"> o  Retrieving Matched Subpatterns</p>
        <p id="p_13984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13984"> o  Retrieving the Entire Pattern: $ &amp;</p>
        <p id="p_13985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13985"> o  Retrieving the Unmatched Text: the $ v  and $ ' Variables</p>
        <p id="p_13986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13986"> o  The $+ Variable</p>
        <p id="p_13987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13987"> File System Variables</p>
        <p id="p_13988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13988"> o  The Default Print Format: $ ~</p>
        <p id="p_13989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13989"> o  Specifying Page Length: $ = o  Lines Remaining on the Page: $-</p>
        <p id="p_13990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13990"> o  The Page Header Print Format: $  A</p>
        <p id="p_13991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13991"> o  Buffering Output: $ | o  The Current Page Number: $%</p>
        <p id="p_13992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13992"> •  Array System Variables</p>
        <p id="p_13993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13993"> o  The @_ Variable o  The  @ARGV  Variable o  The  @F  Variable o  The @  INC  Variable o  The %  INC  Variable o  The  %ENV  Variable o  The  % S I G  Variable</p>
        <p id="p_13994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13994"> •  Built-In File Variables</p>
        <p id="p_13995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13995"> o  STD IN. STD OUT, and STDERR</p>
        <p id="p_13996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13996"> o  ARGV o  DATA</p>
        <p id="p_13997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13997"> o  The Underscore File Variable</p>
        <p id="p_13998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13998"> •  Specifying System Variable Names as Words</p>
        <p id="p_13999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_13999"> •  Summary</p>
        <p id="p_14000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14000"> •  O&amp;A</p>
        <p id="p_14001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14001"> •  Workshop</p>
        <p id="p_14002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14002"> o  Quiz o  Exercises</p>
        <p id="p_14003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14003"> Today's lesson describes the built-in system variables that can be referenced from every Perl program. These system variables are divided into five groups:</p>
        <p id="p_14004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14004"> • Global scalar variables</p>
        <p id="p_14005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14005"> • Pattern system variables</p>
        <p id="p_14006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14006"> • File system variables</p>
        <p id="p_14007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14007"> • Array system variables</p>
        <p id="p_14008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14008"> • Built-in file variables</p>
        <p id="p_14009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14009"> The following sections describe these groups of system variables, and also describe how to provide English-language equivalents of their variable names.</p>
        <p id="p_14010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14010"> Global Scalar Variables</p>
        <p id="p_14011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14011"> The  global scalar variables  are built-in system variables that behave just like the scalar variables you create in the main body of your program. This means that these variables have the following properties:</p>
        <p id="p_14012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14012"> • Each built-in global scalar variable stores only one scalar value.</p>
        <p id="p_14013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14013"> • Only one copy of a global scalar variable is defined in a program.</p>
        <p id="p_14014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14014"> Other kinds of built-in scalar variables, which you will see later in this lesson, do not behave in this way. The following sections describe the global scalar variables your Perl programs can use.</p>
        <p id="p_14015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14015"> The Default Scalar Variable: $_</p>
        <p id="p_14016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14016"> The most commonly used global scalar variable is the $_ variable. Many Perl functions and operators modify the contents of $_ if you do not explicitly specify the scalar variable on which they are to operate.</p>
        <p id="p_14017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14017"> The following functions and operators work with the $_ variable by default:</p>
        <p id="p_14018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14018"> • The pattern-matching operator</p>
        <p id="p_14019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14019"> • The substitution operator</p>
        <p id="p_14020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14020"> • The translation operator</p>
        <p id="p_14021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14021"> • The &lt;&gt; operator, if it appears in a while or for conditional expression</p>
        <p id="p_14022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14022"> • The chop function</p>
        <p id="p_14023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14023"> • The print function</p>
        <p id="p_14024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14024"> • The study function</p>
        <p id="p_14025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14025"> The Pattern-Matching Operator and $_</p>
        <p id="p_14026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14026"> Normally, the pattern-matching operator examines the value stored in the variable specified by a corresponding =~ or ! ~ operator. For example, the following statement prints hi if the string abc is contained in the value stored in $val:</p>
        <p id="p_14027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14027"> print ("hi") if ($val =~ /abc/);</p>
        <p id="p_14028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14028"> By default, the pattern-matching operator examines the value stored in $_. This means that you can leave out the =~ operator if you are searching $_:</p>
        <p id="p_14029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14029"> NOTE</p>
        <p id="p_14030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14030"> If you want to use the ! ~ (true-if-pattern-not-matched) operator, you will always need to specify it explicitly, even if you are examining $_:</p>
        <p id="p_14031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14031"> print ("hi") if ($_ !~ /abc/);</p>
        <p id="p_14032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14032"> If the Perl interpreter sees just a pattern enclosed in / characters, it assumes the existence of a =~ operator</p>
        <p id="p_14033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14033"> $_ enables you to use pattern-sequence memory to extract subpatterns from a string and assign them to an array variable:</p>
        <p id="p_14034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14034"> $_ = "This string contains the number 25.11.";</p>
        <p id="p_14035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14035"> @array =  /-?(\d+)\.?(\d+)/;</p>
        <p id="p_14036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14036"> In the second statement shown, each subpattern enclosed in parentheses becomes an element of the list assigned to @ array.  As a consequence, @ array  is assigned  (25,11).</p>
        <p id="p_14037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14037"> In Perl 5, a statement such as</p>
        <p id="p_14038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14038"> @array =  /-?(\d+)\.?(\d+)/;</p>
        <p id="p_14039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14039"> also assigns the extracted subpatterns to the pattern-sequence scalar variables $ 1, $2, and so on. This means that the statement assigns 2 5 to $1 and 11 to $2. Perl 4 supports assignment of subpatterns to arrays, but does not assign the subpatterns to the pattern-sequence variables.</p>
        <p id="p_14040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14040"> The Substitution Operator and $_</p>
        <p id="p_14041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14041"> The substitution operator, like the pattern-matching operator, normally modifies the contents of the variable specified by the =~ or ! ~ operator. For example, the following statement searches for  abc  in the value stored in  $val  and replaces it with  def:</p>
        <p id="p_14042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14042"> $val =~ s/abc/def/;</p>
        <p id="p_14043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14043"> The substitution operator uses the $_ valuable if you do not specify a variable using =~. For example, the following statement replaces the first occurrence of  abc  in $_ with  def:</p>
        <p id="p_14044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14044"> s/abc/def/;</p>
        <p id="p_14045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14045"> Similarly, the following statement replaces all white space (spaces, tabs, and newline characters) in $_ with a single space:</p>
        <p id="p_14046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14046"> /\s + / /g;</p>
        <p id="p_14047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14047"> When you substitute inside $_, the substitution operator returns the number of substitutions performed:</p>
        <p id="p_14048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14048"> $subcount = s/abc/def/g;</p>
        <p id="p_14049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14049"> Here, $  subcount  contains the number of occurrences of  abc  that have been replaced by  def . If  abc  is not contained in the value stored in $_, $  subcount  is assigned 0.</p>
        <p id="p_14050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14050"> The Translation Operator and $_</p>
        <p id="p_14051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14051"> The behavior of the translation operator is similar to that of the pattern-matching and substitution operators: it normally operates on the variable specified by =~, and it operates on $_ if no =~ operator is included. For example, the following statement translates all lowercase letters in the value stored in $_ to their uppercase equivalents:</p>
        <p id="p_14052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14052"> tr/a-z/A-Z/;</p>
        <p id="p_14053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14053"> Like the substitution operator, if the translation operator is working with $_, it returns the number of operations performed. For example:</p>
        <p id="p_14054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14054"> $conversions = tr/a-z/A-Z/;</p>
        <p id="p_14055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14055"> Here, $  conversions  contains the number of lowercase letters converted to uppercase.</p>
        <p id="p_14056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14056"> You can use this feature of  tr  to count the number of occurrences of particular characters in a file. Listing 17.1 is an example of a program that performs this operation.</p>
        <p id="p_14057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14057"> Type</p>
        <p id="p_14058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14058"> Listing 17.1.  A  program that counts using tr.</p>
        <p id="p_14059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14059"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14060"> 2  :</p>
        <p id="p_14061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14061"> $ programl7_l filel</p>
        <p id="p_14062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14062"> Specify the nonblank characters you want to count: abc</p>
        <p id="p_14063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14063"> a appears 8 times c appears 3 times b appears 2 times</p>
        <p id="p_14064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14064"> $</p>
        <p id="p_14065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14065"> Analysis</p>
        <p id="p_14066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14066"> This program first asks the user for a line of input containing the characters to be counted. These characters can be separated by spaces or jammed into a single word.</p>
        <p id="p_14067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14067"> Line 5 takes the line of input containing the characters to be counted and removes the trailing newline character. Line 6 then splits the line of input into separate characters, each of which is stored in an element of the array @  chars.  The pattern / \  s  * / splits on zero or more occurrences of a whitespace character; this splits on every nonblank character and skips over the blank characters.</p>
        <p id="p_14068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14068"> Line 7 reads a line of input from a file whose name is specified on the command line. Line 8 takes this line and stores it in the system variable $_. (In most cases, system variables can be assigned to, just like other variables.)</p>
        <p id="p_14069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14069"> Lines 9-12 count the number of occurrences of each character in the input string read in line 4. Each character, in turn, is stored in  $char,  and the value of  $char  is substituted into the string in line 10. This string is then passed to  eval,  which executes the translate operation contained in the string.</p>
        <p id="p_14070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14070"> The translate operation doesn't actually do anything because it is "translating" a character to itself. However, it returns the number of translations performed, which means that it returns the number of occurrences of the character. This count is assigned to $  count.</p>
        <p id="p_14071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14071"> For example, suppose that the variable  $char  contains the character  e  and that $_ contains  Hi there !. In this case, the string in line 10 becomes the following because  e  is substituted for  $char  in the string:</p>
        <p id="p_14072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14072"> $count =  tr/e/e/;</p>
        <p id="p_14073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14073"> The call to  eval  executes this statement, which counts the number of  e's  in  Hi there  !. Because there are two  e's  in  Hi there !, $ count  is assigned  2.</p>
        <p id="p_14074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14074"> An associative array, %  count,  keeps track of the number of occurrences of each of the characters being counted. Line 11 adds the count returned by line 10 to the associative array element whose subscript is the character currently being counted. For example, if the program is currently counting the number of  e's,  this number is added to the element $  count  {  " e  "  }.</p>
        <p id="p_14075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14075"> After all input lines have been read and their characters counted, lines 14-16 print the total number of occurrences of each character by examining the elements of %  count.</p>
        <p id="p_14076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14076"> The &lt;&gt; Operator and $_</p>
        <p id="p_14077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14077"> In Listing 17.1, which you've just seen, the program reads a line of input into a scalar variable named $ input  and then assigns it to $_. There is a quicker way to carry out this task, however. You can replace</p>
        <p id="p_14078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14078"> while ($input = &lt;&gt;) {</p>
        <p id="p_14079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14079"> $_ = $input;</p>
        <p id="p_14080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14080"> # more stuff here</p>
        <p id="p_14081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14081"> }</p>
        <p id="p_14082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14082"> with the following code:</p>
        <p id="p_14083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14083"> while (&lt;&gt;) {</p>
        <p id="p_14084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14084"> # more stuff here</p>
        <p id="p_14085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14085"> }</p>
        <p id="p_14086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14086"> If the &lt;&gt; operator appears in a conditional expression that is part of a loop (an expression that is part of a conditional statement such as  while  or  for)  and it is not to the right of an assignment operator, the Perl interpreter automatically assigns the resulting input line to the scalar variable $_.</p>
        <p id="p_14087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14087"> For example, Listing 17.2 shows a simple way to print the first character of every input line read from the standard input file.</p>
        <p id="p_14088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14088"> Type</p>
        <p id="p_14089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14089"> Listing 17.2. A simple program that assigns to $_ using &lt;STDIN&gt;.</p>
        <p id="p_14090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14090"> Output</p>
        <p id="p_14091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14091"> $ program!7_2</p>
        <p id="p_14092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14092"> This  is a test.</p>
        <p id="p_14093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14093"> T</p>
        <p id="p_14094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14094"> Here is another line. H</p>
        <p id="p_14095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14095"> A D</p>
        <p id="p_14096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14096"> $</p>
        <p id="p_14097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14097"> Analysis</p>
        <p id="p_14098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14098"> Because  &lt;STDIN&gt;  is inside a conditional expression and is not assigned to a scalar variable, the Perl interpreter assigns the input line to $_. The program then retrieves the first character by passing $_ to</p>
        <p id="p_14099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14099"> split.</p>
        <p id="p_14100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14100"> NOTE</p>
        <p id="p_14101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14101"> The &lt;&gt; operator assigns to $_ only if it is contained in a conditional expression in a loop. The statement</p>
        <p id="p_14102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14102"> &lt;STDIN&gt;;</p>
        <p id="p_14103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14103"> reads a line of input from the standard input file and throws it away without changing the contents of $_. Similarly, the following statement does not change the value of $_:</p>
        <p id="p_14104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14104"> if (&lt;&gt;) {</p>
        <p id="p_14105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14105"> print ("The input files are not all empty.\n");</p>
        <p id="p_14106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14106"> }</p>
        <p id="p_14107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14107"> The chop Function and $_</p>
        <p id="p_14108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14108"> By default, the  chop  function operates on the value stored in the $_ variable. For example:</p>
        <p id="p_14109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14109"> while (&lt;&gt;) {</p>
        <p id="p_14110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14110"> chop;</p>
        <p id="p_14111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14111"> # you can do things with $_ here</p>
        <p id="p_14112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14112"> }</p>
        <p id="p_14113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14113"> Here, the call to  chop  removes the last character from the value stored in $_. Because the conditional expression in the  while  statement has just assigned a line of input to $_,  chop  gets rid of the newline character that terminates each input line.</p>
        <p id="p_14114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14114"> The print Function and $_</p>
        <p id="p_14115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14115"> The  print  function also operates on  $_by  default. The following statement writes the contents of $_ to the standard output file:</p>
        <p id="p_14116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14116"> print;</p>
        <p id="p_14117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14117"> Listing 17.3 is an example of a program that simply writes out its input, which it assumes is stored in $_. This program is an implementation of the UNIX  cat  command, which reads input files and displays their contents.</p>
        <p id="p_14118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14118"> Type</p>
        <p id="p_14119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14119"> Listing 17.3.  A  simple version of the cat command using $_.</p>
        <p id="p_14120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14120"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14121"> 2  :</p>
        <p id="p_14122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14122"> 3: print while (&lt;&gt;);</p>
        <p id="p_14123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14123"> Output</p>
        <p id="p_14124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14124"> $ programl7_3 filel</p>
        <p id="p_14125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14125"> This is the only line in file "filel". $</p>
        <p id="p_14126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14126"> This program uses the &lt;&gt; operator to read a line of input at a time and store it in $_. If the line is nonempty, the  print  function is called; because no variable is specified with  print,  it writes out the contents of $_.</p>
        <p id="p_14127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14127"> Analysis</p>
        <p id="p_14128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14128"> NOTE</p>
        <p id="p_14129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14129"> You can use this default version of  print  only if you are writing to the default output file (which is usually  STDOUT  but can be changed using the  select  function). If you are specifying a file variable when you call  print,  you also must specify the value you are printing.</p>
        <p id="p_14130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14130"> For example, to send the contents of $_ to the output file  MYFILE, use the following command:</p>
        <p id="p_14131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14131"> print MYFILE ($_)</p>
        <p id="p_14132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14132"> The study Function and $_</p>
        <p id="p_14133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14133"> If you do not specify a variable when you call  study,  this function uses $_ by default:</p>
        <p id="p_14134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14134"> study;</p>
        <p id="p_14135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14135"> The  study  function increases the efficiency of programs that repeatedly search the same variable. It is described on  Day 13 ,  "Process, String, and Mathematical Functions."</p>
        <p id="p_14136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14136"> Benefits of the $_ Variable</p>
        <p id="p_14137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14137"> The default behavior of the functions listed previously is useful to remember when you are writing one-line Perl programs for use with the  -e  option. For example, the following command is a quick way to display the contents of the files  f ilel, f ile2,  and  f ile3:</p>
        <p id="p_14138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14138"> $ perl -e "print while &lt;&gt;;" filel file2 file3</p>
        <p id="p_14139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14139"> Similarly, the following command changes all occurrences  ofabcin  filel, file2,  and  file3  to  def:</p>
        <p id="p_14140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14140"> $ perl -ipe "s/abc/def/g" filel file2 file3</p>
        <p id="p_14141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14141"> Although $_ is useful in cases such as the preceding one, don't overuse it. Many Perl programmers write programs that have references to $_ running like an invisible thread through their programs.</p>
        <p id="p_14142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14142"> Programs that overuse $_ are hard to read and are easier to break than programs that explicitly reference scalar variables you have named yourself</p>
        <p id="p_14143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14143"> The Program Name: $0</p>
        <p id="p_14144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14144"> The $  0  variable contains the name of the program you are running. For example, if your program is named peril, the statement</p>
        <p id="p_14145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14145"> print ("Now executing  $0...\n");</p>
        <p id="p_14146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14146"> displays the following on your screen:</p>
        <p id="p_14147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14147"> Now executing peril...</p>
        <p id="p_14148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14148"> The $  0  variable is useful if you are writing programs that call other programs. If an error occurs, you can determine which program detected the error:</p>
        <p id="p_14149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14149"> die  ("$0:  can't open input  file\n");</p>
        <p id="p_14150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14150"> Here, including $  0  in the string passed to  die  enables you to specify the filename in your error message. (Of course, you can always leave off the trailing newline, which tells Perl to print the filename and the line number when printing the error message. However, $  0  enables you to print the filename without the line number, if that's what you want.)</p>
        <p id="p_14151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14151"> NOTE</p>
        <p id="p_14152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14152"> You can change your program name while it is running by modifying the value stored in $ 0</p>
        <p id="p_14153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14153"> The User ID: $&lt; and $&gt;</p>
        <p id="p_14154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14154"> The $ &lt; and $ &gt; variables contain, respectively, the real user ID and effective user ID for the program. The real user ID is the ID under which the user of the program logged in. The effective user ID is the ID</p>
        <p id="p_14155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14155"> associated with this particular program (which is not always the same as the real user ID).</p>
        <p id="p_14156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14156"> NOTE</p>
        <p id="p_14157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14157"> If you are not running your Perl program on the UNIX operating system, the $ &lt; and $ &gt; variables might have no meaning. Consult your local documentation for more details</p>
        <p id="p_14158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14158"> Listing 17.4 uses the real user ID to determine the user name of the person running the program.</p>
        <p id="p_14159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14159"> Type</p>
        <p id="p_14160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14160"> Listing 17.4. A program that uses the $&lt; variable.</p>
        <p id="p_14161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14161"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14162"> 2  :</p>
        <p id="p_14163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14163"> 3: ($username) = getpwuid($&lt;);</p>
        <p id="p_14164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14164"> 4: print ("Hello, $username!\n");</p>
        <p id="p_14165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14165"> Output</p>
        <p id="p_14166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14166"> $ programl7_4 Hello, dave!</p>
        <p id="p_14167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14167"> $</p>
        <p id="p_14168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14168"> The $ &lt; variable contains the real user ID, which is the login ID of the person running this program. Line 3 passes this user ID to  getpwuid,  which retrieves the password file entry corresponding to this user ID. The user name is the first element in this password file, and it is stored in the scalar variable $username.  Line 4 then prints this user name.</p>
        <p id="p_14169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14169"> Analysis</p>
        <p id="p_14170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14170"> NOTE</p>
        <p id="p_14171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14171"> On certain UNIX machines, you can assign $&lt; to $&gt; (set the effective user ID to be the real user ID) or vice versa. If you have superuser privileges, you can set $ &lt; or $ &gt; to any defined user ID</p>
        <p id="p_14172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14172"> The Group ID: $ ( and $)</p>
        <p id="p_14173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14173"> The $ ( and $) variables define the real group ID and the effective group ID for this program. The real group ID is the group to which the real user ID (stored in the variable $&lt;) belongs; the effective group ID is the group to which the effective user ID (stored in the variable $&gt;) belongs.</p>
        <p id="p_14174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14174"> If your system enables users to be in more than one group at a time, $ ( and $) contain a list of group IDs, with each pair of group IDs being separated by spaces. You can convert this into an array by calling split.</p>
        <p id="p_14175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14175"> Normally, you can only assign $ ( to $), and vice versa. If you are the superuser, you can set $ ( or $) to any defined group ID.</p>
        <p id="p_14176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14176"> NOTE</p>
        <p id="p_14177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14177"> $ ( and $) might not have any useful meaning if you are running Perl on a machine running an operating system other than UNIX</p>
        <p id="p_14178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14178"> The Version Number: $]</p>
        <p id="p_14179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14179"> The $ ] system variable contains the current version number. You can use this variable to ensure that the Perl on which you are running this program is the right version of Perl (or is a version that can run your program).</p>
        <p id="p_14180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14180"> Normally, $ ] contains a character string similar to this:</p>
        <p id="p_14181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14181"> $RCSfile: perl.c,v $$Revision: 4.0.1.8 $$Date: 1993/02/05 19:39:30 $</p>
        <p id="p_14182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14182"> Patch level: 36</p>
        <p id="p_14183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14183"> The useful parts of this string are the revision number and the patch level. The first part of the revision number indicates that this is version 4 of Perl. The version number and the patch level are often combined; in this notation, this is version 4.036 of Perl.</p>
        <p id="p_14184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14184"> You can use the pattern-matching operator to extract the useful information from $ ]. Listing 17.5 shows one way to do it.</p>
        <p id="p_14185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14185"> Type</p>
        <p id="p_14186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14185"/>
        <p id="p_14187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14187"> Listing 17.5. A program that extracts information from the $] variable.</p>
        <p id="p_14188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14188"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14189"> 2  :</p>
        <p id="p_14190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14190"> 3: $] =~ /Revision:  ([0-9.]+)/;</p>
        <p id="p_14191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14191"> 4:  $revision = $1;</p>
        <p id="p_14192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14192"> 5: $] =~ /Patch level:  ([0-9]+)/;</p>
        <p id="p_14193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14193"> 6:  $patchlevel = $1;</p>
        <p id="p_14194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14194"> 7: print ("revision $revision, patch level $patchlevel\n");</p>
        <p id="p_14195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14195"> Output</p>
        <p id="p_14196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14196"> $ programl7_5</p>
        <p id="p_14197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14197"> revision  4.0.1.8,  patch level 36 $</p>
        <p id="p_14198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14198"> This program just extracts the revision and patch level from $ ] using the pattern-matching operator. The built-in system variable $ 1, described later today, is defined when a pattern is matched. It contains the substring that appears in the first subpattern enclosed in parentheses. In line 3, the first subpattern enclosed in parentheses is [  0-9  . ] +. This subpattern matches one or more digits mixed with decimal points, and so it matches 4. 0 .1.8.  This means that 4. 0 .1.8  is assigned to $ 1 by line 3 and is assigned to $  revision  by line 4.</p>
        <p id="p_14199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14199"> Similarly, line 5 assigns 36 to $1 (because the subpattern [  0-9  ] +, which matches one or more digits, is the first subpattern enclosed in parentheses). Line 6 then assigns 36 to $patchlevel.</p>
        <p id="p_14200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14200"> Analysis</p>
        <p id="p_14201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14201"> Warning</p>
        <p id="p_14202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14202"> On some machines, the value contained in $ ] might be completely different from the value used in this example. If you are not sure whether $ ] has a useful value, write a little program that just prints $ ]. If this program prints something useful, you'll know that you can run programs that compare $ ] with an expected value</p>
        <p id="p_14203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14203"> The Input Line Separator: $ /</p>
        <p id="p_14204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14204"> When the Perl interpreter is told to read a line of input from a file, it usually reads characters until it reads a newline character. The newline character can be thought of as an input line separator; it indicates the end of a particular line.</p>
        <p id="p_14205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14205"> The system variable $/ contains the current input line separator. To change the input line separator, change the value of $ /. The $ / variable can be more than one character long to handle the case in which lines are separated by more than one character. If you set $ / to the null character, the Perl interpreter assumes that the input line separator is two newline characters.</p>
        <p id="p_14206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14206"> Listing 17.6 shows how changing $/ can affect your program.</p>
        <p id="p_14207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14207"> Type</p>
        <p id="p_14208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14208"> Listing 17.6. A program that changes the value of $/.</p>
        <p id="p_14209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14209"> Output</p>
        <p id="p_14210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14210"> $ program!7_6</p>
        <p id="p_14211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14211"> Here is some  test  input: here is the end.</p>
        <p id="p_14212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14212"> Here is some test input:</p>
        <p id="p_14213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14213"> $</p>
        <p id="p_14214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14214"> Analysis</p>
        <p id="p_14215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14215"> I Line 3 sets the value of $ / to a colon. This means that when line 4 reads from the standard input file, it reads until it sees a colon. As a consequence,  $line  contains the following character string:</p>
        <p id="p_14216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14216"> Here is some test input:</p>
        <p id="p_14217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14217"> Note that the colon is included as part of the input line (just as, in the normal case, the trailing newline character is included as part of the line).</p>
        <p id="p_14218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14218"> Warning</p>
        <p id="p_14219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14219"> The - 0 (zero, not the letter O) switch sets the value of $ /. If you change the value of $ / in your program, the value specified by - 0 will be thrown away.</p>
        <p id="p_14220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14220"> To temporarily change the value of $ / and then restore it to the value specified by -0, save the current value of $ / in another variable before changing it.</p>
        <p id="p_14221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14221"> For more information on -0, refer to  Day  16 ,  "Command-Line Options.</p>
        <p id="p_14222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14222"> The Output Line Separator: $</p>
        <p id="p_14223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14223"> The system variable $ \ contains the current output line separator. This is a character or sequence of characters that is automatically printed after every call to  print.</p>
        <p id="p_14224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14224"> By default, $ \ is the null character, which indicates that no output line separator is to be printed. Listing 17.7 shows how you can set an output line separator.</p>
        <p id="p_14225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14225"> Warning</p>
        <p id="p_14226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14226"> Listing 17.7.  A  program that uses the $ \ variable.</p>
        <p id="p_14227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14227"> #!/usr/local/bin/perl</p>
        <p id="p_14228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14228"> 1:</p>
        <p id="p_14229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14229"> 2  :</p>
        <p id="p_14230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14230"> 3: $\ = "\n" ;</p>
        <p id="p_14231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14231"> 4:  print ("Here   is   one  line.");</p>
        <p id="p_14232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14232"> 5:  print ("Here is another  line.");</p>
        <p id="p_14233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14233"> Output</p>
        <p id="p_14234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14234"> $ programl7_7 Here is one line.</p>
        <p id="p_14235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14235"> Here is another line. $</p>
        <p id="p_14236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14236"> Analysis</p>
        <p id="p_14237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14237"> I Line 3 sets the output line separator to the newline character. This means that a list passed to a subsequent  print  statement always appears on its own output line. Lines 4 and 5 now no longer need to include a newline character as the last character in the line.</p>
        <p id="p_14238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14238"> Warning</p>
        <p id="p_14239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14239"> The -1 option sets the value of $ \ . If you change $ \ in your program without saving it first, the value supplied with -1 will be lost. See  Day 16  for more information on the -1 option</p>
        <p id="p_14240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14240"> The Output Field Separator: $,</p>
        <p id="p_14241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14241"> The $, variable contains the character or sequence of characters to be printed between elements when print  is called. For example, in the following statement the Perl interpreter first writes the contents of  $a:</p>
        <p id="p_14242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14242"> print  ($a, $b);</p>
        <p id="p_14243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14243"> It then writes the contents of $, and then finally, the contents of  $b.</p>
        <p id="p_14244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14244"> Normally, the $, variable is initialized to the null character, which means that the elements of a  print statement are printed next to one another. Listing 17.8 is a program that sets $, before calling  print.</p>
        <p id="p_14245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14245"> Type</p>
        <p id="p_14246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14246"> Listing 17.8.  A  program that uses the $, variable.</p>
        <p id="p_14247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14247"> Output</p>
        <p id="p_14248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14248"> $ programl7_8 hello there $</p>
        <p id="p_14249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14249"> Analysis</p>
        <p id="p_14250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14250"> I Line 5 sets the value of $, to a space. Consequently, line 7 prints a space after printing  $a  and before printing  $b.</p>
        <p id="p_14251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14251"> Note that $\, the default output separator, is set to the newline character. This setting ensures that the terminating newline character immediately follows  $b.  By contrast, the following statement prints a space before printing the trailing newline character:</p>
        <p id="p_14252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14252"> print ($a, $b,  "\n");</p>
        <p id="p_14253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14253"> NOTE</p>
        <p id="p_14254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14254"> Here's another way to print the newline immediately after the final element that doesn't involve setting $ \:</p>
        <p id="p_14255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14255"> print ($a, $b . "\n");</p>
        <p id="p_14256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14256"> Here, the trailing newline character is part of the second element being printed. Because  $b  and  \n  are part of the same element, no space is printed between them</p>
        <p id="p_14257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14257"> The Array Element Separator: $ "</p>
        <p id="p_14258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14258"> Normally, if an array is printed inside a string, the elements of the array are separated by a single space. For example:</p>
        <p id="p_14259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14259"> @array = ("This", "is", "a", "list");</p>
        <p id="p_14260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14260"> print ( "@array\n");</p>
        <p id="p_14261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14261"> Here, the  print  statement prints</p>
        <p id="p_14262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14262"> This is a list</p>
        <p id="p_14263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14263"> A space is printed between each pair of array elements.</p>
        <p id="p_14264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14264"> The built-in system variable that controls this situation is the $ " variable. By default, $ " contains a space. Listing 17.9 shows how you can control your array output by changing the value of $ ".</p>
        <p id="p_14265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14265"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14266"> 2 :</p>
        <p id="p_14267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14267"> 2 • i; n  =  n . . H .</p>
        <p id="p_14268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14268"> 4: @array = ("This", "is", "a", "list");</p>
        <p id="p_14269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14269"> 5:  print  ("@array\n");</p>
        <p id="p_14270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14270"> Output</p>
        <p id="p_14271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14271"> $  programl7_9 This::is::a::list $</p>
        <p id="p_14272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14272"> Analysis</p>
        <p id="p_14273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14273"> Line 3 sets the array element separator to : : (two colons). Array element separators, like other separators you can define, can be more than one character long.</p>
        <p id="p_14274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14274"> Line  5  prints the contents of @  array.  Each pair of elements is separated by the value stored in $ ", which is two colons.</p>
        <p id="p_14275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14275"> NOTE</p>
        <p id="p_14276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14276"> The $ " variable affects only entire arrays printed inside strings. If you print two variables together in a string, as in</p>
        <p id="p_14277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14277"> print  ("$a$b\n");</p>
        <p id="p_14278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14278"> the contents of the two variables are printed with nothing separating them regardless of the value of $ ".</p>
        <p id="p_14279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14279"> To change how arrays are printed outside strings, use $\, described earlier today</p>
        <p id="p_14280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14280"> The Number Output Format: $#</p>
        <p id="p_14281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14281"> By default, when the  print  function prints a number, it prints it as a 20-digit floating point number in compact format. This means that the following statements are identical if the value stored in  $x  is a number:</p>
        <p id="p_14282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14282"> print   ($x);</p>
        <p id="p_14283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14283"> printf  ("%.20g", $x) ;</p>
        <p id="p_14284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14284"> To change the default format that  print  uses to print numbers, change the value of the $# variable. For example, to specify only 15 digits of precision, use this statement:</p>
        <p id="p_14285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14285"> $#</p>
        <p id="p_14286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14286"> 15 g " ;</p>
        <p id="p_14287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14287"> This value must be a floating-point field specifier, as used in  printf  and  sprintf.</p>
        <p id="p_14288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14288"> NOTE</p>
        <p id="p_14289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14289"> The $ # variable does not affect values that are not numbers and has no effect on the  printf, write,  and  sprintf  functions</p>
        <p id="p_14290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14290"> For more information on the field specifiers you can use as the default value in $#, see "Formatting Output Using  printf"  on  Day 11 ,  "Formatting Your Output."</p>
        <p id="p_14291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14291"> NOTE</p>
        <p id="p_14292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14292"> The $# variable is deprecated in Perl 5. This means that although $# is supported, it is not recommended for use and might be removed from future versions of Perl</p>
        <p id="p_14293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14293"> The eval Error Message: $@</p>
        <p id="p_14294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14294"> If a statement executed by the  eval  function contains an error, or an error occurs during the execution of the statement, the error message is stored in the system variable $@. The program that called  eval  can decide either to print the error message or to perform some other action.</p>
        <p id="p_14295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14295"> For example, the statement</p>
        <p id="p_14296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14296"> eval ("This is not a perl statement");</p>
        <p id="p_14297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14297"> assigns the following string to $@:</p>
        <p id="p_14298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14298"> syntax error in file (eval) at line  1 ,  next 2 tokens "This is</p>
        <p id="p_14299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14299"> The $@ variable also returns the error generated by a call to  die  inside an  eval.  The following statement</p>
        <p id="p_14300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14300"> assigns this string to $@:</p>
        <p id="p_14301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14301"> eval ("die (V'nothing happened\")"); nothing happened at (eval) line 1.</p>
        <p id="p_14302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14302"> NOTE</p>
        <p id="p_14303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14303"> The $ @ variable also returns error messages generated by the require  function. See  Day  19 ,  "Object-Oriented Programming in Perl," for more information on  require</p>
        <p id="p_14304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14304"> The System Error Code: $?</p>
        <p id="p_14305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14305"> The $ ? variable returns the error status generated by calls to the  system  function or by calls to functions enclosed in back quotes, as in the following:</p>
        <p id="p_14306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14306"> $username = 'hostname';</p>
        <p id="p_14307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14307"> The error status stored in $ ? consists of two parts:</p>
        <p id="p_14308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14308"> • The exit value (return code) of the process called by  system  or specified in back quotes</p>
        <p id="p_14309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14309"> • A status field that indicates how the process was terminated, if it terminated abnormally</p>
        <p id="p_14310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14310"> The value stored in $ ? is a 16-bit integer. The upper eight bits are the exit value, and the lower eight bits are the status field. To retrieve the exit value, use the &gt;&gt; operator to shift the eight bits to the right:</p>
        <p id="p_14311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14311"> $retcode = $? &gt;&gt; 8;</p>
        <p id="p_14312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14312"> For more information on the status field, refer to the online manual page for the  wait  function or to the file /usr/include/sys/wait.h.  For more information on commands in back quotes, refer to  Day 20 , "Miscellaneous Features of Perl."</p>
        <p id="p_14313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14313"> The System Error Message: $ !</p>
        <p id="p_14314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14314"> Some Perl library functions call system library functions. If a system library function generates an error, the error code generated by the function is assigned to the $ ! variable. The Perl library functions that call system library functions vary from machine to machine.</p>
        <p id="p_14315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14315"> NOTE</p>
        <p id="p_14316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14316"> The $ ! variable in Perl is equivalent to the  errno  variable in the C programming language</p>
        <p id="p_14317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14317"> The Current Line Number: $ .</p>
        <p id="p_14318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14318"> The $ . variable contains the line number of the last line read from an input file. If more than one input file is being read, $ . contains the line number of the last input file read. Listing 17.10 shows how $ . works.</p>
        <p id="p_14319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14319"> Type</p>
        <p id="p_14320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14320"> Listing 17.10. A program that uses the $ . variable.</p>
        <p id="p_14321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14321"> Output</p>
        <p id="p_14322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14322"> 2</p>
        <p id="p_14323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14323"> $</p>
        <p id="p_14324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14324"> Analysis</p>
        <p id="p_14325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14325"> I When line 9 is executed, the input file  FI LEI  has had two lines read from it. This means that $ . contains the value  2.  Line 10 then reads from FILE 2.  Because it reads the first line from this file, $ . now has the value 1. When line 12 reads a third line from  FI LEI,  $ . is set to the value 3. The Perl interpreter remembers that two lines have already been read from FILE1.</p>
        <p id="p_14326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14326"> NOTE</p>
        <p id="p_14327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14327"> If the program is reading using &lt;&gt;, which reads from the files listed on the command line, $ . treats the input files as if they are one continuous file. The line number is not reset when a new input file is opened</p>
        <p id="p_14328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14328"> You can use  eof  to test whether a particular file has ended, and then reset $ . yourself (by assigning zero to it) before reading from the next file.</p>
        <p id="p_14329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14329"> Multiline Matching: $*</p>
        <p id="p_14330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14330"> Normally, the operators that match patterns (the pattern-matching operator and the substitution operator) assume that the character string being searched is a single line of text. If the character string being searched consists of more than one line of text (in other words, it contains newline characters), set the system variable $* to 1.</p>
        <p id="p_14331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14331"> NOTE</p>
        <p id="p_14332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14332"> By default, $ * is set to 0, which indicates that multiline pattern matches are not required</p>
        <p id="p_14333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14333"> Warning</p>
        <p id="p_14334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14334"> The $ * variable is deprecated in Perl 5. If you are running Perl 5, use the m pattern-matching option when matching in a multiple-line string. See  Day  7 ,  "Pattern Matching," for more details on this option</p>
        <p id="p_14335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14335"> The First Array Subscript: $ [</p>
        <p id="p_14336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14336"> Normally, when a program references the first element of an array, it does so by specifying the subscript 0. For example:</p>
        <p id="p_14337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14337"> @myarray = ("Here", "is", "a", "list");</p>
        <p id="p_14338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14338"> $here = $myarray[0];</p>
        <p id="p_14339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14339"> The array element  $myarray [ 0  ] contains the string  Here,  which is assigned to  $here.</p>
        <p id="p_14340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14340"> If you are not comfortable with using 0 as the subscript for the first element of an array, you can change this setting by changing the value of the $ [ variable. This variable indicates which value is to be used as the subscript for the first array element.</p>
        <p id="p_14341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14341"> Here is the preceding example, modified to use 1 as the first array element subscript:</p>
        <p id="p_14342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14342"> $[ = l;</p>
        <p id="p_14343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14343"> @myarray = ("Here", "is", "a", "list");</p>
        <p id="p_14344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14344"> $here = $myarray[l];</p>
        <p id="p_14345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14345"> In this case, the subscript 1 now references the first array element. This means that  $here  is assigned Here,  as before.</p>
        <p id="p_14346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14346"> TIP</p>
        <p id="p_14347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14347"> Don't change the value of $ [. It is too easy for a casual reader of your program to forget that the subscript 0 no longer references the first element of the array. Besides, using 0 as the subscript for the first element is standard practice in many programming languages, including C and C++</p>
        <p id="p_14348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14348"> NOTE</p>
        <p id="p_14349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14349"> $7</p>
        <p id="p_14350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14350"> is deprecated in Perl 5</p>
        <p id="p_14351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14351"> Multidimensional Associative Arrays and the $; Variable</p>
        <p id="p_14352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14352"> So far, all the arrays you've seen have been one-dimensional arrays, which are arrays in which each array element is referenced by only one subscript. For example, the following statement uses the subscript  f oo  to access an element of the associative array named  %array:</p>
        <p id="p_14353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14353"> $myvar  = $array{"foo"};</p>
        <p id="p_14354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14354"> Perl does not support multidimensional arrays directly. The following statement is not a legal Perl statement:</p>
        <p id="p_14355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14355"> $myvar  = $array{"foo"}{"bar"};</p>
        <p id="p_14356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14356"> However, Perl enables you to simulate a multidimensional associative array using the built-in system variable $;.</p>
        <p id="p_14357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14357"> Here is an example of a statement that accesses a (simulated) multidimensional array:</p>
        <p id="p_14358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14358"> $myvar  = $array{"foo","bar"};</p>
        <p id="p_14359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14359"> When the Perl interpreter sees this statement, it converts it to this:</p>
        <p id="p_14360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14360"> $myvar  = $array{"foo" . $; . "bar"};</p>
        <p id="p_14361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14361"> The system variable $; serves as a subscript separator. It automatically replaces any comma that is separating two array subscripts.</p>
        <p id="p_14362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14362"> Here is another example of two equivalent statements:</p>
        <p id="p_14363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14363"> $myvar  = $array{"si", 4, "hi there"};</p>
        <p id="p_14364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14364"> $myvar  = $array{"si".$; .4 . $; . "hi there"};</p>
        <p id="p_14365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14365"> The second statement shows how the value of the $; variable is inserted into the array subscript.</p>
        <p id="p_14366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14366"> By default, the value of $; is \ 034  (the Ctrl+\ character). You can define $; to be any value you want. Listing 17.11 is an example of a program that sets $;.</p>
        <p id="p_14367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14367"> Listing 17.11.  A  program that uses the $; variable.</p>
        <p id="p_14368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14368"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14369"> 2  :</p>
        <p id="p_14370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14370"> 3 : $ ; = " : : " ;</p>
        <p id="p_14371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14371"> 4: $array{"hello","there"} = 46;</p>
        <p id="p_14372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14372"> 5: $testl = $array{"hello","there"};</p>
        <p id="p_14373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14373"> 6: $test2 = $array{"hello::there"};</p>
        <p id="p_14374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14374"> 7: print ("$testl $test2\n");</p>
        <p id="p_14375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14375"> $ programl7_ll 46 46 $</p>
        <p id="p_14376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14376"> 3  |</p>
        <p id="p_14377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14377"> (Line 3 sets $; to the string : :. As a consequence, the subscript  "hello" , "there"  in lines 4 and 5 is really  hello : : there  because the Perl interpreter replaces the comma with the value of $;.</p>
        <p id="p_14378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14378"> Line 7 shows that both  "hello" , "there"  and  hello : : there  refer to the same element of the associative array.</p>
        <p id="p_14379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14379"> If you set $;, be careful not to set it to a character that you are actually using in a subscript. For example, if you set $; to : :, the following statements reference the same element of the array:</p>
        <p id="p_14380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14380"> $array{"a::b",  "c"}  = 1;</p>
        <p id="p_14381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14381"> $array{"a", "b::c"} = 2;</p>
        <p id="p_14382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14382"> In each case, the Perl interpreter replaces the comma with : :, producing the subscript  a: : b: : c</p>
        <p id="p_14383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14383"> The Word-Break Specifier: $ :</p>
        <p id="p_14384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14384"> On  Day 11   you learned how to format your output using print formats and the  write  statement. Each print format contains one or more value fields that specify how output is to appear on the page.</p>
        <p id="p_14385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14385"> If a value field in a print format begins with the  A  character, the Perl interpreter puts a word in the value field only if there is room enough for the entire word. For example, in the following program (a duplicate of Listing 11.9),</p>
        <p id="p_14386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14386"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14387"> 2  :</p>
        <p id="p_14388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14388"> 3: $string = "Here\nis an  unbalanced line  of\ntext.\n";</p>
        <p id="p_14389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14389"> 4: $~ =  "OUTLINE";</p>
        <p id="p_14390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14390"> 5: write;</p>
        <p id="p_14391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14391"> 6 :</p>
        <p id="p_14392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14392"> 7:  format OUTLINE =</p>
        <p id="p_14393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14393"> 9: $string</p>
        <p id="p_14394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14394"> 10 : .</p>
        <p id="p_14395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14395"> the call to  write  uses the  OUTLINE  print format to write the following to the screen:</p>
        <p id="p_14396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14396"> Here is an unbalanced line</p>
        <p id="p_14397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14397"> Note that the word  of  is not printed because it cannot fit into the  OUTLINE  value field.</p>
        <p id="p_14398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14398"> To determine whether a word can fit in a value field, the Perl interpreter counts the number of characters between the next character to be formatted and the next word-break character. A  word-break character  is one that denotes either the end of a word or a place where a word can be split into two parts.</p>
        <p id="p_14399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14399"> By default, the legal word-break characters in Perl are the space character, the newline character, and the -(hyphen) character. The acceptable word break characters are stored in the system variable $ :.</p>
        <p id="p_14400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14400"> To change the list of acceptable word-break characters, change the value of $ :. For example, to ensure that all hyphenated words are in the same line of formatted output, define $ : as shown here:</p>
        <p id="p_14401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14401"> $ : = " \ n" ;</p>
        <p id="p_14402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14402"> Now only the space and newline characters are legal word-break characters.</p>
        <p id="p_14403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14403"> NOTE</p>
        <p id="p_14404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14404"> Normally, the tab character is not a word-break character. To allow lines to be broken on tabs, add the tab character to the list specified by the $ : variable:</p>
        <p id="p_14405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14405"> $ :</p>
        <p id="p_14406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14406"> \t\n-"</p>
        <p id="p_14407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14407"> The Perl Process ID: $$</p>
        <p id="p_14408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14408"> The $ $ system variable contains the process ID for the Perl interpreter itself. This is also the process ID for your program.</p>
        <p id="p_14409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14409"> The Current Filename:  $ARGV</p>
        <p id="p_14410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14410"> When you use the &lt;&gt; operator, the Perl interpreter reads input from each file named on the command line. For example, suppose that you are executing the program  myprog  as shown here:</p>
        <p id="p_14411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14411"> $ myprog  testl test2 test3</p>
        <p id="p_14412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14412"> In  myprog,   the first occurrence of the &lt;&gt; operator reads from  testl.  Subsequent occurrences of &lt;&gt; continue reading from  testl  until it is exhausted; at this point, &lt;&gt; reads from  test 2.  This process continues until all the input files have been read.</p>
        <p id="p_14413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14413"> On  Day 6 ,  "Reading from and Writing to Files," you learned that the @  ARGV  array lists the elements of the command line and that the first element of  @ARGV  is removed when the &lt;&gt; operator reads a line.  (@ARGV also is discussed later today.)</p>
        <p id="p_14414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14414"> When the &lt;&gt; operator reads from a file for the first time, it assigns the name of the file to the  $ARGV  system variable. This enables you to keep track of what file is currently being read. Listing 17.12 shows how you can use  $ARGV.</p>
        <p id="p_14415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14415"> Type</p>
        <p id="p_14416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14416"> Listing 17.12.  A  simple file-searching program using $ARGV.</p>
        <p id="p_14417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14417"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14418"> 2  :</p>
        <p id="p_14419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14419"> 3: print ("Enter the search pattern:\n");</p>
        <p id="p_14420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14420"> 4: $ string = &lt;STDIN&gt;;</p>
        <p id="p_14421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14421"> 5: chop ($string);</p>
        <p id="p_14422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14422"> 6: while ($line = &lt;&gt;) {</p>
        <p id="p_14423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14423"> 7: if ($line =~ /$string/) {</p>
        <p id="p_14424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14424"> 8: print ("$ARGV:$line");</p>
        <p id="p_14425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14425"> 9: }</p>
        <p id="p_14426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14426"> 10 : }</p>
        <p id="p_14427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14427"> Output</p>
        <p id="p_14428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14428"> $ programl7_12 filel file2 file3 Enter the string to search: the</p>
        <p id="p_14429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14429"> filel:This line contains the word "the".</p>
        <p id="p_14430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14430"> $</p>
        <p id="p_14431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14431"> Analysis</p>
        <p id="p_14432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14432"> This program reads each line of the input files supplied on the command line. If a line contains the pattern specified by $  st r ing,  line 8 prints the name of the file and then the line itself. Note that the pattern in $  string  can contain special pattern characters.</p>
        <p id="p_14433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14433"> NOTE</p>
        <p id="p_14434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14434"> If &lt;&gt; is reading from the standard input file (which occurs when you have not specified any input files on the command line),  $ARGV contains the string - (a single hyphen)</p>
        <p id="p_14435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14435"> The Write Accumulator:  $ A A</p>
        <p id="p_14436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14436"> The $  A A  variable is used by  write  to store formatted lines to be printed. The contents of $  A A  are erased after the line is printed.</p>
        <p id="p_14437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14437"> This variable is defined only in Perl 5.</p>
        <p id="p_14438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14438"> The Internal Debugging Value:  $ A D</p>
        <p id="p_14439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14439"> The $  A D  variable displays the current internal debugging value. This variable is defined only when the  -D switch has been specified and when your Perl interpreter has been compiled with debugging included.</p>
        <p id="p_14440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14440"> See your online Perl documentation for more details on debugging Perl. (Unless you are using an experimental version of Perl, you are not likely to need to debug it.)</p>
        <p id="p_14441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14441"> The System File Flag:  $ A F</p>
        <p id="p_14442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14442"> The $  A F  variable controls whether files are to be treated as system files. Its value is the largest UNIX file descriptor that is treated as a system file.</p>
        <p id="p_14443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14443"> Normally, only  STDIN, STDOUT,  and  STDERR  are treated as system files, and the value assigned to $  A F  is 2. Unless you are on a UNIX machine, are familiar with file descriptors, and want to do something exotic with them, you are not likely to need to use the $  A F  system variable.</p>
        <p id="p_14444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14444"> Controlling File Editing Using  $ A I</p>
        <p id="p_14445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14445"> The $  A I  variable is set to a nonzero value by the Perl interpreter when you specify the  -i  option (which edits files as they are read by the &lt;&gt; operator).</p>
        <p id="p_14446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14446"> The following statement turns off the editing of files being read by &lt;&gt;:</p>
        <p id="p_14447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14447"> undef  ($ A I);</p>
        <p id="p_14448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14448"> When $  A I is undefined, the next input file is opened for reading, and the standard output file is no longer changed.</p>
        <p id="p_14449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14449"> Don't</p>
        <p id="p_14450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14450"> DO  open the files for input and output yourself if your program wants to edit some of its input files and not others; this process is easier to follow.</p>
        <p id="p_14451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14451"> DON’T  use $  A I  if you are reading files using the  -n  or  -p  option unless you really know what you are doing, because you are not likely to get the behavior you expect. If  -i  has modified the default output file, undefining $  A I does not automatically set the default output file to  STDOUT</p>
        <p id="p_14452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14452"> The Format Form-Feed Character:  $ A L</p>
        <p id="p_14453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14453"> The $  A   L  variable contains the character or characters written out whenever a print format wants to start a new page. The default value is \ f, the form-feed character.</p>
        <p id="p_14454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14454"> Controlling Debugging:  $ A P</p>
        <p id="p_14455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14455"> The $  A P  variable is used by the Perl debugger. When this variable is set to zero, debugging is turned off.</p>
        <p id="p_14456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14456"> You normally won't need to use $  A P  yourself, unless you want to specify that a certain chunk of code does not need to be debugged.</p>
        <p id="p_14457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14457"> The Program Start Time:  $ A T</p>
        <p id="p_14458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14458"> The $  A T  variable contains the time at which your program began running. This time is in the same format as is returned by the  time  function: the number of seconds since January 1, 1970.</p>
        <p id="p_14459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14459"> The following statement sets the file-access and -modification times of the file  test  1 to the time stored in</p>
        <p id="p_14460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14460"> $  A  T:</p>
        <p id="p_14461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14461"> utime  ($ A T, $ A T, "testl");</p>
        <p id="p_14462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14462"> For more information on the  time  and  utime  functions, refer to  Day  12 ,  "Working with the File System."</p>
        <p id="p_14463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14463"> The time format used by $  A   T  is also the same as that used by the file test operators  -A, -C,  and  -M</p>
        <p id="p_14464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14464"> NOTE</p>
        <p id="p_14465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14465"> Suppressing Warning Messages:  $ A W</p>
        <p id="p_14466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14466"> The $  A W  system variable controls whether warning messages are to be displayed. Normally, $  A W  is set to a nonzero value only when the  -w  option is specified.</p>
        <p id="p_14467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14467"> You can set $  A W  to zero to turn off warnings inside your program. This capability is useful if your program contains statements that generate warnings you want to ignore (because you know that your statements are correct). For example:</p>
        <p id="p_14468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14468"> $ A W =0; # turn off warning messages</p>
        <p id="p_14469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14469"> # code that generates warnings goes here $ A W =1; # turn warning messages back on</p>
        <p id="p_14470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14470"> Warning</p>
        <p id="p_14471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14471"> Some warnings are printed before program execution starts (for example, warnings of possible typos). You cannot turn off these warnings by setting  $ A W  to zero</p>
        <p id="p_14472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14472"> The $  A X  Variable</p>
        <p id="p_14473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14473"> The $  A X  variable displays the first word of the command line you used to start this program. If you started this program by entering its name, the name of the program appears in $  A X.  If you used the  perl  command to start this program, $  A X  contains  perl.</p>
        <p id="p_14474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14474"> The following statement checks to see whether you started this program with the command  perl:</p>
        <p id="p_14475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14475"> if ($ A X ne "perl") {</p>
        <p id="p_14476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14476"> print ("You did not use the 'perl' command "); print ("to start this program.\n");</p>
        <p id="p_14477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14477"> }</p>
        <p id="p_14478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14478"> Pattern System Variables</p>
        <p id="p_14479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14479"> The system variables you have seen so far are all defined throughout your program. The following system</p>
        <p id="p_14480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14480"> variables are defined only in the current block of statements you are running. (A  block  of statements is any group of statements enclosed in the brace characters { and }.) These  pattern system variables  are set by the pattern-matching operator and the other operators that use patterns (such as, for example, the substitution operator). Many of these pattern system variables were first introduced on  Day  7 .</p>
        <p id="p_14481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14481"> TIP</p>
        <p id="p_14482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14482"> Even though the pattern system variables are defined only inside a particular block of statements, your programs should not take advantage of that fact. The safest way to use the pattern-matching variables is to assign any variable that you might need to a scalar variable of your own</p>
        <p id="p_14483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14483"> Retrieving Matched Subpatterns</p>
        <p id="p_14484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14484"> When you specify a pattern for the pattern-matching or substitution operator, you can enclose parts of the pattern in parentheses. For example, the following pattern encloses the subpattern  \d+  in parentheses. (The parentheses themselves are not part of the pattern.)</p>
        <p id="p_14485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14485"> /(\d+)\ . /</p>
        <p id="p_14486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14486"> This subpattern matches one or more digits.</p>
        <p id="p_14487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14487"> After a pattern has been matched, the system variables $1, $2, and so on match the subpatterns enclosed in parentheses. For example, suppose that the following pattern is successfully matched:</p>
        <p id="p_14488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14488"> /(\d+)([a-z]+)/</p>
        <p id="p_14489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14489"> In this case, the match found must consist of one or more digits followed by one or more lowercase letters. After the match has been found, $ 1 contains the sequence of one or more digits, and $2 contains the sequence of one or more lowercase letters.</p>
        <p id="p_14490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14490"> Fisting 17.13 is an example of a program that uses $1,  $2,  and  $3  to match subpatterns.</p>
        <p id="p_14491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14491"> Type</p>
        <p id="p_14492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14492"> Listing 17.13. A program that uses variables containing matched subpatterns.</p>
        <p id="p_14493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14493"> 1:  #!/usr/local/bin/perl</p>
        <p id="p_14494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14494"> $ 2 "</p>
        <p id="p_14495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14495"> {</p>
        <p id="p_14496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14496"> part</p>
        <p id="p_14497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14497"> Output</p>
        <p id="p_14498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14498"> $ programl7_13 filel</p>
        <p id="p_14499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14499"> exponent e-02</p>
        <p id="p_14500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14500"> $</p>
        <p id="p_14501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14501"> This program reads each input line and searches for floating-point numbers. Line 4 matches if a floating-point number is found. (Line 4 is a  while  statement, not an  if,  to enable the program to detect lines containing more than one floating-point number. The loop starting in line 4 iterates until no more matches are found on the line.)</p>
        <p id="p_14502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14502"> When a match is found, the first set of parentheses matches the digits before the decimal point; these digits are copied into $ 1. The second set of parentheses matches the digits after the decimal point; these matched digits are stored in  $2.  The third set of parentheses matches an optional exponent; if the exponent exists, it is stored in $ 3.</p>
        <p id="p_14503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14503"> Line 5 prints the values of $ 1 and $2 for each match. If $3 is defined, its value is printed by line 7.</p>
        <p id="p_14504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14504"> Analysis</p>
        <p id="p_14505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14505"> Don't</p>
        <p id="p_14506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14506"> DO  use $ 1, not $ 0, to retrieve the first matched subpattern. $ 0 contains the name of the program you are running.</p>
        <p id="p_14507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14507"> DON'T  confuse $1 with \1. \1, \2, and so on are defined only inside a pattern. See  Day 7 for more information on \ 1</p>
        <p id="p_14508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14508"> In patterns, parentheses are counted starting from the left. This rule tells the Perl interpreter how to handle nested parentheses:</p>
        <p id="p_14509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14509"> / (\d+  (\.)  ?\d+) /</p>
        <p id="p_14510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14510"> This pattern matches one or more digits optionally containing a decimal point. When this pattern is matched, the outer set of parentheses is considered to be the first set of parentheses; these parentheses contain the entire matched number, which is stored in $ 1.</p>
        <p id="p_14511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14511"> The inner set of parentheses is treated as the second set of parentheses because it includes the second left parenthesis seen by the pattern matcher. The variable $ 2, which contains the subpattern matched by the second set of parentheses, contains . (a period) if a decimal point is matched and the empty string if it is not.</p>
        <p id="p_14512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14512"> Retrieving the Entire Pattern: $&amp;</p>
        <p id="p_14513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14513"> When a pattern is matched successfully, the matched text string is stored in the system variable $&amp;. This is the only way to retrieve the matched pattern because the pattern matcher returns a true or false value indicating whether the pattern match is successful. (This is not strictly true, because you could enclose the entire pattern in parentheses and then check the value of $ 1; however, $ &amp; is easier to use in this case.) Listing 17.14 is a program that uses $ &amp; to count all the digits in a set of input files.</p>
        <p id="p_14514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14514"> Type</p>
        <p id="p_14515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14515"> Listing 17.14. A program that uses $&amp;.</p>
        <p id="p_14516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14516"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14517"> 2  :</p>
        <p id="p_14518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14518"> 3: while ($line = &lt;&gt;) {</p>
        <p id="p_14519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14519"> 4 :</p>
        <p id="p_14520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14520"> while ($line =~ /\d/g) {</p>
        <p id="p_14521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14521"> 5 :</p>
        <p id="p_14522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14522"> $digitcount[$&amp;]++;</p>
        <p id="p_14523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14523"> Output</p>
        <p id="p_14524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14524"> $ programl7_14 filel Totals for each digit: 0  :  11 1 :  6 2: 3 3: 1 4: 2 5 :</p>
        <p id="p_14525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14525"> 6 :  1</p>
        <p id="p_14526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14526"> 7 :</p>
        <p id="p_14527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14527"> 8  :</p>
        <p id="p_14528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14528"> 9: 1</p>
        <p id="p_14529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14529"> $</p>
        <p id="p_14530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14530"> Analysis</p>
        <p id="p_14531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14531"> This program reads one line at a time from the files specified on the command line. Line 4 matches each digit in the input line in turn; the matched digit is stored in $ &amp;.</p>
        <p id="p_14532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14532"> Line 5 takes the value of $&amp; and uses it as the subscript for the array  @digitcount.  This array keeps a count of the number of occurrences of each digit.</p>
        <p id="p_14533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14533"> When the input files have all been read, lines 9-11 print the totals for each digit.</p>
        <p id="p_14534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14534"> NOTE</p>
        <p id="p_14535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14535"> If you need the value of $ &amp; , be sure to get it before exiting the  while loop or other statement block in which the pattern is matched. (A statement block is exited when the Perl interpreter sees a } character.)</p>
        <p id="p_14536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14536"> For example, the pattern matched in line 4 cannot be accessed outside of lines 4-6 because this copy of $ &amp; is defined only in these lines. (This rule also holds true for all the other pattern system variables defined in today's lesson.)</p>
        <p id="p_14537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14537"> The best rule to follow is to either use or assign a pattern system variable immediately following the statement that matches the pattern</p>
        <p id="p_14538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14538"> Retrieving the Unmatched Text: the $ v  and $ ' Variables</p>
        <p id="p_14539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14539"> When a pattern is matched, the text of the match is stored in the system variable $ &amp;. The rest of the string stored in two other system variables:</p>
        <p id="p_14540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14540"> • The unmatched text preceding the match is stored in the $' variable.</p>
        <p id="p_14541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14541"> • The unmatched text following the match is stored in the $ ' variable.</p>
        <p id="p_14542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14542"> For example, if the Perl interpreter searches for the  /\d+/  pattern in the string  qwertyl234uiop,  it matches  12 3 4,  which is stored in $&amp;. The substring  qwerty,  which precedes the match, is stored in $'. The rest of the string,  uiop,  is stored in $ '.</p>
        <p id="p_14543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14543"> If the beginning of a text string is matched, $' is set to the empty string. Similarly, if the last character in the string is part of the match, $ ' is set to the empty string.</p>
        <p id="p_14544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14544"> The $+ Variable</p>
        <p id="p_14545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14545"> The $+ variable matches the last subpattern enclosed in parentheses. For example, when the following pattern is matched, $+ matches the digits after the decimal point:</p>
        <p id="p_14546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14546"> /(\d+)\.(\d+)/</p>
        <p id="p_14547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14547"> This variable is useful when the last part of a pattern is the only part you really need to look at.</p>
        <p id="p_14548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14548"> File System Variables</p>
        <p id="p_14549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14549"> Several system variables are associated with file variables. One copy of each file system variable is defined for each file that is referenced in your Perl program. Many of these system variables were first introduced on Day 11 .  The variables mentioned there are redefined here for your convenience.</p>
        <p id="p_14550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14550"> The Default Print Format: $~</p>
        <p id="p_14551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14551"> When the  write  statement sends formatted output to a file, it uses the value of the $~ system variable for that file to determine the print format to use.</p>
        <p id="p_14552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14552"> When a program starts running, the default value of $ ~ for each file is the same as the name of the file variable that represents the file. For example, when you write to the file represented by the file variable MYFILE,  the default value of $~ is  MYFILE.  This means that  write  normally uses the  MYFILE  print format. (For the standard output file, this default print format is named  STDOUT.)</p>
        <p id="p_14553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14553"> If you want to specify a different print format, change the value of $~ before calling the  write  function. For example, to use the print format  MYFORMAT  when writing to the standard output file, use the following code:</p>
        <p id="p_14554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14554"> select (STDOUT); # making sure you are writing to STDOUT</p>
        <p id="p_14555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14555"> $~ = "MYFORMAT"; write;</p>
        <p id="p_14556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14556"> This call to  write  uses  MYFORMAT  to format its output.</p>
        <p id="p_14557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14557"> Warning</p>
        <p id="p_14558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14558"> Remember that one copy of $~ is defined for each file variable. Therefore, the following code is incorrect:</p>
        <p id="p_14559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14559"> $~ = "MYFORMAT"; select (MYFILE); write;</p>
        <p id="p_14560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14560"> In this example, the assignment to $ ~ changes the default print format for whatever the current output file happens to be. This assignment does not affect the default print format for  MYFILE  because  MYFILE is selected after $~ is assigned. To change the default print format for MYFILE,  select it first:</p>
        <p id="p_14561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14561"> select (MYFILE);</p>
        <p id="p_14562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14562"> $~ = "MYFORMAT" ; write;</p>
        <p id="p_14563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14563"> This call to  write  now uses  MYFORMAT  to write to  MYFILE</p>
        <p id="p_14564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14564"> Specifying Page Length: $=</p>
        <p id="p_14565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14565"> The $= variable defines the page length (number of lines per page) for a particular output file. $= is normally initialized to 60, which is the value that the Perl interpreter assumes is the page length for every output file. This page length includes the lines left for page headers, and it is the length that works for most printers.</p>
        <p id="p_14566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14566"> If you are directing a particular output file to a printer with a nonstandard page length, change the value of $= for this file before writing to it:</p>
        <p id="p_14567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14567"> select ("WEIRDLENGTH"); $= = 72;</p>
        <p id="p_14568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14568"> This code sets the page length for the  WEIRDLENGTH  file to  72.</p>
        <p id="p_14569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14569"> Warning</p>
        <p id="p_14570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14570"> $= is set to 60 by default only if a page header format is defined for the page. If no page header is defined, $= is set to 9999999 because Perl assumes that you want your output to be a continuous stream.</p>
        <p id="p_14571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14571"> If you want paged output without a page header, define an empty page header for the output file</p>
        <p id="p_14572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14572"> Lines Remaining on the Page: $-</p>
        <p id="p_14573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14573"> The $ - variable associated with a particular file variable lists the number of lines left on the current page of that file. Each call to  write  subtracts the number of lines printed from $-. If  write  is called when $- is zero, a new page is started. (If $- is greater than zero, but  write  is printing more lines than the value of $-, write  starts a new page in the middle of its printing operation.)</p>
        <p id="p_14574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14574"> When a new page is started, the initial value of $- is the value stored in $=, which is the number of lines on the page.</p>
        <p id="p_14575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14575"> The program in Listing 17.15 displays the value of $-.</p>
        <p id="p_14576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14576"> Type</p>
        <p id="p_14577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14577"> Listing 17.15. A program that displays $-.</p>
        <p id="p_14578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14578"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14579"> 2  :</p>
        <p id="p_14580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14580"> 3: open (OUTFILE, "&gt;outfile");</p>
        <p id="p_14581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14581"> 4: select ("OUTFILE");</p>
        <p id="p_14582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14582"> 5: write;</p>
        <p id="p_14583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14583"> 6: print STDOUT ("lines to go before write: $-\n");</p>
        <p id="p_14584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14584"> 7: write;</p>
        <p id="p_14585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14585"> 8: print STDOUT ("lines to go after write: $-\n");</p>
        <p id="p_14586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14586"> 9: format OUTFILE =</p>
        <p id="p_14587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14587"> 10: This is a test.</p>
        <p id="p_14588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14588"> 11  : .</p>
        <p id="p_14589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14589"> 12: format OUTFILE_TOP =</p>
        <p id="p_14590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14590"> 13: This is a test.</p>
        <p id="p_14591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14591"> 14: .</p>
        <p id="p_14592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14592"> Output</p>
        <p id="p_14593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14593"> $ programl7_15</p>
        <p id="p_14594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14594"> lines to go before write: 58 lines to go after write: 57</p>
        <p id="p_14595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14595"> $</p>
        <p id="p_14596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14596"> Analysis</p>
        <p id="p_14597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14597"> Line 3 opens the output file  out file  and associates the file variable  OUTFILE  with this file. Line 4 then calls  select,  which sets the default output file to  OUTFILE.</p>
        <p id="p_14598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14598"> Line 5 calls  write,  which starts a new page. Line 6 then sends the value of $- to the standard output file, STDOUT,  by specifying  STDOUT  in the call to  print.  Note that the copy of $- printed is the copy associated with  OUTFILE,  not  STDOUT,  because  OUTFILE  is currently the default output file.</p>
        <p id="p_14599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14599"> Line  7  calls  write,  which sends a line of output to  OUTFILE  and decreases the value of $- by one. Line  8 prints this new value of $-.</p>
        <p id="p_14600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14600"> NOTE</p>
        <p id="p_14601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14601"> If you want to force your next output to appear at the beginning of a new page, you can set $- to zero yourself before calling  write.</p>
        <p id="p_14602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14602"> When a file is opened, the copy of $ - for this file is given the initial value of zero. This technique ensures that the first call to  write always starts a page (and generates the header for the page)</p>
        <p id="p_14603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14603"> The Page Header Print Format: $ A</p>
        <p id="p_14604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14604"> When  write  starts a new page, you can specify the page header that is to appear on the page. To do this, define a page header print format for the output file to which the page is to be sent.</p>
        <p id="p_14605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14605"> The system variable $  A  contains the name of the print format to be used for printing page headers. If this format is defined, page headers are printed; if it does not exist, no page headers are printed.</p>
        <p id="p_14606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14606"> By default, the copy of $  A  for a particular file is set equal to the name of the file variable plus the string _TOP.  For example, for the file represented by the file variable  MYFILE,  $  A  is given an initial value of</p>
        <p id="p_14607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14607"> MYFILE_TOP.</p>
        <p id="p_14608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14608"> To change the page header print format for a particular file, set the default output file by calling  select, and then set $  A  to the print format you want to use. For example:</p>
        <p id="p_14609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14609"> select (MYFILE);</p>
        <p id="p_14610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14610"> $ A  = "MYHEADER";</p>
        <p id="p_14611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14611"> This code changes the default output file to  MYFILE  and then changes the page header print format for MYFILE  to  MYHEADER.  As always, you must remember to  select  the file before changing $  A  because each file has its own copy of $  A .</p>
        <p id="p_14612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14612"> Buffering Output: $ |</p>
        <p id="p_14613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14613"> When you send output to a file using  print  or  write,  the operating system might not write it right away. Some systems first send the output to a special array known as a  buffer,  when the buffer becomes full, it is written all at once. This process of output buffering is usually a more efficient way to write data.</p>
        <p id="p_14614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14614"> In some circumstances, you might want to send output straight to your output file without using an intervening buffer. (For example, two processes might be sending output to the standard output file at the same time.)</p>
        <p id="p_14615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14615"> The $ | system variable indicates whether a particular file is buffered. By default, the Perl interpreter defines a buffer for each output file, and $ | is set to 0. To eliminate buffering for a particular file, select the file and then set the $ | variable to a nonzero value. For example, the following code eliminates buffering for the MYFILE  output file:</p>
        <p id="p_14616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14616"> select ("MYFILE"); $1  =  1 ;</p>
        <p id="p_14617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14617"> These statements set  MYFILE  as the default output file and then turn off buffering for it.</p>
        <p id="p_14618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14618"> Warning</p>
        <p id="p_14619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14619"> If you want to eliminate buffering for a particular file, you must set $ | before writing to the file for the first time because the operating system creates the buffer when it performs the first write operation</p>
        <p id="p_14620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14620"> The Current Page Number: $%</p>
        <p id="p_14621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14621"> Each output file opened by a Perl program has a copy of the $% variable associated with it. This variable stores the current page number. When  write  starts a new page, it adds one to the value of $ %. Each copy of $% is initialized to 0, which ensures that $% is set to 1 when the first page is printed. $% often is displayed by page header print formats.</p>
        <p id="p_14622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14622"> Array System Variables</p>
        <p id="p_14623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14623"> The system variables you've seen so far have all been scalar variables. The following sections describe the array variables that are automatically defined for use in Perl programs. All of these variables, except for the @_ variable, are global variables: their value is the same throughout a program.</p>
        <p id="p_14624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14624"> The Variable</p>
        <p id="p_14625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14625"> The variable, which is defined inside each subroutine, is a list of all the arguments passed to the subroutine.</p>
        <p id="p_14626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14626"> For example, suppose that the subroutine  my_sub  is called as shown here:</p>
        <p id="p_14627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14627"> &amp;my_sub("hello", 46, $var);</p>
        <p id="p_14628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14628"> The values  hello  and  4 6,  plus the value stored in  $var,  are combined into a three-element list. Inside my_sub,  this list is stored in @_.</p>
        <p id="p_14629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14629"> In a subroutine, the @_ array can be referenced or modified, just as with any other array variable. Most subroutines, however, assign @_ to locally defined scalar variables using the  local  function:</p>
        <p id="p_14630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14630"> sub my_sub {</p>
        <p id="p_14631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14631"> local ($argl, $arg2, $arg3) = @_;</p>
        <p id="p_14632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14632"> # more stuff goes here</p>
        <p id="p_14633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14633"> }</p>
        <p id="p_14634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14634"> Here, the  local  statement defines three local variables,  $argl, $arg2,  and  $arg3. $argl  is assigned the first element of the list stored in @_,  $arg2  is assigned the second, and  $arg3  is assigned the third.</p>
        <p id="p_14635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14635"> For more information on subroutines, refer to  Day 9 ,  "Using Subroutines."</p>
        <p id="p_14636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14636"> NOTE</p>
        <p id="p_14637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14637"> If the  shift  function is called inside a subroutine with no argument specified, the @_ variable is assumed, and its first element is removed</p>
        <p id="p_14638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14638"> The  @ARGV  Variable</p>
        <p id="p_14639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14639"> When you run a Perl program, you can specify values that are to be passed to the program by including them on the command line. For example, the following command calls the Perl program  myprog  and passes it the values  hello  and  4 6:</p>
        <p id="p_14640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14640"> $ myprog "hello" 46</p>
        <p id="p_14641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14641"> Inside the Perl program, these values are stored in a special built-in array named  @ARGV.  In this example, @ARGV  contains the list  ("hello",  46).</p>
        <p id="p_14642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14642"> Here is a simple statement that prints the values passed on the command line:</p>
        <p id="p_14643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14643"> print ("@ARGV\n");</p>
        <p id="p_14644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14644"> The  @ARGV  array also is associated with the &lt;&gt; operator. This operator treats the elements in  @ARGV  as filenames; each file named in  @ARGV  is opened and read in turn. Refer to  Day 6   for a description of the &lt;&gt; operator.</p>
        <p id="p_14645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14645"> NOTE</p>
        <p id="p_14646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14646"> If the  shift  function is called in the main body of a program (outside a subroutine) and no arguments are passed with it, the Perl interpreter assumes that the  @ARGV  array is to have its first element removed.</p>
        <p id="p_14647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14647"> The following loop assigns each element of  @ARGV,  in turn, to the variable  $var:</p>
        <p id="p_14648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14648"> while ($var = shift) {</p>
        <p id="p_14649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14649"> # stuff</p>
        <p id="p_14650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14650"> }</p>
        <p id="p_14651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14651"> The  @F  Variable</p>
        <p id="p_14652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14652"> In Perl, if you specify the  -n  or  -p  option, you can also supply the  -a  option. This option tells the Perl interpreter to break each input line into individual words (throwing away all tabs and spaces). These words are stored in the built-in array variable  @F.  After an input line has been (automatically) read, the  @F  array variable behaves like any other array variable.</p>
        <p id="p_14653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14653"> For more information on the  -a, -n,  or  -p  options, refer to  Day  16 ,  "Command-Line Options."</p>
        <p id="p_14654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14654"> NOTE</p>
        <p id="p_14655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14655"> When the  -a  option is specified and an input line is broken into words, the original input line can still be accessed because it is stored in the $_ system variable</p>
        <p id="p_14656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14656"> The  @INC  Variable</p>
        <p id="p_14657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14657"> The @  INC  array variable contains a list of directories to be searched for files requested by the  require function. This list consists of the following items, in order from first to last:</p>
        <p id="p_14658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14658"> • The directories specified by the -1 option</p>
        <p id="p_14659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14659"> • The Perl library directory, which is normally  /usr/local/bin/perl</p>
        <p id="p_14660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14660"> • The current working directory (represented by the . character)</p>
        <p id="p_14661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14661"> Like any array variable, @  INC  can be added to or modified.</p>
        <p id="p_14662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14662"> For more information on the  require  function, refer to  Day  19 .</p>
        <p id="p_14663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14663"> The %INC Variable</p>
        <p id="p_14664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14664"> The built-in associative array  %INC  lists the files requested by the  require  function that have already been found.</p>
        <p id="p_14665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14665"> When  require  finds a file, the associative array element  $INC{file}  is defined, in which  file  is the name of the file. The value of this associative array element is the location of the actual file.</p>
        <p id="p_14666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14666"> When  require  requests a file, the Perl interpreter first looks to see whether an associative array element has already been created for this file. This action ensures that the interpreter does not try to include the same code twice.</p>
        <p id="p_14667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14667"> The %ENV Variable</p>
        <p id="p_14668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14668"> The  %ENV  associative array lists the environment variables defined for this program and their values. The environment variables are the array subscripts, and the values of the variables are the values of the array elements.</p>
        <p id="p_14669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14669"> For example, the following statement assigns the value of the environment variable  TERM  to the scalar variable  $term:</p>
        <p id="p_14670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14670"> $term = $ENV{"TERM"};</p>
        <p id="p_14671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14671"> The %SIG Variable</p>
        <p id="p_14672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14672"> In the UNIX environment, processes can send signals to other processes. These signals can, for example, interrupt a running program, trigger an alarm in the program, or kill off the program.</p>
        <p id="p_14673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14673"> You can control how your program responds to signals it receives. To do this, modify the  %SIG  associative array. This array contains one element for each available signal, with the signal name serving as the subscript for the element. For example, the  INT  (interrupt) signal is represented by the $ SIG { " INT " } element.</p>
        <p id="p_14674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14674"> The value of a particular element of  %SIG  is the action that is to be performed when the signal is received. By default, the value of an array element is  DEFAULT,  which tells the program to do what it normally does when it receives this signal.</p>
        <p id="p_14675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14675"> You can override the default action for some of the signals in two ways: you can tell the program to ignore the signal, or you can define your own signal handler. (Some signals, such as  KILL,  cannot be overridden.)</p>
        <p id="p_14676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14676"> To tell the program to ignore a particular type of signal, set the value of the associative array element for this signal to  IGNORE.  For example, the following statement indicates that the program is to ignore any I NT  signals it receives:</p>
        <p id="p_14677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14677"> $SIG{"INT"}  = "IGNORE";</p>
        <p id="p_14678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14678"> If you assign any value other than  DEFAULT  or  IGNORE  to a signal array element, this value is assumed to be the name of a function that is to be executed when this signal is received. For example, the following statement tells the program to jump to the subroutine named  interrupt  when it receives an  INT  signal:</p>
        <p id="p_14679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14679"> $SIG{"INT"} = "interrupt";</p>
        <p id="p_14680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14680"> Subroutines that can be jumped to when a signal is received are called  interrupt handlers,  because signals interrupt normal program execution. Listing 17.16 is an example of a program that defines an interrupt handler.</p>
        <p id="p_14681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14681"> Type</p>
        <p id="p_14682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14682"> Listing 17.16. A program containing an interrupt handler.</p>
        <p id="p_14683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14683"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14684"> 2  :</p>
        <p id="p_14685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14685"> 3: $ SIG{"INT"} = "wakeup";</p>
        <p id="p_14686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14686"> 4: sleep();</p>
        <p id="p_14687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14687"> 5:</p>
        <p id="p_14688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14688"> 6: sub wakeup {</p>
        <p id="p_14689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14689"> 7: print ("I have woken up!\n");</p>
        <p id="p_14690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14690"> 8: exit();</p>
        <p id="p_14691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14691"> 9: }</p>
        <p id="p_14692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14692"> Output</p>
        <p id="p_14693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14693"> $ programl7_16 I have woken up! $</p>
        <p id="p_14694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14694"> Analysis</p>
        <p id="p_14695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14695"> Line 3 tells the Perl interpreter that the program is to jump to the  wakeup  subroutine when it receives the  INT  signal. Line 4 tells the program to go to sleep. Because no argument is passed to  sleep, the program will sleep until a signal wakes it up.</p>
        <p id="p_14696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14696"> To wake up the process, get the process ID using the  ps  command, and then send an  INT  signal to the process using the  kill  command. (See the manual page for  kill,  and the related documentation for signal handling, to see how to perform this task in your environment.)</p>
        <p id="p_14697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14697"> When the program receives the  INT  signal, it executes the  wakeup  subroutine. This subroutine prints the following message and then exits:</p>
        <p id="p_14698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14698"> I have woken up!</p>
        <p id="p_14699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14699"> If desired, you can use the same subroutine to handle more than one signal. The signal actually sent is passed as an argument to the called subroutine, which ensures that your subroutine can determine which signal triggered it:</p>
        <p id="p_14700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14700"> sub interrupt {</p>
        <p id="p_14701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14701"> local ($signal) = @_;</p>
        <p id="p_14702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14702"> print ("Interrupted by the $signal signal.\n");</p>
        <p id="p_14703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14703"> }</p>
        <p id="p_14704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14704"> If a subroutine exits normally, the program returns to where it was executing when it was interrupted. If a subroutine calls  exit  or  die,  the program execution is terminated.</p>
        <p id="p_14705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14705"> NOTE</p>
        <p id="p_14706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14706"> When a program continues executing after being interrupted, the element of  %SIG  corresponding to the received signal is reset to DEFAULT.  To ensure that repeated signals are trapped by your interrupt handler, redefine the appropriate element of  %SIG</p>
        <p id="p_14707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14707"> Built-In File Variables</p>
        <p id="p_14708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14708"> Perl provides several built-in file variables, most of which you have previously seen. The only file variables that have not yet been discussed are  DATA  and _ (underscore). The others are briefly described here for the sake of completeness.</p>
        <p id="p_14709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14709"> STDIN, STDOUT, and STDERR</p>
        <p id="p_14710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14710"> The file variable  STDIN  is, by default, associated with the standard input file. Using  STDIN  with the &lt;&gt; operator, as in  &lt;STDIN&gt;,  normally reads data from your keyboard. If your shell has used &lt; or some equivalent redirection operator to specify input from a file,  &lt;STDIN&gt;  reads from that file.</p>
        <p id="p_14711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14711"> The file variable  STDOUT  normally writes to the standard output file, which is usually directed to your screen. If your shell has used &gt; or the equivalent to redirect standard output to a file, writing to  STDOUT sends output to that file.</p>
        <p id="p_14712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14712"> STDERR  represents the standard error file, which is almost always directed to your screen. Writing to STDERR  ensures that you see error messages even when you have redirected the standard output file.</p>
        <p id="p_14713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14713"> You can associate  STDIN, STDOUT,  or  STDERR  with some other file using  open:</p>
        <p id="p_14714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14714"> Opening a file and associating it with  STDIN  overrides the default value of  STDIN,  which means that you can no longer read from the standard input file. Similarly, opening a file and associating it with  STDOUT  or STDERR  means that writing to that particular file variable no longer sends output to the screen.</p>
        <p id="p_14715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14715"> To associate a file variable with the standard input file after you have redirected  STDIN,  specify a filename of</p>
        <p id="p_14716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14716"> open (MYSTDIN,</p>
        <p id="p_14717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14717"> To associate a file variable with the standard output file, specify a filename of &gt;-:</p>
        <p id="p_14718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14718"> open (MYSTDOUT,</p>
        <p id="p_14719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14719"> You can, of course, specify  STDIN  with - or  STDOUT  with &gt;- to restore the original values of these file variables.</p>
        <p id="p_14720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14720"> ARGV</p>
        <p id="p_14721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14721"> ARGV  is a special file variable that is associated with the current input file being read by the &lt;&gt; operator. For example, consider the following statement:</p>
        <p id="p_14722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14722"> $line = &lt;&gt;;</p>
        <p id="p_14723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14723"> This statement reads from the current input file. Because  ARGV  represents the current input file, the preceding statement is equivalent to this:</p>
        <p id="p_14724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14724"> $line = &lt;ARGV&gt;;</p>
        <p id="p_14725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14725"> You normally will not need to access  ARGV  yourself except via the &lt;&gt; operator.</p>
        <p id="p_14726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14726"> DATA</p>
        <p id="p_14727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14727"> The  DATA  file variable is used with the_ END _special value, which can be used to indicate the end of a</p>
        <p id="p_14728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14728"> program. Reading from  DATA  reads the line after_ END _, which enables you to include a program and its</p>
        <p id="p_14729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14729"> data in the same file.</p>
        <p id="p_14730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14730"> Listing 17.17 is an example of a program that reads from  DATA.</p>
        <p id="p_14731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14731"> Listing 17.17. An example  of  the  DATA  file variable.</p>
        <p id="p_14732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14732"> 1: #!/usr/local/bin/perl</p>
        <p id="p_14733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14733"> 2  :</p>
        <p id="p_14734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14734"> 3: $line = &lt;DATA&gt;;</p>
        <p id="p_14735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14735"> 4 :</p>
        <p id="p_14736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14736"> print</p>
        <p id="p_14737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14737"> ( "$line");</p>
        <p id="p_14738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14738"> 5 :</p>
        <p id="p_14739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14739"> END</p>
        <p id="p_14740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14740"> 6: This is my line of data.</p>
        <p id="p_14741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14741"> Output</p>
        <p id="p_14742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14742"> $ programl7_17 This is my line of data. $</p>
        <p id="p_14743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14743"> Analysis</p>
        <p id="p_14744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14744"> i  The _  _END _value in line 5 indicates the end of the program. When line 3 reads from the</p>
        <p id="p_14745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14745"> DATA  file variable, the first line after_ END _is read in and is assigned to  $line.  (Subsequent requests</p>
        <p id="p_14746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14746"> for input from  DATA  read successive lines, if any exist.) Line  6  then prints this input line.</p>
        <p id="p_14747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14747"> NOTE</p>
        <p id="p_14748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14748"> For more information on_ END _and methods of indicating the end</p>
        <p id="p_14749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14749"> of the program, refer to  Day 20 ,  "Miscellaneous Features of Perl.</p>
        <p id="p_14750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14750"> The Underscore File Variable</p>
        <p id="p_14751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14751"> The _ (underscore) file variable represents the file specified by the last call to either the  stat  function or a file test operator. For example:</p>
        <p id="p_14752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14752"> $readable = -r "/u/jqpublic/myfile"; $writeable = -w</p>
        <p id="p_14753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14753"> Here, the _ file variable used in the second statement refers to  /u/ jqpublic/myf ile  because this is the filename that was passed to -r.</p>
        <p id="p_14754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14754"> You can use _ anywhere that a file variable can be used, provided that the file has been opened appropriately:</p>
        <p id="p_14755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14755"> if (—T $myoutfile) {</p>
        <p id="p_14756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14756"> print _ ("here is my output\n");</p>
        <p id="p_14757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14757"> }</p>
        <p id="p_14758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14758"> Here, the file whose name is stored in  $my out file  is associated with _ because this name was passed to -T  (which tests whether the file is a text file). The call to  print  writes output to this file.</p>
        <p id="p_14759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14759"> The main benefit of _ is that it saves time when you are using several file-test operators at once:</p>
        <p id="p_14760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14760"> if (-r "myfile" || —w   || —x  ) {</p>
        <p id="p_14761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14761"> print ("I can read, write, or execute myfile.\n");</p>
        <p id="p_14762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14762"> }</p>
        <p id="p_14763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14763"> Using _ rather than  myfile  saves time because file test operators normally call the UNIX system function st at.  If you specify the Perl interpreter is told to use the results of the preceding call to the UNIX  stat function and to not bother calling it again.</p>
        <p id="p_14764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14764"> Specifying System Variable Names as Words</p>
        <p id="p_14765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14765"> As you have seen, the system variables defined by Perl normally consist of a $, @ or % followed by a single non-alphanumeric character. This ensures that you cannot define a variable whose name is identical to that of a Perl system variable.</p>
        <p id="p_14766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14766"> If you find Perl system variable names difficult to remember or type, Perl 5 provides an alternative for most of them. If you add the statement</p>
        <p id="p_14767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14767"> use English;</p>
        <p id="p_14768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14768"> at the top of your program, Perl defines alternative variable names that more closely resemble English words. This makes it easier to understand what your program is doing. Table 17.1 lists these alternative variable names.</p>
        <p id="p_14769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14769"> Table 17.1. Alternative names for Perl system variables.</p>
        <p id="p_14770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14770"> $EFFECTIVE_USER_IDor $EUID</p>
        <p id="p_14771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14771"> $FORMAT_LINES_LEFT</p>
        <p id="p_14772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14772"> Summary</p>
        <p id="p_14773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14773"> Today you learned about the built-in system variables available within every Perl program. These system variables are divided into five groups:</p>
        <p id="p_14774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14774"> • Global scalar variables, which are defined everywhere in the program and contain a single scalar value</p>
        <p id="p_14775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14775"> • Pattern system variables, which are defined immediately after a pattern-matching or substitution operation has been performed</p>
        <p id="p_14776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14776"> • File system variables, which are defined for each input or output file accessible from the program</p>
        <p id="p_14777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14777"> • Array system variables, each of which contains a list</p>
        <p id="p_14778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14778"> • Built-in file variables, which are associated with files that are automatically open or automatically available</p>
        <p id="p_14779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14779"> You also learned how to specify English-language equivalents for Perl system variables.</p>
        <p id="p_14780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14780"> Q&amp;A</p>
        <p id="p_14781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14781"> Q: Why do some system variables use special characters rather than letters in their names?</p>
        <p id="p_14782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14782"> A:  To distinguish them from variables that you define and to ensure that the  reset   function</p>
        <p id="p_14783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14783"> (described in the next chapter) cannot affect them.</p>
        <p id="p_14784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14784"> Q: Why do some functions use $_ as the default, whereas others do not?</p>
        <p id="p_14785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14785"> A:  The functions that use $_ as the default are those that are likely to appear in Perl programs</p>
        <p id="p_14786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14786"> specified on the command line using the  -e   option.</p>
        <p id="p_14787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14787"> Q: What is the current line number when $ . is used with the &lt;&gt; operator?</p>
        <p id="p_14788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14788"> A:  Effectively, the &lt;&gt; operator treats its input files as if they are a single file. This means that $ .</p>
        <p id="p_14789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14789"> contains the total number of lines seen, not the line number of the current input file. (If you want $ .</p>
        <p id="p_14790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14790"> to contain the line number of the current file, set $ . to zero each time  eof   returns true.)</p>
        <p id="p_14791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14791"> Q: Are pattern system variables local or global?</p>
        <p id="p_14792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14792"> A:  Each pattern system variable is defined only in the current subroutine or block of statements.</p>
        <p id="p_14793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14793"> Q: Why does Perl define both the $ " and the $, system variables?</p>
        <p id="p_14794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14794"> A:  Some programs like to treat the following statements differently:</p>
        <p id="p_14795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14795"> print  ("@array"); print  (@array);</p>
        <p id="p_14796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14796"> (In fact, by default, the first statement puts a space between each pair of elements in the array, and the second does not.) The $ " and $, variables handle these two separate cases.</p>
        <p id="p_14797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14797"> Workshop</p>
        <p id="p_14798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14798"> The Workshop provides quiz questions to help you solidify your understanding of the material covered, and exercises to provide you with experience in using what you've learned.</p>
        <p id="p_14799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14799"> Quiz</p>
        <p id="p_14800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14800"> 1. List the functions and operators that use $_ by default.</p>
        <p id="p_14801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14801"> 2. What do the following variables contain?</p>
        <p id="p_14802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14802"> a. $ =</p>
        <p id="p_14803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14803"> b. $/</p>
        <p id="p_14804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14804"> c. $ ?</p>
        <p id="p_14805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14805"> d. $!</p>
        <p id="p_14806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14806"> d. @_</p>
        <p id="p_14807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14807"> 3. Explain the differences between  ARGV, $ARGV,  and  @ARGV.</p>
        <p id="p_14808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14808"> 4. Explain the difference between @  INC  and  %INC.</p>
        <p id="p_14809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14809"> 5. Explain the difference between  $0  and $1.</p>
        <p id="p_14810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14810"> Exercises</p>
        <p id="p_14811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14811"> 1. Write a program that reads lines of input, replaces multiple blanks and tabs with a single space, converts all uppercase letters to lowercase, and prints the resulting lines. Use no explicit variable names in this program.</p>
        <p id="p_14812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14812"> 2. Write a program that uses $ ' and $_ to remove all extra spaces from input lines.</p>
        <p id="p_14813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14813"> 3. Write a program that prints the directories in your  PATH  environment variable, one per line.</p>
        <p id="p_14814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14814"> 4. Write a program that prints numbers, starting with 1 and continuing until interrupted by an  INT signal.</p>
        <p id="p_14815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14815"> 5. Write a program whose data consists of one or more numbers per input line. Put the input lines in the program file itself. Add the numbers and print their total.</p>
        <p id="p_14816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14816"> 6.  BUG BUSTER:  What is wrong with the following statement?</p>
        <p id="p_14817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14817"> if ($line =~ /abc/) {</p>
        <p id="p_14818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14818"> $' =~ s/ +/ /;</p>
        <p id="p_14819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14819"> }</p>
        <p id="p_14820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14820"> Chapter 18</p>
        <p id="p_14821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14821"> References in Perl 5</p>
        <p id="p_14822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14822"> by Kamran Husain</p>
        <p id="p_14823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14823"> CONTENTS</p>
        <p id="p_14824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14824"> •  Introduction to References</p>
        <p id="p_14825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14825"> •  Using References</p>
        <p id="p_14826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14826"> •  Using the Backslash Operator</p>
        <p id="p_14827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14827"> •  References and Arrays</p>
        <p id="p_14828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14828"> •  Multidimensional Arrays</p>
        <p id="p_14829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14829"> •  References to Subroutines</p>
        <p id="p_14830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14830"> o  Using Subroutine Templates</p>
        <p id="p_14831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14831"> •  Using Subroutines to Work with Multiple Arrays</p>
        <p id="p_14832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14832"> o  Pass By Value or By Reference?</p>
        <p id="p_14833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14833"> •  References to File Handles</p>
        <p id="p_14834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14834"> o  What Does the * variable Operator Do?</p>
        <p id="p_14835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14835"> •  Using Symbolic References... Again</p>
        <p id="p_14836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14836"> o  Declaring Variables with Curly Braces</p>
        <p id="p_14837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14837"> •  More on Hard Versus Symbolic References</p>
        <p id="p_14838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14838"> •  For More Information</p>
        <p id="p_14839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14839"> •  Summary</p>
        <p id="p_14840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14840"> •  Q&amp;A</p>
        <p id="p_14841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14841"> •  Workshop</p>
        <p id="p_14842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14842"> o  Quiz</p>
        <p id="p_14843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14843"> •  Exercises</p>
        <p id="p_14844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14844"> Today's lesson describes the use of Perl references and the concept of pointers. Today's lesson also shows you how to use references to create complex data structures, pass pointers around, and work with subroutines. You learn the following topics:</p>
        <p id="p_14845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14845"> • Hard and symbolic references</p>
        <p id="p_14846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14846"> • Using references to arrays and scalars</p>
        <p id="p_14847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14847"> • Passing arrays to subroutines by reference</p>
        <p id="p_14848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14848"> • References to subroutines</p>
        <p id="p_14849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14849"> Introduction to References</p>
        <p id="p_14850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14850"> A  reference  is simply a pointer to something, such as a Perl variable, array, hash (also known as an associative array), or even a subroutine. The concept of a reference is probably familiar to Pascal or C programmers. A reference is simply an address to a value. How you use that value is up to you as the programmer and what the language lets you get away with. In Perl, you can refer to a pointer as a reference; in fact, you can use the terms pointer and reference interchangeably without any loss of meaning.</p>
        <p id="p_14851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14851"> References are useful in creating complex data structures in Perl. In fact, you cannot really define any complicated structures in Perl without using references.</p>
        <p id="p_14852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14852"> The two types of references in Perl 5 are  hard  and  symbolic.  A symbolic reference contains the name of a variable. Symbolic references are useful for creating variable names and addressing them at runtime. Basically, a symbolic link is like the name of a file or a soft link on a UNIX system. Hard references are more like hard links in the file system (that is, merely another path to the same underlying item).</p>
        <p id="p_14853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14853"> Perl 4 permits only symbolic references, which are difficult to use. For example, in Perl 4, you have to use names to index to an associative array called _main { } of symbol names for a package. Perl 5 now lets you have hard references to data.</p>
        <p id="p_14854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14854"> Hard references keep track of reference counts. When the reference count becomes zero, Perl automatically frees the item referred to. If that item happens to be a Perl object, the object is  destructed -freed to the memory pool. Perl is object-oriented in itself because everything in Perl is an object. Packages and modules make it much easier to use objects in Perl.</p>
        <p id="p_14855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14855"> Hard references are easy to use in Perl as long as you use them as scalars. To use hard references as anything but scalars, you have to explicitly de-reference the variable and tell it how you want it to behave. If this sounds confusing, don't worry; references are covered on  Day 19 ,  "Object-Oriented Programming in Perl," to help make this concept clearer.</p>
        <p id="p_14856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14856"> Using References</p>
        <p id="p_14857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14857"> In today's lesson, a scalar value refers to a variable such as  $pointer.  The variable  $pointer  contains one data item; whether the item is a number, string, or an address is determined by how you use it.</p>
        <p id="p_14858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14858"> Any scalar can hold a hard reference, and because arrays and hashes do contain scalars, it follows that you can now easily build complex data structures of different combinations of arrays of arrays, arrays of hashes, hashes of functions, and so on. As long as you understand that you are working only with scalars, you should be able to navigate through the most complex structures with proper dereferencing.</p>
        <p id="p_14859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14859"> Let's cover some of the basics first before we get too deep into the chapter.</p>
        <p id="p_14860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14860"> To use the value of  $pointer  as the pointer to an array, you reference the items in the array as @$pointer.  This notation of "@ $pointer"  roughly translates to "take the address in  $pointer  and then use it as an array." Similarly for hashes, you would use  %$pointer  as the reference to the first element in the hash.</p>
        <p id="p_14861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14861"> Because there are several ways to construct references, you can have references to just about anything, such as arrays, scalar variables, subroutines, file handles, and, yes-to the delight of C programmers-even other</p>
        <p id="p_14862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14862"> references. Perl gives you the power to write enough complicated code to hang yourself.</p>
        <p id="p_14863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14863"> Now look at some of the ways that you can create and use references in Perl.</p>
        <p id="p_14864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14864"> Using the Backslash Operator</p>
        <p id="p_14865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14865"> Using the backslash operator is analogous to using the ampersand (&amp;) operator in C to pass the address of an operator. Usually, you use the backslash operator to create a second, new reference to a variable. The following code shows how to create a reference to a scalar variable:</p>
        <p id="p_14866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14866"> $variable = 22; $pointer = \$variable; $ice = "jello"</p>
        <p id="p_14867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14867"> $iceptr = \$ice;</p>
        <p id="p_14868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14868"> $pointer  points to the location that contains the value of  $variable.  The pointer  $iceptr  points to "jello".  Even if the original reference $  variable  gets destroyed, you can still access the value from the  $pointer  reference. There is a hard reference at work here, so you will have to get rid of both $po inter  and $  variable  for the space in which  22  is allocated to be freed back to the memory pool.</p>
        <p id="p_14869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14869"> In the preceding code, the variable  $pointer  contains the address of  $variable,  not the value itself. To get the value, you have to de-reference  $pointer  with two $$. The following sample script shows how this works:</p>
        <p id="p_14870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14870"> #!/usr/bin/perl</p>
        <p id="p_14871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14871"> $value = 10;</p>
        <p id="p_14872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14872"> $pointer = \$value;</p>
        <p id="p_14873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14873"> printf "\n Pointer Address $pointer of $value \n";</p>
        <p id="p_14874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14874"> printf "\n What Pointer *($pointer) points to $$pointer\n";</p>
        <p id="p_14875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14875"> The  $value  in the script is set to 10. The  $pointer  is set to point to the address of  $value.  The two printf  statements show how the value of the variable is referenced. If you run the script shown, you see</p>
        <p id="p_14876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14876"> something very close to the following output:</p>
        <p id="p_14877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14877"> Pointer Address SCALAR(0x806c520) of 10</p>
        <p id="p_14878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14878"> What Pointer *(SCALAR(0x806c520)) points to 10</p>
        <p id="p_14879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14879"> The address in the output from your script will probably be different from what's shown. However, you can see that  $pointer  gave the address and  $$pointer  gave the value of the scalar that $  variable  points to.</p>
        <p id="p_14880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14880"> Pay attention to how the address is shown in the pointer variable. The word  SCALAR  is followed by a long hexadecimal number. The word  SCALAR  tells you that the address points to a scalar variable. The number following  SCALAR  is the address where the actual value of the scalar variable is kept.</p>
        <p id="p_14881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14881"> NOTE</p>
        <p id="p_14882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14882"> A pointer is an address. The data at that address is referred to by a pointer. If the pointer happens to point to an invalid address, you can get bad data. Generally, Perl will simply return a  NULL  value, but you should not rely on this, and should program to initialize all your pointers to refer to valid data items</p>
        <p id="p_14883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14883"> References and Arrays</p>
        <p id="p_14884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14884"> Perhaps the most important point you must remember about Perl is that all Perl @ ARRAYS and %HASHes are always one-dimensional. As such, the arrays and hashes hold scalar values only and do not directly contain other arrays or complex data structures. A member of an array is either a number or a reference (including strings).</p>
        <p id="p_14885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14885"> You can use the backslash operator on arrays and hashes just as you would for scalar variables. You would use something like Listing 18.1 for arrays.</p>
        <p id="p_14886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14886"> Type</p>
        <p id="p_14887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14887"> Listing 18.1. Using the backslash operator on arrays.</p>
        <p id="p_14888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14888"> 1 #!/usr/bin/perl</p>
        <p id="p_14889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14889"> #</p>
        <p id="p_14890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14890"> 2</p>
        <p id="p_14891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14891"> 3 # Using Array references</p>
        <p id="p_14892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14892"> 4 #</p>
        <p id="p_14893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14893"> 5 $pointer = \@ARGV;</p>
        <p id="p_14894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14894"> 6 printf "\n Pointer Address of ARGV = $pointer\n";</p>
        <p id="p_14895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14895"> 7 $i = scalar(@$pointer);</p>
        <p id="p_14896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14896"> 8 printf "\n Number of arguments : $i \n";</p>
        <p id="p_14897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14897"> 9 $i = 0;</p>
        <p id="p_14898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14898"> 10 foreach (@$pointer) {</p>
        <p id="p_14899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14899"> 11 printf "$i : $$pointer[$i++];  \n";</p>
        <p id="p_14900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14900"> 12  }</p>
        <p id="p_14901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14901"> Output</p>
        <p id="p_14902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14902"> $ test  1234</p>
        <p id="p_14903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14903"> Pointer Address of ARGV = ARRAY(0x806c378)</p>
        <p id="p_14904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14904"> Number of arguments : 4</p>
        <p id="p_14905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14905"> 0 : 1 ;</p>
        <p id="p_14906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14906"> 1  :  2 ;</p>
        <p id="p_14907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14907"> 2 : 3;</p>
        <p id="p_14908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14908"> 3:4;</p>
        <p id="p_14909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14909"> Examine the lines that pertain to references in the shell script shown, which prints the contents of the input argument array  @ARGV.  Line 5 is where the reference  $pointer  is set to point to the array  @ARGV.  Line 6 simply prints the address of  ARGV.  You probably will never have to use the address of  ARGV,  but had you been using another array, this is a quick way to get to the address of the first element of the array.</p>
        <p id="p_14910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14910"> The $pointer  returns the address of the first element of an array. In Listing 18.1, the array happened to be  @ARGV.  A pointer to an array should sound familiar to C programmers because a reference to a one¬ dimensional array is simply a pointer to the first element of the array.</p>
        <p id="p_14911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14911"> Line 7 calls the function  scalar  () (not to be confused with the type of variable  scalar)  to get the count of the number of elements in an array. The parameter passed in could be  @ARGV,  but with the pointer $pointer,  you must specify the type of parameter that is expected by the  scalar ( ) function. Therefore, you specify the type of parameter as an array by using  @$pointer.</p>
        <p id="p_14912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14912"> The type of  $pointer  in this case is a pointer to the array whose number of elements you must return from the  scalar ( ) function. The call to the function has @  $pointer  as the passed parameter. The $pointer  gives the address of the first element, and the @ sign forces the passing of the address of the first element as an array reference.</p>
        <p id="p_14913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14913"> Line 10 contains the same reference to the array that line 7 contains. Line 11 lists all the elements of the array using the  $$pointer [ $ 1 ]  item. How do you interpret this? The  $pointer  points to the first element in the array. The program then gets the ($ i  - 1) -th item in the array ( $pointer [ $i++ ]  ) and increments  $i.  Finally, the value at  $ $pointer [ $ 1 ]  is returned as a scalar. Because the autoincrement operator is low on the operator precedence priority list, $  i  is incremented last of all.</p>
        <p id="p_14914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14914"> You can also use the backslash operator with associative arrays. The idea is the same-you are substituting the  $pointer  for all references to the name of the associative array. The number following the word ARRAY  in the pointer address of  ARGV  in the previous example is the address of  ARGV.  The address itself won't do you any good, because most programs do not need this information, but just realize that references to arrays and scalars are displayed with the type that they happen to be pointing to.</p>
        <p id="p_14915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14915"> For pointers to functions, the address is printed with the word  CODE,  and for a hash, it is printed as  HASH. See Listing 18.2 for an example of how to print out an address to a hash.</p>
        <p id="p_14916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14916"> Listing 18.2. Using references to a hash.</p>
        <p id="p_14917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14917"> #!/usr/bin/perl 1 #</p>
        <p id="p_14918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14918"> 2 # Using Associative Array references</p>
        <p id="p_14919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14919"> 4 %month = (</p>
        <p id="p_14920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14920"> 18</p>
        <p id="p_14921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14921"> 19 $pointer = \%month;</p>
        <p id="p_14922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14922"> 20</p>
        <p id="p_14923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14923"> 21 printf "\n Address of hash = $pointer\n</p>
        <p id="p_14924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14924"> 22</p>
        <p id="p_14925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14925"> 23 #</p>
        <p id="p_14926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14926"> 24 # The following lines would be used to print out the</p>
        <p id="p_14927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14927"> 25 # contents of the associative array if %month was used.</p>
        <p id="p_14928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14928"> 26 #</p>
        <p id="p_14929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14929"> 27 # foreach $i (sort keys %month) {</p>
        <p id="p_14930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14930"> 28 # printf "\n $i $$pointer{$i}</p>
        <p id="p_14931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14931"> 29 # }</p>
        <p id="p_14932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14932"> 30</p>
        <p id="p_14933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14933"> 31 #</p>
        <p id="p_14934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14934"> 32 # The reference to the associative array via $pointer</p>
        <p id="p_14935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14935"> 33 #</p>
        <p id="p_14936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14936"> 34 foreach $i (sort keys %$pointer) {</p>
        <p id="p_14937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14937"> 35 printf "$i is $$pointer{$i} \n";</p>
        <p id="p_14938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14938"> 36 }</p>
        <p id="p_14939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14939"> Output</p>
        <p id="p_14940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14940"> $ mth</p>
        <p id="p_14941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14941"> Address of hash = HASH(0x806c52c)</p>
        <p id="p_14942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14942"> The reference to the associative array is made with the code in line 19,  $pointer = \%month; . As with ordinary arrays, the references to the elements of the array are made with the $$pointer { $ index } construct. Of course, because the array is really a hash, the $  index  is the key into the hash and not a number. See lines 34 and 35 to see how elements in the array are being referenced.</p>
        <p id="p_14943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14943"> You don't have to construct associative arrays using the comma operator. You can use the =&gt; operator instead. In the later Perl module and sample code in this chapter, you will see the =&gt; operator, which is the same as the comma operator. Using =&gt; makes the code a bit easier to read. See Listing 18.3 for a sample usage of the =&gt; operator.</p>
        <p id="p_14944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14944"> Analysis</p>
        <p id="p_14945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14945"> Type</p>
        <p id="p_14946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14946"> Listing 18.3. Using the =&gt; operator.</p>
        <p id="p_14947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14947"> 1 #!/usr/bin/perl</p>
        <p id="p_14948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14948"> 2  #</p>
        <p id="p_14949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14949"> 3 # Using Array references</p>
        <p id="p_14950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14950"> 4 #</p>
        <p id="p_14951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14951"> 5 %weekday = (</p>
        <p id="p_14952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14952"> 15 $i =  '05';</p>
        <p id="p_14953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14953"> 16 printf "\n</p>
        <p id="p_14954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14954"> start test</p>
        <p id="p_14955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14955"> \n";</p>
        <p id="p_14956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14956"> 17 #</p>
        <p id="p_14957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14957"> 18 # These next two lines should show an output</p>
        <p id="p_14958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14958"> 19 #</p>
        <p id="p_14959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14959"> 20 printf '$$pointer{$i} is</p>
        <p id="p_14960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14960"> 21 printf "$$pointer{$i} \n";</p>
        <p id="p_14961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14961"> 22 printf '${$pointer}{$i} is</p>
        <p id="p_14962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14962"> 23 printf "${$pointer}{$i} \n";</p>
        <p id="p_14963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14963"> 24 printf '$pointer-&gt;{$i} is</p>
        <p id="p_14964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14964"> 25</p>
        <p id="p_14965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14965"> 26 printf "$pointer-&gt;{$i}\n";</p>
        <p id="p_14966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14966"> 27 #</p>
        <p id="p_14967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14967"> 28 # These next two lines should not show anything</p>
        <p id="p_14968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14968"> 29 #</p>
        <p id="p_14969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14969"> 30 printf '${$pointer{$i}} is</p>
        <p id="p_14970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14970"> 31 printf "${$pointer{$i}} \n";</p>
        <p id="p_14971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14971"> 32 printf '${$pointer-&gt;{$i}} is</p>
        <p id="p_14972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14972"> 33 printf "${$pointer-&gt;{$i}}";</p>
        <p id="p_14973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14973"> 34 printf "\n  ==================   end  of test  =================  \n";</p>
        <p id="p_14974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14974"> 35</p>
        <p id="p_14975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14975"> start test</p>
        <p id="p_14976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14976"> Output</p>
        <p id="p_14977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14977"> $$pointer{$i} is Fri</p>
        <p id="p_14978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14978"> ${$pointer}{$i} is Fri</p>
        <p id="p_14979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14979"> $pointer-&gt;{$i} is Fri</p>
        <p id="p_14980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14980"> ${$pointer{$i}} is ${$pointer-&gt;{$i}} is</p>
        <p id="p_14981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14981"> end of test</p>
        <p id="p_14982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14982"> Analysis</p>
        <p id="p_14983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14983"> As you can see, the first two lines provided the expected output. The first reference is used in the same way as references to regular arrays. The second line uses the $ { pointer } and then indexes using {  $i  }, and the leftmost $ de-references (gets) the value at the location reached after the indexing. See Lines 20 through 23.</p>
        <p id="p_14984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14984"> NOTE</p>
        <p id="p_14985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14985"> When in doubt, print it out. Always use the print statements in Perl to print out values of suspect code. This way you can be sure of how Perl is interpreting your code. Print statements are a cheap tool to use for learning how the Perl interpreter works</p>
        <p id="p_14986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14986"> Then, two lines of the output didn't work as expected. In the third line,  $pointer{$i}  tries to reference an array where there is no first element. Because the first element does not point to a valid string, nothing is printed. Nothing is printed in the fourth line of the output for the same reason. See lines 30 through 33.</p>
        <p id="p_14987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14987"> Multidimensional Arrays</p>
        <p id="p_14988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14988"> You create a reference to an array through the statement  @array = list.  You use square brackets to create a reference to a complex anonymous array. Consider the following statement, which sets the parameters for a three-dimensional drawing program:</p>
        <p id="p_14989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14989"> $line = ['solid', 'black',  ['1','2','3'] , ['4', '5', '6']];</p>
        <p id="p_14990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14990"> The preceding statement constructs an array of four elements. The array is referred to by the scalar  $line. The first two elements are scalars, indicating the type and color of the line to draw. The next two elements are references to anonymous arrays and contain the starting and ending points of the line.</p>
        <p id="p_14991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14991"> To get to the elements of the inner array elements, you can use the following multidimensional syntax:</p>
        <p id="p_14992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14992"> $arrayReference-&gt;[$index]</p>
        <p id="p_14993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14993"> $arrayReference-&gt;[$index1][$index2]</p>
        <p id="p_14994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14994"> $arrayReference-&gt;[$indexl][$index2][$index3]</p>
        <p id="p_14995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14995"> single-dimensional array two-dimensional array three-dimensional array</p>
        <p id="p_14996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14996"> You can create as complex a structure as your sanity, design practices, and computer memory allow. Be kind to the person who might have to manage your code-please keep it as simple as possible. On the other hand, if you are just trying to impress someone with your coding ability, Perl gives you a lot of opportunity to mystify yourself and improve your social life.</p>
        <p id="p_14997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14997"> TIP</p>
        <p id="p_14998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14998"> When you have more than three dimensions for any array, consider using a different data structure to simplify the code.</p>
        <p id="p_14999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_14999"> Let's see how creating arrays within arrays works in practice. See Listing 18.4 to see how to print out the information pointed at by the  $list  reference.</p>
        <p id="p_15000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15000"> Type</p>
        <p id="p_15001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15001"> Listing 18.4. Using multi-dimensional array references.</p>
        <p id="p_15002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15002"> 1 #!/usr/bin/perl</p>
        <p id="p_15003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15003"> 2  #</p>
        <p id="p_15004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15004"> 3 # Using Multi-dimensional Array references</p>
        <p id="p_15005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15005"> 4 #</p>
        <p id="p_15006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15006"> Output</p>
        <p id="p_15007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15007"> $line-&gt;[0] = solid $line-&gt;[l] = black $line-&gt;[2][0] =1 $line-&gt;[2][1] =2 $line-&gt;[2][2] = 3 $line-&gt;[3][0] = 4 $line-&gt;[3][1] =5 $line-&gt;[3][2] = 6</p>
        <p id="p_15008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15008"> What about the third dimension for an array? Look at a modified version of the same program but add a new twist to the list just created. See Listing 18.5.</p>
        <p id="p_15009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15009"> Type</p>
        <p id="p_15010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15010"> Listing 18.5. Using multi-dimensional array references again.</p>
        <p id="p_15011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15011"> 1 #!/usr/bin/perl</p>
        <p id="p_15012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15012"> 2  #</p>
        <p id="p_15013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15013"> 3 # Using Multi-dimensional Array references again</p>
        <p id="p_15014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15014"> 4 #</p>
        <p id="p_15015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15015"> Output</p>
        <p id="p_15016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15016"> There is no output for this listing.</p>
        <p id="p_15017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15017"> In this example of an array that's three deep, you must use a reference such as $  line -&gt;  [2]  [3 ] [  0  ]. For a C programmer, this is akin to the statement  Array_pointer  [2 ] [3] [  0  ], where the pointer is pointing to what's declared as an array with three indices.</p>
        <p id="p_15018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15018"> Can you see how easy it is to set up complex structures of arrays within arrays? The examples shown thus far have used only hard-coded numbers as the indices. There is nothing preventing you from using variables instead.</p>
        <p id="p_15019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15019"> As with array constructors, you can mix and match hashes and arrays to create as complex a structure as you want.</p>
        <p id="p_15020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15020"> Let's see how these two hashes and arrays can be combined. Listing 18.6 uses the point numbers and coordinates to define a cube.</p>
        <p id="p_15021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15021"> Analysis</p>
        <p id="p_15022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15022"> Type</p>
        <p id="p_15023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15023"> Listing 18.6. Defining a cube.</p>
        <p id="p_15024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15024"> 1 #!/usr/bin/perl</p>
        <p id="p_15025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15025"> 2  #</p>
        <p id="p_15026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15026"> 3 # Using Multi-dimensional Array and Hash references</p>
        <p id="p_15027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15027"> 4 #</p>
        <p id="p_15028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15028"> 5 %cube = (</p>
        <p id="p_15029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15029"> 6</p>
        <p id="p_15030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15030"> 15  $pointer = \%cube;</p>
        <p id="p_15031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15031"> 16 print "\n Da Cube \n";</p>
        <p id="p_15032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15032"> 17 foreach $i (sort keys %$pointer) {</p>
        <p id="p_15033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15033"> 18 $list = $$pointer{$i};</p>
        <p id="p_15034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15034"> 19 $x = $list-&gt;[0];</p>
        <p id="p_15035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15035"> 20 $y = $list-&gt;[l];</p>
        <p id="p_15036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15036"> 21 $z = $list-&gt;[2];</p>
        <p id="p_15037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15037"> 22 printf " Point $i = $x,$y,$z \n";</p>
        <p id="p_15038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15038"> 23 }</p>
        <p id="p_15039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15039"> Output</p>
        <p id="p_15040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15040"> There is no output for this listing.</p>
        <p id="p_15041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15041"> In Listing 18.6,  %cube  contains point numbers and coordinates in a hash. Each coordinate itself is an array of three numbers. The  $list  variable is used to get a reference to each coordinate definition with the following statement:</p>
        <p id="p_15042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15042"> Analysis</p>
        <p id="p_15043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15043"> $list = $$pointer{$i};</p>
        <p id="p_15044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15044"> After you get the list, you can reference off of it to get to each element in the list with the following statement:</p>
        <p id="p_15045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15045"> $x =  $list-&gt;[ 0 ];</p>
        <p id="p_15046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15046"> $y = $list-&gt;[1];</p>
        <p id="p_15047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15047"> The same result-assigning values to $x, $y, and $z-could be achieved with the following two lines of code:</p>
        <p id="p_15048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15048"> ($x,$y,$z) = @$list;</p>
        <p id="p_15049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15049"> $x = $list-&gt;[0];</p>
        <p id="p_15050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15050"> This works because you are de-referencing what  $list  points to and using it as an array, which in turn is assigned to the list ( $x, $y, $z) . The  $x  is still assigned with the -&gt; operator.</p>
        <p id="p_15051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15051"> When you're working with hashes or arrays, de-referencing by -&gt; is similar to de-referencing by $. When you are accessing individual array elements, you are often faced with writing statements such as the following:</p>
        <p id="p_15052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15052"> $$names[0] = "Karnran";</p>
        <p id="p_15053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15053"> $names-&gt;[0] = "Karnran";</p>
        <p id="p_15054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15054"> Both lines are equivalent. The $  names  in the first line has been replaced with the -&gt; operator in the second line. In the case of hashes, the two statements that do the same type of referencing are listed as shown in the following code:</p>
        <p id="p_15055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15055"> $$lastnames{"Karnran"} = "Husain"; $lastnames-&gt;{"Karnran"} = "Husain";</p>
        <p id="p_15056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15056"> Array references are created automatically when they are first referenced in the left side of an equation. Using a reference such as $  array  [ $ i ] creates an array into which you can index with $1. Scalars and even multidimensional arrays are created the same way. The following statement creates the  contours array if it did not already exist:</p>
        <p id="p_15057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15057"> $contours[$x] [$y] [ $z]</p>
        <p id="p_15058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15058"> &amp;xlate($mouseX,$mouseY);</p>
        <p id="p_15059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15059"> Arrays in Perl can be created and grown on demand. Referencing them for the first time creates the array. Referencing them again at different indices creates the referenced elements for you.</p>
        <p id="p_15060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15060"> References to Subroutines</p>
        <p id="p_15061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15061"> In the same way you reference individual items such as arrays and scalar variables, you can also point to subroutines. This is similar to pointing to a function in C. To construct such a reference, you use the following type of statement:</p>
        <p id="p_15062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15062"> $pointer_to_sub = sub { ... declaration of sub ... } ;</p>
        <p id="p_15063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15063"> Notice the use of the semicolon at the end of the  sub  declaration. The subroutine pointed to by $pointer_to_sub  points to the same function reference even if this statement is placed in a loop. This feature of Perl enables you to declare anonymous  sub  () functions in a loop without worrying about whether you are chewing up memory by declaring the same function over and over.</p>
        <p id="p_15064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15064"> To call a subroutine by reference, you must use the following type of reference:</p>
        <p id="p_15065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15065"> &amp;$pointer_to_sub( parameters );</p>
        <p id="p_15066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15066"> This code works because you are de-referencing the  $pointer_to_sub  and using it with the ampersand (&amp;) as a pointer to a function. The  parameters  portion might or might not be empty depending on how your function is defined.</p>
        <p id="p_15067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15067"> The code within a  sub  is simply a declaration created through a previous statement. The code within the sub  is not executed immediately, however. It is compiled and set for each use. Consider Listing 18.7.</p>
        <p id="p_15068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15068"> Type</p>
        <p id="p_15069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15069"> Listing 18.7. References to subroutines.</p>
        <p id="p_15070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15070"> 1 #!/usr/bin/perl</p>
        <p id="p_15071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15071"> 2 sub print_coor{</p>
        <p id="p_15072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15072"> 3 my  ($x,$y,$z)  = @_;</p>
        <p id="p_15073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15073"> 4 print "$x $y $z  \n";</p>
        <p id="p_15074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15074"> 5</p>
        <p id="p_15075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15075"> return  $x;};</p>
        <p id="p_15076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15076"> 6</p>
        <p id="p_15077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15077"> $k = 1;</p>
        <p id="p_15078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15078"> 7 $j = 2;</p>
        <p id="p_15079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15079"> 8 $m = 4;</p>
        <p id="p_15080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15080"> 9 $this = print_coor($k,$j,$m);</p>
        <p id="p_15081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15081"> 10 $that = print_coor(4,5, 6) ;</p>
        <p id="p_15082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15082"> Output</p>
        <p id="p_15083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15083"> $ test 12 3 4 5 6</p>
        <p id="p_15084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15084"> Analysis</p>
        <p id="p_15085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15085"> This output reflects that the assignment of  $x, $y,  and  $z  was done when the first declaration of  print_coor  was encountered as a call. In Listing 18.7, each reference  $this  and  $that  points to a different subroutine, the arguments to which were passed at run- time.</p>
        <p id="p_15086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15086"> Using Subroutine Templates</p>
        <p id="p_15087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15087"> Subroutines are not limited to returning data types only; they can also return references to other subroutines. The returned subroutines run in the context of the calling routine but are set up in the original call that created them. This behavior is due to the way closure is handled in Perl.  Closure  means that if you define a function in one context, it runs in that particular context where it was first defined. (See a book on object-oriented programming to get more information on closure.)</p>
        <p id="p_15088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15088"> For an example of how closure works, Listing 18.8 shows code that you could use to set up different types of error messages. Such subroutines are useful in creating templates of all error messages.</p>
        <p id="p_15089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15089"> Type</p>
        <p id="p_15090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15090"> Listing 18.8, Using closures.</p>
        <p id="p_15091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15091"> #!/usr/bin/perl</p>
        <p id="p_15092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15092"> sub errorMsg {</p>
        <p id="p_15093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15093"> my $lvl = shift;</p>
        <p id="p_15094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15094"> #</p>
        <p id="p_15095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15095"> # define the subroutine to run when called.</p>
        <p id="p_15096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15096"> #</p>
        <p id="p_15097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15097"> return sub {</p>
        <p id="p_15098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15098"> my $msg = shift; # Define the error type now. print "Err Level $lvl:$msg\n"; }; # print later.</p>
        <p id="p_15099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15099"> }</p>
        <p id="p_15100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15100"> $severe = errorMsg("Severe");</p>
        <p id="p_15101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15101"> $fatal = errorMsg("Fatal") ;</p>
        <p id="p_15102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15102"> $annoy = errorMsg("Annoying") ;</p>
        <p id="p_15103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15103"> &amp;$severe("Divide by zero");</p>
        <p id="p_15104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15104"> &amp;$fatal("Did you forget to use a semi-colon?"); &amp;$annoy("Uninitialized variable in  use");</p>
        <p id="p_15105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15105"> Output</p>
        <p id="p_15106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15106"> $severe = errorMsg("Severe") ; $fatal = errorMsg("Fatal") ;</p>
        <p id="p_15107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15107"> $annoy</p>
        <p id="p_15108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15108"> = errorMsg("Annoying") ;</p>
        <p id="p_15109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15109"> Analysis</p>
        <p id="p_15110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15110"> The subroutine  errorMsg  declared here uses a local variable called  lvl.  After this declaration,  errorMsg  uses  $lvl  in the subroutine it returns to the caller. The value of  $lvl  is therefore set in the context when the subroutine  errorMsg  is first called, even though the keyword  my  is used. The three calls that follow set up three different  $lvl  variable values, each in their own context:</p>
        <p id="p_15111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15111"> $severe = errorMsg("Severe") ;</p>
        <p id="p_15112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15112"> $fatal = errorMsg("Fatal");</p>
        <p id="p_15113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15113"> $annoy = errorMsg("Annoying") ;</p>
        <p id="p_15114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15114"> When the subroutine,  errorMsg,  returns, the value of  $lvl  is retained for each context in which  $lvl was declared. The  $msg  value from the referenced call is used, but the value of  $lvl  remains what was first set in the actual creation of the function.</p>
        <p id="p_15115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15115"> Sounds confusing? It is. This is primarily the reason you do not see such code in most Perl programs.</p>
        <p id="p_15116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15116"> Using Subroutines to Work with Multiple Arrays</p>
        <p id="p_15117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15117"> Using arrays is great for collecting relevant information in one place. Now let's see how we can work with multiple arrays through subroutines. You pass one or more arrays into Perl subroutines by reference. However, you have to keep in mind a few subtle things about using the @_ symbol when you process these arrays in the subroutine. Look at Listing 18.9, which is an example of a subroutine that expects a list of names and a list of phone numbers.</p>
        <p id="p_15118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15118"> Type</p>
        <p id="p_15119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15119"> Listing 18.9, Passing multiple arrays.</p>
        <p id="p_15120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15120"> 1 #!/usr/bin/perl</p>
        <p id="p_15121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15121"> 2 @names = (mickey, goofy, daffy );</p>
        <p id="p_15122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15122"> 3 Sphones = (5551234, 5554321, 666 );</p>
        <p id="p_15123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15123"> 4 $1 = 0;</p>
        <p id="p_15124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15124"> 5 sub listem { my (@a,@b) = @_;</p>
        <p id="p_15125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15125"> 6</p>
        <p id="p_15126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15126"> 7</p>
        <p id="p_15127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15127"> foreach</p>
        <p id="p_15128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15128"> (@a) {</p>
        <p id="p_15129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15129"> 8 print "a[$i] = ". $a[$i] . " " . "\tb[$i] = " . $b[$i]</p>
        <p id="p_15130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15130"> . "\n";</p>
        <p id="p_15131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15131"> 9 $i++;</p>
        <p id="p_15132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15132"> 10  }</p>
        <p id="p_15133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15133"> 11  }</p>
        <p id="p_15134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15134"> 12 &amp;listem(@names, Sphones);</p>
        <p id="p_15135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15135"> Output</p>
        <p id="p_15136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15136"> Analysis</p>
        <p id="p_15137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15137"> Whoa! What happened to the  @b  array, and why is the rest of @  a  just like the array  @b?  This result occurs because the array @_ of parameters in a subroutine is one-I repeat, only one-long list of parameters. If you pass in fifty arrays, the @_ is one array of all the elements of the fifty arrays concatenated together.</p>
        <p id="p_15138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15138"> In the subroutine in Listing 18.9, the assignment  my  (@  a, @b ) = @_ gets loosely interpreted by your Perl interpreter as, "Let's see, @  a  is an array, so assign one array from @_ to  @a  and then assign everything else to  @b."  Never mind that the @_ is itself an array and will therefore get assigned to  @a,  leaving nothing to assign to  @b.</p>
        <p id="p_15139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15139"> To illustrate this point, let's change the script to how it appears in Listing 18.10.</p>
        <p id="p_15140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15140"> Type</p>
        <p id="p_15141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15141"> Listing 18.10. Passing a scalar and an array.</p>
        <p id="p_15142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15142"> #!/usr/bin/perl</p>
        <p id="p_15143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15143"> @names = (mickey, goofy, daffy );</p>
        <p id="p_15144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15144"> @phones = (5551234, 5554321, 666 );</p>
        <p id="p_15145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15145"> $i = 0;</p>
        <p id="p_15146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15146"> sub listem {</p>
        <p id="p_15147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15147"> my ($a,@b) = @_;</p>
        <p id="p_15148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15148"> print " \$a is " . $a . "\n";</p>
        <p id="p_15149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15149"> foreach (@b) {</p>
        <p id="p_15150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15150"> print "b[$i] = $b[$i] \n";</p>
        <p id="p_15151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15151"> $i++;</p>
        <p id="p_15152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15152"> }</p>
        <p id="p_15153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15153"> #-</p>
        <p id="p_15154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15154"> # Actually, you could write the for loop as</p>
        <p id="p_15155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15155"> # foreach (@b) {</p>
        <p id="p_15156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15156"> # print $_ . "\n" ;</p>
        <p id="p_15157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15157"> # }</p>
        <p id="p_15158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15158"> # This your secret answer to Quiz question  18.4.</p>
        <p id="p_15159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15159"> # -</p>
        <p id="p_15160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15160"> }</p>
        <p id="p_15161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15161"> &amp;listem(@names, Sphones);</p>
        <p id="p_15162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15162"> Output</p>
        <p id="p_15163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15163"> $ testArray</p>
        <p id="p_15164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15164"> $a is mickey b[0]  = goofy</p>
        <p id="p_15165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15165"> b[i;</p>
        <p id="p_15166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15166"> daffy</p>
        <p id="p_15167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15167"> b[2]  = 5551234</p>
        <p id="p_15168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15168"> b[3]  = 5554321</p>
        <p id="p_15169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15169"> b [ 4 ]</p>
        <p id="p_15170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15170"> 6 6 6</p>
        <p id="p_15171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15171"> Analysis</p>
        <p id="p_15172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15172"> I  Do you see how  $a  was assigned the first value and then  @b  was assigned the rest of the values? In order get around this @_ interpretation feature and pass arrays into subroutines, you have to pass arrays in by reference, which you do by modifying the script to look like the following:</p>
        <p id="p_15173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15173"> #!/usr/bin/perl</p>
        <p id="p_15174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15174"> @names = (mickey, goofy, daffy );</p>
        <p id="p_15175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15175"> Sphones = (5551234, 5554321, 666 );</p>
        <p id="p_15176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15176"> $1  =  0 ;</p>
        <p id="p_15177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15177"> sub listem {</p>
        <p id="p_15178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15178"> my ($a,$b) = @_; foreach (@$a) {</p>
        <p id="p_15179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15179"> print  "a[$i] = " . @$a[$i] . " " . "\tb[$i] = " . @$b[$i]</p>
        <p id="p_15180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15180"> .  "\n" ;</p>
        <p id="p_15181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15181"> $i++;</p>
        <p id="p_15182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15182"> }</p>
        <p id="p_15183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15183"> }</p>
        <p id="p_15184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15184"> &amp;listem(\@names, \@phones);</p>
        <p id="p_15185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15185"> The following major changes were necessary to bring the original script to this point:</p>
        <p id="p_15186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15186"> • The local variables for the sub li stem  are now scalars, not array references. As a result,  $a  is the first item on the @_ list, and  $b  is the second item.</p>
        <p id="p_15187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15187"> • The local parameters  ($a  and  $b)  are used as array references with the statements @  $a  and @  $b, respectively.</p>
        <p id="p_15188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15188"> • The call to the subroutine passes the references to the arrays with the backslash, \@ names  and \@phones,  thus passing only two items to the subroutine.</p>
        <p id="p_15189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15189"> The following output matches what we expected:</p>
        <p id="p_15190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15190"> $ testArray2</p>
        <p id="p_15191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15191"> Don't</p>
        <p id="p_15192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15192"> DO  pass by reference whenever possible.</p>
        <p id="p_15193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15193"> DO  pass arrays by reference when you are passing more than one array to a subroutine.</p>
        <p id="p_15194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15194"> DON’T  use (@  variable ) =@_ in a subroutine unless you want to concatenate all the passed parameters into one long array</p>
        <p id="p_15195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15195"> Pass By Value or By Reference?</p>
        <p id="p_15196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15196"> When used in a subroutine argument list, scalar variables are always passed by reference. You do not have a choice here. You can, however, modify the values of these variables if you really want to. To access these variables, you can use the @_ array and index each individual element in it using $_ [ $  index  ], where $ index  counts from zero up.</p>
        <p id="p_15197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15197"> Arrays and hashes are different beasts altogether. You can either pass them as references once or pass references to each element in the array. For long arrays, the choice should be fairly obvious-pass the reference to the array only. In either case, you can use the references to modify what you want in the original array.</p>
        <p id="p_15198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15198"> The mechanism concatenates all the input arrays in a subroutine into one long array. This feature is nice if you do want to process the incoming arrays as one long array. Usually, you want to keep the arrays</p>
        <p id="p_15199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15199"> separate when you process them in a subroutine, and passing by reference is the best way to do that. Hold that thought: Don't use globals.</p>
        <p id="p_15200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15200"> In short, pass by reference and respect the value of any global variable unless there is a strong compelling reason not to.</p>
        <p id="p_15201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15201"> References to File Handles</p>
        <p id="p_15202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15202"> Sometimes, you have to write the same output to different output files. For example, an application programmer might want the output to go to the screen in one instance, the printer in another, and a file in another-or even all three at the same time. Rather than make separate statements for each handle, it would be nice to write something like the following:</p>
        <p id="p_15203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15203"> spitOut(\*STDIN); spitOut(\*LPHANDLE) ; spitOut(\*LOGHANDLE) ;</p>
        <p id="p_15204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15204"> Notice that the file handle reference is sent with the \ *FILEHANDLE  syntax because you refer to the symbol table in the current package. In the subroutine that handles the output to the file handle, you would have code that looks something like the following:</p>
        <p id="p_15205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15205"> sub spitOut {</p>
        <p id="p_15206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15206"> my $fh = shift;</p>
        <p id="p_15207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15207"> print $fh "Gee Wilbur, I like this lettuce\n";</p>
        <p id="p_15208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15208"> }</p>
        <p id="p_15209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15209"> What Does the ^variable Operator Do?</p>
        <p id="p_15210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15210"> In UNIX (and other operating systems), the asterisk is a sort of wildcard operator. In Perl, you can refer to other variables and so on by using the asterisk operator:</p>
        <p id="p_15211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15211"> *iceCream;</p>
        <p id="p_15212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15212"> When used in this manner, the asterisk is also known as a  typeglob.  The asterisk at the beginning of a term can be thought of as a wildcard match for all the mangled names generated internally by Perl.</p>
        <p id="p_15213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15213"> You can use a typeglob in the same way you use a reference because the de-reference syntax always indicates the kind of reference you want. $ {  *iceCream}  and $ {  \$iceCream}  both indicate the same</p>
        <p id="p_15214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15214"> scalar variable. Basically,  *iceCream  refers to the entry in the internal  _main  associative array of all symbol names for the  _main  package.  *kamran  really translates to  $_main { ' kamran  ' } if you are in the  _main  package context. If you are in another package, the  _packageName  { } hash is used.</p>
        <p id="p_15215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15215"> When evaluated, a typeglob produces a scalar value that represents the first objects of that name. This includes file handles, format specifiers, and subroutines.</p>
        <p id="p_15216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15216"> Using Symbolic References... Again</p>
        <p id="p_15217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15217"> Using brackets around references makes constructing strings easier:</p>
        <p id="p_15218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15218"> $road = ($w) ? "free":"high";</p>
        <p id="p_15219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15219"> print "${road}way";</p>
        <p id="p_15220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15220"> The preceding line prints  highway  or  freeway  depending on the value of  $w.  This syntax will be familiar to you if you write make files or shell scripts. In fact, you can use this $ { variable } construct outside of double quotes, as in the following example:</p>
        <p id="p_15221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15221"> print ${road};</p>
        <p id="p_15222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15222"> print ${road} . "way";</p>
        <p id="p_15223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15223"> print ${ road } . "way";</p>
        <p id="p_15224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15224"> You can also use reserved words in the $ { } brackets. Check out the following lines:</p>
        <p id="p_15225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15225"> $if = "road";</p>
        <p id="p_15226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15226"> print "\n  ${if}  way \n";</p>
        <p id="p_15227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15227"> Using reserved words for anything other than their intended purpose, however, is playing with fire. Be imaginative and make up your own variables. You can use reserved words but will have to remember to force interpretation as a reserved word by adding anything that makes it more than a reference. It's generally not a good idea to use a variable called $ { while }, because it is confusing to read.</p>
        <p id="p_15228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15228"> When you work with hashes, you have to create an extra reference to the index. In other words, you cannot use something like this:</p>
        <p id="p_15229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15229"> $clients { \$credit }</p>
        <p id="p_15230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15230"> "despicable" ;</p>
        <p id="p_15231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15231"> The \ $  credit  variable will be converted to a string and won't be used correctly as an index in the hash. You have to use a two-step procedure such as this:</p>
        <p id="p_15232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15232"> $chist = \@credit;</p>
        <p id="p_15233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15233"> $x{ $chist } = "despicable";</p>
        <p id="p_15234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15234"> Declaring Variables with Curly Braces</p>
        <p id="p_15235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15235"> The preceding section brings up an interesting point about curly braces for a use other than indexing into hashes. In Perl, curly braces are usually reserved for delimiting blocks of code. Assume you were returning the passed list by sorting it in reverse order. The passed list is in @_ of the called subroutine, so the following two statements are equivalent:</p>
        <p id="p_15236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15236"> sub backward {</p>
        <p id="p_15237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15237"> { reverse sort @_ ; }</p>
        <p id="p_15238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15238"> };</p>
        <p id="p_15239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15239"> sub backward {</p>
        <p id="p_15240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15240"> reverse sort @</p>
        <p id="p_15241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15241"> };</p>
        <p id="p_15242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15242"> When preceded by the @ operator, curly braces enable you to set up small blocks of evaluated code.</p>
        <p id="p_15243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15243"> #!/usr/bin/perl</p>
        <p id="p_15244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15244"> sub average {</p>
        <p id="p_15245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15245"> ($a,$b,$c) = @_;</p>
        <p id="p_15246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15246"> $x = $a + $b + $c;</p>
        <p id="p_15247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15247"> $x2 = $a*$a + $b*$b + $c*$c;</p>
        <p id="p_15248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15248"> return  ($x/3, $x2/3 ); }</p>
        <p id="p_15249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15249"> print "The midpt  is @{[^average($x,$y,$z)]} \n";</p>
        <p id="p_15250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15250"> This script prints 27 and 1121.6666. In the last line of code with the @ { } in the double-quoted string, the contents of the @ { } are evaluated as a block of code. The block creates a reference to an anonymous array that contains the results of the call to the subroutine  average  ($x, $y, $z ) . The array is constructed because of the brackets around the call. As a result, the [ ] construct returns a reference to an array, which in turn is converted by @ { } into a string and inserted into the double-quoted string.</p>
        <p id="p_15251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15251"> More on Hard Versus Symbolic References</p>
        <p id="p_15252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15252"> By now, you should be able to see the difference between hard and symbolic links. Let's look at some of the minor details of the two types of links and how these links are handled in Perl.</p>
        <p id="p_15253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15253"> When you use a symbolic reference that does not exist, Perl creates the variable for you and uses it. For variables that already exist, the value of the variable is substituted for the $  variable  string. This substitution is a powerful feature of Perl because you can construct variable names from variable names.</p>
        <p id="p_15254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15254"> Consider the following example:</p>
        <p id="p_15255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15255"> Look at line 5. The $ $  lang  is first reduced to $  j ava.  Then recognizing that $  j ava  can also be re-parsed, the value of $  java  ("coffee")  is used.</p>
        <p id="p_15256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15256"> The value of the scalar produced by $ $  lang  is taken to be the name of a new variable, and the variable at $name  is used. The following is the output from this example:</p>
        <p id="p_15257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15257"> java</p>
        <p id="p_15258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15258"> hot java coffee</p>
        <p id="p_15259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15259"> The difference between a hard reference  ($lang)  and a symbolic reference  ($$lang)  is how the variable name is derived. With a hard reference, you are referring to a variable's value directly. Either the variable exists in the symbol table for the package you are in (that is, which lexical context you are in), or the variable does not exist. With a symbolic reference, you are using another level of indirection by constructing or deriving a symbol name from an existing variable.</p>
        <p id="p_15260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15260"> To force only hard references in a program and protect yourself from accidentally creating symbolic references, you can use the module called  strict,  which forces Perl to do strict type checking. To use this module, place the following statement at the top of your Perl script:</p>
        <p id="p_15261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15261"> use strict  'refs';</p>
        <p id="p_15262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15262"> From this point on, only hard references are allowed for the rest of the script. You place this  use strict . . . statement within curly braces to limit the type checking to the code block within the braces. For example, in the following code, the type checking would be limited to the code in the subroutine  j ava () :</p>
        <p id="p_15263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15263"> sub java {</p>
        <p id="p_15264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15264"> use strict  "refs";</p>
        <p id="p_15265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15265"> #</p>
        <p id="p_15266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15266"> # type checking here.</p>
        <p id="p_15267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15267"> }</p>
        <p id="p_15268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15268"> # no type checking here.</p>
        <p id="p_15269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15269"> To turn off the strict type checking at any time within a code block, use this statement:</p>
        <p id="p_15270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15270"> no strict  'refs';</p>
        <p id="p_15271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15271"> One last point: Symbolic references cannot be used on variables declared with the  my  construct because these variables are not kept in any symbol table. Variables declared with the  my  construct are valid only for the block in which they are created. Variables declared with the  local  word are visible to all ensuing</p>
        <p id="p_15272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15272"> lower code blocks because they are in a symbol table.</p>
        <p id="p_15273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15273"> For More Information</p>
        <p id="p_15274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15274"> In addition to consulting the obvious documents such as the Perl man pages, look at the Perl source code for more information. The '  t / op  ' directory in the Perl source tree has some regression test routines that should definitely get you thinking. A lot of documents and references are available at the Web sites</p>
        <p id="p_15275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15275"> www.perl.com  and  www.metronet.com.</p>
        <p id="p_15276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15276"> Summary</p>
        <p id="p_15277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15277"> The two types of references in Perl 5 are hard and symbolic. Hard links work like hard links in UNIX file systems. You can have more than one hard link to the same item; Perl keeps a reference count for you. This reference count is incremented or decremented as references to the item are created or destroyed. When the count goes to zero, the link and the object it is pointing to are both destroyed. Symbolic links, which are created through the $ { } construct, are useful in providing multiple stages of references to objects.</p>
        <p id="p_15278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15278"> You can have references to scalars, arrays, hashes, subroutines, and even other references. References themselves are scalars and have to be de-referenced to the context before being used. Use @  $pointer  for an array,  %$pointer  for a hash, &amp;  $pointer  for a subroutine, and so on for dereferencing.</p>
        <p id="p_15279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15279"> Multidimensional arrays are possible using references in arrays and hashes.</p>
        <p id="p_15280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15280"> Parameters are passed into a subroutine through references. The @_ array is really all the passed parameters concatenated in one long array. To send separate arrays, use the references to the individual items.</p>
        <p id="p_15281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15281"> Tomorrow's lesson covers Perl objects and references to objects. We have deliberately not covered Perl objects in this chapter because it requires some knowledge of references. References are used to create and refer to objects, constructors, and packages.</p>
        <p id="p_15282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15282"> Q&amp;A</p>
        <p id="p_15283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15283"> Q: How do I know what type of address a pointer is pointing to?</p>
        <p id="p_15284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15284"> A: The address printed out with the  print  statement on a reference has a qualifier word in front of it.</p>
        <p id="p_15285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15285"> For example, a reference to a hash has the word  HASH  followed by an address value, an array has the word  ARRAY,  and so on.</p>
        <p id="p_15286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15286"> Q: How are multidimensional arrays possible using Perl?</p>
        <p id="p_15287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15287"> A: References in Perl point to scalars only. References to arrays point to the beginning of the array.</p>
        <p id="p_15288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15288"> Arrays can contain references to other arrays, hashes, and so on. The way to create multidimensional arrays in Perl is by using references to references.</p>
        <p id="p_15289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15289"> Q: What's the best way to pass more than one array into a subroutine?</p>
        <p id="p_15290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15290"> A: Pass references to the arrays, using the \ @arrayname for each array passed-as in the following</p>
        <p id="p_15291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15291"> call:</p>
        <p id="p_15292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15292"> mysub(\@one,  \@two);</p>
        <p id="p_15293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15293"> Within the subroutine, take each reference off one at a time.</p>
        <p id="p_15294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15294"> my  ($a,   $b)  =  @_;</p>
        <p id="p_15295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15295"> Now use @  $a  and @ $b to get to the arrays passed into the subroutines.</p>
        <p id="p_15296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15296"> Q: Why is *moo more efficient to use than $_main { ' moo ' } ? Is there a difference in usage?</p>
        <p id="p_15297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15297"> A: Both *moo and $_main { ' moo ' } mean the same variable (as long as you aren't using a package).</p>
        <p id="p_15298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15298"> * moo is more efficient because the reference is looked up once at compile time, whereas $_main { ' moo ' } is evaluated at runtime and evaluated each time it is run.</p>
        <p id="p_15299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15299"> Workshop</p>
        <p id="p_15300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15300"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_15301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15301"> Quiz</p>
        <p id="p_15302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15302"> 1. Given that  $pointer  is a pointer to a hash, what's wrong with the following line of code?</p>
        <p id="p_15303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15303"> $x= ${$pointer-&gt;{$i}} ;</p>
        <p id="p_15304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15304"> 2.  Why is  $b  not being set in the following line of code? What do you have to do to make it okay?</p>
        <p id="p_15305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15305"> sub xxx { my ($a, $b) = @_;</p>
        <p id="p_15306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15306"> }</p>
        <p id="p_15307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15307"> 3. What's the difference between these two lines of code? printf "$i : $$pointer[$i++];</p>
        <p id="p_15308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15308"> printf " and $i : $pointer-&gt;[$i++]; \n";</p>
        <p id="p_15309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15309"> 4. What do the following lines of code print out?</p>
        <p id="p_15310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15310"> $HelpHelpHelp = WV'Help"; print $$$$HelpHelpHelp;</p>
        <p id="p_15311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15311"> 5. What's the use of the $ { variable } construct? How could the following three lines of code be rewritten?</p>
        <p id="p_15312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15312"> $name = ${$scalarref};</p>
        <p id="p_15313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15313"> draw(@{$coordinates}, $display);</p>
        <p id="p_15314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15314"> ${$months}[0] = "March";</p>
        <p id="p_15315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15315"> Exercises</p>
        <p id="p_15316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15316"> 1. Write a Perl script to print out address types of different variables and complex structures.</p>
        <p id="p_15317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15317"> 2.  Write a Perl code fragment that constructs an array of pointers to functions. How would you use it? Strong Hint:</p>
        <p id="p_15318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15318"> $foo = sub too { print "foo\n"; }</p>
        <p id="p_15319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15319"> $bar = sub bar { print "bar\n"; }</p>
        <p id="p_15320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15320"> $yuk = sub yuk { print "yuk\n"; }</p>
        <p id="p_15321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15321"> $huh = sub huh { print "huh\n"; }</p>
        <p id="p_15322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15322"> @list = ($foo, $bar, $yuk, $huh);</p>
        <p id="p_15323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15323"> 3. Explain the difference between hard and symbolic references.</p>
        <p id="p_15324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15324"> 4. Write a Perl subroutine that takes two arrays as arguments and returns the reverse-sorted copy of each array.</p>
        <p id="p_15325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15325"> 5. Modify the following script to print the value of  $this  and  $that.  Are they the same? If not, why not?</p>
        <p id="p_15326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15326"> #!/usr/bin/perl sub print_coor{ my  ($x,$y,$z)  = @_;</p>
        <p id="p_15327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15327"> print "$x $y $z \n"; return  $x;};</p>
        <p id="p_15328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15328"> $ k = 1 ;</p>
        <p id="p_15329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15329"> $j  = 2;</p>
        <p id="p_15330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15330"> $m = 4;</p>
        <p id="p_15331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15331"> $this = print_coor( $k, $j , $m);</p>
        <p id="p_15332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15332"> $that = print_coor(4,5,6);</p>
        <p id="p_15333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15333"> Chapter 19</p>
        <p id="p_15334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15334"> Object-Oriented Programmin g  in Perl</p>
        <p id="p_15335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15335"> by Kamran Husain</p>
        <p id="p_15336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15336"> CONTENTS</p>
        <p id="p_15337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15337"> •  An Introduction to Modules</p>
        <p id="p_15338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15338"> o  The Three Important Rules</p>
        <p id="p_15339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15339"> •  Classes in Perl</p>
        <p id="p_15340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15340"> •  Creating a Class</p>
        <p id="p_15341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15341"> •  Blessing a Constructor</p>
        <p id="p_15342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15342"> o  Instance Variables</p>
        <p id="p_15343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15343"> •  Methods</p>
        <p id="p_15344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15344"> •  Exporting Methods</p>
        <p id="p_15345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15345"> •  Invoking Methods</p>
        <p id="p_15346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15346"> •  Overrides</p>
        <p id="p_15347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15347"> •  Destructors</p>
        <p id="p_15348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15348"> •  Inheritance</p>
        <p id="p_15349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15349"> •  Overriding Methods</p>
        <p id="p_15350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15350"> •  A Few Comments About Classes and Objects in Perl</p>
        <p id="p_15351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15351"> •  Summary</p>
        <p id="p_15352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15352"> •  Q&amp;A</p>
        <p id="p_15353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15353"> •  Workshop</p>
        <p id="p_15354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15354"> o  Quiz o  Exercises</p>
        <p id="p_15355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15355"> Today's lesson teaches you how to use the object-oriented programming (OOP) features of Perl as well as how to construct objects in Perl. The discussion also includes inheritance, overriding methods, and data encapsulation.</p>
        <p id="p_15356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15356"> An Introduction to Modules</p>
        <p id="p_15357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15357"> A  module  is a Perl package. Objects in Perl are based on references to data items within a package. An object  in Perl is simply a reference to something that knows which class it belongs to. (References are covered on  Day 18 ,  "References in Perl 5.") For more information, you can consult the  perlmod  and per lob j  text files at  http: //www.metronet.  com .  These files are the primary source of information on the Internet about Perl modules.</p>
        <p id="p_15358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15358"> In object-oriented programming with other languages, you declare a class and then create objects of that class. All objects of a particular class behave in a certain way, which is governed by the methods of that class. You can create new classes by defining new ones or by inheriting properties from an existing class.</p>
        <p id="p_15359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15359"> Programmers already familiar with object-oriented principles will recognize the terminology used here. Perl is, and pretty much always has been, an object-oriented language. In Perl 4, the use of packages provides different symbol tables from which to choose symbol names. Perl 5 changes the syntax a bit and somewhat formalizes the use of objects.</p>
        <p id="p_15360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15360"> The Three Important Rules</p>
        <p id="p_15361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15361"> The next three declarations are extremely important to understanding how objects, classes, and methods work in Perl.</p>
        <p id="p_15362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15362"> • A  class  is a Perl package. This package for a class provides the methods for objects.</p>
        <p id="p_15363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15363"> • A  method  is simply a Perl subroutine. The only catch with writing such methods is that the name of the class is the first argument.</p>
        <p id="p_15364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15364"> • An object in Perl is simply a reference to some data item within the class.</p>
        <p id="p_15365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15365"> The rest of today's lesson covers each of the preceding items in more detail.</p>
        <p id="p_15366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15366"> Classes in Perl</p>
        <p id="p_15367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15367"> One rule is important enough to repeat: A Perl class is simply a package. When you see a Perl document that refers to a "class," think "package." Existing Perl 5 syntax enables you to create a class. If you are already a C programmer, you do not have to know a lot of new syntax. What might be a new concept to Perl 4 programmers is the use of the double colon (: :) to signify the base and inherited classes.</p>
        <p id="p_15368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15368"> One of the key features of OOP is inheritance. The inheritance feature offered by Perl, however, is not the same as you might expect from other object-oriented languages. Perl classes inherit methods only; you must use your own mechanisms to implement data inheritance.</p>
        <p id="p_15369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15369"> Because each class is a package, it has its own name space with its own associative array of symbol names. Each class can therefore use its own independent set of symbol names. As with package references, you can address the variables in a class with the back quote (') operator. Members of a class are addressed as $ class ' $member.  In Perl 5, you can use the double colon instead of the ' to get the reference. For example,  $ class ' member  is the same as  $ class : : $member.</p>
        <p id="p_15370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15370"> Creating a Class</p>
        <p id="p_15371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15371"> This section covers the requisite steps to take when you create a new class. The example illustrates the semantics in the creation of a simple class called  Cocoa,  which is used for printing the required parts of a source code file for a simple Java application. You will not become a Java expert, nor will this package require you to have any experience in Java; the focus is the concept of creating a class. The example could have just as easily used a phone book application, but how many similar examples have you already seen in books?</p>
        <p id="p_15372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15372"> NOTE</p>
        <p id="p_15373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15373"> I am currently still developing the package  Java. pm.  It's named Cocoa. pm  in development because it does not have the high caffeine content of a full-featured, or even mildly useful,  Java. pm  package. Perhaps after reading today's lesson you will be able to contribute to the  Java. pm  Perl package; if so, send e-mail to khusain@ikra.com.</p>
        <p id="p_15374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15374"> Time now for a shameless plug for  Perl Unleashed,  which is also by Sams Publishing, due the summer of 1996. It will contain gobs of information about writing and using classes and packages-and track the initial development stages of the  Java. pm  package. (Hmmm. Maybe the package should be called  Bean. pm  in its early stages.)</p>
        <p id="p_15375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15375"> First of all, create a package file called  Cocoa. pm.  (The .  pm  extension, which is the default extension for packages, stands for Perl module.) A module is a package, and a package is a class for all practical purposes. Before you do anything else, place a 1; in the file. As you add more lines to the package file, make sure you keep the 1; as the last line. The following code shows the basic structure of the file:</p>
        <p id="p_15376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15376"> package Cocoa;</p>
        <p id="p_15377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15377"> #</p>
        <p id="p_15378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15378"> # Put "require" statements in for all required,imported packages</p>
        <p id="p_15379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15379"> #</p>
        <p id="p_15380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15380"> #</p>
        <p id="p_15381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15381"> # Just add code here</p>
        <p id="p_15382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15382"> #</p>
        <p id="p_15383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15383"> 1; # terminate the package with the required 1;</p>
        <p id="p_15384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15384"> This requirement is important: Don't forget to always keep the 1; line as the last of the package file. This statement is required for all packages in Perl. If you forget this statement, your package will not be processed by Perl.</p>
        <p id="p_15385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15385"> Congratulations; you have just created your first package file. Now you are ready to add your methods to this package and make it a class. The first method you should add is the  new  () method, which must be called whenever you create a new object. The  new  () method is the constructor for the object.</p>
        <p id="p_15386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15386"> Blessing a Constructor</p>
        <p id="p_15387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15387"> A  constructor  is a Perl subroutine in a class that returns a reference to something that has the class name attached to it. Connecting a class name with a reference is referred to as "blessing" an object because the function to establish the connection is called  bless.</p>
        <p id="p_15388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15388"> The following code segment shows the syntax for the  bless  function:</p>
        <p id="p_15389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15389"> bless YeReference [,classname]</p>
        <p id="p_15390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15390"> YeReference  is the reference to the object being blessed. The  classname  is optional and specifies the name of the package from which the object will get methods. If the  classname  is not specified, the name of the current package is used instead.</p>
        <p id="p_15391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15391"> The way to create a constructor in Perl is to return a reference to an internal structure that has been blessed into this  Cocoa  class. Listing 19.1 shows the initial  Cocoa. pm  package.</p>
        <p id="p_15392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15392"> Type</p>
        <p id="p_15393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15393"> Listing 19.1. The initial Cocoa. pm package, package Cocoa;</p>
        <p id="p_15394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15394"> sub new {</p>
        <p id="p_15395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15395"> my $this = {}; # Create an anonymous hash, and #self points to</p>
        <p id="p_15396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15396"> it.</p>
        <p id="p_15397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15397"> bless $this; # Connect the hash to the package Cocoa,</p>
        <p id="p_15398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15398"> return $this; # Return the reference to the hash.</p>
        <p id="p_15399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15399"> }</p>
        <p id="p_15400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15400"> i;</p>
        <p id="p_15401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15401"> Output</p>
        <p id="p_15402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15402"> There is no output for Listing 19.1.</p>
        <p id="p_15403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15403"> Analysis</p>
        <p id="p_15404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15404"> The {} constructs a reference to a hash that contains no key/value pairs. The returned value to this hash is assigned to the local variable $this. The bless () function takes that reference to $this, tells the object it references that it's now a Cocoa, and returns the reference.</p>
        <p id="p_15405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15405"> The returned value to the calling function now refers to this anonymous hash. On return from the new () function, the $this reference is destroyed, but the calling function keeps a reference to this hash. Therefore, the reference count to the hash won't be zero and Perl keeps the hash in memory. (You do not have to keep it around, but it's nice to have it around for reference later.)</p>
        <p id="p_15406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15406"> To create an object, you make a call such as the following:</p>
        <p id="p_15407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15407"> $cup = new Cocoa;</p>
        <p id="p_15408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15408"> Listing 19.2 shows you how to use this package to create the constructor.</p>
        <p id="p_15409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15409"> Type</p>
        <p id="p_15410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15410"> Listing 19.2. Creating the constructor.</p>
        <p id="p_15411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15411"> 1 #!/usr/bin/perl</p>
        <p id="p_15412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15412"> 2 push (@INC,'pwd');</p>
        <p id="p_15413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15413"> 3 use Cocoa;</p>
        <p id="p_15414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15414"> 4 $cup = new Cocoa;</p>
        <p id="p_15415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15415"> Analysis</p>
        <p id="p_15416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15416"> Line 1 refers to the location of the Perl interpreter to use. Your Perl interpreter may be located at /usr/local/bin/perl or wherever you installed it.</p>
        <p id="p_15417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15417"> In line 2, the local directory is added to the search path in @ INC for the list of paths to use when looking for a package. You can create your module in a different directory and specify the path explicitly there. Had I created the package in /home/khusain/test/scripts/, line 2 would read as follows:</p>
        <p id="p_15418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15418"> push (@INC,"/home/khusain/test/scripts");</p>
        <p id="p_15419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15419"> In line  3,  you include the package Cocoa. pm to get all the functionality in your script. The  use statement asks Perl to look in the @INC path for a file named Cocoa. pm and include it in the copy of the source file being parsed. The  use  statement is required if you want to work with a class.</p>
        <p id="p_15420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15420"> Line 4 creates the Cocoa object by calling the new function on it. Now comes the beauty (and confusion and power) of Perl. There is more than one way to do this. You can rewrite line 4 as the following:</p>
        <p id="p_15421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15421"> $cup = Cocoa-&gt;new();</p>
        <p id="p_15422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15422"> If you are a C programmer, you can use the double colons (: :) to force the function new () from the Cocoa package. As a result, line 4 could also be written as the following:</p>
        <p id="p_15423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15423"> $cup =  Cocoa::new();</p>
        <p id="p_15424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15424"> Nothing prevents you from adding more code in the constructor than what is shown here. For the Cocoa. pm module, you can, if you like, print a disclaimer when each object is created. You might want to use the constructor to initialize variables or set up arrays or pointers specific to the module.</p>
        <p id="p_15425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15425"> Don't</p>
        <p id="p_15426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15426"> DO  initialize variables in your module in the constructor.</p>
        <p id="p_15427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15427"> DO  use the  my  construct to create variables in a method.</p>
        <p id="p_15428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15428"> DON’T  use the  local  construct in a method unless you really do want the variables to be passed down to other subroutines.</p>
        <p id="p_15429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15429"> DON’T  use global variables in the class module.</p>
        <p id="p_15430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15430"> TIP</p>
        <p id="p_15431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15431"> When you are working with instance variables, it is sometimes easy to visualize a Perl object as simply an associative array. Then it's easy to see that each index in the associative array is a member of that class and each item at the index of the associative array is a value of that member.</p>
        <p id="p_15432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15432"> Listing 19.3 shows what the Cocoa constructor looks like.</p>
        <p id="p_15433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15433"> Type</p>
        <p id="p_15434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15434"> Listing 19.3. Revised constructor for Cocoa. pm. sub new {</p>
        <p id="p_15435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15435"> my $this = {};</p>
        <p id="p_15436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15436"> print "\n /* \n ** Created by Cocoa.pm \n ** Use at own risk"; print "\n ** Did this code even get pass the javac compiler? print "\n **/ \n"; bless $this; return $this;</p>
        <p id="p_15437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15437"> }</p>
        <p id="p_15438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15438"> Output</p>
        <p id="p_15439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15439"> The following shows the output from running the test script called testme on this bare-bones class:</p>
        <p id="p_15440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15440"> $ testme</p>
        <p id="p_15441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15441"> /*</p>
        <p id="p_15442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15442"> ** Created by Cocoa.pm ** Use at own risk</p>
        <p id="p_15443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15443"> ** Did this code even get pass the javac compiler?</p>
        <p id="p_15444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15444"> **/</p>
        <p id="p_15445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15445"> Regardless of which of the three methods shown here you used to create the Cocoa object, you should see the same output.</p>
        <p id="p_15446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15446"> Analysis</p>
        <p id="p_15447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15447"> Great. Now you've created some comments at the beginning of a file with some print</p>
        <p id="p_15448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15448"> statements. You can just as easily call other functions in or outside of the package to get more initialization functionality. For example, as development progresses, you see the  new  () function evolve to resemble the following:</p>
        <p id="p_15449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15449"> sub new {</p>
        <p id="p_15450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15450"> my $this = {} bless $this;</p>
        <p id="p_15451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15451"> $this-&gt;doInitialization() ; return $this;</p>
        <p id="p_15452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15452"> }</p>
        <p id="p_15453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15453"> When you create any given class, you should allow it to be inherited. You should be able to call the new  operator with the class name as the first parameter. This capability to parse the class name from the first argument causes the class to be inherited. As a result, the new function becomes more or less like the following:</p>
        <p id="p_15454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15454"> sub new {</p>
        <p id="p_15455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15455"> my $class = shift; # Get the request class name</p>
        <p id="p_15456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15456"> my $this = {} ;</p>
        <p id="p_15457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15457"> bless $this, $class # Use class name to bless ()</p>
        <p id="p_15458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15458"> reference</p>
        <p id="p_15459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15459"> $this-&gt;doInitialization() ; return $this;</p>
        <p id="p_15460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15460"> }</p>
        <p id="p_15461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15461"> The preceding method forces your class users to make calls in the form of one of three ways:</p>
        <p id="p_15462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15462"> • Cocoa::new()</p>
        <p id="p_15463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15463"> • Cocoa-&gt;new()</p>
        <p id="p_15464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15464"> • new Cocoa;</p>
        <p id="p_15465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15465"> What if you wanted to use a reference to the object instead, such as $ob j-&gt;new () ? The dolnitialization () method used will be whatever  $class  you blessed the object into. The following code uses the function call ref () to determine if the class exists per se. The ref () function returns true if the item passed to it is a reference and null if it is not a reference. With classes, the true value returned from the ref () function is the name of the class.</p>
        <p id="p_15466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15466"> sub new {</p>
        <p id="p_15467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15467"> my $this = shift; # Get the class name</p>
        <p id="p_15468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15468"> my $class = ref($this) || $this;</p>
        <p id="p_15469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15469"> A# If class exists, use it else use reference, my $this = {};</p>
        <p id="p_15470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15470"> bless $this, $class $this-&gt;doInitialization() ;</p>
        <p id="p_15471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15471"> return $this;</p>
        <p id="p_15472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15472"> }</p>
        <p id="p_15473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15473"> Within the class package, the methods typically treat the reference as an ordinary reference. Outside the class package, the reference is generally treated as an opaque value that can only be accessed through the class's methods. You can access the values within the package directly, but it's not a good idea to do so because such access defeats the whole purpose of object orientation.</p>
        <p id="p_15474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15474"> It's possible to bless a reference object more than once. However, the caveat is that the new class must get rid of the object at the previously blessed reference. For C and Pascal programmers, this is like assigning a pointer to malloced memory and then assigning the same pointer to another location without first freeing the previous location. In effect, a Perl object must belong to only one class at a time.</p>
        <p id="p_15475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15475"> What's the real difference between an object and a reference? Perl objects are blessed to belong to a class. References are not blessed; if they are, they belong to a class and are objects. Objects know to which class they belong. References do not have a class to which they belong.</p>
        <p id="p_15476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15476"> Instance Variables</p>
        <p id="p_15477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15477"> The arguments to a  new  () function for a constructor are called  instance variables.  Instance variables are used to do initialization for each instance of an object as it's created. For example, the  new () function could expect a name for each new instance of the object created. Using instance variables allows you to customize each object as it is created.</p>
        <p id="p_15478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15478"> You can use either an anonymous array or anonymous hash to hold instance variables. To use a hash to store the parameters coming in, the code would resemble the following:</p>
        <p id="p_15479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15479"> sub new {</p>
        <p id="p_15480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15480"> my $type = shift; my %parm = my $this = {} ;</p>
        <p id="p_15481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15481"> $this-&gt;{'Name  1 } = $parm{'Name'};</p>
        <p id="p_15482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15482"> $this-&gt;{'x'} = $parm{'x'};</p>
        <p id="p_15483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15483"> $this-&gt;{'y'} = $parm{'y'};</p>
        <p id="p_15484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15484"> bless $this, $type;</p>
        <p id="p_15485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15485"> }</p>
        <p id="p_15486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15486"> You can also use an array instead of a hash to store the instance variables.</p>
        <p id="p_15487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15487"> sub new {</p>
        <p id="p_15488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15488"> my $type = shift; my %parm = my $this = [] ;</p>
        <p id="p_15489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15489"> $this-&gt;[0] = $parm{'Name'};</p>
        <p id="p_15490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15490"> $this-&gt;[l] = $parm{'x'};</p>
        <p id="p_15491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15491"> $this-&gt;[2] = $parm{'y'}; bless $this, $type;</p>
        <p id="p_15492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15492"> }</p>
        <p id="p_15493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15493"> To construct an object, you can pass the parameters with the new () function call. For example, the call to create the Cocoa object becomes the following:</p>
        <p id="p_15494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15494"> $mug = Cocoa::new( 'Name' =&gt; 'top',</p>
        <p id="p_15495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15495"> 'x' =&gt; 10,</p>
        <p id="p_15496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15496"> 'y'  =&gt;20 ) ;</p>
        <p id="p_15497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15497"> The =&gt; operator has the same function of the comma operator, but =&gt; is a bit more readable. You can write this code with commas instead of the =&gt; operator if you prefer.</p>
        <p id="p_15498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15498"> To access the variables as you would any other data members, you can use the following statements:</p>
        <p id="p_15499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15499"> Methods</p>
        <p id="p_15500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15500"> A  method  in a Perl class is simply a Perl subroutine. Perl doesn't provide any special syntax for method definition. A method expects its first argument to be the object or package on which it is invoked. Perl has two types of methods: static and virtual.</p>
        <p id="p_15501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15501"> A  static  method expects a class name as the first argument. A  virtual  method expects a reference to an object as the first argument. The way each method handles the first argument determines whether the method is static or virtual.</p>
        <p id="p_15502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15502"> A static method applies functionality to the entire class as a whole because it uses the name of the class. Functionality in static methods is therefore applicable to all objects of the class. Generally, static methods ignore the first argument because they already know which class they are in. Constructors are static methods.</p>
        <p id="p_15503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15503"> A virtual method expects a reference to an object as its first argument. Typically, the first thing a virtual method does is shift the first argument into a self or this variable and then use that shifted value as an ordinary reference. For example, consider the following code:</p>
        <p id="p_15504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15504"> 1. sub nameLister {</p>
        <p id="p_15505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15505"> 2. my $this = shift;</p>
        <p id="p_15506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15506"> 3. my ($keys ,$value );</p>
        <p id="p_15507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15507"> 4. while  (($key,  $value) = each  (%$this))  {</p>
        <p id="p_15508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15508"> 5.  print  "\t$key is $value.\n";</p>
        <p id="p_15509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15509"> 6 . }</p>
        <p id="p_15510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15510"> 7. }</p>
        <p id="p_15511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15511"> Line 2 in the listing is where the $this variable is set to point to the object. In line 4, the $this</p>
        <p id="p_15512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15512"> array is de-referenced at every  $key  location.</p>
        <p id="p_15513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15513"> TIP</p>
        <p id="p_15514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15514"> Look at the .pm files in the Perl distribution for sample code that will show you how methods are declared and used.</p>
        <p id="p_15515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15515"> Exporting Methods</p>
        <p id="p_15516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15516"> If you tried to invoke the Cocoa. pm package right now, you'd get an error message from Perl at compile time about the methods not being found. This error occurs because the Cocoa. pm methods have not been exported. To export these functions, you need the Exporter module. Add the following lines to the beginning of code in the package:</p>
        <p id="p_15517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15517"> require Exporter; ©ISA = qw(Exporter) ;</p>
        <p id="p_15518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15518"> These two lines force the inclusion of the Exporter. pm module and then set the ©ISA array with the name of the Exporter class to look for.</p>
        <p id="p_15519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15519"> To export your own class's methods, you list them in the ©EXPORT array. For example, to export the closeMain and declareMain methods, you use the following statement:</p>
        <p id="p_15520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15520"> ©EXPORT(declareMain, closeMain);</p>
        <p id="p_15521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15521"> Inheritance in a Perl class is through the  ©ISA  array. The  ©ISA  array does not have to be defined in every package; however, when it is defined, Perl treats it as a special array of directory names. This array is similar to the  ©INC  array, where directories are searched for files to include. The  ©ISA  array contains the names of the classes (packages) to look for methods in other classes in if a method in the current package is not found. The  ©ISA  array contains the names of the base classes from which the current class inherits. The search is done in the order that the classes are listed in the  ©ISA  arrays.</p>
        <p id="p_15522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15522"> All methods called by a class must belong to the same class or the base classes defined in the  ©ISA array. If a method isn't found in the  ©ISA  array, Perl looks for an  AUTOLOAD  () routine. This optional routine is defined as  sub  in the current package. To use the  AUTOLOAD  function, you call the autoload. pm package with the  use  Autoload; statement. The  AUTOLOAD  function tries to load the called function from the installed Perl libraries. If the  AUTOLOAD  call also fails, Perl makes one final try at the  UNIVERSAL  class, which is the catch-all for all methods not defined elsewhere. Perl generates an error about unresolved functions if this step also fails.</p>
        <p id="p_15523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15523"> Invoking Methods</p>
        <p id="p_15524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15524"> There are two ways to invoke a method for an object: by making a reference to an object (virtual) or explicitly referring to the class name (static). You have to export a method to be able to call it. Add a few more methods to the Cocoa class to get the file to resemble the following code:</p>
        <p id="p_15525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15525"> package Cocoa; require Exporter;</p>
        <p id="p_15526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15526"> @ISA = qw(Exporter);</p>
        <p id="p_15527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15527"> ©EXPORT = qw(setImports, declareMain, closeMain);</p>
        <p id="p_15528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15528"> #</p>
        <p id="p_15529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15529"> # This routine creates the references for imports in Java functions</p>
        <p id="p_15530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15530"> #</p>
        <p id="p_15531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15531"> sub setImports{</p>
        <p id="p_15532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15532"> my $class = shift my @names =</p>
        <p id="p_15533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15533"> foreach (©names) {</p>
        <p id="p_15534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15534"> print "import " . $_ .  ";\n";</p>
        <p id="p_15535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15535"> }</p>
        <p id="p_15536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15536"> }</p>
        <p id="p_15537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15537"> #</p>
        <p id="p_15538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15538"> # This routine declares the main function in a Java script</p>
        <p id="p_15539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15539"> #</p>
        <p id="p_15540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15540"> sub declareMain{</p>
        <p id="p_15541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15541"> my $class = shift @_;</p>
        <p id="p_15542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15542"> my ( $name, $extends, $implements) = @_;</p>
        <p id="p_15543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15543"> print "\n public class $name"; if ($extends) {</p>
        <p id="p_15544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15544"> print " extends " . $extends;</p>
        <p id="p_15545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15545"> }</p>
        <p id="p_15546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15546"> if ($implements) {</p>
        <p id="p_15547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15547"> print " implements " . $implements;</p>
        <p id="p_15548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15548"> }</p>
        <p id="p_15549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15549"> print " { \n";</p>
        <p id="p_15550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15550"> }</p>
        <p id="p_15551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15551"> #</p>
        <p id="p_15552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15552"> # This routine declares the main function in a Java script</p>
        <p id="p_15553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15553"> #</p>
        <p id="p_15554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15554"> sub closeMain{ print "} \n";</p>
        <p id="p_15555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15555"> }</p>
        <p id="p_15556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15556"> #</p>
        <p id="p_15557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15557"> # This subroutine creates the header for the file.</p>
        <p id="p_15558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15558"> #</p>
        <p id="p_15559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15559"> sub new {</p>
        <p id="p_15560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15560"> my $this = {};</p>
        <p id="p_15561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15561"> print "\n /* \n ** Created by Cocoa.pm \n ** Use at own risk \n */ \n";</p>
        <p id="p_15562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15562"> bless $this;</p>
        <p id="p_15563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15563"> return $this;</p>
        <p id="p_15564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15564"> }</p>
        <p id="p_15565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15565"> i;</p>
        <p id="p_15566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15566"> Now, write a simple Perl script to use the methods for this class. Because you can only start and close the header, examine the following code for a script to create a skeleton Java applet source:</p>
        <p id="p_15567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15567"> #!/usr/bin/perl</p>
        <p id="p_15568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15568"> use Cocoa;</p>
        <p id="p_15569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15569"> $cup = new Cocoa;</p>
        <p id="p_15570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15570"> $cup-&gt;setImports( 'java.io.InputStream',  'java.net.*'); $cup-&gt;declareMain( "Msg" , "java.applet.Applet", "Runnable"); $cup-&gt;closeMain();</p>
        <p id="p_15571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15571"> This script generates code for a Java applet called Msg that extends the java. applet. Applet applet and implements functions that are runnable. You call the function with the $cup-&gt; . . . call. The following three lines of code:</p>
        <p id="p_15572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15572"> $cup-&gt;setImports( 'java.io.InputStream',  'java.net.*');3</p>
        <p id="p_15573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15573"> $cup-&gt;declareMain( "Msg" , "java.applet.Applet", "Runnable"); $cup-&gt;closeMain();</p>
        <p id="p_15574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15574"> could be rewritten as functions:</p>
        <p id="p_15575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15575"> Cocoa::setlmports($cup, 'java.io.InputStream',  'java.net.*');</p>
        <p id="p_15576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15576"> Cocoa::declareMain($cup, "Msg" , "java.applet.Applet", "Runnable");</p>
        <p id="p_15577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15577"> Cocoa::closeMain($cup);</p>
        <p id="p_15578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15578"> This type of equivalence was shown in the section "Blessing a Constructor," earlier today. In both cases, the first parameter is the reference to the object itself. Running the test script shown generates</p>
        <p id="p_15579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15579"> the following output:</p>
        <p id="p_15580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15580"> /*</p>
        <p id="p_15581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15581"> ** Created by Cocoa.pm ** Use at own risk */</p>
        <p id="p_15582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15582"> import java.io.InputStream; import j ava.net.*;</p>
        <p id="p_15583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15583"> public class Msg extends java.applet.Applet implements Runnable {</p>
        <p id="p_15584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15584"> }</p>
        <p id="p_15585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15585"> An important note about calling the methods: If you have any arguments in a method, use parentheses if you are using the -&gt; (also known as indirect) method. The parentheses are required to include all the arguments with the following statement:</p>
        <p id="p_15586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15586"> $cup-&gt;setImports( 'java.io.InputStream',  'java.net.*');</p>
        <p id="p_15587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15587"> However, the following statement:</p>
        <p id="p_15588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15588"> Cocoa::setlmports($cup, 'java.io.InputStream',  'java.net.*');</p>
        <p id="p_15589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15589"> can also be rewritten without parentheses as this:</p>
        <p id="p_15590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15590"> Cocoa::setlmports $cup, 'java.io.InputStream',  'java.net.*'  ;</p>
        <p id="p_15591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15591"> The choice is yours about how you make your code readable to other programmers. Use parentheses if you feel that it will make the code more readable.</p>
        <p id="p_15592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15592"> Overrides</p>
        <p id="p_15593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15593"> Sometimes you want to specify which class's method to use, such as when the same named method is specified in two different classes. For example, if the function grind is defined in both Espresso and Qava classes, you can specify which class's function to use by using the : : operator. The</p>
        <p id="p_15594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15594"> following calls would use the call in Espresso:</p>
        <p id="p_15595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15595"> $mess = Espresso::grind("whole","lotta","bags");</p>
        <p id="p_15596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15596"> Espresso::grind($mess, "whole","lotta","bags");</p>
        <p id="p_15597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15597"> The following calls would use the grind () function in the Qava class:</p>
        <p id="p_15598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15598"> $mess = Qava::grind("whole","lotta","bags");</p>
        <p id="p_15599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15599"> Qava::grind($mess, "whole","lotta","bags");</p>
        <p id="p_15600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15600"> You might want to call a method based on some action that the program you are writing has already taken. In other words, you want to use the Qava method for a certain condition and the Espresso method for another. In this case, you can use symbolic references to make the call to the required function, as in the following example:</p>
        <p id="p_15601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15601"> $method = $local ? "Qava::" : "Espresso::";</p>
        <p id="p_15602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15602"> $cup-&gt;{$method}grind(@args) ;</p>
        <p id="p_15603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15603"> Destructors</p>
        <p id="p_15604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15604"> Perl tracks the number of links to objects. When the last reference to an object is freed to the memory pool, the object is automatically destroyed. This destruction of the object could occur after your code stops and the script is about to exit. For global variables, the destruction happens after the last line in your code executes.</p>
        <p id="p_15605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15605"> If you want to capture control just before the object is freed, you can define a DESTROY () method in your class. Note the use of all capital letters in the name. The DESTROY () method is called just before the object is released, which enables you to do any necessary cleanup. The DESTROY () function does not call other DESTROY () functions automatically; Perl doesn't do nested destruction for you. If your constructor re-blessed a reference from one of your base classes, your DESTROY () might need to call DESTROY () for any base classes. All object references that are contained in a given object are freed and destroyed automatically when the current object is freed.</p>
        <p id="p_15606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15606"> Usually, you do not have to define a DESTROY function, but when you do need it, it takes the following form:</p>
        <p id="p_15607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15607"> sub DESTROY {</p>
        <p id="p_15608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15608"> #</p>
        <p id="p_15609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15609"> # Add code here.</p>
        <p id="p_15610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15610"> #</p>
        <p id="p_15611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15611"> }</p>
        <p id="p_15612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15612"> For most purposes, Perl uses a simple, reference-based garbage collection system. The number of references to any given object at the time of garbage collection must be greater than zero, or the memory for that object is freed. When your program exits, an exhaustive search-and-destroy function in Perl does garbage collection. Everything in the process is summarily deleted. In UNIX or UNIX-like systems, this might seem like a waste, but it's actually quite necessary to perform in embedded systems or in a multithreaded environment.</p>
        <p id="p_15613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15613"> Inheritance</p>
        <p id="p_15614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15614"> Methods in classes are inherited with the paths in the  @ISA  array. Variables must be set up explicitly for inheritance. Assume you define a new class called Bean. pm to include some of the functionality that another class Coffee . pm will inherit.</p>
        <p id="p_15615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15615"> The example in this section demonstrates how to inherit instance variables from one class (also referred to as a "superclass" or "base class"). The steps in inheritance require calling the superclass's constructor and adding one's own instance variables to the new object.</p>
        <p id="p_15616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15616"> In this example, the Coffee class inherits values from the base class called Bean. The two files are called Coffee . pm and Bean. pm, respectively.</p>
        <p id="p_15617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15617"> Listing 19.4 is the code for Bean. pm.</p>
        <p id="p_15618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15618"> Type</p>
        <p id="p_15619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15619"> Listing 19.4. The code for Bean. pm.</p>
        <p id="p_15620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15620"> package Bean; require Exporter;</p>
        <p id="p_15621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15621"> @ISA = qw(Exporter);</p>
        <p id="p_15622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15622"> ©EXPORT = qw(setBeanType);</p>
        <p id="p_15623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15623"> sub new {</p>
        <p id="p_15624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15624"> my $type = shift; my $this = {};</p>
        <p id="p_15625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15625"> $this-&gt;{'Bean'} = 'Colombian'; bless $this, $type; return $this;</p>
        <p id="p_15626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15626"> }</p>
        <p id="p_15627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15627"> #</p>
        <p id="p_15628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15628"> # This subroutine sets the class name sub setBeanType{</p>
        <p id="p_15629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15629"> my ($class, $name) =</p>
        <p id="p_15630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15630"> $class-&gt;{'Bean'} = $name; print "Set bean to $name \n";</p>
        <p id="p_15631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15631"> }</p>
        <p id="p_15632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15632"> i;</p>
        <p id="p_15633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15633"> Output</p>
        <p id="p_15634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15634"> Listing 19.4 has no output.</p>
        <p id="p_15635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15635"> In this listing, the $this variable sets a value in the anonymous hash for the ' Bean' type to be ' Colombian'. The setBeanType () method is also declared so that the ' Bean' type can also be changed by a program.</p>
        <p id="p_15636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15636"> The subroutine for resetting the value of '  Bean ' uses the $  class  reference to get to the anonymous hash for the object. Remember that a reference to this anonymous hash created the reference in the first place with the  new  () function.</p>
        <p id="p_15637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15637"> The values in the  Bean  class will be inherited by the  Coffee  class. The  Coffee  . pm file is shown in Listing 19.5.</p>
        <p id="p_15638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15638"> Analysis</p>
        <p id="p_15639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15639"> Listing 19.5. The Coffee . pm file.</p>
        <p id="p_15640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15640"> 1 #</p>
        <p id="p_15641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15641"> 2 # The Coffee.pm file to illustrate inheritance.</p>
        <p id="p_15642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15642"> 3 #</p>
        <p id="p_15643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15643"> 4 package Coffee;</p>
        <p id="p_15644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15644"> 5 require Exporter;</p>
        <p id="p_15645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15645"> 6 require Bean;</p>
        <p id="p_15646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15646"> 7 @ISA = qw(Exporter, Bean);</p>
        <p id="p_15647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15647"> 8 0EXPORT = qw(setlmports, declareMain, closeMain);</p>
        <p id="p_15648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15648"> 9 #</p>
        <p id="p_15649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15649"> 10 # set item</p>
        <p id="p_15650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15650"> 11  #</p>
        <p id="p_15651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15651"> 12 sub setCoffeeType{</p>
        <p id="p_15652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15652"> 13 my ($class,$name) =</p>
        <p id="p_15653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15653"> 14 $class-&gt;{'Coffee'} = $name;</p>
        <p id="p_15654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15654"> 15 print "Set coffee type to $name \n";</p>
        <p id="p_15655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15655"> 16 }</p>
        <p id="p_15656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15656"> 17 #</p>
        <p id="p_15657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15657"> 18 # constructor</p>
        <p id="p_15658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15658"> 19 #</p>
        <p id="p_15659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15659"> 20 sub new {</p>
        <p id="p_15660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15660"> 21 my $type = shift;</p>
        <p id="p_15661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15661"> 22 my $this = Bean-&gt;new();  #####  &lt;- LOOK HERE!!!  ####</p>
        <p id="p_15662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15662"> 23 $this-&gt;{'Coffee'} = 'Instant'; # unless told otherwise</p>
        <p id="p_15663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15663"> 24</p>
        <p id="p_15664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15664"> 25</p>
        <p id="p_15665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15665"> 26</p>
        <p id="p_15666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15666"> 27 1;</p>
        <p id="p_15667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15667"> bless $this, $type; return $this;</p>
        <p id="p_15668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15668"> }</p>
        <p id="p_15669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15669"> Analysis</p>
        <p id="p_15670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15670"> I Note the use of the require Bean; statement in line 6. This line forces the inclusion of the Bean. pm file and all its related functions. Lines 12 through 16 define a sub-routine to use when resetting the value of the local variable in $class-&gt;{ ' Coffee ' }.</p>
        <p id="p_15671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15671"> Look at the new () constructor for the Coffee class in line 20. The $this reference points to the anonymous hash returned by Bean. pm and not a hash created locally. In other words, the following statement creates an entirely different hash that has nothing to do with the hash created in the Bean. pm constructor:</p>
        <p id="p_15672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15672"> my $this my $this</p>
        <p id="p_15673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15673"> {}; # This is not the way to do it for inheritance.</p>
        <p id="p_15674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15674"> $theSuperClass-&gt;new(); # this is the way.</p>
        <p id="p_15675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15675"> Listing 19.6 shows how to call these functions.</p>
        <p id="p_15676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15676"> Type</p>
        <p id="p_15677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15677"> Listing 19.6. Calling inherited methods.</p>
        <p id="p_15678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15678"> 1 #!/usr/bin/perl</p>
        <p id="p_15679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15679"> 2 push (@INC,'pwd');</p>
        <p id="p_15680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15680"> 3 use Coffee;</p>
        <p id="p_15681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15681"> 4</p>
        <p id="p_15682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15682"> $cup = new Coffee;</p>
        <p id="p_15683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15683"> 5</p>
        <p id="p_15684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15684"> print "\n-Initial values-\n";</p>
        <p id="p_15685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15685"> 6 print "Coffee: $cup-&gt;{'Coffee'} \n";</p>
        <p id="p_15686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15686"> 7 print "Bean: $cup-&gt;{'Bean'} \n";</p>
        <p id="p_15687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15687"> 8 print "\n-Change Bean Type-\n";</p>
        <p id="p_15688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15688"> 9 $cup-&gt;setBeanType('Mixed');</p>
        <p id="p_15689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15689"> 10 print "Bean Type is now $cup-&gt;{'Bean'} \n";</p>
        <p id="p_15690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15690"> 11 print "\n-Change Coffee Type-\n";</p>
        <p id="p_15691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15691"> 12 $cup-&gt;setCoffeeType('Instant') ;</p>
        <p id="p_15692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15692"> 13 print "Type of coffee: $cup-&gt;{'Coffee'} \n";</p>
        <p id="p_15693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15693"> Output</p>
        <p id="p_15694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15694"> - Initial values</p>
        <p id="p_15695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15695"> Coffee: Instant Bean: Colombian</p>
        <p id="p_15696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15696"> - Change Bean Type</p>
        <p id="p_15697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15697"> Set bean to Mixed Bean Type is now Mixed</p>
        <p id="p_15698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15698"> - Change Coffee Type</p>
        <p id="p_15699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15699"> Set coffee type to Instant Type of coffee: Instant</p>
        <p id="p_15700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15700"> Analysis</p>
        <p id="p_15701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15701"> I The initial values for the ' Bean' and ' Coffee ' indices in the anonymous hash for the object are printed first. The member functions are called to set the values to different names and then</p>
        <p id="p_15702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15702"> printed.</p>
        <p id="p_15703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15703"> Methods can have several types of arguments. It's how you process the arguments that counts. For example, you can add the following method to the Coffee . pm module:</p>
        <p id="p_15704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15704"> sub makeCup {</p>
        <p id="p_15705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15705"> my ($class, $cream, $sugar, $dope) =</p>
        <p id="p_15706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15706"> print "\n================================== \n";</p>
        <p id="p_15707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15707"> print "Making a cup \n";</p>
        <p id="p_15708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15708"> print "Add cream \n" if ($cream);</p>
        <p id="p_15709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15709"> print "Add $sugar sugar cubes\n" if ($sugar);</p>
        <p id="p_15710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15710"> print "Making some really addictive coffee ;-) \n" if ($dope); print "================================== \n";</p>
        <p id="p_15711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15711"> }</p>
        <p id="p_15712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15712"> The function makeCup () takes three arguments but processes them only if it sees them. To test this functionality, consider Listing 19.7.</p>
        <p id="p_15713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15713"> Listing 19.7. Using the makeCup () function.</p>
        <p id="p_15714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15714"> 1 #!/usr/bin/perl</p>
        <p id="p_15715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15715"> 2 push (@INC,'pwd');</p>
        <p id="p_15716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15716"> 3 use Coffee;</p>
        <p id="p_15717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15717"> 4 $cup = new Coffee;</p>
        <p id="p_15718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15718"> 5 #</p>
        <p id="p_15719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15719"> 6 # With no parameters</p>
        <p id="p_15720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15720"> 7 #</p>
        <p id="p_15721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15721"> 8 print "\n Calling with no parameters: \n";</p>
        <p id="p_15722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15722"> Calling with no parameters:</p>
        <p id="p_15723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15723"> Making a cup</p>
        <p id="p_15724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15724"> Calling with one parameter:</p>
        <p id="p_15725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15725"> Making a cup Add cream</p>
        <p id="p_15726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15726"> Calling with two parameters:</p>
        <p id="p_15727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15727"> Making a cup</p>
        <p id="p_15728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15728"> Add cream</p>
        <p id="p_15729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15729"> Add 2 sugar cubes</p>
        <p id="p_15730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15730"> Calling with three parameters:</p>
        <p id="p_15731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15731"> Making a cup</p>
        <p id="p_15732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15732"> Add cream</p>
        <p id="p_15733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15733"> Add 3 sugar cubes</p>
        <p id="p_15734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15734"> Making some really addictive coffee ;-)</p>
        <p id="p_15735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15735"> Analysis</p>
        <p id="p_15736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15736"> Line 9 calls the function with no parameters. In line 14, the function call has one parameter. The parameters are passed either as strings or integers, something this particular method does not care about. Look at line 19 and line 24, where both strings and numbers are passed in the same function call. However, some methods you write in the future might require this distinction.</p>
        <p id="p_15737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15737"> In any event, you can have default values set in the function if the expected parameter is not passed. The behavior of the method can be different depending on the number of arguments you pass it.</p>
        <p id="p_15738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15738"> Overriding Methods</p>
        <p id="p_15739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15739"> Inheriting functionality from another class is beneficial in that you can get all the exported functionality of the base class in your new class. To see an example of how this works, add a function in the  Bean . pm class called print  Type.  Here's the subroutine:</p>
        <p id="p_15740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15740"> sub printType {</p>
        <p id="p_15741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15741"> my $class = shift @_;</p>
        <p id="p_15742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15742"> print "The type of Bean is $class-&gt;{'Bean'} \n";</p>
        <p id="p_15743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15743"> }</p>
        <p id="p_15744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15744"> Do not forget to update the  ©EXPORT  array by adding the name of the function to export. The new statement should look like this:</p>
        <p id="p_15745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15745"> ©EXPORT = qw(setBeanType, printType, printType);</p>
        <p id="p_15746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15746"> Now call the  printType  function. The next three lines show three ways to call the function:</p>
        <p id="p_15747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15747"> $cup-&gt;Coffee::printType ();</p>
        <p id="p_15748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15748"> $cup-&gt;printType();</p>
        <p id="p_15749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15749"> $cup-&gt;Bean::printType();</p>
        <p id="p_15750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15750"> The output from all three lines is the same:</p>
        <p id="p_15751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15751"> Why is this so? There is no printType () function in the inheriting class, so the printType () function in the base class is used instead. Naturally, if you want your own class to have its own printType function, you have to define it.</p>
        <p id="p_15752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15752"> In the Coffee . pm file, add the following lines:</p>
        <p id="p_15753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15753"> #</p>
        <p id="p_15754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15754"> # This routine prints the type of  $class-&gt;{'Coffee'}</p>
        <p id="p_15755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15755"> #</p>
        <p id="p_15756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15756"> sub printType {</p>
        <p id="p_15757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15757"> my $class = shift</p>
        <p id="p_15758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15758"> print "The type of Coffee is  $class-&gt;{'Coffee'} \n";</p>
        <p id="p_15759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15759"> }</p>
        <p id="p_15760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15760"> You must also modify the @EXPORT to work with this function:</p>
        <p id="p_15761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15761"> 0EXPORT = qw(setImports, declareMain, closeMain, printType);</p>
        <p id="p_15762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15762"> Now the output from the three lines looks like this:</p>
        <p id="p_15763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15763"> The base class function is called only when the Bean: : override is given. In the other cases, only the inherited class function is called.</p>
        <p id="p_15764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15764"> What if you do not know the base class name or even where the name is defined? In this case, you can use the SUPER: : pseudo-class reserved word. Using the SUPER: : override enables you to call an overridden superclass method without actually knowing where that method is defined. The SUPER: : construct is meaningful only within the class.</p>
        <p id="p_15765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15765"> If you're trying to control where the method search begins and you're executing in the class itself, you can use the SUPER: : pseudo class, which instructs Perl to start looking in your base class's @ISA list without explicitly naming it:</p>
        <p id="p_15766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15766"> $this-&gt;SUPER::function( ... argument list ... );</p>
        <p id="p_15767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15767"> Instead of Bean: : we can use SUPER: :. The call to the function printType () becomes</p>
        <p id="p_15768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15768"> $cup-&gt;SUPER::printType();</p>
        <p id="p_15769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15769"> and the output is the following:</p>
        <p id="p_15770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15770"> The type of Bean is Mixed</p>
        <p id="p_15771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15771"> A Few Comments About Classes and Objects in Perl</p>
        <p id="p_15772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15772"> One advertised strength of object-oriented languages is the ease with which new code can use old code. Packages in Perl let you reuse code through the use of objects and inheritance. OOP languages use data encapsulation to let you hide the inner workings of complicated code. Packages and modules in Perl provide a great deal of data encapsulation with the use of the my construct. Perl, however, does not guarantee that a class inheriting your code will not attempt to access your class variables directly, thereby eliminating the advantage of data encapsulation. They can if they really want to; however, this type of procedure is considered bad practice, and shame on you if you do it.</p>
        <p id="p_15773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15773"> Don't</p>
        <p id="p_15774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15774"> DO define methods to access class variables.</p>
        <p id="p_15775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15775"> DON’T access class variables directly from outside the module.</p>
        <p id="p_15776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15776"> When writing a package, you should ensure that everything a method needs is available through the object or is passed as a parameter to the method. From within the package, access any global variables only through references passed through methods.</p>
        <p id="p_15777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15777"> For static or global data to be used by the methods, you have to define the context of the data in the base class using the local () construct. The subclass will then call the base class to get the data for it. On occasion, a subclass might want to override that data and replace it with new data. When this happens, the superclass might not know how to find the new copy of the data. In such cases, it’s best to define a reference to the data and then have all base classes and subclasses modify the variable through that reference.</p>
        <p id="p_15778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15778"> Finally, you will see references to objects and classes such as the following:</p>
        <p id="p_15779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15779"> use Coffee::Bean;</p>
        <p id="p_15780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15780"> This code is interpreted to mean "Look for  Bean . pm in the  Coffee  subdirectory in all the directories in the  @INC  array." If I were to move  Bean . pm into the .  /Coffee  directory, all the previous examples would work with the new  use  statement. The advantage to this approach is that you have one subclass class file in one directory and the base class in a lower directory. It helps keep</p>
        <p id="p_15781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15781"> code organized. To have a statement like the following:</p>
        <p id="p_15782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15782"> use Another::Sub::Menu;</p>
        <p id="p_15783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15783"> you would see a directory sub-tree like this:</p>
        <p id="p_15784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15784"> . /Another/Sub/Menu.pm</p>
        <p id="p_15785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15785"> Summary</p>
        <p id="p_15786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15786"> This chapter provides a brief introduction to object-oriented programming in Perl. Perl provides the OOP features of data encapsulation and inheritance using modules and packages. A class in Perl is simply a package. A package for a class provides all the methods for objects created for the class.</p>
        <p id="p_15787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15787"> An object is simply a reference to data that knows which class it belongs to. A method in a class is simply a subroutine. The only catch about writing such methods is that the name of the class is always the first argument of the method.</p>
        <p id="p_15788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15788"> The  bless  () function is used to tie a reference to a class name. The  bless  () function is called in the constructor function  new  () to create an object and then connect the reference to the object with the name of the class.</p>
        <p id="p_15789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15789"> With inheritance, the base class is the class from which methods (and data) are inherited. The base class is also called the superclass. The class that inherits these items from the superclass is called the subclass. Multiple inheritance is allowed in Perl. Data inheritance is the programmer's responsibility and requires using references. The subclass is allowed to know things about its immediate superclass; the superclass is not allowed to know anything about a subclass.</p>
        <p id="p_15790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15790"> Q&amp;A</p>
        <p id="p_15791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15791"> Q: What does the  bless  () function do?</p>
        <p id="p_15792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15792"> A:  The  bless  ()  function takes one or two arguments. The first argument is a reference to an object.</p>
        <p id="p_15793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15793"> The second argument is optional and specifies the name of a class; if the name is not specified, the default is the current class. After the call, the reference uses the name as its class name. As a result, the reference becomes an object of the class whose name was specified.</p>
        <p id="p_15794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15794"> Q: What's the difference between an object and a reference?</p>
        <p id="p_15795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15795"> A: Objects are blessed; references are not. Objects belong to a class, but references do not have to.</p>
        <p id="p_15796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15796"> Q: What's the difference between static and virtual methods?</p>
        <p id="p_15797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15797"> A:  Static methods expect a class name as the first argument. Virtual methods expect a reference to an</p>
        <p id="p_15798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15798"> object as the first argument. Static methods are class-wide; virtual methods are object-specific.</p>
        <p id="p_15799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15799"> Q: I just added a method to my class file, but it is never called! What's wrong?</p>
        <p id="p_15800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15800"> A:</p>
        <p id="p_15801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15801"> Make sure you are using the  require Exporter ; statement and that the name of the new function is in the  0EXPORTER  array.</p>
        <p id="p_15802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15802"> Workshop</p>
        <p id="p_15803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15803"> The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_15804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15804"> Quiz</p>
        <p id="p_15805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15805"> 1. Show at least three ways to create a new object of a given class, Balloon.</p>
        <p id="p_15806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15806"> 2 .  What's wrong the following lines of code?</p>
        <p id="p_15807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15807"> {</p>
        <p id="p_15808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15808"> my $x; my $y;</p>
        <p id="p_15809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15809"> $x = \$y;</p>
        <p id="p_15810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15810"> }</p>
        <p id="p_15811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15811"> 3 .  What are the three most important rules about OOP in Perl?</p>
        <p id="p_15812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15812"> 4 .  How do you override a call to a method to use the base class instead of the subclass?</p>
        <p id="p_15813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15813"> Exercises</p>
        <p id="p_15814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15814"> 1. Write a simple class to print out the day of the week using the Zellers congruence formula to get the day of the week given a date. The following shows the formula in Perl code:</p>
        <p id="p_15815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15815"> $zy = $year;</p>
        <p id="p_15816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15816"> $zm = ($month + 10) % 12;</p>
        <p id="p_15817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15817"> $zy- if ($m &gt; 10);</p>
        <p id="p_15818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15818"> $zc = int ( $y / 100 );</p>
        <p id="p_15819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15819"> $yy = $year % 100;</p>
        <p id="p_15820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15820"> $zeller = ( int ( (26*$zm - 2)/10) + $dayOfMonth + $yy + int($yy/4)</p>
        <p id="p_15821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15821"> + int ($zc/4) - 2* $zc ) %  7;</p>
        <p id="p_15822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15822"> 2 .  Extend the class you just created to allow specifying a date at creation time where the day, month, year, or all three can be optional. Hint: Use the date function to get the current date.</p>
        <p id="p_15823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15823"> 3 .  Create a class to list the entire directory tree when given a path name.</p>
        <p id="p_15824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15824"> 4 .  Modify the following function to print black if no parameters are passed to it:sub makeCup { my ($class, $cream, $sugar, $dope) =</p>
        <p id="p_15825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15825"> print "\n================================== \n";</p>
        <p id="p_15826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15826"> print "Making a cup \n";</p>
        <p id="p_15827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15827"> print "Add cream \n" if ($cream);</p>
        <p id="p_15828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15828"> print "Add $sugar sugar cubes\n" if ($sugar);</p>
        <p id="p_15829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15829"> print "Making some really nice coffee ;-) \n" if ($dope);</p>
        <p id="p_15830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15830"> print "================================== \n";</p>
        <p id="p_15831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15831"> }</p>
        <p id="p_15832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15832"> Chapter 20</p>
        <p id="p_15833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15833"> Miscellaneous Features of Perl</p>
        <p id="p_15834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15834"> CONTENTS</p>
        <p id="p_15835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15835"> •  The require Function</p>
        <p id="p_15836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15836"> o  The require Function and Subroutine Libraries</p>
        <p id="p_15837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15837"> o  Using require to Specify a Perl Version</p>
        <p id="p_15838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15838"> •  The  $#array  Variables</p>
        <p id="p_15839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15839"> o  Controlling Array Length Using  $#array</p>
        <p id="p_15840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15840"> •  Alternative String Delimiters</p>
        <p id="p_15841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15841"> o  Defining Strings Using &lt;&lt;</p>
        <p id="p_15842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15842"> •  Special Internal Values</p>
        <p id="p_15843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15843"> •  Using Back Quotes to Invoke System Commands</p>
        <p id="p_15844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15844"> •  Pattern Matching Using ?? and the  reset  Function</p>
        <p id="p_15845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15845"> o  Using  reset  with Variables</p>
        <p id="p_15846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15846"> •  Other Features of the &lt;&gt; Operator</p>
        <p id="p_15847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15847"> o  Scalar  Variable Substitution and &lt;&gt;</p>
        <p id="p_15848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15848"> o  Creating a List of Filenames</p>
        <p id="p_15849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15849"> •  Global Indirect References and Aliases</p>
        <p id="p_15850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15850"> •  Packages</p>
        <p id="p_15851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15851"> o  Defining a Package o  Switching Between Packages</p>
        <p id="p_15852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15852"> o  The  main Package o  Referring to One Package from Another</p>
        <p id="p_15853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15853"> o  Specifying No Current Package</p>
        <p id="p_15854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15854"> o  Packages and Subroutines o  Defining Private Data Using Packages</p>
        <p id="p_15855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15855"> o  Packages and System Variables</p>
        <p id="p_15856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15856"> o  Accessing Symbol Tables</p>
        <p id="p_15857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15857"> •  Modules</p>
        <p id="p_15858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15858"> o  Creating a Module o  Importing Modules Into Your Program</p>
        <p id="p_15859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15859"> o  Using Predefined Modules</p>
        <p id="p_15860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15860"> •  Using Perl in C Programs</p>
        <p id="p_15861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15861"> •  Perl and CGI Scripts</p>
        <p id="p_15862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15862"> •  Translators and Other Supplied Code</p>
        <p id="p_15863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15863"> •  Summary</p>
        <p id="p_15864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15864"> •  O&amp;A</p>
        <p id="p_15865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15865"> •  Workshop</p>
        <p id="p_15866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15866"> o  Quiz o  Exercises</p>
        <p id="p_15867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15867"> Today's lesson describes the features of Perl that have not been covered in previous chapters:</p>
        <p id="p_15868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15868"> • The  require  function, which retrieves code from other files</p>
        <p id="p_15869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15869"> • The $# array  variables</p>
        <p id="p_15870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15870"> • Alternative methods of string quoting using  q, qq, qw,  and &lt;&lt;</p>
        <p id="p_15871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15871"> • The special internal values_LINE_,_FILE_, and_END_</p>
        <p id="p_15872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15872"> • Incorporating output from other commands using back quotes</p>
        <p id="p_15873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15873"> • The ?? pattern-matching construct and the  reset  function</p>
        <p id="p_15874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15874"> • Using &lt;&gt; with indirect file variables and as a filename specifier</p>
        <p id="p_15875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15875"> • Using the  *name  construct globally</p>
        <p id="p_15876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15876"> • Packages</p>
        <p id="p_15877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15877"> • Modules</p>
        <p id="p_15878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15878"> Today's lesson also provides a brief overview of the following topics:</p>
        <p id="p_15879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15879"> • Using Perl in C programs</p>
        <p id="p_15880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15880"> • Using Perl in CGI scripts</p>
        <p id="p_15881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15881"> • Translators from other languages to Perl</p>
        <p id="p_15882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15882"> The require Function</p>
        <p id="p_15883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15883"> The  require  function provides a way to break your program into separate files and create libraries of functions. For example, if you have stored Perl statements in the file  myfile.pl,  you can include them as part of your program by adding the following statement:</p>
        <p id="p_15884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15884"> require ("myfile.pl");</p>
        <p id="p_15885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15885"> When the Perl interpreter sees this  require  statement, it searches the directories specified by the built-in array variable @  INC  for a file named  myf ile . pi.  If such a file is found, the statements in the file are executed; if no such file exists, the program terminates and prints the error message</p>
        <p id="p_15886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15886"> Can't find myfile.pl in @INC</p>
        <p id="p_15887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15887"> on your screen (by writing it to the standard error file  STDERR).  (For more details on the @  INC  array, refer to  Day 17 ,  "System Variables.")</p>
        <p id="p_15888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15888"> As in a subroutine call, the last expression evaluated inside a file included by  require  becomes the return value. The  require  function checks whether this value is zero, and terminates if it is. For example,</p>
        <p id="p_15889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15889"> suppose that the file  myfile.pl  contains the following statements:</p>
        <p id="p_15890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15890"> print ("hello, world!\n");</p>
        <p id="p_15891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15891"> $var = 14;</p>
        <p id="p_15892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15892"> If the statements in this file are executed by</p>
        <p id="p_15893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15893"> require ("myfile.pl");</p>
        <p id="p_15894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15894"> the return value of  my file .pi  is the following expression, which has the value  14:</p>
        <p id="p_15895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15895"> $var = 14</p>
        <p id="p_15896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15896"> Because this value is not zero, the program continues execution with the statement following the  require. If  my file . pi  contains the following statements, the return value of  my file .pi  is  0:</p>
        <p id="p_15897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15897"> print ("hello, world!\n");</p>
        <p id="p_15898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15898"> $var = 0;</p>
        <p id="p_15899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15899"> Because this value is zero, the Perl interpreter prints the following error message along with the name and current line number of your program; then it exits:</p>
        <p id="p_15900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15900"> myfile.pl did not return true value</p>
        <p id="p_15901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15901"> TIP</p>
        <p id="p_15902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15902"> By convention, files containing Perl statements normally have the suffix  .pi.  This makes it easy to determine which files in a directory contain Perl programs or code included in Perl programs using</p>
        <p id="p_15903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15903"> require.</p>
        <p id="p_15904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15904"> You can pass any scalar value to  require,  including those stored in scalar variables or array elements:</p>
        <p id="p_15905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15905"> @reqlist = ("filel.pl", "file2.pl",</p>
        <p id="p_15906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15906"> "file3.pl") ;</p>
        <p id="p_15907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15907"> require</p>
        <p id="p_15908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15908"> ($reqlist[$0]</p>
        <p id="p_15909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15909"> r</p>
        <p id="p_15910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15910"> require ($reqlist[$1]) ; require ($reqlist[$2]) ;</p>
        <p id="p_15911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15911"> Here, the successive calls to  require  include the contents of  f ilel.pi, f ile2 .pi,  and  f ile3 .pi. You can also specify no filename, as in the following:</p>
        <p id="p_15912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15912"> require;</p>
        <p id="p_15913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15913"> In this case, the value of the scalar variable $_ is the filename whose contents are to be executed.</p>
        <p id="p_15914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15914"> Warning</p>
        <p id="p_15915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15915"> One limitation Perl imposes on the  require  statement is that the contents of a particular file can be included only once in a program.</p>
        <p id="p_15916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15916"> To repeat a block of code many times, your only alternative is to put it in a separate program and call it using the  system  function or the eval  function.</p>
        <p id="p_15917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15917"> Also, if two directories in @  INC  contain a file named by  require, only the first one is included.</p>
        <p id="p_15918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15918"> The require Function and Subroutine Libraries</p>
        <p id="p_15919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15919"> The  require  function enables you to create libraries of subroutines that can be used in all your Perl programs. To create a subroutine library, you need only take the following steps:</p>
        <p id="p_15920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15920"> 1. Decide on a directory in which to store your subroutine library.</p>
        <p id="p_15921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15921"> 2. Move your subroutines to separate files, and move these files to your subroutine directory.</p>
        <p id="p_15922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15922"> 3. To each file, add an executable statement that contains an expression with a nonzero value. This step is necessary because files executed by  require  must return a nonzero value, and an empty program is assumed to have the value zero. The easiest way to perform this task is to add the following statement to the bottom of each file:</p>
        <p id="p_15923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15923"> l;</p>
        <p id="p_15924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15924"> 4. This statement is just a simple expression (the number 1) with a nonzero value.</p>
        <p id="p_15925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15925"> 5.  In your main program, use  require  to refer to one or more of the files that contain your library subroutines, as needed.</p>
        <p id="p_15926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15926"> 6. When you start your main program, use the - I option to specify the name of the subroutine directory. Alternatively, add the subroutine directory to the @  INC  array before calling  require.</p>
        <p id="p_15927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15927"> For example, suppose that the directory  /u/ jqpublic/perldir  contains your Perl subroutine library and that the subroutine  my sub  is stored in the file  mysub.pl  in that directory. (Naming the file after the subroutine is an easy way to remember where the subroutine is located.) Now, to include  my sub  as part of your program, add the following statements:</p>
        <p id="p_15928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15928"> unshift (@INC, "/u/jqpublic/perldir"); require ("mysub.pl");</p>
        <p id="p_15929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15929"> The call to  unshift  adds the directory  /u/ jqpublic/perldir  to the @  INC  array, which ensures that any subsequent calls to  require  will search this directory. The call to  require  then includes the contents of  my sub. pi  as part of your program, which means that  my sub  now is included.</p>
        <p id="p_15930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15930"> TIP</p>
        <p id="p_15931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15931"> You should use  unshift,  not  push,  to add to the  @INC  array. The push  function adds to the end of the list stored in @  INC,  which means that your subroutine library directory will be searched last.</p>
        <p id="p_15932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15932"> As a consequence, if your subroutine file has the same name as a file contained in  /usr/local/lib/perl,  your file will not be included, because  require  includes only the first file matching the specified name.</p>
        <p id="p_15933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15933"> You can control the search order of @  INC  by creating or reshuffling it yourself before calling  require.</p>
        <p id="p_15934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15934"> Using require to Specify a Perl Version</p>
        <p id="p_15935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15935"> Perl  5  enables you to use a  require  statement to specify the version of Perl needed to run your program. When Perl sees a  require  statement with a numeric associated value, it only runs the program if the version of Perl is greater than or equal to the number. For example, the following statement indicates that the program is to be run only if the Perl interpreter is version  5.001  or higher:</p>
        <p id="p_15936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15936"> require 5.001;</p>
        <p id="p_15937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15937"> If it is not, the program terminates.</p>
        <p id="p_15938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15938"> This is useful if your program uses a feature of Perl that you know does not work properly in earlier versions of the language.</p>
        <p id="p_15939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15939"> NOTE</p>
        <p id="p_15940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15940"> Because Perl 4 does not understand</p>
        <p id="p_15941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15941"> require 5.001;</p>
        <p id="p_15942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15942"> it detects an error and terminates when it sees this statement. This is basically what you want to have happen</p>
        <p id="p_15943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15943"> The $#array Variables</p>
        <p id="p_15944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15944"> For each array variable defined in your program, a variable named  $#array,  in which  array  is the name of your array, is also defined. This variable contains the subscript of the last element of the array. For example:</p>
        <p id="p_15945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15945"> @myarray</p>
        <p id="p_15946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15946"> ("goodbye", "cruel", "world");</p>
        <p id="p_15947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15947"> $last sub</p>
        <p id="p_15948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15948"> $#myarray;</p>
        <p id="p_15949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15949"> Here, there are three elements in  @myarray,  which are referenced by the subscripts 0, 1, and  2.  Because the subscript of the last element of the array is  2, $#myarray  contains the value  2.</p>
        <p id="p_15950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15950"> NOTE</p>
        <p id="p_15951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15951"> Because the value of the maximum subscript is affected by the system variable $ [, the value of each $#  array  variable is also affected by $ [. For example:</p>
        <p id="p_15952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15952"> $[ = l;</p>
        <p id="p_15953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15953"> @myarray = ("goodbye", "cruel", "world"); $lastsub = $#myarray;</p>
        <p id="p_15954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15954"> Here, the first subscript of the array is 1, because $ [ is set to that value. This means that the maximum subscript is 3 and the value of</p>
        <p id="p_15955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15955"> $#myarray  is also 3</p>
        <p id="p_15956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15956"> Any  $#array  variable that does not correspond to a defined array has the value -1. For example:</p>
        <p id="p_15957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15957"> $sublength = $#notdefined;</p>
        <p id="p_15958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15958"> Here, if the array  @notdef ined  does not exist,  $sublength  is assigned -1.</p>
        <p id="p_15959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15959"> A $#array  variable is also defined for each built-in array variable. This means, for example, that the $#ARGV  variable contains the number of elements included on the command line. You can use this variable to check whether files have been specified on the command line:</p>
        <p id="p_15960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15960"> if ($ #ARGV == -1) {</p>
        <p id="p_15961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15961"> die ("No files specified.\n");</p>
        <p id="p_15962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15962"> }</p>
        <p id="p_15963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15963"> If there are no "holes" (undefined elements) in the array, you can use a  $#array  variable in a loop. Listing 20.1 shows how you can carry out this action.</p>
        <p id="p_15964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15964"> Type</p>
        <p id="p_15965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15965"> Listing 20.1.  A  program that uses a $#array variable in a loop.</p>
        <p id="p_15966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15966"> 1: #!/usr/local/bin/perl</p>
        <p id="p_15967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15967"> 2  :</p>
        <p id="p_15968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15968"> 3: gmyarray = ("testing", 98.6, "Olerud", 47);</p>
        <p id="p_15969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15969"> 4: for ($i = 0; $i &lt;= $#myarray; $i++) {</p>
        <p id="p_15970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15970"> 5: print ("$myarray[$i]\n");</p>
        <p id="p_15971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15971"> 6 : }</p>
        <p id="p_15972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15972"> Output</p>
        <p id="p_15973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15973"> $ program20_l testing</p>
        <p id="p_15974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15974"> 98.599999999999994</p>
        <p id="p_15975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15975"> Olerud</p>
        <p id="p_15976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15976"> 47</p>
        <p id="p_15977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15977"> $</p>
        <p id="p_15978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15978"> I  Line  3  assigns a four-element list to the array variable  @myarray.  Therefore, the largest subscript used in the array is  3;  this value is automatically assigned to the variable  $#myarray.</p>
        <p id="p_15979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15979"> The  for  statement in line 4 terminates when  $i  is greater than  $#myarray.  This technique ensures that each element of  @myarray  is printed, in turn, by line 5.</p>
        <p id="p_15980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15980"> Warning</p>
        <p id="p_15981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15981"> Using  $#myarray  to terminate the loop isn't as useful if the array contains undefined elements as in the following:</p>
        <p id="p_15982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15982"> @myarray = ("testl", "test2");</p>
        <p id="p_15983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15983"> $myarray[5] = "test3";</p>
        <p id="p_15984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15984"> for ($i = 0; $i &lt;= $#myarray; $i++) {</p>
        <p id="p_15985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15985"> print ("$myarray[$i]\n");</p>
        <p id="p_15986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15986"> }</p>
        <p id="p_15987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15987"> This loop iterates six times, because the largest subscript of the array is 5. Therefore, three blank lines are printed, because the elements of @myarray  with the subscripts 2, 3, and 4 have not been defined. (You can get around this by using the  defined  function.)</p>
        <p id="p_15988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15988"> Controlling Array Length Using $#array</p>
        <p id="p_15989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15989"> You can use  $#array  to control the length of an array variable.</p>
        <p id="p_15990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15990"> If a $# array  variable is assigned a value that is larger than the current largest subscript of the corresponding array, the missing elements are created and initialized to the special internal undefined value (equivalent to the null string). For example:</p>
        <p id="p_15991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15991"> gmyarray = ("hi", "there"); $#myarray = 4;</p>
        <p id="p_15992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15992"> This code sets the maximum subscript of  $#myarray  to  4.  Because the subscript of the last defined element is 1, three empty elements are created with subscripts 2, 3, and 4.</p>
        <p id="p_15993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15993"> You can use this technique to create a large array all at once:</p>
        <p id="p_15994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15994"> $#bigarray = 9999;</p>
        <p id="p_15995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15995"> This statement creates an array large enough to hold 10,000 values (or fails trying). If this statement executes successfully, you know that your machine has enough space to store  @bigarray  before actually assigning to all or part of it.</p>
        <p id="p_15996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15996"> In Perl  5,  if the value you assign to a  $#array  variable is less than the current maximum subscript, the leftover array values are destroyed. For example:</p>
        <p id="p_15997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15997"> @myarray = ("hello", "there", "Dave!"); $#myarray = 1;</p>
        <p id="p_15998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15998"> Here,  gmyarray  is originally assigned a three-element list, which means that its maximum subscript is 2. Assigning 1 to  $#myarray  sets the maximum subscript to 1, which means that  @myarray  now contains ( "hello" , "there"  ). The third element,  Dave  !, is destroyed.</p>
        <p id="p_15999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_15999"> NOTE</p>
        <p id="p_16000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16000"> This is one instance in which Perl 5 and Perl 4 behave differently. In Perl 4, array elements are not destroyed when  $#array  is assigned a value less than the current maximum subscript.</p>
        <p id="p_16001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16001"> In Perl 4, array elements that have been "removed" by assigning to the $#array  variable can be restored to existence by resetting  $#array to its original value.</p>
        <p id="p_16002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16002"> Alternative String Delimiters</p>
        <p id="p_16003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16003"> As you've seen, Perl enables you to enclose character strings in either single quotation marks or double quotation marks. Strings in double quotation marks are searched for variable names, which are replaced with their values when found; strings in single quotation marks are not searched.</p>
        <p id="p_16004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16004"> Consider the following example:</p>
        <p id="p_16005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16005"> $var = 5;</p>
        <p id="p_16006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16006"> print ("$var\n");</p>
        <p id="p_16007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16007"> print  ('$var\n');</p>
        <p id="p_16008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16008"> The first call to  print  prints  5  followed by a newline character; the second prints the string  $var\n  as is.</p>
        <p id="p_16009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16009"> Perl enables you to use any delimiter you want in place of either single quotation marks or double quotation marks. To specify a string that-like a single-quoted string-is not searched for variable names, use  q  followed by the delimiter you want to use. For example, the following strings are equivalent:</p>
        <p id="p_16010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16010"> qlhello $there!</p>
        <p id="p_16011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16011"> 'hello $there'</p>
        <p id="p_16012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16012"> A useful trick is to use newline characters as delimiters:</p>
        <p id="p_16013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16013"> q</p>
        <p id="p_16014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16014"> this is my string</p>
        <p id="p_16015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16015"> This example is equivalent to the following because the newline after the  q  indicates the beginning of the string, and the newline after  string  indicates the end of the string:</p>
        <p id="p_16016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16016"> 'this is my string'</p>
        <p id="p_16017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16017"> To define a string that is searched for valuable names, use qq:</p>
        <p id="p_16018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16018"> qq/This string contains $var./</p>
        <p id="p_16019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16019"> The / characters delimit the string</p>
        <p id="p_16020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16020"> This string contains $var.</p>
        <p id="p_16021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16021"> which is then searched for variable names. This means that  $var  is replaced by its current value.</p>
        <p id="p_16022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16022"> NOTE</p>
        <p id="p_16023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16023"> If you use a left parenthesis as the opening delimiter for a string defined using  q  or  qq,  the Perl interpreter expects a right parenthesis as the closing delimiter. This method of operation enables you to treat q  and  qq  as if they were functions:</p>
        <p id="p_16024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16024"> q(Here is a single quoted string); qq(Here is a double quoted string);</p>
        <p id="p_16025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16025"> These are equivalent to both of the following:</p>
        <p id="p_16026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16026"> 'Here is a single quoted string'</p>
        <p id="p_16027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16027"> "Here is a double quoted string"</p>
        <p id="p_16028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16028"> Be careful not to leave a space between the  q  or  qq  and the left parenthesis; if you do, the Perl interpreter will assume that the space character, not the (, is the delimiter</p>
        <p id="p_16029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16029"> qw,  defined in Perl 5, provides a convenient way of breaking a string into words. The following statements are equivalent:</p>
        <p id="p_16030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16030"> @words = qw/this is a list of words/;</p>
        <p id="p_16031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16031"> Swords = split(' ', q/this is a list of words/);</p>
        <p id="p_16032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16032"> In each case,  Swords  is assigned the list</p>
        <p id="p_16033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16033"> ("this", "is", "a", "list",  "of",  "words")</p>
        <p id="p_16034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16034"> qw  supports any alternative string delimiter supported by  q  and  qq.</p>
        <p id="p_16035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16035"> Defining Strings Using &lt;&lt;</p>
        <p id="p_16036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16036"> You can use &lt;&lt; (two left angle brackets) to indicate the beginning of a string. This string continues until the next blank line. The following is an example:</p>
        <p id="p_16037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16037"> $longstring = &lt;&lt;</p>
        <p id="p_16038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16038"> Here is the first part of the string.</p>
        <p id="p_16039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16039"> Here is the last part of the string.</p>
        <p id="p_16040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16040"> # here is the next statement</p>
        <p id="p_16041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16041"> This example defines a string consisting of the two input lines</p>
        <p id="p_16042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16042"> Here is the first part of the string.</p>
        <p id="p_16043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16043"> Here is the last part of the string.</p>
        <p id="p_16044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16044"> and assigns it to  $longstring.  The newline characters are included as part of the string.</p>
        <p id="p_16045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16045"> You can specify the characters that indicate "end of string" by including them after the &lt;&lt;. For example:</p>
        <p id="p_16046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16046"> $longstring = &lt;&lt;END</p>
        <p id="p_16047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16047"> Here is the first part of the string.</p>
        <p id="p_16048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16048"> Here is the last part of the string.</p>
        <p id="p_16049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16049"> END</p>
        <p id="p_16050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16050"> # here is the next statement.</p>
        <p id="p_16051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16051"> Here,  END  indicates the end of the string.</p>
        <p id="p_16052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16052"> You can enclose the end-of-string characters in either single or double quotation marks. Single-quoted end-of-string characters behave like normal end-of-string characters:</p>
        <p id="p_16053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16053"> $longstring = CC'END'</p>
        <p id="p_16054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16054"> Here is the first part of the string.</p>
        <p id="p_16055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16055"> Here is the last part of the string.</p>
        <p id="p_16056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16056"> END</p>
        <p id="p_16057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16057"> # here is the next statement</p>
        <p id="p_16058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16058"> Double-quoted end-of-string characters are searched for variable names, which are replaced by their values if found.</p>
        <p id="p_16059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16059"> $endchars = "END";</p>
        <p id="p_16060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16060"> $longstring = «"$endchars"</p>
        <p id="p_16061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16061"> Here is the first part of the string.</p>
        <p id="p_16062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16062"> Here is the last part of the string.</p>
        <p id="p_16063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16063"> END</p>
        <p id="p_16064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16064"> # here is the next statement</p>
        <p id="p_16065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16065"> Here,  $endchars  is replaced by its value,  END,  which is used to indicate the end of the string.</p>
        <p id="p_16066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16066"> A string created using &lt;&lt; can be used wherever a string is expected. For example, the statement</p>
        <p id="p_16067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16067"> print &lt;&lt;END Hello there!</p>
        <p id="p_16068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16068"> This is a  test!</p>
        <p id="p_16069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16069"> END</p>
        <p id="p_16070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16070"> writes the following to the standard output file:</p>
        <p id="p_16071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16071"> Hello there!</p>
        <p id="p_16072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16072"> This is a  test!</p>
        <p id="p_16073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16073"> (This is one place where omitting the parentheses when you pass an argument to a function becomes useful.) You can use the  x  operator to write a string more than once:</p>
        <p id="p_16074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16074"> print &lt;&lt;END x 2 Hello there!</p>
        <p id="p_16075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16075"> END</p>
        <p id="p_16076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16076"> This sends the following to the standard output file:</p>
        <p id="p_16077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16077"> Hello there!</p>
        <p id="p_16078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16078"> Hello there!</p>
        <p id="p_16079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16079"> You can supply more than one &lt;&lt; at a time. If you do, they are processed in the order in which they are received. For example, the statement</p>
        <p id="p_16080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16080"> $longstring = &lt;&lt;END1 &lt;&lt;END2 This is the first part.</p>
        <p id="p_16081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16081"> END1</p>
        <p id="p_16082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16082"> This is the second part.</p>
        <p id="p_16083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16083"> END 2</p>
        <p id="p_16084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16084"> assigns the following (including the trailing newlines) to  $longstring:</p>
        <p id="p_16085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16085"> This is the first part.</p>
        <p id="p_16086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16086"> This is the second part.</p>
        <p id="p_16087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16087"> Don't</p>
        <p id="p_16088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16088"> DON'T leave a space between the &lt;&lt; and the end-of-string characters. (If you do, the Perl interpreter will terminate the string when it sees the next blank line.)</p>
        <p id="p_16089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16089"> DON'T put anything else in the line containing the end-of-string characters.</p>
        <p id="p_16090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16090"> Special Internal Values</p>
        <p id="p_16091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16091"> Perl defines three special internal values your program can use:_ LINE ,  FILE _, and _ END_.</p>
        <p id="p_16092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16092"> _ LINE  and  FILE  contain, respectively, the current line number and current filename of the</p>
        <p id="p_16093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16093"> program you are running. These are the values that  die  and  warn  use when printing the line number and filename on which an error or a warning occurs.</p>
        <p id="p_16094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16094"> _END  is a special value that indicates "end of file." Everything after_ END  is treated as data. If the</p>
        <p id="p_16095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16095"> program is contained in a file, you can read the data after_ END _by reading from the  DATA  file variable:</p>
        <p id="p_16096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16096"> $data</p>
        <p id="p_16097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16097"> &lt;DATA&gt;;</p>
        <p id="p_16098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16098"> NOTE</p>
        <p id="p_16099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16099"> _LINE _and_ FILE _cannot be substituted into double-quoted</p>
        <p id="p_16100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16100"> strings.</p>
        <p id="p_16101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16101"> You can use the  A D  or  A Z  character (Ctrl+D or Ctrl+Z) in place of</p>
        <p id="p_16102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16102"> END</p>
        <p id="p_16103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16103"> Warning</p>
        <p id="p_16104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16104"> _END_does not need to appear on a line by itself as long as some</p>
        <p id="p_16105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16105"> white space separates it from the next item in the file. However, the first line of the file represented by  DATA  is always the line immediately following the_ END _. For example:</p>
        <p id="p_16106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16106"> _END _ Here is some input.</p>
        <p id="p_16107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16107"> Here is some more input.</p>
        <p id="p_16108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16108"> In this case, the first line read by  &lt;DATA&gt;  is</p>
        <p id="p_16109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16109"> Here is some more input.</p>
        <p id="p_16110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16110"> The information immediately following the_ END _is lost.</p>
        <p id="p_16111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16111"> Using Back Quotes to Invoke System Commands</p>
        <p id="p_16112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16112"> Perl provides a way to treat the value printed by a system command as a string. To do this, enclose the system command in back quote characters (the ' character).</p>
        <p id="p_16113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16113"> For example, here is a way to include your user name in a Perl program:</p>
        <p id="p_16114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16114"> $myname = 'whoami'; chop ($myname);</p>
        <p id="p_16115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16115"> The first statement calls the system command  whoami,  which prints the name of the person logged on. This name is assigned to  $myname.  (The call to  chop  is necessary because  whoami  appends a newline character to the name, which enables it to appear on its own line on the screen.)</p>
        <p id="p_16116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16116"> The Perl interpreter performs variable substitution on the string enclosed in back quotes before treating it as a system command.</p>
        <p id="p_16117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16117"> $command = "whoami";</p>
        <p id="p_16118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16118"> $myname = '$command'; chop ($myname);</p>
        <p id="p_16119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16119"> Here, the value of $  command, whoami,  is substituted into the string enclosed in back quotes, and it becomes the system command that is called.</p>
        <p id="p_16120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16120"> When a system command is executed, the return code from the command is stored in the system variable $ ?. To determine whether the system command has executed properly, check this system variable. (Normally, a value of zero indicates successful execution, and any other value indicates an error. The actual error value depends on the command.)</p>
        <p id="p_16121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16121"> To use a character other than a back quote as a delimiter, use  qx:</p>
        <p id="p_16122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16122"> $myname = qx#whoami#; chop ($myname);</p>
        <p id="p_16123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16123"> As with  q  and  qq,  described previously, the first character after  qx  is treated as the string delimiter. The string continues until another string delimiter-in this case, #-is seen.</p>
        <p id="p_16124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16124"> NOTE</p>
        <p id="p_16125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16125"> If ( is used as an opening string delimiter, ) becomes the closing string delimiter:</p>
        <p id="p_16126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16126"> $myname = qx(whoami);</p>
        <p id="p_16127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16127"> Pattern Matching Using ?? and the reset Function</p>
        <p id="p_16128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16128"> The ? ? pattern matching operator is identical to the / / pattern-matching operator you have been using all along, except that it matches only once, even if it is inside a loop. For example, the following statement loops only once, because the pattern  ?abc  ? is not matched the second time it is executed:</p>
        <p id="p_16129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16129"> while ($line =~ ?abc?) {</p>
        <p id="p_16130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16130"> # stuff goes here</p>
        <p id="p_16131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16131"> }</p>
        <p id="p_16132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16132"> To make the ? ? pattern matching operator match again, call the  reset  function. This function tells the Perl interpreter that a particular ? ? operator can be used to match a pattern again. Listing 20.2 is an example of a program that uses ?? and  reset.</p>
        <p id="p_16133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16133"> Type</p>
        <p id="p_16134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16134"> Listing 20.2. A demonstration of ?? and the reset function.</p>
        <p id="p_16135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16135"> 1: #!/usr/local/bin/perl</p>
        <p id="p_16136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16136"> 2 :</p>
        <p id="p_16137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16137"> 3: while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_16138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16138"> 4: last unless ($line =~ ?\bthe\b?);</p>
        <p id="p_16139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16139"> 5: print  ("$'$");</p>
        <p id="p_16140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16140"> 6:  reset;</p>
        <p id="p_16141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16141"> 7: }</p>
        <p id="p_16142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16142"> Output</p>
        <p id="p_16143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16143"> $ program20_2 this is the first line this is first line the next line of input next line of input last line-not matched $</p>
        <p id="p_16144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16144"> Analysis</p>
        <p id="p_16145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16145"> Line 4 of this program uses the ? ? pattern matching operator to check whether the word</p>
        <p id="p_16146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16146"> the appears in the current input line. If it does not, the program terminates. If it does, line 5 uses the $' and $_ variables to print the parts of the line not matched.</p>
        <p id="p_16147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16147"> Line 6 calls reset, which resets the ?? operator in line 4. If reset is not called, line 4 will not match even if the new input line contains the word the.</p>
        <p id="p_16148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16148"> Warning</p>
        <p id="p_16149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16149"> The ? ? operator is deprecated in Perl version 5. This means that the operator is still supported but is considered obsolete. Future versions of Perl might not support this operator</p>
        <p id="p_16150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16150"> Using reset with Variables</p>
        <p id="p_16151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16151"> You also can use the reset function to clear all variables whose name begins with a specified character. The following statement assigns the null string to all scalar variables whose names begin with the letter w (such as, for instance, $ which) and assigns the empty list to all array variables whose names begin with this letter:</p>
        <p id="p_16152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16152"> reset  ("w");</p>
        <p id="p_16153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16153"> The following statement assigns the null string or the empty list to all variables whose names begin with a or e:</p>
        <p id="p_16154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16154"> reset  ("ae");</p>
        <p id="p_16155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16155"> You can use ranges of letters with reset:</p>
        <p id="p_16156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16156"> reset  ("a-d");</p>
        <p id="p_16157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16157"> This example resets all variables whose names begin with a, b, c, or d.</p>
        <p id="p_16158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16158"> Be careful with  reset  because it resets all variables whose names begin with the specified letters, including built-in variables such as</p>
        <p id="p_16159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16159"> @ARGV.</p>
        <p id="p_16160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16160"> Other Features of the &lt;&gt; Operator</p>
        <p id="p_16161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16161"> As you've seen, the &lt;&gt; operator reads from the file specified by the enclosed file variable. For example, the following statement reads a line from the file represented by  MYFILE:</p>
        <p id="p_16162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16162"> $line = &lt;MYFILE&gt;;</p>
        <p id="p_16163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16163"> The following sections describe how to use &lt;&gt; with scalar variable substitution and how to use &lt;&gt; to create a list of filenames.</p>
        <p id="p_16164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16164"> Scalar Variable Substitution and &lt;&gt;</p>
        <p id="p_16165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16165"> If a scalar variable is contained in the &lt;&gt; operator, the value of the variable is assumed to be the name of a file variable. For example:</p>
        <p id="p_16166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16166"> $filename = "MYFILE";</p>
        <p id="p_16167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16167"> $line = &lt;$filename&gt;;</p>
        <p id="p_16168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16168"> Here, the value of  $f ilename, MYFILE,  is assumed to be the file variable associated with the input file to read from. When you change the value of $  filename,  you change the input file.</p>
        <p id="p_16169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16169"> Creating a List of Filenames</p>
        <p id="p_16170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16170"> UNIX commands that manipulate files, such as mv and cp, enable you to supply a pattern to generate a list of filenames. Any filename matching this pattern is included as part of the list. For example, the following command copies every file whose name ends in . pi to the directory /u/jqpublic/srcdir:</p>
        <p id="p_16171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16171"> $ cp *.pl /u/jqpublic/srcdir</p>
        <p id="p_16172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16172"> In Perl, if the &lt;&gt; operator encloses something other than a file variable or a scalar variable containing a file variable, it is assumed to be a pattern that matches a list of files. For example, the following statement assigns a list of the filenames ending in . pi to the array variable  @f ilelist:</p>
        <p id="p_16173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16173"> @filelist = &lt;*.pl&gt;;</p>
        <p id="p_16174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16174"> You can use filename patterns in loops:</p>
        <p id="p_16175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16175"> while ($line =</p>
        <p id="p_16176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16176"> &lt;*.pl&gt;) {</p>
        <p id="p_16177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16177"> print ("$line\n");</p>
        <p id="p_16178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16178"> }</p>
        <p id="p_16179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16179"> This code prints each filename ending in . pi on a separate line.</p>
        <p id="p_16180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16180"> Global Indirect References and Aliases</p>
        <p id="p_16181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16181"> On  Day 9 ,  "Using Subroutines," you learned that you can pass the name of an array to a subroutine using an alias. For example:</p>
        <p id="p_16182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16182"> sub my_sub {</p>
        <p id="p_16183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16183"> local (*subarray) =</p>
        <p id="p_16184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16184"> $arraylength = @subarray;</p>
        <p id="p_16185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16185"> }</p>
        <p id="p_16186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16186"> The * subarray definition in my_sub tells the Perl interpreter to operate on the actual list instead of making a copy. When this subroutine is called by a statement such as the following, the Perl interpreter realizes that myarray and subarray refer to the same array variable:</p>
        <p id="p_16187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16187"> &amp;my_sub(*myarray);</p>
        <p id="p_16188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16188"> When a name is given an alias, all variables with that name can be referred to using the alias. This means, in this example, that the @ subarray variable and the @myarray variable refer to the same array. If the program also defines variables named $ subarray and % subarray, you can use $myarray and %myarray, respectively, to refer to these variables.</p>
        <p id="p_16189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16189"> In the earlier example, the following two statements:</p>
        <p id="p_16190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16190"> my_sub (*myarray); local (*subarray) =</p>
        <p id="p_16191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16191"> are equivalent to the assignment</p>
        <p id="p_16192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16192"> local (*subarray) = *myarray;</p>
        <p id="p_16193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16193"> In each case, the name subarray is defined to be an alias of the name myarray. Because * subarray is contained inside a local definition in a subroutine, subarray and myarray are equivalent only while the subroutine is being executed.</p>
        <p id="p_16194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16194"> If desired, you can define an alias for a name that remains in force throughout your program. For example:</p>
        <p id="p_16195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16195"> *subarray = *myarray;</p>
        <p id="p_16196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16196"> If this statement is part of your main program, subarray becomes an alias for myarray in all parts of your program, including all subroutines. The values of $ subarray, @ subarray, and % subarray, if they are defined, are lost.</p>
        <p id="p_16197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16197"> Type</p>
        <p id="p_16198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16198"> Listing 20.3 is a simple example of a program that defines and uses a global alias. Listing 20.3. An example of a global alias.</p>
        <p id="p_16199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16199"> 1:</p>
        <p id="p_16200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16200"> 2:</p>
        <p id="p_16201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16201"> 3:</p>
        <p id="p_16202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16202"> 4:</p>
        <p id="p_16203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16203"> 5:</p>
        <p id="p_16204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16204"> #!/usr/local/bin/perl</p>
        <p id="p_16205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16205"> *name2 = *namel; $namel = 14; print ("$name2\n") ;</p>
        <p id="p_16206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16206"> Output</p>
        <p id="p_16207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16207"> $ program20_3 14</p>
        <p id="p_16208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16208"> $</p>
        <p id="p_16209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16209"> Analysis</p>
        <p id="p_16210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16210"> Line 3 of this program defines name2 as an alias for namel. Every variable named namel can therefore be referred to using the name name2. As a result, $namel and $name2 are really the same scalar variable; this means that line 5 prints the value assigned in line 4.</p>
        <p id="p_16211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16211"> DON'T  use aliases unless you absolutely must, because they can become very confusing.</p>
        <p id="p_16212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16212"> DO,  instead, substitute the variable name into a string and then execute it using  eval.  This is a better way to reference a variable indirectly. For example:</p>
        <p id="p_16213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16213"> $name2 = '$namel'; eval ("$name2 =  14;");</p>
        <p id="p_16214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16214"> The string $  namel  is substituted for the variable name $  name 2,  yielding the string</p>
        <p id="p_16215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16215"> $namel = 14;</p>
        <p id="p_16216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16216"> eval  then executes this statement, which assigns  14  to  $namel.</p>
        <p id="p_16217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16217"> Packages</p>
        <p id="p_16218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16218"> A Perl program keeps track of the variables and subroutines defined within it by storing their names in a symbol table. In Perl, the collection of names in a symbol table is called a  package.  The following sections describe packages and how to use them.</p>
        <p id="p_16219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16219"> Defining a Package</p>
        <p id="p_16220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16220"> Perl enables you to define more than one package for a program, with each package contained in a separate symbol table. To define a package, use the package statement.</p>
        <p id="p_16221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16221"> package mypack;</p>
        <p id="p_16222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16222"> This statement creates a new package named mypack. All variable and subroutine names defined from this point on in the program are stored in the symbol table associated with the new package. This process continues until another package statement is encountered.</p>
        <p id="p_16223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16223"> Each symbol table contains its own set of variable and subroutine names, and each set of names is independent. This means that you can use the same variable name in more than one package.</p>
        <p id="p_16224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16224"> $var = 14; package mypack; $var = 6;</p>
        <p id="p_16225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16225"> The first statement creates a variable named $var and stores it in the main symbol table. The statement following the package statement creates another variable named $var and stores it in the symbol table for the mypack package.</p>
        <p id="p_16226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16226"> Switching Between Packages</p>
        <p id="p_16227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16227"> You can switch back and forth between packages at any time. Listing 20.4 shows how you can carry out this action.</p>
        <p id="p_16228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16228"> Type</p>
        <p id="p_16229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16229"> Listing 20.4. A program that switches between packages.</p>
        <p id="p_16230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16230"> 1: #!/usr/local/bin/perl</p>
        <p id="p_16231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16231"> 2 :</p>
        <p id="p_16232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16232"> 3: package packl;</p>
        <p id="p_16233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16233"> 4: $var = 26;</p>
        <p id="p_16234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16234"> 5 : package pack2;</p>
        <p id="p_16235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16235"> 6: $var = 34;</p>
        <p id="p_16236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16236"> 7 : package packl;</p>
        <p id="p_16237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16237"> 8: print  ("$var\n");</p>
        <p id="p_16238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16238"> Output</p>
        <p id="p_16239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16239"> $ program20_4</p>
        <p id="p_16240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16240"> 26</p>
        <p id="p_16241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16241"> $</p>
        <p id="p_16242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16242"> Analysis</p>
        <p id="p_16243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16243"> Line 3 defines a package named packl. Line 4 creates a variable named $var, which is then stored in the symbol table for the packl package. Line 5 then defines a new package, pack2. Line 6 creates another variable named $var, which is stored in the symbol table for the pack2 package. Two separate copies of $var now exist, one in each package.</p>
        <p id="p_16244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16244"> Line 7 specifies the packl package again. Because packl has already been defined, this statement just sets the current package to be packl; therefore, all variable and subroutine references and definitions refer to names stored in the symbol table for this package.</p>
        <p id="p_16245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16245"> As a consequence, when line 8 refers to $var, it refers to the $var stored in the packl package. The value stored in this variable, 26, is retrieved and printed.</p>
        <p id="p_16246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16246"> The main Package</p>
        <p id="p_16247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16247"> The default symbol table, in which variable and subroutine names are normally stored, is associated with the package named main. If you have defined a package using the package statement and you want to switch back to using the normal default symbol table, specify the main package as shown here:</p>
        <p id="p_16248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16248"> package main;</p>
        <p id="p_16249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16249"> When this statement is executed, your program resumes behaving as though no package statements have ever been seen. Subroutine and variable names are stored as they normally are.</p>
        <p id="p_16250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16250"> Referring to One Package from Another</p>
        <p id="p_16251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16251"> To refer to a variable or subroutine defined in one package from inside another package, precede the variable name with the package name followed by a single quotation-mark character. For example:</p>
        <p id="p_16252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16252"> package mypack;</p>
        <p id="p_16253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16253"> $var = 26;</p>
        <p id="p_16254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16254"> package main;</p>
        <p id="p_16255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16255"> print ("$mypack'var\n");</p>
        <p id="p_16256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16256"> Here, $mypack' var refers to the variable named $var located in the package mypack.</p>
        <p id="p_16257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16257"> Warning</p>
        <p id="p_16258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16258"> Do not put any spaces between the quotation-mark character and either the package name or the variable name. The following examples are not correct:</p>
        <p id="p_16259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16259"> $mypack ' var $mypack' var $mypack 'var</p>
        <p id="p_16260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16260"> NOTE</p>
        <p id="p_16261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16261"> In Perl 5, the package name and variable name are separated by a pair of colons instead of a quotation mark:</p>
        <p id="p_16262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16262"> $mypack::var</p>
        <p id="p_16263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16263"> The quotation-mark character is supported for now but might not be understood in future versions of Perl.</p>
        <p id="p_16264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16264"> Specifying No Current Package</p>
        <p id="p_16265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16265"> Perl 5 enables you to state that there is to be no current package. To do this, specify a package statement without a package name, as in the following:</p>
        <p id="p_16266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16266"> package;</p>
        <p id="p_16267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16267"> This tells the Perl interpreter that all variables must have their package names explicitly specified in order for a statement to be valid.</p>
        <p id="p_16268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16268"> $mypack::var = 21; # OK</p>
        <p id="p_16269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16269"> $var  =21;  # error - no current package</p>
        <p id="p_16270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16270"> This restriction remains in effect until a current package is explicitly defined by another package statement.</p>
        <p id="p_16271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16271"> Packages and Subroutines</p>
        <p id="p_16272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16272"> A package definition affects all the statements in a program, including subroutine definitions. For</p>
        <p id="p_16273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16273"> example:</p>
        <p id="p_16274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16274"> package mypack; subroutine mysub {</p>
        <p id="p_16275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16275"> local ($myvar) ;</p>
        <p id="p_16276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16276"> # stuff goes here</p>
        <p id="p_16277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16277"> }</p>
        <p id="p_16278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16278"> Here, the names mysub and myvar are both part of the mypack package. To call the subroutine mysub from outside the package mypack, specify &amp;mypack' mysub.</p>
        <p id="p_16279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16279"> You can change packages in the middle of a subroutine:</p>
        <p id="p_16280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16280"> package packl; subroutine mysub {</p>
        <p id="p_16281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16281"> $varl = 1; package pack2;</p>
        <p id="p_16282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16282"> $varl = 2;</p>
        <p id="p_16283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16283"> }</p>
        <p id="p_16284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16284"> This code creates two copies of $varl, one in packl and one in pack2.</p>
        <p id="p_16285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16285"> NOTE</p>
        <p id="p_16286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16286"> Local variables that are part of packages can be referenced only in the subroutine or statement block in which they are defined. (In other words, they behave just like ordinary local variables do.)</p>
        <p id="p_16287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16287"> Defining Private Data Using Packages</p>
        <p id="p_16288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16288"> The most common use of packages is in files containing subroutines and global variables that are used in these subroutines. By defining a package for these subroutines, you can ensure that the global variables used in the subroutines are used nowhere else; such variables are called  private data.</p>
        <p id="p_16289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16289"> Better still, you can ensure that the package name itself is used nowhere else. Listing 20.5 is an example of a file containing a package name and variable names that are used nowhere else.</p>
        <p id="p_16290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16290"> Type</p>
        <p id="p_16291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16291"> Listing 20.5. A file that contains private data.</p>
        <p id="p_16292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16292"> 1: package privpack;</p>
        <p id="p_16293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16293"> 2: $valtoprint = 46;</p>
        <p id="p_16294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16294"> 3:</p>
        <p id="p_16295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16295"> 4: package main;</p>
        <p id="p_16296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16296"> 5: # This function is the link to the outside world.</p>
        <p id="p_16297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16297"> 6: sub printval {</p>
        <p id="p_16298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16298"> 7: &amp;privpack'printval();</p>
        <p id="p_16299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16299"> 8 : }</p>
        <p id="p_16300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16300"> 9:</p>
        <p id="p_16301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16301"> 10: package privpack;</p>
        <p id="p_16302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16302"> 11: sub printval {</p>
        <p id="p_16303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16303"> 12: print ("$valtoprint\n") ;</p>
        <p id="p_16304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16304"> 13: }</p>
        <p id="p_16305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16305"> 14:</p>
        <p id="p_16306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16306"> 15: package main;</p>
        <p id="p_16307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16307"> 16: 1; # return value for require</p>
        <p id="p_16308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16308"> Output</p>
        <p id="p_16309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16309"> This subroutine, by itself, cannot generate its output until printval is called.</p>
        <p id="p_16310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16310"> Analysis</p>
        <p id="p_16311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16311"> This file can be divided into two parts: the part that communicates with the outside world and the part that does the work. The part that communicates is in the main or default package, and</p>
        <p id="p_16312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16312"> the part that does the work is in a special package named privpack. This package is defined only in this file.</p>
        <p id="p_16313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16313"> The subroutine printval, defined in lines 6-8, is designed to be called from programs and subroutines defined elsewhere. Its only task is to call the version of printval defined in the privpack package.</p>
        <p id="p_16314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16314"> The version of printval in the privpack package prints the number by retrieving it from the scalar variable $valtoprint. This variable is also part of the privpack package, and it is defined only inside it.</p>
        <p id="p_16315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16315"> Lines 15 and 16 ensure that this file behaves properly if it is included in a program by require. Line 15 sets the current package to the default package, and line 16 is a nonzero return value to ensure that require does not generate an error.</p>
        <p id="p_16316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16316"> Packages and System Variables</p>
        <p id="p_16317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16317"> The following variables are assumed to be in the main package, even when referenced from inside another package:</p>
        <p id="p_16318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16318"> • The file variables  STD IN, STDOUT, STDERR,  and  ARGV</p>
        <p id="p_16319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16319"> • The  %ENV, %INC, @INC, $ARGV,  and  @ARGV  variables</p>
        <p id="p_16320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16320"> • Any system variable with a special character in its name (such as, for example, $_ and $%)</p>
        <p id="p_16321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16321"> Accessing Symbol Tables</p>
        <p id="p_16322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16322"> To actually look in a symbol table from within a program, use the associative array %_package, in which package is the name of the package whose symbol table you want to access. For example, the variable %_main contains the default symbol table.</p>
        <p id="p_16323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16323"> Normally, you will not need to look in the symbol table yourself.</p>
        <p id="p_16324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16324"> Modules</p>
        <p id="p_16325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16325"> Most large programs are divided into components, each of which performs a specific task or set of tasks. Each component normally contains one or more executable functions, plus the variables needed to make these functions work. The collection of functions and variables in a component is known as a program module.  One module can appear in a variety of programs.</p>
        <p id="p_16326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16326"> Creating a Module</p>
        <p id="p_16327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16327"> Perl 5 enables you to use packages to define modules. To define a module in Perl 5, create the package and store it in a file of the same name. For example, a package named Mymodule would be stored in the file Mymodule . pm. (The . pm suffix indicates that the file is a Perl module.)</p>
        <p id="p_16328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16328"> Listing 20.6 creates a module named Mymodule, containing subroutines myf unci and myf unc2,</p>
        <p id="p_16329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16329"> and variables $myvarl and $myvar2. This code should be stored in the file Mymodule . pm.</p>
        <p id="p_16330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16330"> Type</p>
        <p id="p_16331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16331"> Listing 20.6. Code that creates a Perl module.</p>
        <p id="p_16332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16332"> 1: #/usr/local/bin/perl</p>
        <p id="p_16333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16333"> 2 :</p>
        <p id="p_16334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16334"> 3: package Mymodule;</p>
        <p id="p_16335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16335"> 4: require Exporter;</p>
        <p id="p_16336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16336"> 5: @ISA = qw(Exporter);</p>
        <p id="p_16337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16337"> 6: 0EXPORT = qw(myfuncl myfunc2);</p>
        <p id="p_16338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16338"> 7: @EXPORT_OK = qw($myvarl $myvar2);</p>
        <p id="p_16339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16339"> 8:</p>
        <p id="p_16340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16340"> 9: sub myfuncl {</p>
        <p id="p_16341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16341"> 10: $myvarl += 1;</p>
        <p id="p_16342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16342"> 11 : }</p>
        <p id="p_16343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16343"> 12:</p>
        <p id="p_16344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16344"> 13: sub myfunc2 {</p>
        <p id="p_16345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16345"> 14: $myvar2 += 2;</p>
        <p id="p_16346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16346"> 15: }</p>
        <p id="p_16347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16347"> Lines 3-7 use the standard Perl module definition conventions. Line 3 defines the package. Line  4  includes a built-in Perl module,  Exporter,  which provides information about these definition conventions. Lines 6 and 7 define the subroutines and variables that are to be made available to the outside world.</p>
        <p id="p_16348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16348"> Line  6  creates a special array named  @EXPORT.  This array lists the subroutines that can be called by other programs. Here, the subroutines myfuncl and myfunc2 are accessible. Any subroutine</p>
        <p id="p_16349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16349"> Analysis</p>
        <p id="p_16350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16350"> defined inside a module that is not included in the list assigned to @EXPORT is a private subroutine, and can only be called inside the module.</p>
        <p id="p_16351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16351"> Line 7 creates another special array, called @EXPORT_OK, that lists the variables that can be accessed by other programs. Here, the variables $myvarl and $myvar2 are accessible from the outside world.</p>
        <p id="p_16352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16352"> Importing Modules Into Your Program</p>
        <p id="p_16353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16353"> To import a module into your Perl program, use the use statement. For example, the following statement imports the Mymodule module into a program:</p>
        <p id="p_16354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16354"> use Mymodule;</p>
        <p id="p_16355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16355"> The subroutines and variables in Mymodule can now be used in your program.</p>
        <p id="p_16356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16356"> To undefine a previously imported module, use the no statement. For example, the following statement undefines the Mymodule module:</p>
        <p id="p_16357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16357"> no Mymodule;</p>
        <p id="p_16358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16358"> Listing 20.7 is an example of a program that imports and undefines a module. The integer module referenced here specifies that all arithmetic operations are to be on integers. Floating-point numbers are converted to integers before the arithmetic operations are performed.</p>
        <p id="p_16359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16359"> Type</p>
        <p id="p_16360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16360"> Listing 20.7. A program that uses the use and no statements.</p>
        <p id="p_16361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16361"> 1: #/usr/local/bin/perl</p>
        <p id="p_16362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16362"> 2:</p>
        <p id="p_16363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16363"> 3: use integer;</p>
        <p id="p_16364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16364"> 4: $result = 2.4 +  2.4;</p>
        <p id="p_16365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16365"> 5:  print  ("$result\n");</p>
        <p id="p_16366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16366"> 6 :</p>
        <p id="p_16367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16367"> 7: no integer;</p>
        <p id="p_16368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16368"> 8: $result = 2.4 +  2.4;</p>
        <p id="p_16369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16369"> 9:  print ("$result\n");</p>
        <p id="p_16370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16370"> Output</p>
        <p id="p_16371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16371"> $ program20_7 4</p>
        <p id="p_16372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16372"> 4.8</p>
        <p id="p_16373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16373"> $</p>
        <p id="p_16374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16374"> Line 3 of this program imports the integer module. As a consequence, Line 4 converts 2.4 to 2 before performing the addition, yielding the result 4.</p>
        <p id="p_16375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16375"> Line 7 undefines the integer module. This tells the Perl interpreter to revert to using floating-point numbers in arithmetic operations.</p>
        <p id="p_16376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16376"> Warning</p>
        <p id="p_16377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16377"> If a  use  or  no  statement appears inside a statement block, it remains in effect only for the duration of that block. For example:</p>
        <p id="p_16378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16378"> use integer;</p>
        <p id="p_16379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16379"> $resultl = 2.4 +  2.4; if ($resultl == 4) {</p>
        <p id="p_16380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16380"> no integer;</p>
        <p id="p_16381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16381"> $result2 = 3.4 +  3.4;</p>
        <p id="p_16382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16382"> }</p>
        <p id="p_16383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16383"> $result3 = 4.4 +  4.4;</p>
        <p id="p_16384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16384"> Here, the  no  statement is only in effect inside the  if  statement. In the statement after the  if,  the  integer  module is still in use, which means that 4.4 is converted to 4 before the addition is performed.</p>
        <p id="p_16385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16385"> Using Predefined Modules</p>
        <p id="p_16386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16386"> Perl 5 provides a variety of predefined modules that perform useful tasks. Each module can be imported by the use statement and removed by the no statement.</p>
        <p id="p_16387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16387"> The following are some of the most useful modules in this library:</p>
        <p id="p_16388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16388"> A complete list of the predefined modules included with Perl 5 can be found in your Perl documentation.</p>
        <p id="p_16389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16389"> TIP</p>
        <p id="p_16390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16390"> Perl 5 users all over the world write useful modules and make them available to the Perl community through the Internet. The Comprehensive Perl Archive Network (CPAN) of Perl archives provides a complete list of these modules. More information on the CPAN network is available at the Web site located at http://www.perl.com/perl/CPAN/README. html .</p>
        <p id="p_16391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16391"> Using Perl in C Programs</p>
        <p id="p_16392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16392"> Perl 5 enables you to call Perl subroutines from within C programs. To add this capability, you need to do two things: add references to Perl to your program source, and then link the Perl library when you compile your program.</p>
        <p id="p_16393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16393"> See the Perl documentation for more details on how to use Perl subroutines in C programs.</p>
        <p id="p_16394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16394"> Perl and CGI Scripts</p>
        <p id="p_16395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16395"> The Common Gateway Interface (CGI) is a standard for interfacing external applications with information servers (such as those found on the World Wide Web).</p>
        <p id="p_16396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16396"> For more information on CGI, go to the Web page located at</p>
        <p id="p_16397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16397"> http: //hoohoo. ncsa. uiuc. edu/cqi .  A library of CGI scripts written in Perl can be found at</p>
        <p id="p_16398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16398"> http://www.bio.cam.ac.uk/web/cqi-lib.pl.txt .</p>
        <p id="p_16399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16399"> Translators and Other Supplied Code</p>
        <p id="p_16400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16400"> The Perl distribution provides programs that translate the following items into Perl:</p>
        <p id="p_16401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16401"> • Programs written in the awk programming language</p>
        <p id="p_16402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16402"> • Scripts written for the sed command</p>
        <p id="p_16403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16403"> • Commands sent to the find command</p>
        <p id="p_16404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16404"> • Include files written in the C programming language</p>
        <p id="p_16405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16405"> For information on these translation programs, refer to the documentation supplied with your Perl distribution.</p>
        <p id="p_16406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16406"> Summary</p>
        <p id="p_16407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16407"> Today you learned about features of Perl that were not discussed on previous days.</p>
        <p id="p_16408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16408"> • require, which includes code from other files</p>
        <p id="p_16409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16409"> • The $#array variable, which returns the largest subscript of an array</p>
        <p id="p_16410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16410"> • Alternative methods of enclosing strings using q, qq, qw, «, and qx</p>
        <p id="p_16411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16411"> • The special internal values_LINE_,_FILE_, and_END_, which retrieve the current</p>
        <p id="p_16412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16412"> filename and line number and end the program</p>
        <p id="p_16413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16413"> • Using back quotes to treat the output from a command as a scalar value</p>
        <p id="p_16414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16414"> • Using ?? to match a pattern once, and using reset to reset ?? and variables</p>
        <p id="p_16415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16415"> • Using &lt;&gt; with indirect file variables and file lists</p>
        <p id="p_16416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16416"> • Global aliasing using *</p>
        <p id="p_16417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16417"> • Packages and modules</p>
        <p id="p_16418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16418"> • Using Perl in C programs</p>
        <p id="p_16419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16419"> Q&amp;A</p>
        <p id="p_16420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16420"> Q: Why does a file included by require need to execute a statement? Why does require</p>
        <p id="p_16421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16421"> check a return code?</p>
        <p id="p_16422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16422"> A:  Because files included by  require  can contain statements that are immediately executed,</p>
        <p id="p_16423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16423"> checking for a return code enables programs to determine whether code included by  require generated any errors.</p>
        <p id="p_16424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16424"> Q: Is a $#array variable defined for system array variables such as @ARGV?</p>
        <p id="p_16425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16425"> A:  Yes. For example,  $#ARGV  contains the largest subscript of the  @ARGV  array; you can test this to</p>
        <p id="p_16426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16426"> determine whether your program was passed enough arguments.</p>
        <p id="p_16427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16427"> Q: Are $# array variables defined for associative arrays?</p>
        <p id="p_16428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16428"> A:  No, because there is no concept of a "largest subscript" in associative arrays.</p>
        <p id="p_16429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16429"> Q: What happens to system variables when reset is called? For example, is @ARGV reset when</p>
        <p id="p_16430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16430"> reset is passed  "A"?</p>
        <p id="p_16431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16431"> A:</p>
        <p id="p_16432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16432"> The  reset  function affects all variables, including system variables. For this reason, you should be careful when you use  reset.</p>
        <p id="p_16433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16433"> Workshop</p>
        <p id="p_16434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16434"> The Workshop provides quiz questions to help you solidify your understanding of the material covered, and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.</p>
        <p id="p_16435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16435"> Quiz</p>
        <p id="p_16436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16436"> 1. What do these constants contain?</p>
        <p id="p_16437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16437"> a.  _LINE_</p>
        <p id="p_16438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16438"> b.  _FILE_</p>
        <p id="p_16439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16439"> C.  _END_</p>
        <p id="p_16440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16440"> 2.  What is the value of each of the following strings? (Assume that $var has the value hello.)</p>
        <p id="p_16441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16441"> 1. q(It's time to say $var)</p>
        <p id="p_16442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16442"> 2.  qq "It's time to say $var"; # a comment</p>
        <p id="p_16443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16443"> 3.  qx/echo $var/</p>
        <p id="p_16444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16444"> 2.  What is stored in @array after the following statements have been executed?</p>
        <p id="p_16445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16445"> @array = ("one", "two", "three", "four");</p>
        <p id="p_16446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16446"> $#array = 2;</p>
        <p id="p_16447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16447"> $array[4] = "five";</p>
        <p id="p_16448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16448"> 3.  How can you include code from another file in your program?</p>
        <p id="p_16449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16449"> Exercises</p>
        <p id="p_16450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16450"> 1. Write a program that uses the &lt;&gt; operator to list all the files in a directory in alphabetical order.</p>
        <p id="p_16451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16451"> 2.  Write a program that uses a subroutine named sum to add the numbers in a list and return the total. Read the list from standard input (one per line). Assume that the subroutine is contained in the file /u/jqpublic/perlfiles/sum.pl. Print the total returned by sum.</p>
        <p id="p_16452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16452"> 3.  Write a program that creates two packages named packl and pack2. For each package, read a line from standard input and assign it to the variable $var. Assume that each $var contains a number, add the two numbers together, and print the total.</p>
        <p id="p_16453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16453"> 4. BUG BUSTER: What is wrong with the following statements? print ("Perl files in this directory:\n");</p>
        <p id="p_16454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16454"> $filepattern = "*.pl";</p>
        <p id="p_16455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16455"> while ($name = &lt;$filepattern&gt;) { print ("$name\n") ;</p>
        <p id="p_16456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16456"> }</p>
        <p id="p_16457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16457"> 5. BUG BUSTER: What is wrong with the following statement? print «  EOF</p>
        <p id="p_16458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16458"> Here is part of my string.</p>
        <p id="p_16459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16459"> Here is the rest of my string. EOF</p>
        <p id="p_16460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16460"> Chapter 21</p>
        <p id="p_16461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16461"> The Perl Debugger</p>
        <p id="p_16462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16462"> CONTENTS</p>
        <p id="p_16463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16463"> •  Entering and Exiting the Perl Debugger</p>
        <p id="p_16464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16464"> o  Entering the Debugger o  Exiting the Debugger</p>
        <p id="p_16465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16465"> •  Listing Your Program</p>
        <p id="p_16466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16466"> o  The 1 command o  The Command o  The  w Command o  The / / and ? ? Commands</p>
        <p id="p_16467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16467"> o  The  S  Command</p>
        <p id="p_16468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16468"> •  Stepping Through Programs</p>
        <p id="p_16469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16469"> o  The s Command O  The  n Command o  The f command o  The Carriage-Return Command</p>
        <p id="p_16470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16470"> o  The  r  Command</p>
        <p id="p_16471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16471"> •  Displaying Variable Values</p>
        <p id="p_16472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16472"> o  The  X  Command o  The  V  Command</p>
        <p id="p_16473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16473"> •  Breakpoints</p>
        <p id="p_16474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16474"> o  The b Command o  The c Command o  The  L  Command and Breakpoints</p>
        <p id="p_16475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16475"> o  The  d  and  D  Commands</p>
        <p id="p_16476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16476"> •  Tracing Program Execution</p>
        <p id="p_16477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16477"> •  Line Actions</p>
        <p id="p_16478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16478"> o  The a Command o  The  A  Command o  The &lt; and &gt; Commands</p>
        <p id="p_16479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16479"> o  Displaying Line Actions Using the  L  Command</p>
        <p id="p_16480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16480"> •  Other Debugging Commands</p>
        <p id="p_16481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16481"> o  Executing Other Perl Statements o  The  H  Command: Listing Preceding Commands</p>
        <p id="p_16482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16482"> o  The ! Command: Executing Previous Commands</p>
        <p id="p_16483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16483"> The T Command: Stack Tracing</p>
        <p id="p_16484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16484"> o</p>
        <p id="p_16485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16485"> o</p>
        <p id="p_16486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16486"> The p Command: Printing an Expression</p>
        <p id="p_16487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16487"> o  The = Command: Defining Aliases</p>
        <p id="p_16488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16488"> o  Predefining Aliases o  The  h  Command: Debugger Help</p>
        <p id="p_16489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16489"> •  Summary</p>
        <p id="p_16490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16490"> •  Q&amp;A</p>
        <p id="p_16491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16491"> •  Workshop</p>
        <p id="p_16492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16492"> o  Quiz</p>
        <p id="p_16493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16493"> Today's lesson describes the Perl debugging facility. You'll learn the following:</p>
        <p id="p_16494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16494"> • How to enter and exit the Perl debugger</p>
        <p id="p_16495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16495"> • How to list parts of your program</p>
        <p id="p_16496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16496"> • How to execute one statement at a time</p>
        <p id="p_16497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16497"> • How to set breakpoints and trace program execution</p>
        <p id="p_16498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16498"> • How to perform line actions</p>
        <p id="p_16499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16499"> • About other useful debugging commands</p>
        <p id="p_16500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16500"> Entering and Exiting the Perl Debugger</p>
        <p id="p_16501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16501"> The following sections describe how to start the Perl debugger and how to exit.</p>
        <p id="p_16502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16502"> Entering the Debugger</p>
        <p id="p_16503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16503"> To debug a Perl program, specify the -d option when you ran the program. For example, to debug a program named  debugtest,  specify the following command:</p>
        <p id="p_16504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16504"> $ perl -d debugtest</p>
        <p id="p_16505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16505"> You can supply other options along with  -d  if you want to.</p>
        <p id="p_16506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16506"> When the Perl interpreter sees the  -d  option, it starts the Perl debugger. The debugger begins by displaying a message similar to the following one on your screen:</p>
        <p id="p_16507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16507"> Loading DB routines from $RCSfile: perldb.pl,v $$Revision:  4.0.1.3 $$Date:  92/06/08 13:43:57 $</p>
        <p id="p_16508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16508"> Emacs support available.</p>
        <p id="p_16509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16509"> Enter h for help.</p>
        <p id="p_16510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16510"> main::(debugtest:3): $dircount = 0;</p>
        <p id="p_16511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16511"> DB&lt;1&gt;</p>
        <p id="p_16512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16512"> The first few lines display the date on which this version of the debugger was created. The only lines of interest are the last two.</p>
        <p id="p_16513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16513"> The second-to-last line in this display lists the line that the debugger is about to execute. When the debugger starts, the first executable line of the program is displayed.</p>
        <p id="p_16514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16514"> When the debugger displays a line that it is about to execute, it also provides the following information about the line:</p>
        <p id="p_16515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16515"> • The package in which the line is contained (in this case, the default package, which is  main)</p>
        <p id="p_16516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16516"> • The name of the file containing the line (here, the file is named  debugtest)</p>
        <p id="p_16517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16517"> • The current line number (which, in this example, is 3)</p>
        <p id="p_16518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16518"> The last line of the display prompts you for a debugging command. The number enclosed in angle brackets indicates the command number; in this case, the number is 1, because you are about to specify the first debugging command.</p>
        <p id="p_16519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16519"> Later today you will learn how to use the debugging command number to re-enter debugging commands you have previously executed.</p>
        <p id="p_16520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16520"> NOTE</p>
        <p id="p_16521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16521"> To enter the debugger without supplying a program, supply the  -e option with the  -d  option:</p>
        <p id="p_16522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16522"> $ perl -d -e "1; "</p>
        <p id="p_16523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16523"> This line starts the debugger with a "program" consisting of the single statement</p>
        <p id="p_16524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16524"> i;</p>
        <p id="p_16525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16525"> (which is an expression that doesn't do anything meaningful).</p>
        <p id="p_16526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16526"> Starting the debugger without a program enables you to examine the predefined system variables or supply statements to be executed. You will learn how to perform both of these tasks later in today's lesson</p>
        <p id="p_16527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16527"> Exiting the Debugger</p>
        <p id="p_16528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16528"> To exit the debugger, enter the debugging command  q:</p>
        <p id="p_16529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16529"> DB&lt;1&gt; q</p>
        <p id="p_16530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16530"> This command halts program execution immediately and returns you to the command shell.</p>
        <p id="p_16531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16531"> Listing Your Program</p>
        <p id="p_16532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16532"> You can list any part of your program from within the debugger. The following sections describe debugging commands that perform the display operations.</p>
        <p id="p_16533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16533"> The 1 command</p>
        <p id="p_16534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16534"> The simplest way to list part of your program is with the 1 command, which lists the next few statements in your program:</p>
        <p id="p_16535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16535"> DB&lt;1&gt; 1</p>
        <p id="p_16536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16536"> The 1 command lists a window of executable statements, which is a group of statements small enough to be easily displayed on your screen. A window usually consists of about ten statements. The line number of each statement is displayed at the beginning of its line.</p>
        <p id="p_16537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16537"> Entering the 1 command again displays the window of statements immediately following the last displayed line, which in this example is line 12:</p>
        <p id="p_16538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16538"> DB&lt;1&gt; 1</p>
        <p id="p_16539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16539"> 13: }</p>
        <p id="p_16540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16540"> 14: $curdir = &amp;menudir($curdir) ;</p>
        <p id="p_16541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16541"> 15: }</p>
        <p id="p_16542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16542"> 16:</p>
        <p id="p_16543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16543"> 17 :</p>
        <p id="p_16544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16544"> 18: # Find all subdirectories of the given directory,</p>
        <p id="p_16545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16545"> 19: # and store them in an associative array.</p>
        <p id="p_16546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16546"> 20 : #</p>
        <p id="p_16547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16547"> 21: # The associative array subscripts and values are:</p>
        <p id="p_16548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16548"> 22: # &lt;directory name&gt;: 1</p>
        <p id="p_16549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16549"> You can specify the lines displayed by the 1 command. If you specify a single line number with the 1 command, it displays that line:</p>
        <p id="p_16550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16550"> DB&lt;1&gt; 1 10</p>
        <p id="p_16551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16551"> 10: $curdir =~  s/ A \s+|\s+$//g;</p>
        <p id="p_16552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16552"> To display a range of lines, specify the first and last line number, and separate them with a hyphen:</p>
        <p id="p_16553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16553"> DB&lt;1&gt; 1 10-15</p>
        <p id="p_16554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16554"> &amp;readsubdirs($curdir) ;</p>
        <p id="p_16555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16555"> 12 :</p>
        <p id="p_16556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16556"> 13: }</p>
        <p id="p_16557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16557"> 14: $curdir = &amp;menudir($curdir) ;</p>
        <p id="p_16558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16558"> You also can use the 1 command to specify a subroutine to display. To do this, provide the name of the subroutine to display:</p>
        <p id="p_16559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16559"> DB&lt;1&gt; 1 readsubdirs</p>
        <p id="p_16560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16560"> return;</p>
        <p id="p_16561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16561"> 35 :</p>
        <p id="p_16562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16562"> This command lists the statements in the subroutine. If the subroutine is too large to fit in a single window, only the first few statements are listed; you can list subsequent statements by entering 1 with no arguments.</p>
        <p id="p_16563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16563"> The - Command</p>
        <p id="p_16564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16564"> You can display the lines immediately preceding the last displayed line by entering the - command. For example, the following - command lists the window of lines immediately preceding the subroutine</p>
        <p id="p_16565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16565"> readsubdirs.</p>
        <p id="p_16566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16566"> DB&lt;1&gt; -16 :</p>
        <p id="p_16567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16567"> 17 :</p>
        <p id="p_16568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16568"> 18: # Find all subdirectories of the given directory,</p>
        <p id="p_16569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16569"> 19: # and store them in an associative array.</p>
        <p id="p_16570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16570"> 20 : #</p>
        <p id="p_16571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16571"> 21: # The associative array subscripts and values are:</p>
        <p id="p_16572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16572"> 22: # &lt;directory name&gt;: 1</p>
        <p id="p_16573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16573"> 23: # (indicates that directory has been read)</p>
        <p id="p_16574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16574"> 24: # &lt;directory name&gt;.&lt;num&gt; the &lt;num&gt;th subdirectory</p>
        <p id="p_16575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16575"> 25 :</p>
        <p id="p_16576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16576"> Subsequent - commands go back further in the file.</p>
        <p id="p_16577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16577"> The w Command</p>
        <p id="p_16578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16578"> To list a window of lines containinga specified line, use the  w  command, and specify the number of the line to be included:</p>
        <p id="p_16579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16579"> DB&lt;1&gt; w 7</p>
        <p id="p_16580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16580"> 4 :</p>
        <p id="p_16581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16581"> $curdir</p>
        <p id="p_16582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16582"> U f! .</p>
        <p id="p_16583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16583"> r</p>
        <p id="p_16584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16584"> The  w  command displays the three lines before the specified line and fills the window with the lines following it.</p>
        <p id="p_16585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16585"> The // and ?? Commands</p>
        <p id="p_16586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16586"> You can search for a line containing a particular pattern by enclosing the pattern in slashes:</p>
        <p id="p_16587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16587"> DB&lt;1&gt; /Find/</p>
        <p id="p_16588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16588"> 18: # Find all subdirectories of the given directory.</p>
        <p id="p_16589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16589"> The debugger searches forward from the last displayed line for a line matching the specified pattern. If it finds such a line, the line is displayed.</p>
        <p id="p_16590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16590"> To search backward for a particular pattern, enclose the pattern in question marks:</p>
        <p id="p_16591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16591"> DB&lt;1&gt; ?readsubdirs?</p>
        <p id="p_16592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16592"> 12: &amp;readsubdirs($curdir);</p>
        <p id="p_16593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16593"> This command starts with the last displayed line and searches backward until it finds a line matching the specified pattern.</p>
        <p id="p_16594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16594"> NOTE</p>
        <p id="p_16595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16595"> Patterns specified by / / and ? ? can contain any special character understood by the Perl interpreter.</p>
        <p id="p_16596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16596"> You optionally can omit the final / or ? character when you match a pattern.</p>
        <p id="p_16597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16597"> The S Command</p>
        <p id="p_16598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16598"> The  S  command lists all the subroutines in the current file, one subroutine per line:</p>
        <p id="p_16599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16599"> DBO S</p>
        <p id="p_16600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16600"> main::display main::followlink main::menudir main::readsubdirs</p>
        <p id="p_16601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16601"> Each subroutine name is preceded by the package name and a single quotation mark.</p>
        <p id="p_16602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16602"> Stepping Through Programs</p>
        <p id="p_16603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16603"> One of the most useful features of the Perl debugger is the capability to execute a program one statement at a time. The following sections describe the statements that carry out this action.</p>
        <p id="p_16604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16604"> The s Command</p>
        <p id="p_16605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16605"> To execute a single statement of your program, use the  s  command:</p>
        <p id="p_16606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16606"> DB&lt;2&gt; s</p>
        <p id="p_16607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16607"> main::(debugtest:4): $curdir =</p>
        <p id="p_16608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16608"> This command executes one statement of your program and then displays the next statement to be executed. If the statement executed needs to read from the standard input file, the debugger waits until the input is provided before displaying the next line to execute.</p>
        <p id="p_16609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16609"> [tip</p>
        <p id="p_16610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16610"> If you have forgotten which line is the next line to execute (because, for example, you have displayed lines using the 1 command), you can list the next line to execute using the  L  command:</p>
        <p id="p_16611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16611"> DB&lt;2&gt; L</p>
        <p id="p_16612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16612"> 3: $dircount = 0;</p>
        <p id="p_16613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16613"> The  L  command lists the last lines executed by the program. It also lists any breakpoints and line actions that have been defined for particular lines. Breakpoints and line actions are discussed later today.</p>
        <p id="p_16614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16614"> If the statement executed by the  s  command calls a subroutine, the Perl debugger enters the subroutine but does not execute any statements in it. Instead, it stops at the first executable statement in the subroutine and displays it. For example, if the following is the current line:</p>
        <p id="p_16615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16615"> main::(debugtest:12):</p>
        <p id="p_16616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16616"> &amp;readsubdirs($curdir) ;</p>
        <p id="p_16617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16617"> specifying the  s  command tells the Perl debugger to enter  readsubdirs  and display the following, which is the first executable line of  readsubdirs:</p>
        <p id="p_16618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16618"> main::readsubdirs(debugtest:27): local ($dirname) =</p>
        <p id="p_16619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16619"> The  s  command assumes that you want to debug the subroutine you have entered. If you know that a particular subroutine works properly and you don't want to step through it one statement at a time, use the n  command, described in the following section.</p>
        <p id="p_16620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16620"> The n Command</p>
        <p id="p_16621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16621"> The  n  command, like the  s  command, executes one line of your program and displays the next line to be executed:</p>
        <p id="p_16622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16622"> DB&lt;2&gt; n</p>
        <p id="p_16623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16623"> main::(debugtest:5): while (1) {</p>
        <p id="p_16624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16624"> The  n  statement, however, does not enter any subroutines. If the statement executed by  n  contains a subroutine call, the subroutine is executed in its entirety. After the subroutine is executed, the debugger displays the line immediately following the call.</p>
        <p id="p_16625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16625"> For example, if the current line is</p>
        <p id="p_16626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16626"> main::(debugtest:12): &amp;readsubdirs($curdir);</p>
        <p id="p_16627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16627"> the  n  command tells the debugger to execute  readsubdirs  and then display the next line in the program, which is</p>
        <p id="p_16628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16628"> main: : (debugtest: 13 : ) : }</p>
        <p id="p_16629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16629"> Combining the use of  s  and  n  ensures that the debugger examines only the subroutines you want to see.</p>
        <p id="p_16630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16630"> NOTE</p>
        <p id="p_16631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16631"> The Perl debugger does not enable you to enter any library functions. You can enter only subroutines that you have created yourself or that have been created previously and added to a subroutine library</p>
        <p id="p_16632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16632"> The f command</p>
        <p id="p_16633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16633"> The  f  command tells the Perl debugger to execute the remainder of the statements in the current subroutine and then display the line immediately after the subroutine call. This is useful when you are looking for a bug and have determined that the current subroutine does not contain the problem.</p>
        <p id="p_16634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16634"> The Carriage-Return Command</p>
        <p id="p_16635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16635"> If you are stepping through a program using  s  or  n,  you can save yourself some typing by just pressing Enter when you want to execute another statement. When you press Enter, the debugger repeats the last  s or  n  command executed.</p>
        <p id="p_16636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16636"> For example, to step from line 5 to line 7, you can use the  s  command as usual:</p>
        <p id="p_16637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16637"> DB&lt;3&gt;  S</p>
        <p id="p_16638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16638"> main::(debugtest:7):</p>
        <p id="p_16639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16639"> if ($curdir eq "") {</p>
        <p id="p_16640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16640"> (Line 6 is skipped because it contains no executable statements.) To execute line 7, you can now just press Enter:</p>
        <p id="p_16641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16641"> DB&lt;2&gt;</p>
        <p id="p_16642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16642"> main::(debugtest:8): 1 i s t: \ n " ) ;</p>
        <p id="p_16643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16643"> print ("Enter directory to</p>
        <p id="p_16644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16644"> NOTE</p>
        <p id="p_16645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16645"> Pressing Enter has no effect if you have not specified any  s  or  n commands.</p>
        <p id="p_16646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16646"> The r Command</p>
        <p id="p_16647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16647"> If you are inside a subroutine and decide that you no longer need to step through it, you can tell the Perl debugger to finish executing the subroutine and return to the statement after the subroutine call. To do this, use the  r  command:</p>
        <p id="p_16648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16648"> DB&lt;4&gt; r</p>
        <p id="p_16649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16649"> main::(debugtest:13:):</p>
        <p id="p_16650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16650"> }</p>
        <p id="p_16651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16651"> The statement displayed by the debugger is the first statement following the call to the subroutine.</p>
        <p id="p_16652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16652"> Displaying Variable Values</p>
        <p id="p_16653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16653"> Another powerful feature of the Perl debugger is the capability to display the value of any variable at any time. The following sections describe the commands that perform this action.</p>
        <p id="p_16654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16654"> The X Command</p>
        <p id="p_16655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16655"> The  X  command displays variables in the current package (which is  main  if no other package has been specified). If the  X  command is specified by itself, it lists all the variables in the current package, including the system-defined variables and the variables used by the Perl interpreter itself. Usually, you won't want to use the  X  command by itself, because there are a lot of system-defined and internal variables known to the Perl inteipreter.</p>
        <p id="p_16656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16656"> To print the value of a particular variable or variables, specify the variable name or names with the  X command:</p>
        <p id="p_16657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16657"> DB&lt;5&gt; X dircount</p>
        <p id="p_16658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16658"> = ’O'</p>
        <p id="p_16659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16659"> $dircount</p>
        <p id="p_16660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16660"> This capability often is useful when you are checking for errors in your program.</p>
        <p id="p_16661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16661"> Warning</p>
        <p id="p_16662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16662"> You must not supply the $ character with the variable name when you use the  X  command. If you supply the $ character (or the @ or % characters for arrays), the debugger displays nothing.</p>
        <p id="p_16663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16663"> You can use  X  to display the values of array variables and associative array variables.</p>
        <p id="p_16664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16664"> DB&lt;6&gt; X regarray @regarray = (</p>
        <p id="p_16665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16665"> 0 14</p>
        <p id="p_16666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16666"> 1 'hello'</p>
        <p id="p_16667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16667"> 2 36</p>
        <p id="p_16668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16668"> DB&lt;7&gt; X assocarray %assoc_array = (</p>
        <p id="p_16669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16669"> 'hi' 1</p>
        <p id="p_16670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16670"> 'there' 2</p>
        <p id="p_16671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16671"> )</p>
        <p id="p_16672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16672"> Each command prints the subscripts of the array and their values. Regular arrays are printed in order subscript; associative arrays are printed in no particular order.</p>
        <p id="p_16673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16673"> NOTE</p>
        <p id="p_16674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16674"> If you have an array variable and a scalar variable with the same name, the  X  command prints both variables:</p>
        <p id="p_16675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16675"> DB&lt;8&gt; X var $var = 'O'</p>
        <p id="p_16676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16676"> @var = (</p>
        <p id="p_16677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16677"> 0 ' test1'</p>
        <p id="p_16678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16678"> 1 ' test2'</p>
        <p id="p_16679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16679"> )</p>
        <p id="p_16680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16680"> There is no way to use  X  to display one variable but not the other.</p>
        <p id="p_16681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16681"> The V Command</p>
        <p id="p_16682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16682"> The  V  command is identical to the  X  command except that it prints the values of variables in any package. If you specify just a package name, as in the following, this command displays the values of all variables in the package (including system-defined and internal variables):</p>
        <p id="p_16683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16683"> DB&lt;9&gt; V mypack</p>
        <p id="p_16684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16684"> If you specify a package name and one or more variable names, as in the following, the debugger prints the values of the variables (if they are defined in that package):</p>
        <p id="p_16685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16685"> DB&lt;10&gt; V main dircount $dircount = 'O'</p>
        <p id="p_16686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16686"> Breakpoints</p>
        <p id="p_16687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16687"> As you have seen, you can tell the Perl debugger to execute one statement at a time. Another way of controlling program execution is to tell the debugger to execute up to a certain specified point in the program, called a  breakpoint.</p>
        <p id="p_16688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16688"> The following sections describe the commands that create breakpoints, and the command that executes until a breakpoint is detected.</p>
        <p id="p_16689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16689"> The b Command</p>
        <p id="p_16690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16690"> To set a breakpoint in your program, use the  b  command. This command tells the debugger to halt program execution whenever it is about to execute the specified line. For example, the following command tells the debugger to halt when it is about to execute line 10:</p>
        <p id="p_16691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16691"> DB&lt;11&gt; b 10</p>
        <p id="p_16692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16692"> (If the line is not breakable, the debugger will return  Line 10 is not breakable.)</p>
        <p id="p_16693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16693"> NOTE</p>
        <p id="p_16694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16694"> You can have as many breakpoints in your program as you want. The debugger will halt program execution if it is about to execute any of the statements at which a breakpoint has been defined.</p>
        <p id="p_16695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16695"> The  b  command also accepts subroutine names:</p>
        <p id="p_16696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16696"> DB&lt;12&gt; b menudir</p>
        <p id="p_16697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16697"> This sets a breakpoint at the first executable statement of the subroutine  menudir.</p>
        <p id="p_16698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16698"> You can use the  b  command to tell the program to halt only when a specified condition is true. For example, the following command tells the debugger to halt if it is about to execute line 10 and the variable $curdir  is equal to the null string:</p>
        <p id="p_16699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16699"> DB&lt;12&gt; b 10 ($curdir eq "")</p>
        <p id="p_16700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16700"> The condition specified with the  b  statement can be any legal Perl conditional expression.</p>
        <p id="p_16701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16701"> Warning</p>
        <p id="p_16702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16702"> If a statement is longer than a single line, you can set a breakpoint only at the first line of the statement:</p>
        <p id="p_16703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16703"> 71: print  ("Test",</p>
        <p id="p_16704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16704"> 72:  " here is more output");</p>
        <p id="p_16705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16705"> Here, you can set a breakpoint at line 71, but not line 72.</p>
        <p id="p_16706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16706"> The c Command</p>
        <p id="p_16707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16707"> After you have set a breakpoint, you can tell the debugger to execute until it reaches either the breakpoint</p>
        <p id="p_16708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16708"> or the end of the program. To do this, use the  c  command:</p>
        <p id="p_16709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16709"> DB&lt;13&gt;  C</p>
        <p id="p_16710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16710"> main::(debugtest:10): $curdir =~  s/ A \s+|\s+$//g;</p>
        <p id="p_16711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16711"> DB&lt;14&gt;</p>
        <p id="p_16712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16712"> When the debugger detects that it is about to execute line 10-the line at which the breakpoint was set-it halts and displays the line. (Recall that the debugger always displays the line it is about to execute.)</p>
        <p id="p_16713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16713"> The debugger now prompts you for another debugging command. This action enables you to start executing one statement at a time using  n  or  s,  continue execution using  c,  set more breakpoints using  b, or perform any other debugging operation.</p>
        <p id="p_16714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16714"> You can specify a temporary (one-time-only) breakpoint with the  c  command by supplying a line number:</p>
        <p id="p_16715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16715"> DB&lt;15&gt; c 12</p>
        <p id="p_16716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16716"> main::(debugtest:12): &amp;readsubdirs($curdir);</p>
        <p id="p_16717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16717"> The argument  12  supplied with the  c  command tells the debugger to define a temporary breakpoint at line 12 and then resume execution. When the debugger reaches line 12, it halts execution, displays the line, and deletes the breakpoint. (The line itself still exists, of course.)</p>
        <p id="p_16718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16718"> Using  c  to define a temporary breakpoint is useful if you want to skip a few lines without wasting your time executing the program one statement at a time. Using  c  also means that you don't have to bother defining a breakpoint using  b  and deleting it using  d  (described in the following section).</p>
        <p id="p_16719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16719"> TIP</p>
        <p id="p_16720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16720"> If you intend to define breakpoints using  c  or  b,  it is a good idea to ensure that each line of your program contains at most one statement. If you are in the habit of writing lines that contain more than one statement, such as</p>
        <p id="p_16721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16721"> $x++; $y++;</p>
        <p id="p_16722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16722"> you won't get as much use out of the debugger, because it can't stop in the middle of a line</p>
        <p id="p_16723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16723"> The L Command and Breakpoints</p>
        <p id="p_16724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16724"> To list all of your breakpoints, use the  L  command. This command lists the last few lines executed, the current line, the breakpoints you have defined, and the conditions under which the breakpoints go into effect.</p>
        <p id="p_16725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16725"> DB&lt;16&gt; L</p>
        <p id="p_16726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16726"> break if (1)</p>
        <p id="p_16727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16727"> 0 ;</p>
        <p id="p_16728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16728"> II . r</p>
        <p id="p_16729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16729"> ($curdir eq " $ curdir</p>
        <p id="p_16730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16730"> s/ A \s+|\s + $//g;</p>
        <p id="p_16731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16731"> Here, the program has executed lines 3-7, and a breakpoint is defined for line 10. (Line 6 is not listed because it is a comment.) You can distinguish breakpoints from executed lines by looking for the breakpoint conditional expression, which immediately follows the breakpoint. Here, the conditional expression is  (1),  which indicates that the breakpoint is always in effect.</p>
        <p id="p_16732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16732"> The d and D Commands</p>
        <p id="p_16733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16733"> When you are finished with a breakpoint, you can delete it using the  d  command.</p>
        <p id="p_16734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16734"> DB&lt;16&gt; d 10</p>
        <p id="p_16735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16735"> This command tells the debugger to delete the breakpoint at line 10. The line itself remains in the program.</p>
        <p id="p_16736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16736"> If you do not specify a breakpoint to delete, the debugger assumes that a breakpoint is defined for the next line to be executed, and deletes it.</p>
        <p id="p_16737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16737"> main::(debugtest:12): DB&lt;17&gt; d</p>
        <p id="p_16738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16738"> &amp;readsubdirs($curdir) ;</p>
        <p id="p_16739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16739"> Here, line 12 is the next line to be executed, so the debugger deletes the breakpoint at line 12. To delete all your breakpoints, use the  D  command.</p>
        <p id="p_16740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16740"> DB&lt;18&gt; D</p>
        <p id="p_16741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16741"> This command deletes all the breakpoints you have defined with the  b  command.</p>
        <p id="p_16742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16742"> Tracing Program Execution</p>
        <p id="p_16743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16743"> When you run a program using the Perl debugger, you can tell it to display each line as it is executed. When the debugger is doing this, it is said to be in  trace mode.</p>
        <p id="p_16744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16744"> To turn on trace mode, use the  T  command.</p>
        <p id="p_16745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16745"> DB&lt;18&gt; t Trace = on</p>
        <p id="p_16746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16746"> When a statement is executed in trace mode, the statement is displayed. For example, if the current line is line 5 and the command  c 10  (which executes up to line 10) is entered, the following is displayed:</p>
        <p id="p_16747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16747"> DB&lt;18&gt; c 10</p>
        <p id="p_16748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16748"> main: : (debugtest:5) :</p>
        <p id="p_16749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16749"> while (1) {</p>
        <p id="p_16750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16750"> main::(debugtest:7):</p>
        <p id="p_16751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16751"> if ($curdir eq "") {</p>
        <p id="p_16752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16752"> main::(debugtest:10): s/ A \s+|\s + $//g;</p>
        <p id="p_16753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16753"> $curdir</p>
        <p id="p_16754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16754"> DB&lt;19&gt;</p>
        <p id="p_16755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16755"> The debugger prints and executes line 5 and line 7, then displays line 10 and waits for further instructions.</p>
        <p id="p_16756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16756"> To turn off trace mode, specify the  t  command again.</p>
        <p id="p_16757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16757"> DB&lt;19&gt; t</p>
        <p id="p_16758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16758"> Trace = off</p>
        <p id="p_16759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16759"> At this point, trace mode is turned off until another  t  command is entered.</p>
        <p id="p_16760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16760"> Line Actions</p>
        <p id="p_16761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16761"> The Perl debugger enables you to specify one or more statements to be executed whenever the program reaches a specified line. Such statements are known as line actions. The most common line actions are printing the value of a variable and resetting a variable containing an erroneous value to the value you want.</p>
        <p id="p_16762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16762"> The following sections describe the debugging commands that define line actions.</p>
        <p id="p_16763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16763"> The a Command</p>
        <p id="p_16764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16764"> To specify a line action for a particular line, use the  a  command.</p>
        <p id="p_16765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16765"> DB&lt;19&gt; a 10 print ("curdir is $curdir\n");</p>
        <p id="p_16766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16766"> This command tells the debugger to execute the statement</p>
        <p id="p_16767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16767"> print ("curdir is $curdir\n");</p>
        <p id="p_16768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16768"> whenever it is about to execute line 10 of the program. The debugger performs the action just after it displays the current line and before it asks for the next debugging command.</p>
        <p id="p_16769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16769"> To create a line action containing more than one statement, just string the statements together. If you need more than one line for the statements, put a backslash at the end of the first line.</p>
        <p id="p_16770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16770"> DB&lt;20&gt; a 10 print ("curdir is $curdir\n"); print \</p>
        <p id="p_16771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16771"> ("this is a long line action\n");</p>
        <p id="p_16772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16772"> In this case, when the debugger reaches line 10, it executes the following statements:</p>
        <p id="p_16773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16773"> print ("curdir is $curdir\n");</p>
        <p id="p_16774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16774"> print ("this is a long line action\n");</p>
        <p id="p_16775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16775"> The A Command</p>
        <p id="p_16776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16776"> To delete the line actions defined using the  a  command, use the  A  command.</p>
        <p id="p_16777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16777"> DB&lt;21&gt; A</p>
        <p id="p_16778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16778"> This command deletes all line actions currently defined.</p>
        <p id="p_16779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16779"> [note</p>
        <p id="p_16780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16780"> The  A  command does not affect the &lt; and &gt; commands, described in the following section.</p>
        <p id="p_16781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16781"> The &lt; and &gt; Commands</p>
        <p id="p_16782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16782"> To define a line action that is to be executed before the debugger executes any further statements, use the &gt; command.</p>
        <p id="p_16783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16783"> DB&lt;21&gt;  &gt; print ("curdir before execution is $curdir\n");</p>
        <p id="p_16784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16784"> This command tells the debugger to print the value of $  curdir  before continuing.</p>
        <p id="p_16785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16785"> Similarly, the &lt; command defines a line action that is to be performed after the debugger has finished executing statements and before it asks for another debugging command:</p>
        <p id="p_16786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16786"> DB&lt;22&gt;  &lt; print ("curdir after execution is $curdir\n");</p>
        <p id="p_16787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16787"> This command tells the debugger to print the value of $  curdir  before halting execution again.</p>
        <p id="p_16788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16788"> The &lt; and &gt; commands are useful when you know that one of your variables has the wrong value, but you don't know which statement assigned the wrong value to the variable. By single-stepping through the program using  s  or  n,  and printing the variable either before or after executing each statement, you can determine where the variable was given its incorrect value.</p>
        <p id="p_16789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16789"> [note</p>
        <p id="p_16790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16790"> To delete a line action defined by the &lt; command, enter another &lt; command with no line action defined.</p>
        <p id="p_16791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16791"> DB&lt;23&gt; &lt;</p>
        <p id="p_16792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16792"> Similarly, the following command undoes the effects of a &gt; command:</p>
        <p id="p_16793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16793"> DB&lt;24&gt; &gt;</p>
        <p id="p_16794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16794"> Displaying Line Actions Using the L Command</p>
        <p id="p_16795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16795"> The  L  command prints any line actions you have defined using the  a  command (as well as breakpoints and executed lines). For example, suppose that you have defined a line action using the following command:</p>
        <p id="p_16796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16796"> DB&lt;25&gt; a 10 print ("curdir is $curdir\n");</p>
        <p id="p_16797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16797"> The  L  command then displays this line action as shown here:</p>
        <p id="p_16798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16798"> main::(debugtest:10): $curdir =~</p>
        <p id="p_16799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16799"> s/ A \s+|\s+$//g;</p>
        <p id="p_16800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16800"> action: print ("curdir is $curdir\n");</p>
        <p id="p_16801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16801"> The line action is always displayed immediately after the line for which it is defined. This method of display enables you to distinguish lines containing line actions from other lines displayed by the  L command.</p>
        <p id="p_16802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16802"> Other Debugging Commands</p>
        <p id="p_16803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16803"> The following sections describe the debugging commands not previously covered.</p>
        <p id="p_16804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16804"> Executing Other Perl Statements</p>
        <p id="p_16805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16805"> In the debugger, anything that is not a debugging command is assumed to be a Perl statement and is performed right away. For example:</p>
        <p id="p_16806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16806"> DB&lt;4&gt; @array = (1, 2, 3);</p>
        <p id="p_16807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16807"> You can use statements such as this to alter values in your program as it is being executed. This capability is useful when you are testing your code.</p>
        <p id="p_16808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16808"> [note</p>
        <p id="p_16809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16809"> If you wish, you can omit the semicolon at the end of the statement.</p>
        <p id="p_16810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16810"> The H Command: Listing Preceding Commands</p>
        <p id="p_16811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16811"> The  H  (for "history") command lists the preceding few commands you have entered.</p>
        <p id="p_16812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16812"> DB&lt;4&gt; H</p>
        <p id="p_16813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16813"> The commands are listed in reverse order, with the most recently executed command listed first. Each command is preceded by its command number, which is used by the ! command (described in the following section).</p>
        <p id="p_16814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16814"> NOTE</p>
        <p id="p_16815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16815"> The debugger saves only the commands that actually affect the debugging environment. Commands such as 1 and  s,  which perform useful work but do not change how the debugger behaves, are not listed by the  H  command.</p>
        <p id="p_16816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16816"> This is not a significant limitation because you can enter the letter again if needed.</p>
        <p id="p_16817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16817"> The ! Command: Executing Previous Commands</p>
        <p id="p_16818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16818"> Each command that is saved by the debugger and can be listed by the  H  command has a command number. You can use this command number to repeat a previously executed command. For example, to repeat command number 5, make the following entry:</p>
        <p id="p_16819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16819"> DB &lt;11&gt; ! 5 b 8</p>
        <p id="p_16820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16820"> DB &lt;12&gt;</p>
        <p id="p_16821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16821"> The debugger displays command number 5-in this case, the command  b 8-  and then executes it. If you omit the number, the debugger repeats the last command executed.</p>
        <p id="p_16822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16822"> DB &lt;12&gt; $foo += $bar + 1 DB &lt;13&gt; !</p>
        <p id="p_16823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16823"> $foo += $bar + 1</p>
        <p id="p_16824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16824"> DB &lt;14&gt;</p>
        <p id="p_16825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16825"> If you specify a negative number with !, the debugger skips back that many commands:</p>
        <p id="p_16826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16826"> Here, the !  -2  command refers to the command  $foo  +=  $bar + 1.</p>
        <p id="p_16827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16827"> Warning</p>
        <p id="p_16828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16828"> You can use ! only to repeat commands that are actually repeatable. Use the  H  command to list the commands that the debugger has saved and that can be repeated</p>
        <p id="p_16829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16829"> The T Command: Stack Tracing</p>
        <p id="p_16830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16830"> The  T  command enables you to display a stack trace, which is a collection of all the subroutines that have been called, listed in reverse order. Here is an example:</p>
        <p id="p_16831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16831"> DB &lt;16&gt; T</p>
        <p id="p_16832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16832"> Here, the  T  command indicates that the program is currently inside subroutine  sub 2, which was called from line 7 of your program; this subroutine is part of the  main  package. The call to  sub2  is passed the argument ' hi '.</p>
        <p id="p_16833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16833"> The $ = preceding the subroutine name indicates that the subroutine call is expecting a scalar return value. If the call is expecting a list to be returned, the characters @ = appear in front of the subroutine name.</p>
        <p id="p_16834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16834"> The next line of the displayed output tells you that sub2 was called by another subroutine, subl. This subroutine was also passed the argument '  hi  ', and it was called by line  3  of the program. Because the stack trace lists no more subroutines, line  3  is part of your main program.</p>
        <p id="p_16835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16835"> NOTE</p>
        <p id="p_16836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16836"> The list of arguments passed to a subroutine that is displayed by the stack trace is the list of actual values after variable substitution and expression evaluation are performed. This procedure enables you to use the stack trace to check whether your subroutines are being passed the values you expect.</p>
        <p id="p_16837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16837"> The p Command: Printing an Expression</p>
        <p id="p_16838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16838"> An easy way to print the value of an expression from inside the debugger is to use the  p  command.</p>
        <p id="p_16839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16839"> DB &lt;17&gt; p $curdir + 1</p>
        <p id="p_16840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16840"> 1</p>
        <p id="p_16841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16841"> The  p  command evaluates the expression and displays the result.</p>
        <p id="p_16842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16842"> NOTE</p>
        <p id="p_16843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16843"> The  p  command writes to the screen even when the program has redirected  STDOUT  to a file.</p>
        <p id="p_16844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16844"> The = Command: Defining Aliases</p>
        <p id="p_16845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16845"> If you find yourself repeatedly entering a long debugging command and you want to save yourself some typing, you can define an alias for the long command by using the = command. For example:</p>
        <p id="p_16846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16846"> DB &lt;15&gt; = pc print ("curdir is $curdir\n");</p>
        <p id="p_16847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16847"> = pc print ("curdir is $curdir\n");</p>
        <p id="p_16848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16848"> The = command prints the alias you have just defined and then stores it in the associative array %DB'alias  (package  DB,  array name  alias)  for future reference. From here on, the command</p>
        <p id="p_16849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16849"> DB &lt;16&gt; pc</p>
        <p id="p_16850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16850"> is equivalent to the command</p>
        <p id="p_16851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16851"> DB &lt;16&gt; print ("curdir is $curdir\n");</p>
        <p id="p_16852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16852"> To list the aliases you have defined so far, enter the = command by itself:</p>
        <p id="p_16853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16853"> DB &lt;17&gt; =</p>
        <p id="p_16854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16854"> pc = print ("curdir is $curdir\n")</p>
        <p id="p_16855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16855"> This command displays your defined aliases and their equivalent values.</p>
        <p id="p_16856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16856"> Predefining Aliases</p>
        <p id="p_16857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16857"> You can define aliases that are to be created every time you enter the Perl debugger.</p>
        <p id="p_16858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16858"> When the debugger starts, it first searches for a file named .  per ldb  in your home directory. If the debugger finds this file, it executes the statements contained there.</p>
        <p id="p_16859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16859"> To create an alias, add it to the .  per ldb  file. For example, to add the alias</p>
        <p id="p_16860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16860"> = pc print ("curdir is $curdir\n");</p>
        <p id="p_16861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16861"> add the following statement to your .  per ldb  file:</p>
        <p id="p_16862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16862"> $DB'alias{"pc"} = 's/^pc/print ("curdir is  $curdir\n");</p>
        <p id="p_16863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16863"> Here's how this works: when the Perl debugger creates an alias, it adds an element to the  $DB ' alias associative array. The subscript for this element is the alias you are defining, and the value is a substitution command that replaces the alias with the actual command you want to use. In the preceding example, the substitution takes any command starting with pc and replaces it with</p>
        <p id="p_16864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16864"> print ("curdir is $curdir\n");</p>
        <p id="p_16865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16865"> Warning</p>
        <p id="p_16866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16866"> Be careful when you define aliases in this way. For example, your substitution should match only the beginning of a command, as in /  A pc/.  Otherwise, the alias will replace any occurrence of the letters pc with your print command, which is not what you want.</p>
        <p id="p_16867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16867"> The h Command: Debugger Help</p>
        <p id="p_16868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16868"> The  h  (for help) command provides a list of each of the debugger commands listed in today's lesson, along with a one-line explanation of each. This is handy if you are in the middle of debugging a program and forget the syntax of a particular command.</p>
        <p id="p_16869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16869"> Summary</p>
        <p id="p_16870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16870"> Today, you have learned about the Perl debugger. This debugger enables you to perform the following tasks, among others:</p>
        <p id="p_16871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16871"> • List any part of your source file</p>
        <p id="p_16872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16872"> • Step through your program one statement at a time</p>
        <p id="p_16873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16873"> • Display any variables you have defined</p>
        <p id="p_16874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16874"> • Set breakpoints, which tell the debugger when to stop and request further commands</p>
        <p id="p_16875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16875"> • Set line actions, which are statements to be executed when the program reaches a particular line</p>
        <p id="p_16876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16876"> • Trace program execution as it happens</p>
        <p id="p_16877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16877"> • Print a stack trace, which lists the current subroutine you are in and the subroutines that called it</p>
        <p id="p_16878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16878"> Q&amp;A</p>
        <p id="p_16879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16879"> Q: Is it possible to enter more than one debugging command at a time?</p>
        <p id="p_16880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16880"> A: No; however, there's no real need to do so. If you want to perform several single steps at once, use</p>
        <p id="p_16881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16881"> the  c  command to skip ahead to a specified point. If you want to both step ahead and print the value of a variable, use the &lt; or &gt; command.</p>
        <p id="p_16882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16882"> Q: Is it possible to examine variables in one package while inside another?</p>
        <p id="p_16883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16883"> A: Yes. Use the  V  command or the standard Perl package/variable syntax.</p>
        <p id="p_16884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16884"> Q: If I discover that my program works and I want to turn off debugging, what do I do?</p>
        <p id="p_16885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16885"> A: You cannot exit the debugger in the middle of a program. However, if you delete all breakpoints</p>
        <p id="p_16886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16886"> and line actions and then enter the  c  command, the program begins executing normally and is no longer under control of the debugger.</p>
        <p id="p_16887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16887"> Q: How can I convert to a reusable breakpoint a one-time breakpoint created using c?</p>
        <p id="p_16888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16888"> A: By default, the  b  command sets a breakpoint at the line that is about to be executed. This is the</p>
        <p id="p_16889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16889"> line at which  c  has set its one-time breakpoint.</p>
        <p id="p_16890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16890"> Q: How can I execute other UNIX commands from inside the debugger?</p>
        <p id="p_16891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16891"> A: Enter a statement containing a call to the Perl  system  function. For example, to display the</p>
        <p id="p_16892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16892"> contents of the current directory, enter the following command:</p>
        <p id="p_16893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16893"> DB &lt;11&gt; system  ("Is");</p>
        <p id="p_16894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16894"> To temporarily escape from the debugger to a UNIX shell, enter the following command:</p>
        <p id="p_16895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16895"> DB &lt;12&gt; system  ("sh");</p>
        <p id="p_16896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16896"> When you are finished with the shell, enter the command exit, and you will return to the debugger.</p>
        <p id="p_16897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16897"> Q: What special built-in variables can be accessed from inside the debugger?</p>
        <p id="p_16898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16898"> A: All of them.</p>
        <p id="p_16899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16899"> Workshop</p>
        <p id="p_16900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16900"> The Workshop provides quiz questions to help you solidify your understanding of the material covered.</p>
        <p id="p_16901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16901"> Quiz</p>
        <p id="p_16902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16902"> 1. Define the following terms:</p>
        <p id="p_16903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16903"> 1. trace mode</p>
        <p id="p_16904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16904"> 2. stack trace</p>
        <p id="p_16905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16905"> 3. breakpoint</p>
        <p id="p_16906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16906"> 4. line action</p>
        <p id="p_16907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16907"> 2. Explain the differences between the  X  and  V  commands.</p>
        <p id="p_16908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16908"> 3. Explain the differences between the // and ? ? commands.</p>
        <p id="p_16909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16909"> 4. Explain the differences between the &lt; and &gt; commands.</p>
        <p id="p_16910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16910"> 5. Explain the differences between the  s  and  n  commands.</p>
        <p id="p_16911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16911"> 6. What do the following commands do?</p>
        <p id="p_16912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16912"> 1. 1</p>
        <p id="p_16913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16913"> 2. 1 26</p>
        <p id="p_16914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16914"> 3. 1 5-7</p>
        <p id="p_16915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16915"> 4. 1 5 + 7</p>
        <p id="p_16916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16916"> 5.  w</p>
        <p id="p_16917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16917"> Week 3 in Review</p>
        <p id="p_16918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16918"> In the final week of teaching yourself how to use Perl, you've learned about the extensive Perl function library and about built-in system variables and options. The pair of programs in Listings R3.1 and R3.2 use some of the features you've learned about during this week.</p>
        <p id="p_16919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16919"> These programs provide a simple "chat" service. The first program, the chat server, establishes connections with clients and passes messages back and forth. The second program, the chat client, enables users to establish connections to this server and send messages to the other users running the chat program.</p>
        <p id="p_16920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16920"> Each message that a user enters is sent to all the clients currently running the chat program. To quit chatting, the user enters  quit.</p>
        <p id="p_16921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16921"> The server program can be called with the -m (for monitor) option. When -m is specified, each message sent by a client is displayed by the server as it is sent.</p>
        <p id="p_16922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16922"> iMJ</p>
        <p id="p_16923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16923"> Listing R3.1. The chat server program.</p>
        <p id="p_16924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16924"> 1: #!/usr/local/bin/perl -s</p>
        <p id="p_16925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16925"> 2  :</p>
        <p id="p_16926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16926"> 3: # get port from command line, or use 2000 as default</p>
        <p id="p_16927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16927"> 4: if ($ #ARGV == -1) {</p>
        <p id="p_16928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16928"> 5 : $port = 2 000;</p>
        <p id="p_16929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16929"> 6: } else {</p>
        <p id="p_16930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16930"> 7</p>
        <p id="p_16931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16931"> $port = $ARGV[0];</p>
        <p id="p_16932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16932"> }</p>
        <p id="p_16933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16933"> 9: if (getservbyport($port, "tcp")) {</p>
        <p id="p_16934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16934"> 10: die ("can't access port $port\n");</p>
        <p id="p_16935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16935"> 11 : }</p>
        <p id="p_16936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16936"> 12 :</p>
        <p id="p_16937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16937"> 13: # initialization stuff</p>
        <p id="p_16938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16938"> 14: $nextport = $port + 1;</p>
        <p id="p_16939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16939"> 15: $maxclient = 0;</p>
        <p id="p_16940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16940"> 16 :</p>
        <p id="p_16941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16941"> 17: # establish main socket connection: clients use this to</p>
        <p id="p_16942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16942"> 18: # get ports for their own connections</p>
        <p id="p_16943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16943"> 19: ($dl, $d2, $prototype) = getprotobyname ("tcp");</p>
        <p id="p_16944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16944"> 20: $hostname = 'hostname';</p>
        <p id="p_16945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16945"> 21: chop ($hostname);</p>
        <p id="p_16946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16946"> 22: ($dl, $d2, $d3, $d4, $serverraddr) = gethostbyname</p>
        <p id="p_16947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16947"> ($hostname);</p>
        <p id="p_16948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16948"> 23: $serveraddr = pack ("Sna4x8", 2, $port, $serverraddr);</p>
        <p id="p_16949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16949"> 24: socket (SSOCKET, 2, 1, $prototype) ||</p>
        <p id="p_16950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16950"> 25: die ("No main server socket\n") ;</p>
        <p id="p_16951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16951"> 26: bind (SSOCKET, $serveraddr) ||</p>
        <p id="p_16952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16952"> 27: die ("Can't bind main server socket\n");</p>
        <p id="p_16953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16953"> 28: listen (SSOCKET, 5) ||</p>
        <p id="p_16954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16954"> 29: die ("Can't listen on main server socket\n") ;</p>
        <p id="p_16955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16955"> 30: select (STDOUT);</p>
        <p id="p_16956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16956"> 31:</p>
        <p id="p_16957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16957"> 32 :</p>
        <p id="p_16958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16958"> $1 = l;</p>
        <p id="p_16959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16959"> then</p>
        <p id="p_16960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16960"> 58 :</p>
        <p id="p_16961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16961"> $serverraddr)</p>
        <p id="p_16962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16962"> 59 :</p>
        <p id="p_16963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16963"> 60 :</p>
        <p id="p_16964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16964"> 61:</p>
        <p id="p_16965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16965"> 62 :</p>
        <p id="p_16966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16966"> 63 :</p>
        <p id="p_16967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16967"> 64 :</p>
        <p id="p_16968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16968"> 65 :</p>
        <p id="p_16969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16969"> 6 6 :</p>
        <p id="p_16970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16970"> 67 :</p>
        <p id="p_16971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16971"> 68  :</p>
        <p id="p_16972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16972"> $ serverraddr) 6 9 :</p>
        <p id="p_16973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16973"> 70 :</p>
        <p id="p_16974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16974"> 71:</p>
        <p id="p_16975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16975"> 72 :</p>
        <p id="p_16976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16976"> 13 :</p>
        <p id="p_16977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16977"> 74 :</p>
        <p id="p_16978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16978"> 75 :</p>
        <p id="p_16979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16979"> 16 :</p>
        <p id="p_16980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16980"> 11 :</p>
        <p id="p_16981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16981"> 78 : the</p>
        <p id="p_16982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16982"> 19 :</p>
        <p id="p_16983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16983"> 80 :</p>
        <p id="p_16984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16984"> 81:</p>
        <p id="p_16985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16985"> 82 :</p>
        <p id="p_16986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16986"> $msgaddr = pack ("Sna4x8",  2 ,  $recvport,</p>
        <p id="p_16987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16987"> r</p>
        <p id="p_16988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16988"> bind (C1SOCKET, $msgaddr) ||</p>
        <p id="p_16989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16989"> die ("Can't bind receive client\n"); listen (C1SOCKET, 1) | |</p>
        <p id="p_16990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16990"> die ("Can't listen for receive client\n"); $rsockname = "CRSOCKET" . $maxclient;</p>
        <p id="p_16991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16991"> ($clientaddr = accept ($rsockname, C1SOCKET)) ||</p>
        <p id="p_16992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16992"> die ("Can't accept receive client\n"); socket (C2SOCKET, 2,  1 ,  $prototype) ||</p>
        <p id="p_16993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16993"> die ("No send client socket\n") ;</p>
        <p id="p_16994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16994"> $msgaddr = pack ("Sna4x8",  2 ,  $sendport,</p>
        <p id="p_16995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16995"> t</p>
        <p id="p_16996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16996"> bind (C2SOCKET, $msgaddr) ||</p>
        <p id="p_16997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16997"> die ("Can't bind send client\n"); listen (C2SOCKET, 1) | |</p>
        <p id="p_16998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16998"> die ("Can't listen for send client\n"); $ssockname = "CSSOCKET" . $maxclient;</p>
        <p id="p_16999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_16999"> ($clientaddr = accept ($ssockname, C2SOCKET)) ||</p>
        <p id="p_17000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17000"> die ("Can't accept send client\n"); select ($ssockname) ;</p>
        <p id="p_17001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17001"> $1 = l;</p>
        <p id="p_17002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17002"> # when a new client is created, we have to kill all</p>
        <p id="p_17003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17003"> # existing children and start new ones, so that all</p>
        <p id="p_17004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17004"> # of the sockets are known to all of the clients</p>
        <p id="p_17005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17005"> for ($i = 0; $i &lt;= $maxclient-l; $i++) {</p>
        <p id="p_17006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17006"> kill (2, $procids[$i]);</p>
        <p id="p_17007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17007"> Analysis</p>
        <p id="p_17008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17008"> This program starts off by obtaining the number of the port to be used for the main socket connection. This port number is assumed to be the first argument on the command line; if no port number is supplied, 2000 is used. Lines 9-11 call  get servbyport  to check whether this port number is mentioned in the  /etc/ services  file. If it is, the port number is reserved for use by some other program and can't be used here.</p>
        <p id="p_17009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17009"> Next, lines 17-29 define a socket using the specified port number. Client programs use this socket to establish connections to the server. Note, in particular, that lines 20-21 read the machine name by calling the UNIX  hostname  command. This enables you to move this program to another machine without having to edit it.</p>
        <p id="p_17010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17010"> Once the main socket has been established, the server is ready to listen for clients. When a client establishes a connection, the server finds two unused port numbers and sends them to the client. These ports will be used to establish two new socket connections-one for reading and one for writing-which will be used by the server and this particular client. This leaves the main socket free to establish connections with other clients. Lines 44-54 handle the task of obtaining the ports and sending them to the client; lines 55-77 then establish connections to the client using the sockets. (Note that line 53 also sends the process ID of the chat server to the client. This enables the client to kill off the server if something horrible happens.)</p>
        <p id="p_17011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17011"> The chat server communicates with a client by spawning a child process that handles the task of receiving a message from that client and sending it to the other clients. One child process is defined for each client. The call to  fork  in line 85 creates a child process.</p>
        <p id="p_17012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17012"> When a child process is created, it knows the names of the file variables corresponding to the sockets defined for each of the existing clients. However, if a new client appears, the existing child processes cannot send messages to the new client because they cannot access its sockets (because they are created after the children were spawned). To ensure that the existing clients can send messages to the new clients, the server program does the following:</p>
        <p id="p_17013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17013"> 1. It kills all the child processes that communicate with clients. Lines 81-83 accomplish this task by calling  kill.</p>
        <p id="p_17014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17014"> 2. The server creates a new child process for each client. Lines 84-93 perform this task.</p>
        <p id="p_17015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17015"> At this point, each client can talk to every other client because all of the socket connections are known by each child. (Recall that when a program splits into parent and child processes, each process has a copy of all the variables that have been defined to this point.)</p>
        <p id="p_17016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17016"> The chat server uses the global variable  $maxclient  to keep track of how many clients are on the machine. This ensures that the correct number of child processes are created.</p>
        <p id="p_17017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17017"> Each child process created by  fork  calls the subroutine  talk_to_client,  which reads messages from the client and then sends them to all the other clients via the "send" socket connections. Line 106 reads a message from the client. Line 107 checks whether the message is in fact  quit;  if it is, the subroutine (and the process) terminates. Lines 108-111 then print the message if the -m option is specified. Finally, lines 113-118 send the message to the other clients.</p>
        <p id="p_17018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17018"> NOTE</p>
        <p id="p_17019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17019"> The chat server does not make any attempt to clean up after itself or to reuse sockets closed by clients. This means that this particular program can't be used by too many clients at a time, or by too many different clients.</p>
        <p id="p_17020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17020"> If you are looking for a challenging exercise, try modifying this program to close sockets when clients are finished with them. (This is a challenging exercise because the child process somehow has to tell the main server that the socket can be closed. You can do it, but it's not easy!)</p>
        <p id="p_17021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17021"> Now that you've seen how the chat server works, Listing R3.2 shows the chat client program. Users run this program to establish a connection with the chat server and to chat with other users.</p>
        <p id="p_17022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17022"> Listing R3.2. The chat client program.</p>
        <p id="p_17023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17023"> 1 :</p>
        <p id="p_17024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17024"> 2 :</p>
        <p id="p_17025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17025"> 3 :</p>
        <p id="p_17026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17026"> 4 :</p>
        <p id="p_17027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17027"> 5 : 6 :</p>
        <p id="p_17028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17028"> 7 :</p>
        <p id="p_17029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17029"> 8  : 9: 10  : 11 : 12  :</p>
        <p id="p_17030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17030"> 13 :</p>
        <p id="p_17031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17031"> 14 :</p>
        <p id="p_17032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17032"> 15 :</p>
        <p id="p_17033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17033"> 16 :</p>
        <p id="p_17034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17034"> 17 :</p>
        <p id="p_17035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17035"> 18 :</p>
        <p id="p_17036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17036"> 19 :</p>
        <p id="p_17037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17037"> 20  : 21 : 22  :</p>
        <p id="p_17038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17038"> 23 :</p>
        <p id="p_17039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17039"> 24 :</p>
        <p id="p_17040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17040"> #!/usr/local/bin/perl</p>
        <p id="p_17041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17041"> # obtain the server port from the command line;</p>
        <p id="p_17042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17042"> # use 2000 as the default</p>
        <p id="p_17043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17043"> if ($#ARGV == -1) {</p>
        <p id="p_17044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17044"> $servport = 2000;</p>
        <p id="p_17045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17045"> } else {</p>
        <p id="p_17046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17046"> $servport  = $ARGV[0];</p>
        <p id="p_17047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17047"> }</p>
        <p id="p_17048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17048"> # obtain the server machine name from the command lin</p>
        <p id="p_17049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17049"> # use "silver" as the default</p>
        <p id="p_17050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17050"> if ($#ARGV &lt; 1) {</p>
        <p id="p_17051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17051"> $servname = "silver";</p>
        <p id="p_17052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17052"> } else {</p>
        <p id="p_17053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17053"> $servname = $ARGV[1];</p>
        <p id="p_17054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17054"> }</p>
        <p id="p_17055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17055"> # establish socket connection with server to obtain</p>
        <p id="p_17056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17056"> # ports for this client</p>
        <p id="p_17057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17057"> if (getservbyport($servport, "tcp")) {</p>
        <p id="p_17058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17058"> die ("can't access port $servport\n");</p>
        <p id="p_17059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17059"> }</p>
        <p id="p_17060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17060"> ($dl, $d2, $prototype) = getprotobyname  ("tcp");</p>
        <p id="p_17061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17061"> $hostname</p>
        <p id="p_17062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17062"> hostname';</p>
        <p id="p_17063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17063"> 25: chop ($hostname);</p>
        <p id="p_17064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17064"> 26: ($dl, $d2, $d3, $d4, $clientraddr) = gethostbyname</p>
        <p id="p_17065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17065"> ($hostname);</p>
        <p id="p_17066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17066"> 27: ($dl, $d2, $d3, $d4, $serverraddr) = gethostbyname</p>
        <p id="p_17067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17067"> ($servname);</p>
        <p id="p_17068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17068"> 28: $clientaddr = pack ("Sna4x8", 2, 0, $clientraddr);</p>
        <p id="p_17069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17069"> 29: $serveraddr = pack ("Sna4x8",  2 ,  $servport, $serverraddr) 30: socket (SOCKET, 2, 1, $prototype) ||</p>
        <p id="p_17070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17070"> 31: die ("No server socket\n") ;</p>
        <p id="p_17071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17071"> 32: bind (SOCKET, $clientaddr) | |</p>
        <p id="p_17072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17072"> 33: die ("Can't bind server socket\n");</p>
        <p id="p_17073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17073"> 34: connect (SOCKET, $serveraddr) ||</p>
        <p id="p_17074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17074"> 35: die ("Can't connect to server\n");</p>
        <p id="p_17075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17075"> 36: $sendport = &lt;SOCKET&gt;;</p>
        <p id="p_17076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17076"> 37: $recvport = &lt;SOCKET&gt;;</p>
        <p id="p_17077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17077"> 38: $serverid = &lt;SOCKET&gt;;</p>
        <p id="p_17078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17078"> 39: close (SOCKET);</p>
        <p id="p_17079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17079"> 40: chop ($sendport);</p>
        <p id="p_17080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17080"> 41: chop ($recvport);</p>
        <p id="p_17081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17081"> 42 :</p>
        <p id="p_17082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17082"> 43: # use returned ports to create sockets for this client: 44: # first socket is send, the second is receive 45: $conncaddr = pack ("Sna4x8", 2, 0, $clientraddr);</p>
        <p id="p_17083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17083"> 46: $connsaddr = pack ("Sna4x8", 2, $sendport, $serverraddr);</p>
        <p id="p_17084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17084"> 47: socket (SSOCKET, 2, 1, $prototype) ||</p>
        <p id="p_17085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17085"> 48: &amp;nuke ("No send socket");</p>
        <p id="p_17086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17086"> 49: bind (SSOCKET, $conncaddr) | |</p>
        <p id="p_17087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17087"> &amp;nuke  ("Can't  bind send  socket");</p>
        <p id="p_17088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17088"> 50 :</p>
        <p id="p_17089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17089"> 51: connect (SSOCKET, $connsaddr) ||</p>
        <p id="p_17090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17090"> 52: &amp;nuke ("Can't connect to send socket");</p>
        <p id="p_17091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17091"> 53: $connraddr = pack ("Sna4x8", 2, $recvport, $serverraddr) ;</p>
        <p id="p_17092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17092"> 54: socket (RSOCKET, 2, 1, $prototype) ||</p>
        <p id="p_17093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17093"> 55: &amp;nuke ("No receive socket");</p>
        <p id="p_17094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17094"> 56: bind (RSOCKET, $conncaddr) | |</p>
        <p id="p_17095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17095"> 57: &amp;nuke ("Can't bind receive socket");</p>
        <p id="p_17096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17096"> 58: connect (RSOCKET, $connraddr) ||</p>
        <p id="p_17097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17097"> 59: &amp;nuke ("Can't connect to receive socket");</p>
        <p id="p_17098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17098"> 60: select (SSOCKET);</p>
        <p id="p_17099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17099"> 61:  $1=1;</p>
        <p id="p_17100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17100"> 62: select (STDOUT);</p>
        <p id="p_17101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17101"> 63:  $1=1;</p>
        <p id="p_17102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17102"> 64 :</p>
        <p id="p_17103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17103"> 65: # now, we're ready to go: prompt for user name</p>
        <p id="p_17104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17104"> 66: select (STDOUT);</p>
        <p id="p_17105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17105"> 67: print ("Welcome to chat! Who are you? ");</p>
        <p id="p_17106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17106"> 68: $username = &lt;STDIN&gt;;</p>
        <p id="p_17107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17107"> 69: chop ($username);</p>
        <p id="p_17108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17108"> 70: print ("Type 'quit' to exit chat.\n");</p>
        <p id="p_17109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17109"> 71: $child =  fork();</p>
        <p id="p_17110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17110"> 72:  if ($child == 0) {</p>
        <p id="p_17111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17111"> 73: # child: receive messages</p>
        <p id="p_17112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17112"> 74: &amp;receive_msgs() ;</p>
        <p id="p_17113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17113"> 75 :</p>
        <p id="p_17114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17114"> exit(0);</p>
        <p id="p_17115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17115"> 76: }</p>
        <p id="p_17116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17116"> 89: if ($msg !~  / A \s*$/)  {</p>
        <p id="p_17117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17117"> 90:  print ($username . ": " . $msg .  "\n");</p>
        <p id="p_17118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17118"> 91: }</p>
        <p id="p_17119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17119"> 92 : }</p>
        <p id="p_17120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17120"> 93:  kill (9, $child);</p>
        <p id="p_17121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17121"> 94: close (RSOCKET);</p>
        <p id="p_17122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17122"> 95: close (SSOCKET);</p>
        <p id="p_17123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17123"> 9 6 :</p>
        <p id="p_17124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17124"> 97: sub receive_msgs {</p>
        <p id="p_17125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17125"> 98 : local ($msg);</p>
        <p id="p_17126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17126"> 9 9 :</p>
        <p id="p_17127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17127"> 100: while (1) {</p>
        <p id="p_17128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17128"> 101 :</p>
        <p id="p_17129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17129"> $msg</p>
        <p id="p_17130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17130"> &lt;RSOCKET&gt;;</p>
        <p id="p_17131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17131"> Analysis</p>
        <p id="p_17132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17132"> This program starts off by obtaining the port number of the main socket connection employed by the chat server. This port number can be supplied on the command line; if it is not, the chat program uses 2000 as the port number.</p>
        <p id="p_17133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17133"> The chat program then obtains the name of the machine on which the chat server is running. This name also can be supplied on the command line; if it isn't, the chat program assumes the machine is a local machine named  silver.</p>
        <p id="p_17134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17134"> Lines 23-41 establish a connection to the main server socket and receive the port numbers for the sockets to be used by this particular client-server connection. The client also receives the process ID of the server.</p>
        <p id="p_17135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17135"> Once the port numbers have been received, the chat program can connect to the chat server by establishing two socket connections with the server: one to send messages and another to receive them. Lines 43-63 accomplish this task.</p>
        <p id="p_17136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17136"> When these socket connections have been established, the chat program is ready to send messages. Lines 67-68 ask for a name by which you can identify yourself to the other users on the system. Once this name has been read in, the chat program splits itself in two by calling  fork.  The parent process handles the sending of messages, and the child handles messages received by other clients. This ensures that sending and receiving can take place at the same time.</p>
        <p id="p_17137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17137"> Lines 77-95 send messages to the chat server. Line 81 prompts for a line of input. If the line of input is</p>
        <p id="p_17138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17138"> the message  quit,  the client program kills off its child, closes its socket connection, and exits.</p>
        <p id="p_17139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17139"> Otherwise, the message-along with the name of the user sending it-is transmitted to the chat server via the "write" socket. The server then sends it to all of the clients on the system.</p>
        <p id="p_17140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17140"> The child process calls the subroutine  receive_msgs,  which handles the task of receiving messages from the other clients. Messages are received via the "read" socket and are printed as they are received.</p>
        <p id="p_17141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17141"> If something goes radically wrong, the chat program calls the subroutine  nuke,  which kills both itself and the server program.</p>
        <p id="p_17142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17142"> Appendix A Answers</p>
        <p id="p_17143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17143"> CONTENTS</p>
        <p id="p_17144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17144"> •  Answers for Day 1, "Getting Started"</p>
        <p id="p_17145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17145"> o  Quiz o  Exercises</p>
        <p id="p_17146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17146"> •  Answers for Day 2, "Basic Operators and Control Flow"</p>
        <p id="p_17147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17147"> o  Quiz o  Exercises</p>
        <p id="p_17148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17148"> •  Answers for Day 3, "Understanding Scalar Values"</p>
        <p id="p_17149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17149"> o  Quiz o  Exercises</p>
        <p id="p_17150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17150"> •  Answers for Day 4, "More Operators"</p>
        <p id="p_17151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17151"> o  Quiz o  Exercises</p>
        <p id="p_17152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17152"> •  Answers for Day 5, "Lists and Array Variables"</p>
        <p id="p_17153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17153"> o  Quiz o  Exercises</p>
        <p id="p_17154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17154"> •  Answers for Day 6, "Reading from and Writing to Files"</p>
        <p id="p_17155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17155"> o  Quiz o  Exercises</p>
        <p id="p_17156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17156"> •  Answers for Day 7, "Pattern Matching"</p>
        <p id="p_17157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17157"> o  Quiz o  Exercises</p>
        <p id="p_17158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17158"> •  Answers for Day 8, "More Control Structures"</p>
        <p id="p_17159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17159"> o  Quiz o  Exercises</p>
        <p id="p_17160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17160"> •  Answers for Day 9, "Using Subroutines"</p>
        <p id="p_17161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17161"> o  Quiz o  Exercises</p>
        <p id="p_17162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17162"> •  Answers for Day 10, "Associative Arrays"</p>
        <p id="p_17163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17163"> o  Quiz o  Exercises</p>
        <p id="p_17164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17164"> •  Answers for Day 11, "Formatting Your Output"</p>
        <p id="p_17165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17165"> o  Quiz</p>
        <p id="p_17166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17166"> o  Exercises</p>
        <p id="p_17167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17167"> •  Answers for Day 12, "Working with the File System"</p>
        <p id="p_17168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17168"> o  Quiz o  Exercises</p>
        <p id="p_17169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17169"> •  Answers for Day 13, "Process, String, and Mathematical Functions"</p>
        <p id="p_17170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17170"> o  Quiz o  Exercises</p>
        <p id="p_17171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17171"> •  Answers for Day 14, "Scalar-Conversion and List-Manipulation Functions"</p>
        <p id="p_17172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17172"> o  Quiz o  Exercises</p>
        <p id="p_17173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17173"> •  Answers for Day 15, "System Functions"</p>
        <p id="p_17174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17174"> o  Quiz o  Exercises</p>
        <p id="p_17175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17175"> •  Answers for Day 16, "Command-Line Options"</p>
        <p id="p_17176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17176"> o  Quiz o  Exercises</p>
        <p id="p_17177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17177"> •  Answers for Day 17, "System Variables"</p>
        <p id="p_17178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17178"> o  Quiz o  Exercises</p>
        <p id="p_17179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17179"> •  Answers for Day 18, "References in</p>
        <p id="p_17180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17180"> Perl 5"</p>
        <p id="p_17181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17181"> o  Quiz o  Exercises</p>
        <p id="p_17182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17182"> •  Answers for Day 19, "Object-Oriented Programming in Perl"</p>
        <p id="p_17183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17183"> o  Quiz o  Exercises</p>
        <p id="p_17184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17184"> •  Answers for Day 20, "Miscellaneous Features of Perl"</p>
        <p id="p_17185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17185"> o  Quiz o  Exercises</p>
        <p id="p_17186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17186"> •  Answers for Day 21, "The Perl Debugger"</p>
        <p id="p_17187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17187"> o  Quiz</p>
        <p id="p_17188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17188"> Answers for Day 1, ’’Getting Started"</p>
        <p id="p_17189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17189"> Quiz</p>
        <p id="p_17190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17190"> 1. Perl has the power of a high-level programming language such as C, and the ease of use of simple languages such as shell scripts.</p>
        <p id="p_17191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17191"> 2. The Perl interpreter executes your Perl program (starting from the beginning, and continuing</p>
        <p id="p_17192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17192"> one statement at a time).</p>
        <p id="p_17193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17193"> 3. The answers are as follows:</p>
        <p id="p_17194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17194"> 1. A statement is one particular task or instruction (usually corresponding to a line of code). A statement is terminated by a semicolon (;).</p>
        <p id="p_17195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17195"> 2. A token is the smallest unit of information understood by the Perl interpreter. A statement consists of several tokens.</p>
        <p id="p_17196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17196"> 3. An argument is an item passed to a library function (such as $  input line  to  print).</p>
        <p id="p_17197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17197"> 4. Error recovery occurs when the Perl interpreter detects an error in your program. The interpreter tries to deduce what you meant to write and attempts to continue detecting errors in the program.</p>
        <p id="p_17198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17198"> 5. The standard input file is the file that stores the characters you enter at the keyboard.</p>
        <p id="p_17199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17199"> 4. A comment is any text that is preceded by a #. A comment can appear anywhere in your program. Everything after the # character is assumed to be part of the comment.</p>
        <p id="p_17200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17200"> 5.  Perl usually is located in the file  /usr/local/bin/perl.</p>
        <p id="p_17201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17201"> 6. The header comment is the special comment that tells the system that this is a Perl program. It appears as the first line of every Perl program.</p>
        <p id="p_17202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17202"> 7. A library function is defined as part of the Perl interpreter and performs a specific task.</p>
        <p id="p_17203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17203"> Exercises</p>
        <p id="p_17204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17204"> 1. The following is one possible solution:</p>
        <p id="p_17205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17205"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;; print ($inputline, $inputline);</p>
        <p id="p_17206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17206"> 2. The following is one possible solution:</p>
        <p id="p_17207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17207"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;; print ($inputline);</p>
        <p id="p_17208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17208"> $inputline = &lt;STDIN&gt;; print ($inputline);</p>
        <p id="p_17209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17209"> 3. The following is one possible solution:</p>
        <p id="p_17210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17210"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;;</p>
        <p id="p_17211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17211"> $inputline = &lt;STDIN&gt;; # this throws away the previous input line</p>
        <p id="p_17212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17212"> print( $inputline ) ;</p>
        <p id="p_17213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17213"> 4. The third line of the program is missing a semicolon at the end of the statement:</p>
        <p id="p_17214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17214"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;; print ($inputline);</p>
        <p id="p_17215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17215"> 5.  The  print ($inputline)  line is ignored because the entire third line is being treated as a comment. You want the following instead:</p>
        <p id="p_17216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17216"> #!/usr/local/bin/perl $inputline = &lt;STDIN&gt;; print($inputline); # print my line!</p>
        <p id="p_17217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17217"> 6. This program reads two lines of input and prints them in reverse order (second line first).</p>
        <p id="p_17218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17218"> Answers for Day 2, ’’Basic Operators and Control Flow”</p>
        <p id="p_17219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17219"> Quiz</p>
        <p id="p_17220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17220"> 1. The answers are as follows:</p>
        <p id="p_17221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17221"> 1. An expression is a collection of operators and the values on which they operate.</p>
        <p id="p_17222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17222"> 2. An operand is a value associated with an operator.</p>
        <p id="p_17223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17223"> 3. A conditional statement is a statement that is executed only when its conditional expression is true.</p>
        <p id="p_17224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17224"> 4. A statement block is a collection of statements contained inside the braces of a conditional statement. The statement block is executed only when the conditional expression associated with its conditional statement is true.</p>
        <p id="p_17225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17225"> 5. An infinite loop is a conditional statement whose conditional expression is always true.</p>
        <p id="p_17226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17226"> 2. A while statement stops looping when its conditional expression is false.</p>
        <p id="p_17227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17227"> 3. An until statement stops looping when its conditional expression is true.</p>
        <p id="p_17228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17228"> 4. The == operator compares its two operands. If they are numerically equal, the == operator yields a result of true; otherwise, it yields false.</p>
        <p id="p_17229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17229"> 5. 27.</p>
        <p id="p_17230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17230"> 6. The legal ones are a, c, and f.</p>
        <p id="p_17231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17231"> Exercises</p>
        <p id="p_17232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17232"> 1. Here is one possible solution:</p>
        <p id="p_17233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17233"> #!/usr/local/bin/perl</p>
        <p id="p_17234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17234"> print ("Enter a number to be multiplied by  2:\n");</p>
        <p id="p_17235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17235"> $number = &lt;STDIN&gt;; chop ($number);</p>
        <p id="p_17236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17236"> $number = $number * 2;</p>
        <p id="p_17237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17237"> print ("The result is ", $number,  "\n");</p>
        <p id="p_17238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17238"> 2. Here is one possible solution:</p>
        <p id="p_17239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17239"> #!/usr/local/bin/perl</p>
        <p id="p_17240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17240"> print ("Enter the dividend (number to  divide):\n");</p>
        <p id="p_17241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17241"> $dividend = &lt;STDIN&gt;; chop ($dividend);</p>
        <p id="p_17242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17242"> print ("Enter the divisor (number to divide  by):\n");</p>
        <p id="p_17243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17243"> $divisor = &lt;STDIN&gt;; chop ($divisor); if ($divisor == 0) {</p>
        <p id="p_17244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17244"> print  ("Error: can't  divide by  zero!\n");</p>
        <p id="p_17245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17245"> } elsif ($dividend == 0) {</p>
        <p id="p_17246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17246"> $result = $dividend;</p>
        <p id="p_17247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17247"> } elsif ($divisor == 1) {</p>
        <p id="p_17248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17248"> $result = $dividend;</p>
        <p id="p_17249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17249"> } else {</p>
        <p id="p_17250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17250"> $result = $divisor / $dividend;</p>
        <p id="p_17251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17251"> }</p>
        <p id="p_17252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17252"> if ($divisor == 0) {</p>
        <p id="p_17253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17253"> # skip the print, since we detected an error } else {</p>
        <p id="p_17254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17254"> print ("The result is ", $result,  "\n");</p>
        <p id="p_17255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17255"> }</p>
        <p id="p_17256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17256"> 3. Here is one possible solution:</p>
        <p id="p_17257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17257"> #!/usr/local/bin/perl</p>
        <p id="p_17258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17258"> $count = 1;</p>
        <p id="p_17259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17259"> $done = 0;</p>
        <p id="p_17260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17260"> while ($done == 0) {</p>
        <p id="p_17261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17261"> print ($count,  "\n"); if  ($count == 10) {</p>
        <p id="p_17262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17262"> $done = 1;</p>
        <p id="p_17263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17263"> }</p>
        <p id="p_17264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17264"> $count = $count + 1;</p>
        <p id="p_17265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17265"> }</p>
        <p id="p_17266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17266"> 4. Here is one possible solution:</p>
        <p id="p_17267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17267"> #!/usr/local/bin/perl</p>
        <p id="p_17268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17268"> $count = 10;</p>
        <p id="p_17269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17269"> until ($count == 0) {</p>
        <p id="p_17270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17270"> print ($count,  "\n");</p>
        <p id="p_17271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17271"> $count = $count - 1;</p>
        <p id="p_17272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17272"> }</p>
        <p id="p_17273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17273"> 5. There are, in fact, three separate bugs in this program:</p>
        <p id="p_17274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17274"> 1. You must call  chop  to get rid of the trailing newline character in  $value  before comparing it to 17.</p>
        <p id="p_17275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17275"> 2. The conditional expression should read $ value == 17,not$value = 17.</p>
        <p id="p_17276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17276"> 3. There should be a closing brace } before the  else.</p>
        <p id="p_17277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17277"> 6. This program contains an infinite loop. To fix it, add the following statement just before the closing brace }:</p>
        <p id="p_17278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17278"> $input = $input + 1;</p>
        <p id="p_17279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17279"> Also, the statement</p>
        <p id="p_17280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17280"> $input = $terminate +  5;</p>
        <p id="p_17281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17281"> should read</p>
        <p id="p_17282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17282"> $terminate = $input +  5;</p>
        <p id="p_17283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17283"> Answers for Day 3, "Understanding Scalar Values"</p>
        <p id="p_17284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17284"> Quiz</p>
        <p id="p_17285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17285"> 1. The answers are as follows:</p>
        <p id="p_17286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17286"> 1. A round-off error is the difference between the floating-point number that appears in a program and the number as it is represented in the machine.</p>
        <p id="p_17287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17287"> 2. Octal notation is another way of referring to base-8 notation: Each digit can be a number from 0 to 7 and is multiplied by 8 to the exponent  n,   where  n   is the number of digits to skip.</p>
        <p id="p_17288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17288"> 3. The precision of a floating-point representation on a machine is the number of significant digits it can hold.</p>
        <p id="p_17289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17289"> 4. Scientific notation is a way of writing floating-point numbers. It consists of one digit before the decimal point, as many digits as required after the decimal point, and an exponent.</p>
        <p id="p_17290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17290"> 2. The answers are as follows:</p>
        <p id="p_17291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17291"> 1. 255 (the ASCII end-of-file character)</p>
        <p id="p_17292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17292"> 2 .  6</p>
        <p id="p_17293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17293"> 3. 601</p>
        <p id="p_17294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17294"> 3. The answers are as follows:</p>
        <p id="p_17295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17295"> 1. 255</p>
        <p id="p_17296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17296"> 2. 17</p>
        <p id="p_17297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17297"> 3. 48813</p>
        <p id="p_17298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17298"> 4. This line prints I  am bored,  and then backspaces over  bored  and replaces it with happy ! . (I don't know a lot of practical uses for the  \b  escape character, but it’s fun to watch.)</p>
        <p id="p_17299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17299"> 5. The answers are as follows:</p>
        <p id="p_17300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17300"> 1. This string contains 21.</p>
        <p id="p_17301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17301"> 2. \21 is my favorite number.</p>
        <p id="p_17302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17302"> 3.  Assign \$num to this string.</p>
        <p id="p_17303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17303"> 6. The answers are as follows:</p>
        <p id="p_17304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17304"> 1. 4.37leO 1</p>
        <p id="p_17305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17305"> 2.  6.0e-08  (the . 0 is optional)</p>
        <p id="p_17306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17306"> 3. 3. 0e+0 0  (actually, 3 by itself is acceptable)</p>
        <p id="p_17307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17307"> 4.  -1.04e  +  00</p>
        <p id="p_17308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17308"> Exercises</p>
        <p id="p_17309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17309"> 1. Here is one possible solution:</p>
        <p id="p_17310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17310"> #!/usr/local/bin/perl</p>
        <p id="p_17311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17311"> $count = 1;</p>
        <p id="p_17312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17312"> $number = 0.1;</p>
        <p id="p_17313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17313"> until ($count == 10) {</p>
        <p id="p_17314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17314"> print ("$number\n");</p>
        <p id="p_17315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17315"> $number = $number +  0.1;</p>
        <p id="p_17316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17316"> $count = $count + 1;</p>
        <p id="p_17317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17317"> }</p>
        <p id="p_17318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17318"> 2. Here is one possible solution:</p>
        <p id="p_17319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17319"> #!/usr/local/bin/perl</p>
        <p id="p_17320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17320"> $inputline = &lt;STDIN&gt;; chop ($inputline); if ($inputline == 0) {</p>
        <p id="p_17321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17321"> print ( " 0\n");</p>
        <p id="p_17322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17322"> } else { print ("1\n");</p>
        <p id="p_17323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17323"> }</p>
        <p id="p_17324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17324"> 3. Here is one possible solution:</p>
        <p id="p_17325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17325"> #!/usr/local/bin/perl</p>
        <p id="p_17326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17326"> print ("Enter a number:\n");</p>
        <p id="p_17327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17327"> $number = &lt;STDIN&gt;;</p>
        <p id="p_17328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17328"> chop ($number);</p>
        <p id="p_17329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17329"> until ($number == 47) {</p>
        <p id="p_17330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17330"> print ("Wrong! Try again!\n");</p>
        <p id="p_17331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17331"> $number = &lt;STDIN&gt;; chop ($number);</p>
        <p id="p_17332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17332"> }</p>
        <p id="p_17333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17333"> print ( "\aCorrect!\n");</p>
        <p id="p_17334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17334"> 4.  The first string in the  print  statement is not terminated properly, because there is a backslash \ before the final '. To fix this, add another quote:</p>
        <p id="p_17335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17335"> print ('here is the value of \$inputline\'', ": $inputline");</p>
        <p id="p_17336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17336"> 5. This code fragment does not produce the expected result because of a round-off error. Try subtracting  $num3  from  $numl  before adding  $num2  and  $num4.</p>
        <p id="p_17337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17337"> 6. "  Oxce  " converts to 0, not to the hexadecimal constant  Oxce.  To fix this, leave off the quotes.</p>
        <p id="p_17338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17338"> Answers for Day 4, ’’More Operators"</p>
        <p id="p_17339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17339"> Quiz</p>
        <p id="p_17340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17340"> 1. The answers are as follows:</p>
        <p id="p_17341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17341"> 1. An operator is a character or string of characters that represents a particular Perl operation.</p>
        <p id="p_17342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17342"> 2. An operand is a value used by an operator. In Perl, operators require one, two, or three operands.</p>
        <p id="p_17343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17343"> 3. An expression is a collection of operators and operands, yielding a final result.</p>
        <p id="p_17344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17344"> 4. Operator precedence is the order in which different types of operations are performed.</p>
        <p id="p_17345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17345"> 5. Operator associativity is the order in which operations of the same precedence are performed.</p>
        <p id="p_17346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17346"> 2. The answers are as follows:</p>
        <p id="p_17347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17347"> 1. logical AND</p>
        <p id="p_17348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17348"> 2. bitwise AND</p>
        <p id="p_17349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17349"> 3. bitwise XOR</p>
        <p id="p_17350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17350"> 4. string inequality</p>
        <p id="p_17351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17351"> 5. string concatenation</p>
        <p id="p_17352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17352"> 3. The answers are as follows:</p>
        <p id="p_17353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17353"> 1. eq</p>
        <p id="p_17354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17354"> 2 . %</p>
        <p id="p_17355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17355"> 3. x</p>
        <p id="p_17356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17356"> 4. I</p>
        <p id="p_17357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17357"> 5. &gt;=</p>
        <p id="p_17358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17358"> 4. The answers are as follows:</p>
        <p id="p_17359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17359"> 1 .  0000000010101011</p>
        <p id="p_17360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17360"> 2 .  0000010001010001</p>
        <p id="p_17361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17361"> 3. 0 (or 00000000)</p>
        <p id="p_17362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17362"> 5. The answers are as follows:</p>
        <p id="p_17363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17363"> 1 .  100</p>
        <p id="p_17364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17364"> 2. 15</p>
        <p id="p_17365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17365"> 3. 65</p>
        <p id="p_17366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17366"> 6. The answers are as follows:</p>
        <p id="p_17367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17367"> 1. 4</p>
        <p id="p_17368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17368"> 2. 0 (I hope you didn't calculate all of the expression! Once you see the first 0, you should know that the result is 0.)</p>
        <p id="p_17369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17369"> 3. 1819</p>
        <p id="p_17370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17370"> 4.  "abcdede"</p>
        <p id="p_17371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17371"> Exercises</p>
        <p id="p_17372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17372"> 1. The following is just one of many possible answers:</p>
        <p id="p_17373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17373"> #!/usr/local/bin/perl</p>
        <p id="p_17374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17374"> $value = 1;</p>
        <p id="p_17375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17375"> $counter = 0;</p>
        <p id="p_17376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17376"> while ($counter &lt; 16) {</p>
        <p id="p_17377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17377"> print ("2 to the power $counter is $value\n");</p>
        <p id="p_17378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17378"> $value = $value &lt;&lt; 1;</p>
        <p id="p_17379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17379"> $counter++;</p>
        <p id="p_17380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17380"> }</p>
        <p id="p_17381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17381"> 2. The answer is as follows:</p>
        <p id="p_17382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17382"> $result = $varl == 5 || $var2 == 7 ?</p>
        <p id="p_17383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17383"> $varl * $var2 + 16.5 :</p>
        <p id="p_17384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17384"> (print("condition is false\n"), 0);</p>
        <p id="p_17385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17385"> 3. The answer is as follows:</p>
        <p id="p_17386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17386"> if ($varl &lt;= 26) {</p>
        <p id="p_17387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17387"> $result = ++$var2;</p>
        <p id="p_17388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17388"> } else {</p>
        <p id="p_17389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17389"> $result = 0;</p>
        <p id="p_17390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17390"> }</p>
        <p id="p_17391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17391"> 4. The following is just one of many possible answers:</p>
        <p id="p_17392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17392"> #!/usr/local/bin/perl</p>
        <p id="p_17393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17393"> print("Enter the integer to be divided:\n");</p>
        <p id="p_17394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17394"> $dividend = &lt;STDIN&gt;;</p>
        <p id="p_17395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17395"> print("Enter the integer to divide by:\n");</p>
        <p id="p_17396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17396"> $divisor = &lt;STDIN&gt;;</p>
        <p id="p_17397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17397"> # check for division by zero</p>
        <p id="p_17398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17398"> if ($divisor == 0) {</p>
        <p id="p_17399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17399"> print("error: can't divide by zero\n");</p>
        <p id="p_17400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17400"> } else {</p>
        <p id="p_17401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17401"> $quotient = $dividend / $divisor;</p>
        <p id="p_17402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17402"> $remainder = $dividend % $divisor; print("The result is $quotient\n"); print("The remainder is $remainder\n");</p>
        <p id="p_17403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17403"> }</p>
        <p id="p_17404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17404"> 5. Adding 10 0 0  05.2  and then subtracting it causes round-off errors, which means that the final value isn't exactly the same as  5 .1.</p>
        <p id="p_17405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17405"> 6. ($result = ( ( ($varl * 2) &lt;&lt; (5 + 3) ) | | ($var2 ** 3) ) ) , $var3;</p>
        <p id="p_17406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17406"> 7. 81</p>
        <p id="p_17407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17407"> 8. Here is the corrected program, with the fixed errors listed:</p>
        <p id="p_17408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17408"> #!/usr/local/bin/perl</p>
        <p id="p_17409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17409"> $num = &lt;STDIN&gt;; chop ($num);</p>
        <p id="p_17410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17410"> $ x = "";</p>
        <p id="p_17411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17411"> $x  .= "hello"; # += is for integers if  ($x  ne "goodbye" ||  $x  eq "farewell") {</p>
        <p id="p_17412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17412"> # the previous line had two problems:</p>
        <p id="p_17413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17413"> # the operators were numeric, not string;</p>
        <p id="p_17414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17414"> # the or operator was bitwise, not logical.</p>
        <p id="p_17415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17415"> $result = $num == 0 ? 43 : 0;</p>
        <p id="p_17416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17416"> # the : and third operand were missing in the previous</p>
        <p id="p_17417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17417"> # line; eq replaced by ==</p>
        <p id="p_17418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17418"> } else {</p>
        <p id="p_17419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17419"> $result = ++$num; # can't have ++ on both sides</p>
        <p id="p_17420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17420"> }</p>
        <p id="p_17421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17421"> print("the result is $result\n");</p>
        <p id="p_17422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17422"> Answers for Day 5, "Lists and Array Variables”</p>
        <p id="p_17423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17423"> Quiz</p>
        <p id="p_17424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17424"> 1. The answers are as follows:</p>
        <p id="p_17425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17425"> 1. A list is an ordered collection of scalar values.</p>
        <p id="p_17426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17426"> 2. An empty list is a list with zero elements in it.</p>
        <p id="p_17427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17427"> 3.  An array variable is a variable that can store a list.</p>
        <p id="p_17428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17428"> 4. A subscript is a scalar value that refers to an element of a list. The subscript 0 refers to the first element, the subscript 1 refers to the second, and so on.</p>
        <p id="p_17429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17429"> 5. An array slice is a list consisting of some elements of an array variable. (Notice that the elements do not have to be in order.)</p>
        <p id="p_17430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17430"> 2. The answers are as follows:</p>
        <p id="p_17431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17431"> 1. (1, 2, 3)</p>
        <p id="p_17432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17432"> 2. (3, 2)</p>
        <p id="p_17433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17433"> 3. ("hello", 2, 2)</p>
        <p id="p_17434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17434"> 4. (2, 3)</p>
        <p id="p_17435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17435"> 5.  ("",   3,   2,   2)</p>
        <p id="p_17436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17436"> 6. The contents of the standard input file, one line per list element.</p>
        <p id="p_17437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17437"> 3.  The answers are as follows:</p>
        <p id="p_17438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17438"> 1. 2</p>
        <p id="p_17439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17439"> 2. 4</p>
        <p id="p_17440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17440"> 3. "one"</p>
        <p id="p_17441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17441"> 4. 2</p>
        <p id="p_17442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17442"> 5.  "three"</p>
        <p id="p_17443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17443"> 6.  "  " (Only three elements in the list are stored in  @ 1 i s 12.)</p>
        <p id="p_17444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17444"> 4. A list is a collection of scalar values. An array variable is a place where you can store a list.</p>
        <p id="p_17445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17445"> 5. The brackets [ ] enclosing the subscript distinguish an array element from a scalar variable.</p>
        <p id="p_17446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17446"> 6. You can do this in many ways. The two easiest are</p>
        <p id="p_17447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17447"> o  Use single-quoted strings, which do not allow substitutions, o  Put a backslash \ before the character that you want left as is.</p>
        <p id="p_17448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17448"> 7. You can obtain the length of a list stored in an array variable by assigning the array variable to a scalar variable.</p>
        <p id="p_17449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17449"> 8. All undefined array elements are assumed to contain the null string " ".</p>
        <p id="p_17450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17450"> 9. When you assign to an array element that is larger than the current length of the array, the array grows to include the new element.</p>
        <p id="p_17451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17451"> Exercises</p>
        <p id="p_17452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17452"> 1. Here is one possible solution:</p>
        <p id="p_17453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17453"> #!/usr/local/bin/perl</p>
        <p id="p_17454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17454"> $thecount = 0;</p>
        <p id="p_17455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17455"> $line = &lt;STDIN&gt;; while ($line ne "") {</p>
        <p id="p_17456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17456"> chop ($line) ;</p>
        <p id="p_17457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17457"> @words = split (/ /, $line);</p>
        <p id="p_17458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17458"> $wordindex = 1;</p>
        <p id="p_17459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17459"> while ($wordindex &lt;= ©words) {</p>
        <p id="p_17460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17460"> if ($words[$wordindex-l] eq "the") {</p>
        <p id="p_17461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17461"> $thecount += 1;</p>
        <p id="p_17462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17462"> }</p>
        <p id="p_17463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17463"> $wordindex++;</p>
        <p id="p_17464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17464"> }</p>
        <p id="p_17465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17465"> $line = &lt;STDIN&gt;;</p>
        <p id="p_17466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17466"> }</p>
        <p id="p_17467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17467"> print ("Total occurrences of \"the\": $thecount\n");</p>
        <p id="p_17468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17468"> 2. Here is one possible solution:</p>
        <p id="p_17469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17469"> #!/usr/local/bin/perl</p>
        <p id="p_17470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17470"> $grandtotal = 0;</p>
        <p id="p_17471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17471"> $line = &lt;STDIN&gt;; while ($line ne "") {</p>
        <p id="p_17472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17472"> $linetotal = 0;</p>
        <p id="p_17473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17473"> @numbers = split(/ /, $line);</p>
        <p id="p_17474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17474"> $numbercount = 1;</p>
        <p id="p_17475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17475"> while ($numbercount &lt;= ©numbers) {</p>
        <p id="p_17476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17476"> $linetotal += $numbers[$numbercount-l] ;</p>
        <p id="p_17477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17477"> $numbercount++;</p>
        <p id="p_17478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17478"> }</p>
        <p id="p_17479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17479"> print("line total: $linetotal\n");</p>
        <p id="p_17480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17480"> $grandtotal += $linetotal;</p>
        <p id="p_17481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17481"> $line = &lt;STDIN&gt;;</p>
        <p id="p_17482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17482"> }</p>
        <p id="p_17483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17483"> print("grand total: $grandtotal\n") ;</p>
        <p id="p_17484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17484"> 3. Here is one possible solution:</p>
        <p id="p_17485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17485"> #!/usr/local/bin/perl</p>
        <p id="p_17486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17486"> ©lines = &lt;STDIN&gt;; chop (©lines);</p>
        <p id="p_17487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17487"> $longlongline = join(" ", ©lines);</p>
        <p id="p_17488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17488"> ©words = split (/ /, $longlongline);</p>
        <p id="p_17489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17489"> ©words = reverse sort (©words);</p>
        <p id="p_17490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17490"> $index = 0;</p>
        <p id="p_17491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17491"> print("Words sorted in reverse order:\n"); while ($index &lt; ©words) {</p>
        <p id="p_17492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17492"> # note that the first time through, the following</p>
        <p id="p_17493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17493"> # comparison references $words[-l]. This is all</p>
        <p id="p_17494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17494"> # right, as $words[-l] is replaced by the null</p>
        <p id="p_17495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17495"> # string, and we want the first word to be printed</p>
        <p id="p_17496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17496"> if ($words[$index] ne $words[$index-l]) {</p>
        <p id="p_17497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17497"> print ("$words[$index]\n") ;</p>
        <p id="p_17498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17498"> }</p>
        <p id="p_17499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17499"> $index++;</p>
        <p id="p_17500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17500"> }</p>
        <p id="p_17501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17501"> 4. The array element reference should be $ array [ 4 ], not ©array [ 4 ].</p>
        <p id="p_17502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17502"> 5. There are four separate bugs in this program:</p>
        <p id="p_17503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17503"> 1. You must call chop to remove the newline characters from the input lines stored in ©input. Otherwise, they make your output unreadable.</p>
        <p id="p_17504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17504"> 2.  Similarly, you have to append a newline when calling  join:</p>
        <p id="p_17505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17505"> $input [ $currline] = join(" ", @words,  "\n");</p>
        <p id="p_17506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17506"> 3. The conditional expression should read $currline &lt;= @input</p>
        <p id="p_17507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17507"> instead of</p>
        <p id="p_17508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17508"> $currline &lt; @input</p>
        <p id="p_17509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17509"> to make sure that the last line of the input file is read.</p>
        <p id="p_17510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17510"> 4. Your subscripts should read [  $currline-l  ], not [$ currline  ]. (This bug will keep coming up in your programs because it's easy to forget that subscripts start with zero.)</p>
        <p id="p_17511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17511"> Answers for Day 6, "Reading from and Writing to Files"</p>
        <p id="p_17512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17512"> Quiz</p>
        <p id="p_17513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17513"> 1. The answers are as follows:</p>
        <p id="p_17514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17514"> 1. A file variable is a name that represents an open file.</p>
        <p id="p_17515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17515"> 2. A reserved word is a word that can't be used as a variable name because it has a special meaning in Perl (such as if).</p>
        <p id="p_17516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17516"> 3. The file mode specifies how you want to access a file when you open it (read, write, or append).</p>
        <p id="p_17517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17517"> 4. Append mode indicates that you want to open the file for writing and append anything you write to the existing contents of the file.</p>
        <p id="p_17518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17518"> 5. A pipe is a connection between output from one program and input to another.</p>
        <p id="p_17519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17519"> 2. The &lt;&gt; operator reads its data from the files specified on the command line.</p>
        <p id="p_17520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17520"> 3. The answers are as follows:</p>
        <p id="p_17521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17521"> 1. -r tests whether you have permission to read a file.</p>
        <p id="p_17522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17522"> 2.  -x  tests whether you have permission to execute a file (and whether the file is executable).</p>
        <p id="p_17523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17523"> 3. -s indicates the size of a file in bytes.</p>
        <p id="p_17524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17524"> 4. 0ARGV contains the following list:</p>
        <p id="p_17525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17525"> ("filel", "file2 " , "file3")</p>
        <p id="p_17526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17526"> 5. The answers are as follows:</p>
        <p id="p_17527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17527"> 1. To open a file in write mode, put a &gt; character in front of the filename.</p>
        <p id="p_17528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17528"> 2. To open a file in append mode, put two &gt; characters (&gt;&gt;) in front of the filename.</p>
        <p id="p_17529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17529"> 3. To open a file in read mode, just specify the filename. By default, files are opened in read mode.</p>
        <p id="p_17530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17530"> 4. To open a pipe, put a | character in front of the command to be piped to.</p>
        <p id="p_17531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17531"> 6. The &lt;&gt; operator reads data from the files whose names are stored in the array variable 0ARGV. When the &lt;&gt; operator runs out of data in one file, it opens the file named in $ARGV [  0  ] and then calls shift to move the elements of @ARGV over.</p>
        <p id="p_17532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17532"> Exercises</p>
        <p id="p_17533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17533"> 1. Here is one possible solution:</p>
        <p id="p_17534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17534"> #!/usr/local/bin/perl</p>
        <p id="p_17535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17535"> $total = 0;</p>
        <p id="p_17536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17536"> $count = 1;</p>
        <p id="p_17537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17537"> while ($count &lt;= @ARGV) {</p>
        <p id="p_17538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17538"> $total += $ARGV[$count-l] ;</p>
        <p id="p_17539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17539"> $count++;</p>
        <p id="p_17540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17540"> }</p>
        <p id="p_17541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17541"> print ("The total is $total.\n");</p>
        <p id="p_17542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17542"> 2. Here is one possible solution:</p>
        <p id="p_17543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17543"> #!/usr/local/bin/perl</p>
        <p id="p_17544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17544"> $count = 1;</p>
        <p id="p_17545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17545"> while ($count &lt;= ©ARGV) {</p>
        <p id="p_17546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17546"> if (-e $ARGV[$count-l] &amp;&amp; -s $ARGV[$count-l] &gt; 10000) {</p>
        <p id="p_17547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17547"> print ("File $ARGV[$count-l] is a big file!\n");</p>
        <p id="p_17548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17548"> }</p>
        <p id="p_17549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17549"> $count++;</p>
        <p id="p_17550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17550"> }</p>
        <p id="p_17551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17551"> 3. Here is one possible solution:</p>
        <p id="p_17552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17552"> #!/usr/local/bin/perl</p>
        <p id="p_17553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17553"> open (INFILE, "filel") ||</p>
        <p id="p_17554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17554"> die ("Can't open filel for reading\n");</p>
        <p id="p_17555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17555"> open (OUTFILE, "&gt;file2") | |</p>
        <p id="p_17556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17556"> die ("Can't open file2 for writing\n");</p>
        <p id="p_17557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17557"> # the following only works if filel isn't too big ©contents = &lt;INFILE&gt;;</p>
        <p id="p_17558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17558"> print OUTFILE (©contents);</p>
        <p id="p_17559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17559"> # we don't really need the call to close, but they</p>
        <p id="p_17560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17560"> # make things a little clearer close (OUTFILE);</p>
        <p id="p_17561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17561"> open (OUTFILE, "»file2") | |</p>
        <p id="p_17562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17562"> die ("Can't append to file2\n"); print OUTFILE (©contents);</p>
        <p id="p_17563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17563"> 4. Here is one possible solution:</p>
        <p id="p_17564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17564"> #!/usr/local/bin/perl</p>
        <p id="p_17565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17565"> $wordcount = 0; while ($line = &lt;&gt;) {</p>
        <p id="p_17566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17566"> # this isn't the best possible pattern to split with,</p>
        <p id="p_17567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17567"> # but it'll do until you've finished Day 7 ©words = split(/ /, $line);</p>
        <p id="p_17568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17568"> $wordcount += ©words;</p>
        <p id="p_17569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17569"> }</p>
        <p id="p_17570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17570"> open (MESSAGE, "| mail dave") || die ("Can't mail to userid dave.\n");</p>
        <p id="p_17571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17571"> print MESSAGE ("Total number of words: $wordcount\n"); close (MESSAGE);</p>
        <p id="p_17572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17572"> 5. Here is one possible solution:</p>
        <p id="p_17573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17573"> #!/usr/local/bin/perl</p>
        <p id="p_17574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17574"> $count = 1;</p>
        <p id="p_17575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17575"> while ($count &lt;= @ARGV) { print ("File $ARGV[$count-l]:"); if ( !(-e  $ARGV[$count-l])) {</p>
        <p id="p_17576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17576"> print (" does not exist\n");</p>
        <p id="p_17577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17577"> } else {</p>
        <p id="p_17578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17578"> if (-r $ARGV[$count-l]) {</p>
        <p id="p_17579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17579"> print (" read");</p>
        <p id="p_17580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17580"> }</p>
        <p id="p_17581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17581"> if (—w $ARGV[$count-l]) {</p>
        <p id="p_17582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17582"> print (" write");</p>
        <p id="p_17583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17583"> }</p>
        <p id="p_17584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17584"> if (-x $ARGV[$count-l] ) {</p>
        <p id="p_17585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17585"> print (" execute");</p>
        <p id="p_17586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17586"> }</p>
        <p id="p_17587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17587"> print  ("\n");</p>
        <p id="p_17588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17588"> }</p>
        <p id="p_17589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17589"> $count++;</p>
        <p id="p_17590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17590"> }</p>
        <p id="p_17591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17591"> 6. This program is opening out file in read mode, not write mode. To open in write mode, change the call to open to</p>
        <p id="p_17592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17592"> open (OUTFILE, "&gt;outfile");</p>
        <p id="p_17593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17593"> Answers for Day 7, "Pattern Matching"</p>
        <p id="p_17594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17594"> Quiz</p>
        <p id="p_17595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17595"> 1. The answers are as follows:</p>
        <p id="p_17596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17596"> 1. Either the letter  a  or  b,  followed by zero or more occurrences of  c.</p>
        <p id="p_17597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17597"> 2. One, two, or three digits.</p>
        <p id="p_17598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17598"> 3. The words  cat, cot,  and  cut.  (This pattern does not match these letters if they are in the middle of a word.)</p>
        <p id="p_17599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17599"> 4. The first part of this pattern matches a subpattern consisting of x, one or more of y, and z . The rest of the pattern then matches a period, followed by the subpattern first matched.</p>
        <p id="p_17600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17600"> 5. This matches an empty line (the null string).</p>
        <p id="p_17601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17601"> 2. The answers are as follows:</p>
        <p id="p_17602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17602"> 1. /  [a-z]   {5,   }/</p>
        <p id="p_17603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17603"> 2. /1 | one/</p>
        <p id="p_17604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17604"> 3.  /\d+\ . ?\d+/</p>
        <p id="p_17605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17605"> 4.  /([A-Za-z])[aeiou]\1/</p>
        <p id="p_17606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17606"> 5. /\++/</p>
        <p id="p_17607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17607"> 3. Items a, b, c, and f are true; d and e are false.</p>
        <p id="p_17608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17608"> 4. The answers are as follows:</p>
        <p id="p_17609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17609"> 1.  "def 12 3abc"</p>
        <p id="p_17610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17610"> 2. "X123X"</p>
        <p id="p_17611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17611"> 3.  "aWcl23abc"</p>
        <p id="p_17612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17612"> 4.  "abd"</p>
        <p id="p_17613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17613"> 5.  "abc2 4 6abc"</p>
        <p id="p_17614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17614"> 5. The answers are as follows:</p>
        <p id="p_17615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17615"> 1. "ABC123ABC"</p>
        <p id="p_17616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17616"> 2. "abc4 5 6abc"</p>
        <p id="p_17617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17617"> 3. "abc456abc"</p>
        <p id="p_17618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17618"> 4.  "abc abc"</p>
        <p id="p_17619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17619"> 5.  "123"</p>
        <p id="p_17620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17620"> Exercises</p>
        <p id="p_17621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17621"> 1. Here is one possible solution:</p>
        <p id="p_17622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17622"> #!/usr/local/bin/perl</p>
        <p id="p_17623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17623"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17624"> $line =~ tr/aeiou/AEIOU/; print ($line);</p>
        <p id="p_17625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17625"> }</p>
        <p id="p_17626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17626"> 2.  Here is one possible solution:</p>
        <p id="p_17627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17627"> #!/usr/local/bin/perl</p>
        <p id="p_17628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17628"> while ($inputline = &lt;STDIN&gt;) {</p>
        <p id="p_17629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17629"> $inputline =~  tr/0-9/   /c;</p>
        <p id="p_17630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17630"> $inputline =~ s/  +//g;</p>
        <p id="p_17631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17631"> @digits = split  (//,  $inputline);</p>
        <p id="p_17632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17632"> $total += @digits;</p>
        <p id="p_17633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17633"> $count = 1;</p>
        <p id="p_17634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17634"> while ($count &lt;= ©digits) {</p>
        <p id="p_17635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17635"> $dtotal[$digits[$count-l]] += 1;</p>
        <p id="p_17636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17636"> $count++;</p>
        <p id="p_17637" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17637"> }</p>
        <p id="p_17638" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17638"> }</p>
        <p id="p_17639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17639"> print  ("Total  number of digits found:  $total\n") print ("Breakdown:\n") ;</p>
        <p id="p_17640" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17640"> $count = 0;</p>
        <p id="p_17641" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17641"> while ($count &lt;= 9) {</p>
        <p id="p_17642" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17642"> if ($dtotal[$count] &gt; 0) {</p>
        <p id="p_17643" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17643"> print ("\tdigit $count:  $dtotal[$count]\n");</p>
        <p id="p_17644" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17644"> }</p>
        <p id="p_17645" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17645"> $count++;</p>
        <p id="p_17646" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17646"> }</p>
        <p id="p_17647" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17647"> 3. Here is one possible solution:</p>
        <p id="p_17648" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17648"> #!/usr/local/bin/perl</p>
        <p id="p_17649" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17649"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17650" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17650"> $line =~  s/(\w+) (\s + ) (\w+) (\s + ) (\w+)/$5$2$3$4$1/; print ($line) ;</p>
        <p id="p_17651" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17651"> }</p>
        <p id="p_17652" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17652"> 4. Here is one possible solution:</p>
        <p id="p_17653" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17653"> #!/usr/local/bin/perl</p>
        <p id="p_17654" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17654"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17655" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17655"> $line =~  s/\d+/$&amp;+l/eg; print ($line) ;</p>
        <p id="p_17656" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17656"> }</p>
        <p id="p_17657" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17657"> 5. There are two problems. The first is that the pattern matches the entire line, including the closing newline. You do not want to put a quotation mark after the closing newline of each line. In this case, it causes the program to omit the  s  operator, which specifies substitution. The second problem is that the replacement string should contain $ 1, not \ 1. \ 1 is defined only inside the search pattern.</p>
        <p id="p_17658" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17658"> 6. The pattern uses the * special character, which matches zero or more occurrences of any digit. This means the pattern always matches.</p>
        <p id="p_17659" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17659"> The pattern should use the + special character, which matches one or more occurrences of any digit.</p>
        <p id="p_17660" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17660"> Answers for Day 8, ’’More Control Structures”</p>
        <p id="p_17661" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17661"> Quiz</p>
        <p id="p_17662" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17662"> 1. 7</p>
        <p id="p_17663" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17663"> 2 .  11</p>
        <p id="p_17664" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17664"> 3. 5</p>
        <p id="p_17665" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17665"> 4. 6</p>
        <p id="p_17666" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17666"> 5. last if ($x eq "done");</p>
        <p id="p_17667" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17667"> 6. redo if ($list[0] ==  26);</p>
        <p id="p_17668" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17668"> 7.  next  LABEL  if ($scalar eq  "#");</p>
        <p id="p_17669" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17669"> 8. print ("$count\n") while ($count++ &lt; 10);</p>
        <p id="p_17670" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17670"> 9.  The  continue  statement defines a block of code to be executed each time a  while  or until  statement loops.</p>
        <p id="p_17671" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17671"> Exercises</p>
        <p id="p_17672" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17672"> 1. Here is one possible solution:</p>
        <p id="p_17673" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17673"> #!/usr/local/bin/perl</p>
        <p id="p_17674" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17674"> $count = 1; do {</p>
        <p id="p_17675" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17675"> print ("$count\n");</p>
        <p id="p_17676" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17676"> $count++;</p>
        <p id="p_17677" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17677"> } while ($count &lt;= 10);</p>
        <p id="p_17678" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17678"> 2. Here is one possible solution:</p>
        <p id="p_17679" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17679"> #!/usr/local/bin/perl</p>
        <p id="p_17680" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17680"> for ($count = 1; $count &lt;= 10; $count++) { print ("$count\n");</p>
        <p id="p_17681" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17681"> }</p>
        <p id="p_17682" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17682"> 3. Here is one possible solution:</p>
        <p id="p_17683" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17683"> #!/usr/local/bin/perl</p>
        <p id="p_17684" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17684"> for ($count = 1; $count &lt;= 5; $count++) { $line = &lt;STDIN&gt;; last if ($line eq  ""); print ($line) ;</p>
        <p id="p_17685" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17685"> 4. Here is one possible solution:</p>
        <p id="p_17686" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17686"> #!/usr/local/bin/perl</p>
        <p id="p_17687" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17687"> for ($count = 1; $count &lt;= 20; $count++) { next if ($count % 2 == 1); print ("$count\n");</p>
        <p id="p_17688" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17688"> }</p>
        <p id="p_17689" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17689"> 5. Here is one possible solution:</p>
        <p id="p_17690" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17690"> #!/usr/local/bin/perl</p>
        <p id="p_17691" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17691"> $linenum = 0;</p>
        <p id="p_17692" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17692"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17693" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17693"> $linenum += 1;</p>
        <p id="p_17694" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17694"> $occurs = 0;</p>
        <p id="p_17695" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17695"> $line =~ tr/A-Z/a-z/;</p>
        <p id="p_17696" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17696"> @words = split(/\s+/, $line); foreach $word (@words) {</p>
        <p id="p_17697" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17697"> $occurs += 1 if ($word eq "the");</p>
        <p id="p_17698" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17698"> }</p>
        <p id="p_17699" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17699"> if ($occurs &gt; 0) {</p>
        <p id="p_17700" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17700"> print ("line $linenum: $occurs occurrences\n")</p>
        <p id="p_17701" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17701"> }</p>
        <p id="p_17702" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17702"> }</p>
        <p id="p_17703" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17703"> 6. Here is one possible solution:</p>
        <p id="p_17704" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17704"> #!/usr/local/bin/perl</p>
        <p id="p_17705" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17705"> $count = 10;</p>
        <p id="p_17706" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17706"> while ($count &gt;= 1) {</p>
        <p id="p_17707" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17707"> print ("$count\n");</p>
        <p id="p_17708" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17708"> }</p>
        <p id="p_17709" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17709"> continue {</p>
        <p id="p_17710" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17710"> $count-;</p>
        <p id="p_17711" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17711"> }</p>
        <p id="p_17712" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17712"> 7. You can't use the  last  statement inside a  do  statement. To get around this problem, use another loop construct such as  while  or  for,  or put the conditional expression in the  while statement at the bottom.</p>
        <p id="p_17713" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17713"> Answers for Day 9, "Using Subroutines"</p>
        <p id="p_17714" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17714"> Quiz</p>
        <p id="p_17715" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17715"> 1. The answers are as follows:</p>
        <p id="p_17716" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17716"> 1. A subroutine is a separate body of code designed to perform a particular task.</p>
        <p id="p_17717" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17717"> 2.  An invocation is a statement that tells the Perl interpreter to execute a particular subroutine.</p>
        <p id="p_17718" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17718"> 3. An argument is a value that is passed to a subroutine when it is invoked.</p>
        <p id="p_17719" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17719"> 4. A single-exit module is a subroutine whose return value is calculated by the final statement in the subroutine.</p>
        <p id="p_17720" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17720"> 5. Aliasing occurs when one name is defined to be equivalent to another.</p>
        <p id="p_17721" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17721"> 2.  The answers are as follows:</p>
        <p id="p_17722" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17722"> 1 .  0</p>
        <p id="p_17723" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17723"> 2. (4, 5, 6)</p>
        <p id="p_17724" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17724"> 3. (4, 5, 6)</p>
        <p id="p_17725" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17725"> 3.   False (or zero), because the conditional expression $  count  &lt;= 1 0  is the last expression evaluated in the subroutine.</p>
        <p id="p_17726" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17726"> 4.  (1, 5, 3)</p>
        <p id="p_17727" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17727"> Exercises</p>
        <p id="p_17728" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17728"> 1. Here is one possible solution:</p>
        <p id="p_17729" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17729"> sub add_two {</p>
        <p id="p_17730" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17730"> local ($argl, $arg2) =</p>
        <p id="p_17731" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17731"> $result = $argl + $arg2;</p>
        <p id="p_17732" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17732"> }</p>
        <p id="p_17733" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17733"> 2. Here is one possible solution:</p>
        <p id="p_17734" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17734"> sub count_t {</p>
        <p id="p_17735" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17735"> local ($string) = @_;</p>
        <p id="p_17736" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17736"> # There are a couple of tricks you can use to do this.</p>
        <p id="p_17737" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17737"> # This one splits the string into words using "t" as</p>
        <p id="p_17738" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17738"> # the split pattern. The number of occurrences of "t"</p>
        <p id="p_17739" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17739"> # is one less than the number of words resulting from</p>
        <p id="p_17740" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17740"> # the split.</p>
        <p id="p_17741" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17741"> @dummy = split(/t/, $string);</p>
        <p id="p_17742" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17742"> $retval = @dummy - 1;</p>
        <p id="p_17743" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17743"> }</p>
        <p id="p_17744" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17744"> 3. Here is one possible solution:</p>
        <p id="p_17745" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17745"> sub diff {</p>
        <p id="p_17746" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17746"> local ($filel, $file2) =</p>
        <p id="p_17747" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17747"> # return false if we can't open a file</p>
        <p id="p_17748" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17748"> }</p>
        <p id="p_17749" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17749"> return (0) unless open (FILE1, "$filel"); return (0) unless open (FILE2, "$file2"); while (1) {</p>
        <p id="p_17750" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17750"> $linel = &lt;FILE1&gt;;</p>
        <p id="p_17751" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17751"> $line2 = &lt;FILE2&gt;; if ($linel eq "") {</p>
        <p id="p_17752" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17752"> $retval = ($line2 eq last;</p>
        <p id="p_17753" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17753"> }</p>
        <p id="p_17754" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17754"> if ($line2 eq "" || $linel ne $line2) {</p>
        <p id="p_17755" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17755"> $retval = 0; last;</p>
        <p id="p_17756" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17756"> }</p>
        <p id="p_17757" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17757"> }</p>
        <p id="p_17758" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17758"> # you should use close here, as this subroutine may</p>
        <p id="p_17759" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17759"> # be called many times close (FILE1) ;</p>
        <p id="p_17760" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17760"> close (FILE2);</p>
        <p id="p_17761" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17761"> # ensure that the return value is the last evaluated</p>
        <p id="p_17762" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17762"> # expression $retval;</p>
        <p id="p_17763" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17763"> 4. Here is one possible solution:</p>
        <p id="p_17764" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17764"> sub dieroll {</p>
        <p id="p_17765" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17765"> $retval = int (rand(6)) + 1;</p>
        <p id="p_17766" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17766"> }</p>
        <p id="p_17767" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17767"> 5. Here is one possible solution:</p>
        <p id="p_17768" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17768"> # assume that the first call to printlist passes the argument</p>
        <p id="p_17769" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17769"> # 0 as the value for $index sub printlist {</p>
        <p id="p_17770" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17770"> local ($index, @list) = @_;</p>
        <p id="p_17771" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17771"> if ($index + 1 &lt; @list) {</p>
        <p id="p_17772" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17772"> &amp;printlist ($index+l, @list);</p>
        <p id="p_17773" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17773"> }</p>
        <p id="p_17774" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17774"> # the conditional handles the case of an empty list print ( "$list[$index]\n") if (@list &gt; 0);</p>
        <p id="p_17775" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17775"> }</p>
        <p id="p_17776" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17776"> 6. The subroutine  print_ten  overwrites the value stored in the global variable $  count.  To fix this problem, define $  count  as a local variable. (You also should define  $printval  as a local variable, in case someone adds this variable to the main program at a later time.)</p>
        <p id="p_17777" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17777"> 7.  The  local  statement in the subroutine assigns both the list and the search word to @searchlist,  which means that  $searchword  is assigned the empty string. To fix this problem, switch the order of the arguments, putting the search word first.</p>
        <p id="p_17778" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17778"> 8.  If  split  produces a nonempty list, the last expression evaluated in the subroutine is the conditional expression, which has the value 0 (false):</p>
        <p id="p_17779" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17779"> @words == 0</p>
        <p id="p_17780" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17780"> Therefore, the return value of this subroutine is  0,  not the list of words.</p>
        <p id="p_17781" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17781"> To get around this problem, put the following statement after the if statement:</p>
        <p id="p_17782" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17782"> @words;</p>
        <p id="p_17783" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17783"> This ensures that the list of words is always the return value.</p>
        <p id="p_17784" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17784"> Answers for Day 10, "Associative Arrays”</p>
        <p id="p_17785" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17785"> Quiz</p>
        <p id="p_17786" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17786"> 1. The answers are as follows:</p>
        <p id="p_17787" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17787"> 1. An associative array is an array whose subscripts can be any scalar value.</p>
        <p id="p_17788" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17788"> 2. A pointer is an associative array element whose value is the subscript of another associative array element.</p>
        <p id="p_17789" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17789"> 3. A linked list is an associative array in which each element of the array points to the next.</p>
        <p id="p_17790" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17790"> 4. A binary tree is a data structure in which each element points to (at most) two other elements.</p>
        <p id="p_17791" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17791"> 5. A node is an element of a binary tree.</p>
        <p id="p_17792" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17792"> 6. A child is an element of a binary tree that is pointed to by another element.</p>
        <p id="p_17793" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17793"> 2. This statement creates an associative array containing three elements:</p>
        <p id="p_17794" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17794"> o  An element with subscript  17.2  whose value is  hello o  An element with subscript  there  whose value is  4 6 o  An element with subscript  e+6  whose value is  8 8</p>
        <p id="p_17795" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17795"> 3. When you assign an associative array to an ordinary array variable, the value of the array variable becomes a list consisting of all of the subscript/value pairs of the associative array (in the order in which they were stored in the associative array, which is random).</p>
        <p id="p_17796" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17796"> 4. Define a scalar variable containing the value of the list's first element. Then, use the value of one associative array element as the subscript for the next.</p>
        <p id="p_17797" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17797"> 5.  This is a trick question: Because the associative array  %list  stores its elements in random order, it is not clear how many times the  f oreach  loop iterates. It could be one, two, or three.</p>
        <p id="p_17798" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17798"> Exercises</p>
        <p id="p_17799" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17799"> 1. Here is one possible solution:</p>
        <p id="p_17800" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17800"> #!/usr/local/bin/perl</p>
        <p id="p_17801" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17801"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17802" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17802"> $line =~  s/ A \s+|\s+$//g;</p>
        <p id="p_17803" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17803"> ($subscript, $value) =  split(/\s+/,  $line); $array{$subscript} = $value;</p>
        <p id="p_17804" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17804"> 2. Here is one possible solution:</p>
        <p id="p_17805" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17805"> #!/usr/local/bin/perl</p>
        <p id="p_17806" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17806"> $linenum = 0;</p>
        <p id="p_17807" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17807"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17808" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17808"> $linenum += 1;</p>
        <p id="p_17809" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17809"> $line =~  s/ A \s+|\s+$//g;</p>
        <p id="p_17810" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17810"> @words = split  (/\s  +  /,  $line) ; if  ($words[0]  eq "index" &amp;&amp;</p>
        <p id="p_17811" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17811"> $index{$words[1]} eq "") {</p>
        <p id="p_17812" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17812"> $index{$words[1]} = $linenum;</p>
        <p id="p_17813" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17813"> }</p>
        <p id="p_17814" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17814"> }</p>
        <p id="p_17815" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17815"> foreach $item (sort keys (%index)) {</p>
        <p id="p_17816" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17816"> print ("$item: $index{$item}\n");</p>
        <p id="p_17817" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17817"> }</p>
        <p id="p_17818" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17818"> 3. Here is one possible solution:</p>
        <p id="p_17819" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17819"> #!/usr/local/bin/perl</p>
        <p id="p_17820" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17820"> $linenum = 0;</p>
        <p id="p_17821" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17821"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17822" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17822"> $linenum += 1;</p>
        <p id="p_17823" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17823"> $line =~  s/ A \s+|\s+$//g; @words = split  (/\s  +  /,  $line) ;</p>
        <p id="p_17824" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17824"> # This program uses a trick: for each word, the array</p>
        <p id="p_17825" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17825"> # item $index{"word"} stores the number of occurrences</p>
        <p id="p_17826" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17826"> # of that word. Each occurrence is stored in the</p>
        <p id="p_17827" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17827"> # element $index{"wordln"}, where[]is a</p>
        <p id="p_17828" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17828"> # positive integer.</p>
        <p id="p_17829" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17829"> if ($words[0] eq "index") {</p>
        <p id="p_17830" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17830"> if ($index{$words[1]} eq "") {</p>
        <p id="p_17831" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17831"> $index{$words[1]} = 1;</p>
        <p id="p_17832" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17832"> $occurrence = 1;</p>
        <p id="p_17833" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17833"> } else {</p>
        <p id="p_17834" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17834"> $index{$words[1]} += 1;</p>
        <p id="p_17835" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17835"> $occurrence = $index{$words[1]};</p>
        <p id="p_17836" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17836"> }</p>
        <p id="p_17837" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17837"> $index{$words[1$occurrence}  = $linenum;</p>
        <p id="p_17838" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17838"> }</p>
        <p id="p_17839" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17839"> }</p>
        <p id="p_17840" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17840"> # The loop that prints the index takes advantage of the fact</p>
        <p id="p_17841" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17841"> # that, when the list is sorted, the elements that count</p>
        <p id="p_17842" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17842"> # occurrences are always processed just before the</p>
        <p id="p_17843" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17843"> # corresponding elements that store occurrences. For example:</p>
        <p id="p_17844" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17844"> # $index{word}</p>
        <p id="p_17845" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17845"> # $index{word#l}</p>
        <p id="p_17846" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17846"> # $index{word#2}</p>
        <p id="p_17847" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17847"> foreach $item (sort keys (%index)) {</p>
        <p id="p_17848" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17848"> if ($item =~  /#/) {</p>
        <p id="p_17849" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17849"> print  ( "\n$item:");</p>
        <p id="p_17850" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17850"> }  else {</p>
        <p id="p_17851" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17851"> print  (" $index{$item}");</p>
        <p id="p_17852" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17852"> }</p>
        <p id="p_17853" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17853"> }</p>
        <p id="p_17854" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17854"> print  (  "\n");</p>
        <p id="p_17855" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17855"> 4. Here is one possible solution:</p>
        <p id="p_17856" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17856"> #!/usr/local/bin/perl</p>
        <p id="p_17857" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17857"> $student = 0;</p>
        <p id="p_17858" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17858"> @subjects = ("English", "history", "mathematics",</p>
        <p id="p_17859" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17859"> "science", "geography"); while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_17860" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17860"> $line   =~   s/ A \s+|\s+$//g;</p>
        <p id="p_17861" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17861"> @words = split  (/\s  +  /,  $line) ;</p>
        <p id="p_17862" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17862"> ©students[$student++]  =  $words[0]; for  ($count = 1; $count &lt;= 5; $count++)  {</p>
        <p id="p_17863" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17863"> $marks{$words[0].$subjects[$count-l]} $words[$count];</p>
        <p id="p_17864" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17864"> }</p>
        <p id="p_17865" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17865"> }</p>
        <p id="p_17866" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17866"> # now  print the failing grades, one student per line foreach $student  (sort (©students)) {</p>
        <p id="p_17867" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17867"> $has_failed = 0;</p>
        <p id="p_17868" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17868"> }</p>
        <p id="p_17869" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17869"> foreach $subject (sort (@subjects)) {</p>
        <p id="p_17870" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17870"> if ($marks{$student.$subject} &lt; 50) {</p>
        <p id="p_17871" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17871"> if ($has_failed == 0) {</p>
        <p id="p_17872" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17872"> $has_failed = 1;</p>
        <p id="p_17873" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17873"> print ("$student failed:");</p>
        <p id="p_17874" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17874"> }</p>
        <p id="p_17875" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17875"> print (" $subject");</p>
        <p id="p_17876" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17876"> }</p>
        <p id="p_17877" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17877"> }</p>
        <p id="p_17878" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17878"> if ($has_failed == 1) {</p>
        <p id="p_17879" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17879"> print (  "\n");</p>
        <p id="p_17880" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17880"> }</p>
        <p id="p_17881" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17881"> 5. This program has one problem and one unwanted feature.</p>
        <p id="p_17882" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17882"> The problem: Adding a new element to  %list  in the middle of a  foreach  loop that uses the function  keys  yields unpredictable results.</p>
        <p id="p_17883" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17883"> The unwanted feature: The  foreach  loop doubles the size of the associative array because the original elements  Fred, John, Jack,  and  Mary  are not deleted.</p>
        <p id="p_17884" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17884"> Answers for Day 11, "Formatting Your Output"</p>
        <p id="p_17885" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17885"> Quiz</p>
        <p id="p_17886" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17886"> 1. The answers are as follows:</p>
        <p id="p_17887" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17887"> 2 . @&gt;&gt;&gt;&gt;</p>
        <p id="p_17888" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17888"> 3. @|</p>
        <p id="p_17889" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17889"> 4.  @####.###</p>
        <p id="p_17890" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17890"> 5. ~~  A &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;</p>
        <p id="p_17891" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17891"> 2. The answers are as follows:</p>
        <p id="p_17892" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17892"> 1. Five left-justified characters.</p>
        <p id="p_17893" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17893"> 2. Seven centered characters.</p>
        <p id="p_17894" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17894"> 3. One character.</p>
        <p id="p_17895" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17895"> 4. Multiple (unformatted) lines of text.</p>
        <p id="p_17896" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17896"> 5. Ten right-justified characters, with the line being printed only if the line is not blank.</p>
        <p id="p_17897" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17897"> 3. The answers are as follows:</p>
        <p id="p_17898" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17898"> 1. An integer (base 10) in a field of at least five digits.</p>
        <p id="p_17899" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17899"> 2. A floating-point number with a total field width of 11 characters, four of which are to the right of the decimal point.</p>
        <p id="p_17900" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17900"> 3. A base-10 integer in a field of at least 10 digits. Empty characters in the field are filled with zeroes.</p>
        <p id="p_17901" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17901"> 4. A character string of at least 12 characters, left-justified.</p>
        <p id="p_17902" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17902"> 5. An integer in hexadecimal (base-16) form.</p>
        <p id="p_17903" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17903"> 4. Numbers with rounding problems are numbers that normally round up but cannot be exactly stored on the machine. The closest equivalent that can be stored rounds down.</p>
        <p id="p_17904" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17904"> 5. To create a page header for an output file, define a print format named  f ilename_TOP, where  filename  is the file variable associated with the file. (Or, you can create a print format of any name and assign the name to the system variable $  A .)</p>
        <p id="p_17905" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17905"> Exercises</p>
        <p id="p_17906" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17906"> 1. Here is one possible solution:</p>
        <p id="p_17907" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17907"> #!/usr/local/bin/perl</p>
        <p id="p_17908" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17908"> for ($count = 1; $count &lt;= 9; $count += 3) {</p>
        <p id="p_17909" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17909"> }</p>
        <p id="p_17910" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17910"> $numl = 2 ** 10; $num2 = $num3 = write;</p>
        <p id="p_17911" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17911"> format STDOUT</p>
        <p id="p_17912" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17912"> A &gt;&gt;&gt;  A &gt;&gt;&gt;  A &gt;&gt;&gt; $numl $num2 $num3</p>
        <p id="p_17913" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17913"> 2. Here is one possible solution:</p>
        <p id="p_17914" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17914"> #!/usr/local/bin/perl</p>
        <p id="p_17915" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17915"> for ($count = 1; $count &lt;= 10; $count++) { printf ("%4d", 2 ** $count); if ($count %  3 ==  0 )  {</p>
        <p id="p_17916" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17916"> print (  "\n");</p>
        <p id="p_17917" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17917"> } else {</p>
        <p id="p_17918" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17918"> print ( " ");</p>
        <p id="p_17919" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17919"> }</p>
        <p id="p_17920" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17920"> }</p>
        <p id="p_17921" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17921"> print  ("\n");</p>
        <p id="p_17922" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17922"> 3. Here is one possible solution:</p>
        <p id="p_17923" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17923"> #!/usr/local/bin/perl @text = &lt;STDIN&gt;;</p>
        <p id="p_17924" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17924"> $line = join("", @text); write;</p>
        <p id="p_17925" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17925"> format STDOUT</p>
        <p id="p_17926" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17926"> $line</p>
        <p id="p_17927" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17927"> ~k :k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k'k</p>
        <p id="p_17928" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17928"> 4 .  Here is one possible solution:</p>
        <p id="p_17929" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17929"> #!/usr/local/bin/perl $totall = $total2 = 0; while (1) {</p>
        <p id="p_17930" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17930"> $numl =  &lt;STDIN&gt;; last if ($numl eq  ""); chop ($numl);</p>
        <p id="p_17931" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17931"> $num2 =  &lt;STDIN&gt;; last if ($num2 eq "") ; chop ($num2);</p>
        <p id="p_17932" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17932"> $~ = "LINE"; write;</p>
        <p id="p_17933" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17933"> $totall += $numl; $total2 += $num2;</p>
        <p id="p_17934" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17934"> }</p>
        <p id="p_17935" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17935"> $~ = "TOTAL"; write;</p>
        <p id="p_17936" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17936"> $~ = "GRAND_TOTAL";</p>
        <p id="p_17937" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17937"> write;</p>
        <p id="p_17938" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17938"> format LINE</p>
        <p id="p_17939" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17939"> @####.## @####.##</p>
        <p id="p_17940" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17940"> $numl $num2</p>
        <p id="p_17941" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17941"> format TOTAL =</p>
        <p id="p_17942" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17942"> column  totals: @#####.## @#####.##</p>
        <p id="p_17943" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17943"> $totall $total2</p>
        <p id="p_17944" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17944"> format GRAND_TOTAL =</p>
        <p id="p_17945" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17945"> grand  total: @#####.##</p>
        <p id="p_17946" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17946"> $totall + $total2</p>
        <p id="p_17947" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17947"> 5. When  print  writes a line to the page, the $- variable is not automatically updated. This means that the line count is off. To fix this, subtract one from the $- variable yourself. Also, you must specify what is to be printed by the  STD OUT  print format:</p>
        <p id="p_17948" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17948"> format STDOUT =</p>
        <p id="p_17949" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17949"> $ line</p>
        <p id="p_17950" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17950"> Answers for Day 12, "Working with the File System" Quiz</p>
        <p id="p_17951" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17951"> 1. The answers are as follows:</p>
        <p id="p_17952" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17952"> 1. The  tell  function returns the current location in the file being read (the location of the next line to read).</p>
        <p id="p_17953" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17953"> 2. The mkdir function creates a directory.</p>
        <p id="p_17954" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17954"> 3.  The  link  function creates a hard link (defines a second name that refers to a particular file).</p>
        <p id="p_17955" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17955"> 4.  The  unlink  function destroys a hard link (the connection between the filename and the file). If no additional hard links have been defined (using  link), unlink  deletes the file.</p>
        <p id="p_17956" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17956"> 5. The  truncate  function reduces the size of a file to the length specified.</p>
        <p id="p_17957" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17957"> 2. 1st at  assumes that the name it is working with is a symbolic link,  stat  assumes it is working with an actual file.</p>
        <p id="p_17958" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17958"> 3. tell  retrieves the location of the next line to be read in a file,  telldir  retrieves the location of the next name to be read in a directory.</p>
        <p id="p_17959" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17959"> 4. The answers are as follows:</p>
        <p id="p_17960" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17960"> 1. f i 1 e 1  is open for reading only.</p>
        <p id="p_17961" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17961"> 2. file 2  is actually a pipe that is sending the output from a command to this program (where it is treated as input).</p>
        <p id="p_17962" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17962"> 3. f ile3  is open for reading and writing.  (+&lt;f ile3  is equivalent.)</p>
        <p id="p_17963" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17963"> 4. MYFILE is being treated as identical to STDOUT (the two file variables now refer to the same file).</p>
        <p id="p_17964" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17964"> 5. The answers are as follows:</p>
        <p id="p_17965" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17965"> 1. Read and write permissions for everybody.</p>
        <p id="p_17966" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17966"> 2. Read, write, and execute permissions for everybody.</p>
        <p id="p_17967" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17967"> 3.  Read, write, and execute permissions for the owner only.</p>
        <p id="p_17968" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17968"> 4. Read and write permissions for the owner; read permissions for everybody else.</p>
        <p id="p_17969" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17969"> Exercises</p>
        <p id="p_17970" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17970"> 1. Here is one possible solution:</p>
        <p id="p_17971" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17971"> #!/usr/local/bin/perl</p>
        <p id="p_17972" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17972"> opendir (MYDIR, "/u/jqpublic") ||</p>
        <p id="p_17973" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17973"> die ("Can't open directory"); while ($file = readdir (MYDIR)) {</p>
        <p id="p_17974" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17974"> next if ($file =~  / A \.{1,2}$| A [ A .]/); print ("$file\n");</p>
        <p id="p_17975" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17975"> }</p>
        <p id="p_17976" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17976"> closedir (MYDIR);</p>
        <p id="p_17977" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17977"> 2. Here is one possible solution:</p>
        <p id="p_17978" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17978"> #!/usr/local/bin/perl</p>
        <p id="p_17979" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17979"> $filecount</p>
        <p id="p_17980" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17980"> i;</p>
        <p id="p_17981" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17981"> &amp;print_dir ("/u/jqpublic");</p>
        <p id="p_17982" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17982"> sub print_dir {</p>
        <p id="p_17983" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17983"> local ($dirname) =</p>
        <p id="p_17984" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17984"> local ($file, $subdir, $filevar);</p>
        <p id="p_17985" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17985"> $filevar = "MYFILE" . $filecount++; opendir ($filevar, $dirname) | |</p>
        <p id="p_17986" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17986"> die ("Can't open directory");</p>
        <p id="p_17987" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17987"> # first pass: read and print file names print ("\ndirectory $dirname:\n"); while ($file = readdir ($filevar)) {</p>
        <p id="p_17988" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17988"> next if ($file eq || $file eq</p>
        <p id="p_17989" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17989"> next if (-d ($dirname . "/" . $file));</p>
        <p id="p_17990" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17990"> print ("$file\n");</p>
        <p id="p_17991" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17991"> }</p>
        <p id="p_17992" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17992"> # second pass: recursively print subdirectories rewinddir ($filevar);</p>
        <p id="p_17993" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17993"> while ($subdir = readdir ($filevar)) {</p>
        <p id="p_17994" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17994"> next unless (-d ($dirname . "/" . $subdir))</p>
        <p id="p_17995" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17995"> next if ($subdir eq || $subdir eq</p>
        <p id="p_17996" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17996"> &amp;print_dir ($dirname . "/" . $subdir);</p>
        <p id="p_17997" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17997"> }</p>
        <p id="p_17998" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17998"> closedir ($filevar);</p>
        <p id="p_17999" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_17999"> }</p>
        <p id="p_18000" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18000"> 3. Here is one possible solution:</p>
        <p id="p_18001" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18001"> #!/usr/local/bin/perl</p>
        <p id="p_18002" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18002"> opendir (MYDIR, "/u/jqpublic") ||</p>
        <p id="p_18003" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18003"> die ("Can't open directory");</p>
        <p id="p_18004" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18004"> # the following is a trick: is alphabetically less than</p>
        <p id="p_18005" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18005"> # anything we want to print, so it makes a handy</p>
        <p id="p_18006" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18006"> # initial value</p>
        <p id="p_18007" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18007"> $lastfile =  "."; until (1) {</p>
        <p id="p_18008" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18008"> rewinddir (MYDIR);</p>
        <p id="p_18009" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18009"> $currfile =</p>
        <p id="p_18010" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18010"> while ($file = readdir (MYDIR)) { next if ($file =~  / A \./); if ($file gt $lastfile &amp;&amp;</p>
        <p id="p_18011" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18011"> ($currfile eq "" || $file It $currfile)) {</p>
        <p id="p_18012" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18012"> $currfile = $file;</p>
        <p id="p_18013" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18013"> }</p>
        <p id="p_18014" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18014"> }</p>
        <p id="p_18015" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18015"> last if ($currfile eq  ""); print ( "$currfile\n");</p>
        <p id="p_18016" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18016"> $lastfile = $currfile;</p>
        <p id="p_18017" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18017"> }</p>
        <p id="p_18018" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18018"> closedir (MYDIR);</p>
        <p id="p_18019" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18019"> 4. Here is one possible solution:</p>
        <p id="p_18020" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18020"> #!/usr/local/bin/perl</p>
        <p id="p_18021" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18021"> &amp;start_hot_keys;</p>
        <p id="p_18022" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18022"> while (1) {</p>
        <p id="p_18023" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18023"> $char = getc(STDIN); last if ($char eq  "\033"); next if ($char =~  /[ A 0-9]/); print ("$digits[$char]\n" ) ;</p>
        <p id="p_18024" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18024"> }</p>
        <p id="p_18025" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18025"> &amp;end_hot_keys;</p>
        <p id="p_18026" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18026"> sub start_hot_keys {</p>
        <p id="p_18027" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18027"> system  ("stty  cbreak"); system  ("stty  -echo");</p>
        <p id="p_18028" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18028"> }</p>
        <p id="p_18029" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18029"> sub end_hot_keys {</p>
        <p id="p_18030" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18030"> system  ("stty  -cbreak"); system  ("stty  echo");</p>
        <p id="p_18031" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18031"> }</p>
        <p id="p_18032" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18032"> 5. Here is one possible solution:</p>
        <p id="p_18033" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18033"> #!/usr/local/bin/perl</p>
        <p id="p_18034" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18034"> $dir = "/u/dave/newperl/testdir"; opendir (MYDIR, $dir) ||</p>
        <p id="p_18035" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18035"> die ("Can't open directory"); chdir ($dir);</p>
        <p id="p_18036" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18036"> while ($file = readdir (MYDIR)) { next if (-d $file);</p>
        <p id="p_18037" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18037"> next if ($file eq || $file eq  "..");</p>
        <p id="p_18038" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18038"> if ($file =~  /\.pl$/)  {</p>
        <p id="p_18039" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18039"> @stat = stat ($file);</p>
        <p id="p_18040" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18040"> chmod  (($stat[2] | 0700),  $file);</p>
        <p id="p_18041" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18041"> } else {</p>
        <p id="p_18042" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18042"> chmod  (0400,  $file);</p>
        <p id="p_18043" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18043"> }</p>
        <p id="p_18044" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18044"> }</p>
        <p id="p_18045" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18045"> closedir (MYFILE);</p>
        <p id="p_18046" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18046"> 6. This program is trying to use  eof  () to test for the end of a particular input file. In Perl,</p>
        <p id="p_18047" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18047"> eof  () tests for the end of the entire set of input files, and  eof  (with no parentheses) tests for the end of a particular input file.</p>
        <p id="p_18048" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18048"> Answers for Day 13, "Process, String, and Mathematical Functions" Quiz</p>
        <p id="p_18049" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18049"> 1. The answers are as follows:</p>
        <p id="p_18050" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18050"> 1. srand  provides a seed for the random number generator.</p>
        <p id="p_18051" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18051"> 2. pipe  creates an input file variable and output file variable that are linked together. This is most frequently used by  fork  to allow processes to communicate with one another.</p>
        <p id="p_18052" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18052"> 3. atan2  calculates the arctangent for a particular value (in the range -p to p).</p>
        <p id="p_18053" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18053"> 4. sleep suspends the program for a specified number of seconds.</p>
        <p id="p_18054" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18054"> 5.  gmtime  returns the current Greenwich Mean Time (in machine-readable format).</p>
        <p id="p_18055" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18055"> 2.  fork  starts an identical copy of the program currently running,  system  starts a completely different program that runs concurrently (at the same time as the current program),  exec terminates the current program and starts a new one.</p>
        <p id="p_18056" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18056"> 3. wait  waits for any child process to terminate,  waitpid  waits for a particular child process to terminate.</p>
        <p id="p_18057" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18057"> 4. The easiest way to obtain the value of p is with the following statement:</p>
        <p id="p_18058" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18058"> $pi =  atan2(l,   1)  *  4;</p>
        <p id="p_18059" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18059"> This normally produces as close an approximation of p as is possible on your system.</p>
        <p id="p_18060" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18060"> 5. You can obtain e with this statement: $e = exp (1) ;</p>
        <p id="p_18061" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18061"> 6. The answers are as follows:</p>
        <p id="p_18062" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18062"> 1. %x</p>
        <p id="p_18063" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18063"> 2. % o</p>
        <p id="p_18064" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18064"> 3. %e</p>
        <p id="p_18065" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18065"> 4. %f</p>
        <p id="p_18066" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18066"> 7. The answers are as follows:</p>
        <p id="p_18067" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18067"> 1.  "abc"</p>
        <p id="p_18068" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18068"> 2. "efgh"</p>
        <p id="p_18069" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18069"> 3. "gh"</p>
        <p id="p_18070" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18070"> 4. The null string (a length of 0 is being specified)</p>
        <p id="p_18071" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18071"> 8. The answers are as follows:</p>
        <p id="p_18072" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18072"> 1. 1 2. -1</p>
        <p id="p_18073" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18073"> 3. 1</p>
        <p id="p_18074" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18074"> 4. 6</p>
        <p id="p_18075" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18075"> 5. 5</p>
        <p id="p_18076" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18076"> 9.  The answers are as follows:</p>
        <p id="p_18077" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18077"> 1. 4</p>
        <p id="p_18078" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18078"> 2. 9</p>
        <p id="p_18079" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18079"> Exercises</p>
        <p id="p_18080" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18080"> 1.  Here is one possible solution:</p>
        <p id="p_18081" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18081"> #!/usr/local/bin/perl</p>
        <p id="p_18082" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18082"> $child =  fork(); if ($child == 0) {</p>
        <p id="p_18083" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18083"> print ("This line goes  first\n");</p>
        <p id="p_18084" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18084"> exit  (0);</p>
        <p id="p_18085" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18085"> } else {</p>
        <p id="p_18086" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18086"> $child2 =  fork(); if  ($child2 == 0) {</p>
        <p id="p_18087" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18087"> waitpid ($child, 0);</p>
        <p id="p_18088" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18088"> print ("This line goes second\n");</p>
        <p id="p_18089" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18089"> exit  (0);</p>
        <p id="p_18090" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18090"> } else {</p>
        <p id="p_18091" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18091"> waitpid ($child2, 0);</p>
        <p id="p_18092" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18092"> print ("This line goes third\n");</p>
        <p id="p_18093" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18093"> }</p>
        <p id="p_18094" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18094"> }</p>
        <p id="p_18095" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18095"> 2. Here is a program that reads from  temp:</p>
        <p id="p_18096" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18096"> #!/usr/local/bin/perl</p>
        <p id="p_18097" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18097"> open (INFILE, "temp") || die ("Can't open input"); while ($line = &lt;INFILE&gt;) { print ($line);</p>
        <p id="p_18098" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18098"> }</p>
        <p id="p_18099" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18099"> close (INFILE);</p>
        <p id="p_18100" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18100"> Here is a program that writes to  temp  and calls the first program (which is assumed to be named  chi3.2a):</p>
        <p id="p_18101" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18101"> #!/usr/local/bin/perl</p>
        <p id="p_18102" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18102"> open (OUTFILE, "&gt;temp") || die  ("Can't  open output"); while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_18103" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18103"> print OUTFILE ($line);</p>
        <p id="p_18104" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18104"> }</p>
        <p id="p_18105" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18105"> close (OUTFILE);</p>
        <p id="p_18106" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18106"> exec ("chl3.2a");</p>
        <p id="p_18107" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18107"> 3. Here is one possible solution:</p>
        <p id="p_18108" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18108"> #!/usr/local/bin/perl</p>
        <p id="p_18109" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18109"> for ($val = 1; $val &lt;= 100; $val++) {</p>
        <p id="p_18110" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18110"> print ("log of $val is ", log($val), "\n");</p>
        <p id="p_18111" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18111"> }</p>
        <p id="p_18112" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18112"> 4. Here is one possible solution:</p>
        <p id="p_18113" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18113"> #!/usr/local/bin/perl</p>
        <p id="p_18114" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18114"> for ($i = 1; $i &lt;= 6; $i++) {</p>
        <p id="p_18115" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18115"> &amp;sum(10 ** $i) ;</p>
        <p id="p_18116" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18116"> }</p>
        <p id="p_18117" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18117"> sub sum {</p>
        <p id="p_18118" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18118"> local($limit) =</p>
        <p id="p_18119" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18119"> local(@startval, @stopval);</p>
        <p id="p_18120" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18120"> local ($i, $count);</p>
        <p id="p_18121" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18121"> $count = 0;</p>
        <p id="p_18122" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18122"> @startval = times();</p>
        <p id="p_18123" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18123"> for ($i = 1; $i &lt;= $limit; $i++) {</p>
        <p id="p_18124" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18124"> $count += $i;</p>
        <p id="p_18125" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18125"> }</p>
        <p id="p_18126" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18126"> @stopval = times();</p>
        <p id="p_18127" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18127"> print ("sum $limit: ", $stopval[0]-$startval[0], "\n");</p>
        <p id="p_18128" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18128"> }</p>
        <p id="p_18129" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18129"> 5. Here is one possible solution:</p>
        <p id="p_18130" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18130"> #!/usr/local/bin/perl</p>
        <p id="p_18131" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18131"> $degrees = &lt;STDIN&gt;; chop ($degrees);</p>
        <p id="p_18132" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18132"> $radians = $degrees * atan2(l,l) / 45;</p>
        <p id="p_18133" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18133"> $sin = sin ($radians);</p>
        <p id="p_18134" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18134"> $cos = cos ($radians);</p>
        <p id="p_18135" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18135"> print ("sin of $degrees is ", $sin, "\n");</p>
        <p id="p_18136" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18136"> print ("cos of $degrees is ", $cos, "\n");</p>
        <p id="p_18137" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18137"> print ("tan of $degrees is ", $sin/$cos, "\n");</p>
        <p id="p_18138" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18138"> 6. The output specified by the first call to  print  might get jumbled because the call to  system defines its own standard output buffers. To get around this problem, set the system variable $ | to 1 before calling  system.</p>
        <p id="p_18139" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18139"> 7. Here is one possible solution:</p>
        <p id="p_18140" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18140"> #!/usr/local/bin/perl</p>
        <p id="p_18141" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18141"> @searchletters = ("a", "e", "i", "o", "u");</p>
        <p id="p_18142" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18142"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_18143" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18143"> foreach $letter (@searchletters) {</p>
        <p id="p_18144" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18144"> printf("searching for $letter  ...\n");</p>
        <p id="p_18145" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18145"> $location = 0; while (1) {</p>
        <p id="p_18146" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18146"> $location = index ($inputline, $letter, $location);</p>
        <p id="p_18147" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18147"> last if ($location == -1);</p>
        <p id="p_18148" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18148"> print("\tfound at location $location\n")</p>
        <p id="p_18149" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18149"> $location += 1;</p>
        <p id="p_18150" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18150"> }</p>
        <p id="p_18151" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18151"> }</p>
        <p id="p_18152" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18152"> 8. Here is one possible solution:</p>
        <p id="p_18153" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18153"> #!/usr/local/bin/perl</p>
        <p id="p_18154" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18154"> @searchletters =  ("a", "e",  "i",  "o", "u");</p>
        <p id="p_18155" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18155"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_18156" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18156"> foreach $letter (@searchletters) {</p>
        <p id="p_18157" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18157"> printf("searching for $letter  ...\n");</p>
        <p id="p_18158" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18158"> $location = length ($inputline); while (1) {</p>
        <p id="p_18159" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18159"> $location = rindex ($inputline, $letter,</p>
        <p id="p_18160" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18160"> $location);</p>
        <p id="p_18161" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18161"> last if ($location == -1);</p>
        <p id="p_18162" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18162"> print("\tfound at location $location\n")</p>
        <p id="p_18163" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18163"> $location</p>
        <p id="p_18164" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18164"> i;</p>
        <p id="p_18165" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18165"> }</p>
        <p id="p_18166" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18166"> }</p>
        <p id="p_18167" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18167"> 9. Here is one possible solution:</p>
        <p id="p_18168" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18168"> #!/usr/local/bin/perl</p>
        <p id="p_18169" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18169"> @searchletters = ("a", "e", "i", "o", "u");</p>
        <p id="p_18170" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18170"> $inputline = &lt;STDIN&gt;;</p>
        <p id="p_18171" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18171"> $len = length ($inputline); foreach $letter (@searchletters) {</p>
        <p id="p_18172" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18172"> print ("searching for $letter...\n");</p>
        <p id="p_18173" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18173"> $currpos = 0;</p>
        <p id="p_18174" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18174"> while ($currpos &lt; $len) {</p>
        <p id="p_18175" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18175"> $substring = substr ($inputline, $currpos, 1); if ($letter eq $substring) {</p>
        <p id="p_18176" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18176"> print("\tfound at location $currpos\n")</p>
        <p id="p_18177" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18177"> }</p>
        <p id="p_18178" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18178"> $currpos++;</p>
        <p id="p_18179" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18179"> }</p>
        <p id="p_18180" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18180"> }</p>
        <p id="p_18181" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18181"> 10. Here is one possible solution:</p>
        <p id="p_18182" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18182"> #!/usr/local/bin/perl</p>
        <p id="p_18183" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18183"> 11. Here is one possible solution:</p>
        <p id="p_18184" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18184"> #!/usr/local/bin/perl</p>
        <p id="p_18185" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18185"> $number = &lt;STDIN&gt;;</p>
        <p id="p_18186" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18186"> if ($number =~  /\.|[eE]/)  {</p>
        <p id="p_18187" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18187"> printf ("in exponential form: %e\n", $number); printf ("in fixed-point form:  %f\n",  $number);</p>
        <p id="p_18188" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18188"> } else {</p>
        <p id="p_18189" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18189"> printf ("in decimal form: %d\n", $number); printf ("in octal form: 0%o\n", $number); printf ("in hexadecimal form: Ox%x\n", $number);</p>
        <p id="p_18190" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18190"> }</p>
        <p id="p_18191" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18191"> 12. This program goes into an infinite loop if  index  actually finds the substring  xyz.  To get around this problem, increment  $last found  (at the bottom of the loop) before calling index  again.</p>
        <p id="p_18192" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18192"> Answers for Day 14, ”Scalar-Conversion and List-Manipulation Functions”</p>
        <p id="p_18193" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18193"> Quiz</p>
        <p id="p_18194" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18194"> 1. The answers are as follows:</p>
        <p id="p_18195" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18195"> 1. A character string, padded with null characters if necessary.</p>
        <p id="p_18196" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18196"> 2. A character string, padded with blanks if necessary.</p>
        <p id="p_18197" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18197"> 3. A floating-point number (double-precision).</p>
        <p id="p_18198" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18198"> 4. A pointer to a string (as in the C programming language).</p>
        <p id="p_18199" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18199"> 5. Skip to the position specified.</p>
        <p id="p_18200" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18200"> 2. The answers are as follows:</p>
        <p id="p_18201" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18201"> 1. Unpack a character string (unstripped).</p>
        <p id="p_18202" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18202"> 2. Skip four bytes, unpack a 10-character string (stripping null characters and blanks), and then treat the rest of the packed string as integers.</p>
        <p id="p_18203" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18203"> 3. Skip to the end of the packed string, back up four bytes, and unpack four unsigned characters.</p>
        <p id="p_18204" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18204"> 4. Unpack the first integer, skip four bytes, unpack an integer, skip back eight bytes, and unpack another integer. (This, effectively, unpacks the first, third, and second integers in that order.)</p>
        <p id="p_18205" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18205"> 5. Unpack a string of bits in low-to-high order, back up to the beginning, and unpack the same string of bits in high-to-low order.</p>
        <p id="p_18206" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18206"> 3. The answers are as follows:</p>
        <p id="p_18207" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18207"> 1. 1</p>
        <p id="p_18208" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18208"> 2. 3</p>
        <p id="p_18209" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18209"> 4.  defined  tests whether a particular value is equivalent to the special "undefined" value, undef  sets a scalar variable, array element, or array variable to be equal to the special undefined value.</p>
        <p id="p_18210" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18210"> 5. The answers are as follows:</p>
        <p id="p_18211" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18211"> 1. ("new", "2", "3", "4", "5")</p>
        <p id="p_18212" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18212"> 2.  ("1", "2", "test1", "test2", "3", "4", "5")</p>
        <p id="p_18213" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18213"> 3.  ("1", "2", "3", "4", "test1 ",  "test2")</p>
        <p id="p_18214" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18214"> 4.  ("1", "2", "4", "5")</p>
        <p id="p_18215" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18215"> 5.  ("1", "2", "3")</p>
        <p id="p_18216" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18216"> 6. The answers are as follows:</p>
        <p id="p_18217" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18217"> 1. This returns every list element that does not start with an exclamation mark.</p>
        <p id="p_18218" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18218"> 2. This returns every list element that contains a word that consists entirely of digits.</p>
        <p id="p_18219" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18219"> 3. This returns every nonempty list element.</p>
        <p id="p_18220" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18220"> 4. This returns every list element.</p>
        <p id="p_18221" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18221"> 7.   unshift  adds one or more elements to the left end of a list,  shift  removes an element from the left end of a list.</p>
        <p id="p_18222" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18222"> 8. The answers are as follows:</p>
        <p id="p_18223" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18223"> 1. splice (@array, 0, 1);</p>
        <p id="p_18224" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18224"> 2. splice (@array, @array-l, 1);</p>
        <p id="p_18225" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18225"> 3.  splice (@array, scalar  ( @array) ,  0, @sublist);</p>
        <p id="p_18226" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18226"> 4.  splice (@array, 0, 0, @sublist);</p>
        <p id="p_18227" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18227"> 9. You can create a stack using  push  to add elements and  pop  to remove them (or by using shift  and  unshift  in the same way).</p>
        <p id="p_18228" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18228"> 10. You can create a queue using  push  to add elements and  shift  to remove them (or by using unshift  and  pop  in the same way).</p>
        <p id="p_18229" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18229"> Exercises</p>
        <p id="p_18230" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18230"> 1. Here is one possible solution:</p>
        <p id="p_18231" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18231"> #!/usr/local/bin/perl $ stringl = &lt;STDIN&gt;; chop ($stringl);</p>
        <p id="p_18232" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18232"> $lenl = length ($stringl);</p>
        <p id="p_18233" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18233"> $string2 = &lt;STDIN&gt;; chop ($string2);</p>
        <p id="p_18234" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18234"> $len2 = length ($string2); if ($lenl % 8 != 0) {</p>
        <p id="p_18235" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18235"> $stringl = "0" x (8 - $lenl % 8) $lenl += 8 - $lenl % 8;</p>
        <p id="p_18236" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18236"> }</p>
        <p id="p_18237" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18237"> if ($len2 % 8 != 0) {</p>
        <p id="p_18238" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18238"> $string2 = "0" x (8 - $len2 % 8) $len2 += 8 - $len2 % 8;</p>
        <p id="p_18239" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18239"> }</p>
        <p id="p_18240" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18240"> if ($lenl &gt; $len2) {</p>
        <p id="p_18241" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18241"> $string2 = "0" x ($lenl - $len2)</p>
        <p id="p_18242" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18242"> } else {</p>
        <p id="p_18243" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18243"> $stringl = "0" x ($len2 - $lenl) $lenl += ($len2 - $lenl);</p>
        <p id="p_18244" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18244"> . $stringl</p>
        <p id="p_18245" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18245"> . $string2</p>
        <p id="p_18246" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18246"> . $string2</p>
        <p id="p_18247" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18247"> . $stringl</p>
        <p id="p_18248" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18248"> $bytesl = pack ("b*", $stringl);</p>
        <p id="p_18249" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18249"> $bytes2 = pack ("b*", $string2);</p>
        <p id="p_18250" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18250"> $carry = 0;</p>
        <p id="p_18251" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18251"> $count = $lenl - 1; while ($count &gt;= 0) {</p>
        <p id="p_18252" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18252"> $bitl = vec ($bytesl, $count, 1);</p>
        <p id="p_18253" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18253"> $bit2 = vec ($bytes2, $count, 1);</p>
        <p id="p_18254" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18254"> $result = ($bitl + $bit2 + $carry) &amp; 1;</p>
        <p id="p_18255" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18255"> $carry = ($bitl + $bit2 + $carry) &gt;&gt; 1; vec ($bytesl, $count, 1) = $result;</p>
        <p id="p_18256" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18256"> $count-;</p>
        <p id="p_18257" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18257"> }</p>
        <p id="p_18258" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18258"> $resultstring = unpack ("b*", $bytesl);</p>
        <p id="p_18259" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18259"> $resultstring = $carry . $resultstring if ($carry &gt; 0) print ("$resultstring\n");</p>
        <p id="p_18260" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18260"> 2. Here is one possible solution:</p>
        <p id="p_18261" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18261"> #!/usr/local/bin/perl $ stringl = &lt;STDIN&gt;; chop ($stringl);</p>
        <p id="p_18262" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18262"> $lenl = length ($stringl);</p>
        <p id="p_18263" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18263"> $string2 = &lt;STDIN&gt;; chop ($string2);</p>
        <p id="p_18264" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18264"> $len2 = length ($string2); if ($lenl % 8 != 0) {</p>
        <p id="p_18265" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18265"> $stringl = "0" x (8 - $lenl % 8) . $stringl;</p>
        <p id="p_18266" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18266"> $lenl += 8 - $lenl % 8;</p>
        <p id="p_18267" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18267"> }</p>
        <p id="p_18268" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18268"> if ($len2 % 8 != 0) {</p>
        <p id="p_18269" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18269"> $string2 = "0" x (8 - $len2 % 8) . $string2;</p>
        <p id="p_18270" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18270"> $len2 += 8 - $len2 % 8;</p>
        <p id="p_18271" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18271"> }</p>
        <p id="p_18272" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18272"> if ($lenl &gt; $len2) {</p>
        <p id="p_18273" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18273"> $string2 = "0" x ($lenl - $len2) . $string2;</p>
        <p id="p_18274" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18274"> } else {</p>
        <p id="p_18275" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18275"> $stringl = "0" x ($len2 - $lenl) . $stringl; $lenl += ($len2 - $lenl);</p>
        <p id="p_18276" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18276"> }</p>
        <p id="p_18277" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18277"> $bytesl = pack ("h*", $stringl);</p>
        <p id="p_18278" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18278"> $bytes2 = pack ("h*", $string2);</p>
        <p id="p_18279" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18279"> $carry = 0;</p>
        <p id="p_18280" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18280"> $count = $lenl - 1; while ($count &gt;= 0) {</p>
        <p id="p_18281" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18281"> $nybblel = vec ($bytesl, $count, 4);</p>
        <p id="p_18282" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18282"> $nybble2 = vec ($bytes2, $count, 4);</p>
        <p id="p_18283" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18283"> $result = ($nybblel + $nybble2 + $carry) &amp; 15 $carry = ($nybblel + $nybble2 + $carry) &gt;&gt; 4; vec ($bytesl, $count, 4) = $result;</p>
        <p id="p_18284" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18284"> $count-;</p>
        <p id="p_18285" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18285"> }</p>
        <p id="p_18286" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18286"> $resultstring = unpack ("h*", $bytesl);</p>
        <p id="p_18287" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18287"> $resultstring = $carry . $resultstring if ($carry &gt; 0) print ( "$resultstring\n");</p>
        <p id="p_18288" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18288"> 3. Here is one possible solution:</p>
        <p id="p_18289" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18289"> #!/usr/local/bin/perl</p>
        <p id="p_18290" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18290"> $value = &lt;STDIN&gt;;</p>
        <p id="p_18291" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18291"> $value *= 100;</p>
        <p id="p_18292" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18292"> $value = int ($value + 0.5);</p>
        <p id="p_18293" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18293"> $value = sprintf  ("%.2f",  $value / 100); print ("$value\n");</p>
        <p id="p_18294" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18294"> 4. Here is one possible solution:</p>
        <p id="p_18295" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18295"> #!/usr/local/bin/perl</p>
        <p id="p_18296" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18296"> $passwd = crypt ("bluejays", "ez") ;</p>
        <p id="p_18297" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18297"> $try = 1; while (1) {</p>
        <p id="p_18298" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18298"> print ("Enter the secret password:\n" ) ; system ("stty -echo");</p>
        <p id="p_18299" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18299"> $guess = &lt;STDIN&gt;; system ("stty echo");</p>
        <p id="p_18300" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18300"> if (crypt ($guess, substr ($passwd, 0, 2)) eg $passwd) {</p>
        <p id="p_18301" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18301"> print ("Correct!\n");</p>
        <p id="p_18302" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18302"> last;</p>
        <p id="p_18303" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18303"> }</p>
        <p id="p_18304" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18304"> if ($try == 3) {</p>
        <p id="p_18305" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18305"> die ("Sorry! Goodbye!\n");</p>
        <p id="p_18306" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18306"> }</p>
        <p id="p_18307" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18307"> print ("Try again - ");</p>
        <p id="p_18308" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18308"> $try++;</p>
        <p id="p_18309" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18309"> }</p>
        <p id="p_18310" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18310"> 5. This program is actually reading the low-order bit of the bit vector. To read the high-order bit,</p>
        <p id="p_18311" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18311"> usevec ($packed, 7, 1).</p>
        <p id="p_18312" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18312"> 6. Here is one possible solution:</p>
        <p id="p_18313" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18313"> #!/usr/local/bin/perl</p>
        <p id="p_18314" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18314"> # This program uses a very dumb sorting algorithm.</p>
        <p id="p_18315" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18315"> @list = (41, 26, 11, 9, 8); # sample list to sort</p>
        <p id="p_18316" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18316"> for ($outer = 0; $outer &lt; @list; $outer++) {</p>
        <p id="p_18317" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18317"> for ($inner = 0; $inner &lt; @list; $inner++) {</p>
        <p id="p_18318" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18318"> if ($list[$inner] &gt; $list[$inner+l]) {</p>
        <p id="p_18319" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18319"> $x = splice (@list, $inner, 1); splice (@list, $inner+l, 0,  $x);</p>
        <p id="p_18320" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18320"> 7. Here is one possible solution:</p>
        <p id="p_18321" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18321"> #!/usr/local/bin/perl</p>
        <p id="p_18322" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18322"> # assume %oldarray is assigned here while (($subscript, $value) = each (%oldarray)) {</p>
        <p id="p_18323" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18323"> if (defined ($newarray{$value})) {</p>
        <p id="p_18324" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18324"> print STDERR ("$value already defined\n")</p>
        <p id="p_18325" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18325"> } else {</p>
        <p id="p_18326" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18326"> }</p>
        <p id="p_18327" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18327"> $newarray{$value} = $subscript;</p>
        <p id="p_18328" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18328"> }</p>
        <p id="p_18329" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18329"> 8. Here is one possible solution:</p>
        <p id="p_18330" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18330"> #!/usr/local/bin/perl</p>
        <p id="p_18331" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18331"> while ($line = &lt;STDIN&gt;) {</p>
        <p id="p_18332" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18332"> @words = split  (/\s  +  /,  $line) ; @shortwords = grep  (/ A .{1,5}$/,  @words); print ( "@shortwords\n");</p>
        <p id="p_18333" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18333"> }</p>
        <p id="p_18334" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18334"> 9. Here is one possible solution:</p>
        <p id="p_18335" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18335"> #!/usr/local/bin/perl</p>
        <p id="p_18336" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18336"> $line = &lt;STDIN&gt;;</p>
        <p id="p_18337" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18337"> $line</p>
        <p id="p_18338" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18338"> s/ A \s+//;</p>
        <p id="p_18339" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18339"> while (1) {</p>
        <p id="p_18340" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18340"> last if ($line eq  "");</p>
        <p id="p_18341" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18341"> ($word, $line) = split  (/\s+/,  $line, 2); print ("$word\n");</p>
        <p id="p_18342" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18342"> 10. This subroutine is trying to remove an element from a list using  unshift.  The subroutine should use  shift,  not  unshift.</p>
        <p id="p_18343" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18343"> Answers for Day 15, ’’System Functions"</p>
        <p id="p_18344" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18344"> Quiz</p>
        <p id="p_18345" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18345"> 1. The answers are as follows:</p>
        <p id="p_18346" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18346"> 1. endpwent, getpwent, getpwnam, getpwuid,  and  setpwent.</p>
        <p id="p_18347" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18347"> 2. endhostent, gethostbyaddr, gethostbyname, gethostent,  and sethostent.</p>
        <p id="p_18348" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18348"> 3. endnetent, getnetbyaddr,getnetbyname,getnetent,  and  setnetent.</p>
        <p id="p_18349" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18349"> 4. endservent, getservbyname,getservbyport,getservent,  and setservent.</p>
        <p id="p_18350" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18350"> 2.  Server processes call  socket, bind, listen,  and  accept,  in that order. Client processes call  socket, bind,  and  connect,  in that order.</p>
        <p id="p_18351" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18351"> 3.  The answers are as follows:</p>
        <p id="p_18352" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18352"> 1. getpwuid  searches for an entry in  /etc/passwd  that matches a specific user ID.</p>
        <p id="p_18353" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18353"> 2. setprotoent  rewinds the / etc/protocols  file.</p>
        <p id="p_18354" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18354"> 3. gethostbyaddr  searches the  /etc/hosts  file for a particular network (Internet) address.</p>
        <p id="p_18355" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18355"> 4. getgrent  retrieves the next entry from the  /etc/group  file.</p>
        <p id="p_18356" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18356"> 5.  get servbyport  searches the /  etc/services  file for an entry corresponding to a particular port number.</p>
        <p id="p_18357" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18357"> 4.  To send information using a socket, use an output function such as  print  or  printf,  and specify the file variable associated with the socket.</p>
        <p id="p_18358" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18358"> 5. You can obtain all the user IDs on your system by using  getpwent  to read the /etc/passwd  file. This file contains one entry per user ID, and the user ID is part of the entry.</p>
        <p id="p_18359" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18359"> Exercises</p>
        <p id="p_18360" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18360"> 1. Here is one possible solution:</p>
        <p id="p_18361" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18361"> #!/usr/local/bin/perl</p>
        <p id="p_18362" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18362"> while (($gname, $password, $groupid, $userids)</p>
        <p id="p_18363" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18363"> = getgrent()) {</p>
        <p id="p_18364" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18364"> $garray{$gname} = $userids;</p>
        <p id="p_18365" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18365"> }</p>
        <p id="p_18366" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18366"> foreach $gname (sort keys (%garray)) {</p>
        <p id="p_18367" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18367"> print ("Group $gname:\n");</p>
        <p id="p_18368" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18368"> @userids = split  (/\s+/,  $garray{$gname}); foreach $userid (sort (@userids)) { print ( "\t$userid\n");</p>
        <p id="p_18369" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18369"> }</p>
        <p id="p_18370" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18370"> }</p>
        <p id="p_18371" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18371"> 2. Here is one possible solution:</p>
        <p id="p_18372" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18372"> #!/usr/local/bin/perl</p>
        <p id="p_18373" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18373"> while (($name, $dl, $d2, $d3, $d4, $d5, $d6, $homedir)</p>
        <p id="p_18374" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18374"> getpwent()) {</p>
        <p id="p_18375" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18375"> $dirlist{$name} = $homedir;</p>
        <p id="p_18376" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18376"> }</p>
        <p id="p_18377" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18377"> foreach $name (sort keys (%dirlist)) {</p>
        <p id="p_18378" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18378"> printf ("userid %-15s has home directory %s\n", $name, $dirlist{$name});</p>
        <p id="p_18379" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18379"> }</p>
        <p id="p_18380" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18380"> 3. Here is one possible solution:</p>
        <p id="p_18381" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18381"> #!/usr/local/bin/perl</p>
        <p id="p_18382" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18382"> while (@retval = getpwent()) {</p>
        <p id="p_18383" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18383"> $retval[8] = "&lt;null&gt;" if ($retval[8] eq $shellarray{$retval[8]} += 1;</p>
        <p id="p_18384" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18384"> }</p>
        <p id="p_18385" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18385"> foreach $shell (sort count keys (%shellarray)) {</p>
        <p id="p_18386" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18386"> printf ("%-25s %5d %s\n", $shell, $shellarray{$shell}, ($shellarray{$shell} == 1 ?</p>
        <p id="p_18387" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18387"> "occurrence" : "occurrences"));</p>
        <p id="p_18388" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18388"> }</p>
        <p id="p_18389" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18389"> sub count {</p>
        <p id="p_18390" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18390"> $shellarray{$b} &lt;=&gt; $shellarray{$a};</p>
        <p id="p_18391" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18391"> }</p>
        <p id="p_18392" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18392"> 4. Here is one possible solution:</p>
        <p id="p_18393" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18393"> #!/usr/local/bin/perl</p>
        <p id="p_18394" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18394"> $otherid =  fork(); if ($otherid == 0) {</p>
        <p id="p_18395" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18395"> # child process $otherid = getppid();</p>
        <p id="p_18396" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18396"> }</p>
        <p id="p_18397" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18397"> $1=1;  # eliminate print buffers</p>
        <p id="p_18398" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18398"> print ("The process id of the other process is $otherid.\n");</p>
        <p id="p_18399" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18399"> 5. Here is one possible solution:</p>
        <p id="p_18400" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18400"> #!/usr/local/bin/perl</p>
        <p id="p_18401" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18401"> $port = 2000;</p>
        <p id="p_18402" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18402"> while (getservbyport($port, "tcp")) {</p>
        <p id="p_18403" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18403"> $port++;</p>
        <p id="p_18404" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18404"> }</p>
        <p id="p_18405" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18405"> ($dl, $d2, $prototype) = getprotobyname ("tcp");</p>
        <p id="p_18406" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18406"> # in the following, replace "silver" with the name</p>
        <p id="p_18407" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18407"> # of your machine</p>
        <p id="p_18408" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18408"> ($dl, $d2, $d3, $d4, $rawaddr) = gethostbyname ("silver");</p>
        <p id="p_18409" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18409"> $serveraddr = pack ("Sna4x8", 2, $port, $rawaddr); socket (SSOCKET, 2, 1, $prototype) || die ("No socket");</p>
        <p id="p_18410" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18410"> bind (SSOCKET, $serveraddr) || die ("Can't bind"); listen (SSOCKET, 5) || die ("Can't listen");</p>
        <p id="p_18411" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18411"> while (1) {</p>
        <p id="p_18412" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18412"> ($clientaddr = accept (SOCKET, SSOCKET))</p>
        <p id="p_18413" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18413"> || die ("Can't accept"); if  (fork()  ==  0)  {</p>
        <p id="p_18414" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18414"> select (SOCKET);</p>
        <p id="p_18415" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18415"> $1 = l;</p>
        <p id="p_18416" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18416"> open (MYFILE, "/u/jqpublic/testfile") ;</p>
        <p id="p_18417" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18417"> while ($line = &lt;MYFILE&gt;) {</p>
        <p id="p_18418" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18418"> print SOCKET ($line);</p>
        <p id="p_18419" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18419"> }</p>
        <p id="p_18420" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18420"> close (MYFILE); close (SOCKET); exit (0) ;</p>
        <p id="p_18421" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18421"> }</p>
        <p id="p_18422" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18422"> }</p>
        <p id="p_18423" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18423"> 6.  getnetent  returns an address as an array of four bytes, not as a readable address. To convert the address returned by  getnetent  to readable form, call  unpack.</p>
        <p id="p_18424" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18424"> Answers for Day 16, ”Command-Line Options”</p>
        <p id="p_18425" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18425"> Quiz</p>
        <p id="p_18426" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18426"> 1. The answers are as follows:</p>
        <p id="p_18427" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18427"> 1. The  -0  option specifies the end of file character for the input line.</p>
        <p id="p_18428" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18428"> 2. The -s option enables you to specify options for your program.</p>
        <p id="p_18429" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18429"> 3.  The  -w  option tells the Perl interpreter to warn you if it sees something that it thinks is erroneous.</p>
        <p id="p_18430" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18430"> 4. The  -x  option tells the Perl interpreter that your program is to be extracted from a file.</p>
        <p id="p_18431" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18431"> 5.  The  -n  option indicates that each line of the files specified on the command line is to be read.</p>
        <p id="p_18432" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18432"> 2. The answers are as follows:</p>
        <p id="p_18433" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18433"> 1. The input end-of-line character becomes either newline or the character specified by -1. The output end-of-line character becomes either null or the character specified by -0.</p>
        <p id="p_18434" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18434"> 2. The input end-of-line character becomes either the character specified by -1 or the character specified by - 0; if neither option has a value supplied with it, the input line character becomes null. The output end-of-line character becomes either null or the character specified by - 0.</p>
        <p id="p_18435" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18435"> 3.  The  -n  option tells the Perl interpreter to read each line of the input file, but does not explicitly tell it to write out its input. The  -i  option copies the input file to a temporary file, and then opens the input file for writing. If you do not explicitly write to the file yourself, nothing gets written to it.</p>
        <p id="p_18436" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18436"> 4. This is a trick question: It doesn't. You'll have to make sure that your Perl comments are not C</p>
        <p id="p_18437" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18437"> preprocessor commands.</p>
        <p id="p_18438" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18438"> 5. The options for the interpreter appear before the Perl program name in the command line, or in the header comment for the program. The options for the program appear after the program name.</p>
        <p id="p_18439" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18439"> Exercises</p>
        <p id="p_18440" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18440"> 1. Here is one possible solution:</p>
        <p id="p_18441" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18441"> $ perl -i -p -1072 -e testfile</p>
        <p id="p_18442" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18442"> Note that  -e  "; " indicates an empty program. (Otherwise, the Perl interpreter would assume that  testfile  was the program, not the input file.)</p>
        <p id="p_18443" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18443"> 2. Here is one possible solution:</p>
        <p id="p_18444" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18444"> $ perl -ne "print if  (/\bthe\b/);"  filel file2 ...</p>
        <p id="p_18445" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18445"> 3. Here is one possible solution:</p>
        <p id="p_18446" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18446"> $ perl -nae 'print ( "$F [  1]\n");  ' filel file2 ...</p>
        <p id="p_18447" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18447"> 4. Here is one possible solution:</p>
        <p id="p_18448" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18448"> #!/usr/local/bin/perl -s</p>
        <p id="p_18449" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18449"> print ("Hello\n") if ($H == 1);</p>
        <p id="p_18450" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18450"> print ("Goodbye\n") if ($G == 1);</p>
        <p id="p_18451" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18451"> 5. Here is one possible solution:</p>
        <p id="p_18452" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18452"> $ perl -i -pe  "tr/a-z/A-Z/;"  filel file2 ...</p>
        <p id="p_18453" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18453"> 6.  This command line wipes out all of your input files. Use the  -p  option instead of the  -n option.</p>
        <p id="p_18454" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18454"> 7. The -i option can be specified with a value (for creating a backup version of the file). The Perl interpreter thinks that pe is the suffix to append to the filename, and does not realize that these are supposed to be options. (I get tripped up by this problem all the time.)</p>
        <p id="p_18455" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18455"> Answers for Day 17, ”System Variables”</p>
        <p id="p_18456" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18456"> Quiz</p>
        <p id="p_18457" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18457"> 1. The pattern-matching operator, the substitution operator, the translation operator, the &lt; &gt; operator (if it appears in a  while  or  for  conditional expression), the  chop  function, the print  function, and the  study  function.</p>
        <p id="p_18458" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18458"> 2. The answers are as follows:</p>
        <p id="p_18459" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18459"> 1. The $= variable contains the page length of a particular output file.</p>
        <p id="p_18460" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18460"> 2. The $ / variable contains the input end-of-line character.</p>
        <p id="p_18461" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18461"> 3. The $ ? variable contains the return code returned by a command called by the  system function or enclosed in back quotes.</p>
        <p id="p_18462" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18462"> 4. The $ ! variable contains the error code generated by a system library routine.</p>
        <p id="p_18463" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18463"> 5. The variable contains the list of arguments passed to a subroutine by the calling program or calling subroutine.</p>
        <p id="p_18464" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18464"> 3.  ARGV  is the file variable used by the &lt;&gt; operator to read from the list of input files specified on the command line.  $ARGV  is the name of the current file being read by the &lt;&gt; operator. @ARGV  is the list of arguments (or files) specified on the command line.</p>
        <p id="p_18465" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18465"> 4. @ INC contains the directories to search when looking for files to be included. %INC lists the files requested by the  require  function that have already been found.</p>
        <p id="p_18466" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18466"> 5. $ 0 is the name of the program you are running. $ 1 is defined when a pattern is matched, and is the first subpattern enclosed in parentheses in the matched pattern.</p>
        <p id="p_18467" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18467"> Exercises</p>
        <p id="p_18468" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18468"> 1. Here is one possible solution:</p>
        <p id="p_18469" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18469"> #!/usr/local/bin/perl -i</p>
        <p id="p_18470" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18470"> while (&lt;&gt;) {</p>
        <p id="p_18471" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18471"> s/[  \t]+/ /g; tr/A-Z/a-z/; print;</p>
        <p id="p_18472" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18472"> }</p>
        <p id="p_18473" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18473"> All of these statements use the system variable $_ by default.</p>
        <p id="p_18474" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18474"> 2. Here is one possible solution:</p>
        <p id="p_18475" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18475"> #!/usr/local/bin/perl -i</p>
        <p id="p_18476" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18476"> while ($line = &lt;&gt;) {</p>
        <p id="p_18477" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18477"> while ($line =~ /  +/g) {</p>
        <p id="p_18478" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18478"> $line = $' . " " . $ ' ;</p>
        <p id="p_18479" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18479"> }</p>
        <p id="p_18480" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18480"> print ($line);</p>
        <p id="p_18481" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18481"> }</p>
        <p id="p_18482" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18482"> 3. Here is one possible solution:</p>
        <p id="p_18483" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18483"> #!/usr/local/bin/perl</p>
        <p id="p_18484" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18484"> @dirlist = split  (/:/,  $ENV{"PATH"}); foreach $dir (@dirlist) { print ("$dir\n");</p>
        <p id="p_18485" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18485"> }</p>
        <p id="p_18486" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18486"> Note that if your machine uses a character other than : to separate entries in the value of your PATH  environment variable, you should use this character instead.</p>
        <p id="p_18487" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18487"> 4. Here is one possible solution:</p>
        <p id="p_18488" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18488"> #!/usr/local/bin/perl</p>
        <p id="p_18489" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18489"> $ SIG{"INT"} = stopnum; $num = 1; while (1) {</p>
        <p id="p_18490" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18490"> print ("$num\n"); $num++;</p>
        <p id="p_18491" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18491"> }</p>
        <p id="p_18492" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18492"> sub stopnum {</p>
        <p id="p_18493" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18493"> print ("\nlnterrupted.\n"); exit (0) ;</p>
        <p id="p_18494" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18494"> }</p>
        <p id="p_18495" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18495"> 5. Here is one possible solution:</p>
        <p id="p_18496" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18496"> #!/usr/local/bin/perl</p>
        <p id="p_18497" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18497"> $total = 0;</p>
        <p id="p_18498" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18498"> while ($line = &lt;DATA&gt;) {</p>
        <p id="p_18499" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18499"> @nums = split  (/\s+/,  $line); foreach $num (@nums) {</p>
        <p id="p_18500" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18500"> $total += $num;</p>
        <p id="p_18501" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18501"> }</p>
        <p id="p_18502" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18502"> }</p>
        <p id="p_18503" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18503"> print ("The total is $total.\n");</p>
        <p id="p_18504" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18504"> _END_</p>
        <p id="p_18505" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18505"> 4 17 26</p>
        <p id="p_18506" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18506"> 11</p>
        <p id="p_18507" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18507"> 9 5</p>
        <p id="p_18508" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18508"> 6. The substitution operator matches a pattern, so it overwrites the value of $ '. To fix this, copy $ ' into a scalar variable of your own before searching for extra spaces.</p>
        <p id="p_18509" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18509"> Answers for Day 18, ’’References in Perl 5”</p>
        <p id="p_18510" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18510"> Quiz</p>
        <p id="p_18511" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18511"> 1. The correct way to write this is  $pointer-&gt; { $i  }. You are dereferencing more than once in the line shown in the question.</p>
        <p id="p_18512" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18512"> 2. Make the line my($a,$b) look like this: my  (\$a,\$b)</p>
        <p id="p_18513" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18513"> Then use @  $a  and @  $b  to access these arrays by reference.</p>
        <p id="p_18514" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18514"> 3. There is no difference as far as accessing the variable in $ i is concerned.</p>
        <p id="p_18515" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18515"> 4. The word  Help.</p>
        <p id="p_18516" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18516"> 5. The $ { variable  } can be used to create symbolic references. The three lines could be</p>
        <p id="p_18517" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18517"> rewritten by eliminating $ { } constructs and using the values instead.</p>
        <p id="p_18518" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18518"> Exercises</p>
        <p id="p_18519" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18519"> 1. Here is one possible solution:</p>
        <p id="p_18520" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18520"> 2. Use the code in the given hint to construct your function with one exception: you use an array for @ list.  Then you can call each function by using the index in the @ list:</p>
        <p id="p_18521" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18521"> &amp;$list[$index]();</p>
        <p id="p_18522" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18522"> 3. Hard links are maintained by Perl and have to be greater than zero for a variable to exist. Soft links can point to nothing and are created by a user program.</p>
        <p id="p_18523" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18523"> 4. Add the following lines to the end of the code:</p>
        <p id="p_18524" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18524"> printf "\n Address = $this, $that";</p>
        <p id="p_18525" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18525"> printf "\n Difference of Address = %f \n" $this - $that";</p>
        <p id="p_18526" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18526"> The addresses are not different because they point to the same function.</p>
        <p id="p_18527" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18527"> Answers for Day 19, "Object-Oriented Programming in Perl"</p>
        <p id="p_18528" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18528"> Quiz</p>
        <p id="p_18529" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18529"> 1. The following is correct:</p>
        <p id="p_18530" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18530"> Balloon: :new() ;</p>
        <p id="p_18531" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18531"> Balloon-&gt;new(); new Balloon;</p>
        <p id="p_18532" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18532"> 2. This causes a memory leak. The memory allocated for  $y  has an extra reference. The reference count for  $y  is set when  $x  is set to it. After the block of code ends, the  $y  reference count remains nonzero. As a result, the memory  $y  hangs around until the program exits.</p>
        <p id="p_18533" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18533"> 3. A class is only a package that provides methods, an object is a reference, and a method is a</p>
        <p id="p_18534" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18534"> subroutine with the first argument as the name of the class.</p>
        <p id="p_18535" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18535"> 4. Use  BaseClassName  : : explicitly in front of the function name to force Perl to use the base class.</p>
        <p id="p_18536" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18536"> Exercises</p>
        <p id="p_18537" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18537"> 1. Create a file called  Zeller,  pm like this:</p>
        <p id="p_18538" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18538"> package Zeller;</p>
        <p id="p_18539" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18539"> require Exporter;</p>
        <p id="p_18540" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18540"> 0EXPORT = (Zeller);</p>
        <p id="p_18541" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18541"> sub Zeller {</p>
        <p id="p_18542" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18542"> my ($month,$day,$year) =</p>
        <p id="p_18543" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18543"> &lt;&lt;&lt; Insert code from sample here&gt;»</p>
        <p id="p_18544" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18544"> }</p>
        <p id="p_18545" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18545"> i;</p>
        <p id="p_18546" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18546"> 2. Then use the file in your Perl script like this:</p>
        <p id="p_18547" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18547"> use Zeller;</p>
        <p id="p_18548" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18548"> $ z = Zeller(7,21, 1962);</p>
        <p id="p_18549" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18549"> print "\n Day of the week =  $z";</p>
        <p id="p_18550" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18550"> 3. Check if the number of incoming parameters is not three. Use the call to '  date +\%D  '. The answer will return in mm/dd/yy format. Split the response on ' / ' to get the month.</p>
        <p id="p_18551" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18551"> $count</p>
        <p id="p_18552" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18552"> scalar  (@_);</p>
        <p id="p_18553" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18553"> if ($count != 3) {</p>
        <p id="p_18554" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18554"> $dt = 'date +\%D';</p>
        <p id="p_18555" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18555"> ($month,$day,$year) =  split;</p>
        <p id="p_18556" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18556"> else {</p>
        <p id="p_18557" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18557"> my ($month,$day, $year) = @_;</p>
        <p id="p_18558" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18558"> }</p>
        <p id="p_18559" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18559"> $z = Zeller($month,$day,$year) ;</p>
        <p id="p_18560" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18560"> 4. Here is one possible solution:</p>
        <p id="p_18561" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18561"> #!/usr/bin/perl</p>
        <p id="p_18562" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18562"> print 'find . -depth -print ';</p>
        <p id="p_18563" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18563"> 5. Add the following lines of code to the beginning of the function:</p>
        <p id="p_18564" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18564"> if (scalar(@_) == 0) {</p>
        <p id="p_18565" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18565"> print "\n ================================= \n";</p>
        <p id="p_18566" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18566"> print " Making a black cup of coffee.</p>
        <p id="p_18567" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18567"> print "\n ================================= \n";</p>
        <p id="p_18568" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18568"> return;</p>
        <p id="p_18569" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18569"> }</p>
        <p id="p_18570" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18570"> Answers for Day 20, "Miscellaneous Features of Perl"</p>
        <p id="p_18571" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18571"> Quiz</p>
        <p id="p_18572" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18572"> 1. The answers are as follows:</p>
        <p id="p_18573" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18573"> 1.  LINE contains the current line number of the executing program or subroutine.</p>
        <p id="p_18574" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18574"> 2.  FILE contains the current file being executed.</p>
        <p id="p_18575" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18575"> 3. _END_ indicates the end of the Perl program.</p>
        <p id="p_18576" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18576"> 2. The answers are as follows:</p>
        <p id="p_18577" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18577"> 1. It's time to say $var</p>
        <p id="p_18578" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18578"> 2.  "It's time to say hello";  (including the quotes and the semicolon)</p>
        <p id="p_18579" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18579"> 3. hello</p>
        <p id="p_18580" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18580"> 3. ("one", "two", "three", "", "five")</p>
        <p id="p_18581" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18581"> 4. There are two ways:</p>
        <p id="p_18582" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18582"> o  With the  #include  preprocessor command.</p>
        <p id="p_18583" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18583"> o Adding the file's directory to  @ INC  and then passing the filename to  require.</p>
        <p id="p_18584" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18584"> Exercises</p>
        <p id="p_18585" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18585"> 1. Here is one possible solution:</p>
        <p id="p_18586" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18586"> #!/usr/local/bin/perl</p>
        <p id="p_18587" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18587"> @filelist = &lt;*&gt;;</p>
        <p id="p_18588" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18588"> foreach $file (sort (@filelist)) { print ("$file\n");</p>
        <p id="p_18589" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18589"> }</p>
        <p id="p_18590" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18590"> 2. Here is one possible solution:</p>
        <p id="p_18591" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18591"> #!/usr/local/bin/perl</p>
        <p id="p_18592" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18592"> unshift (@INC, "/u/jqpublic/perlfiles"); require ("sum.pl");</p>
        <p id="p_18593" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18593"> @numlist = &lt;STDIN&gt;; chop (@numlist);</p>
        <p id="p_18594" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18594"> $total = &amp;sum (@numlist);</p>
        <p id="p_18595" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18595"> print ("The total is $total.\n");</p>
        <p id="p_18596" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18596"> 3. Here is one possible solution:</p>
        <p id="p_18597" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18597"> #!/usr/local/bin/perl</p>
        <p id="p_18598" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18598"> package packl;</p>
        <p id="p_18599" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18599"> $var = &lt;STDIN&gt;; chop ($var); package pack2;</p>
        <p id="p_18600" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18600"> $var = &lt;STDIN&gt;; chop ($var); package main;</p>
        <p id="p_18601" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18601"> $total = $packl'var + $pack2'var; print ("The total is $total.\n");</p>
        <p id="p_18602" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18602"> 4.  In this case,  &lt;$f ilepattern&gt;  is treated as a scalar variable containing the name of a file variable, not as a scalar variable containing a file list pattern. (To obtain the latter, use</p>
        <p id="p_18603" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18603"> &lt;$ { f ilepattern } &gt;.)</p>
        <p id="p_18604" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18604"> 5. There should be no space between the &lt;&lt; and the  EOF.  The space after the &lt;&lt; means that the end-of-string character string is assumed to be null; therefore,  print  only prints the first of the two lines in the string.</p>
        <p id="p_18605" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18605"> Answers for Day 21, "The Perl Debugger" Quiz</p>
        <p id="p_18606" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18606"> 1. The answers are as follows:</p>
        <p id="p_18607" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18607"> 1. Trace mode controls whether lines are displayed as they are executed. If trace mode is on, lines are displayed; if it is off, they are not.</p>
        <p id="p_18608" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18608"> 2. A stack trace is a display of the current subroutine being executed, plus a listing of the subroutine that called this one, and so on back to the original main program.</p>
        <p id="p_18609" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18609"> 3. A breakpoint is a line in the program before which execution is halted and further debugging commands are requested.</p>
        <p id="p_18610" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18610"> 4. A line action is a statement that is executed whenever a particular line of the program is reached.</p>
        <p id="p_18611" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18611"> 2. The  X  command displays only variables in the current package. The  V  command can display</p>
        <p id="p_18612" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18612"> variables in any package.</p>
        <p id="p_18613" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18613"> 3. The / / command searches forward in the file for a line matching the specified pattern; the ? ? command searches backward.</p>
        <p id="p_18614" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18614"> 4. The &gt; command defines a line action that is to be executed before the debugger executes any further statements. The &lt; command defines a line action that is to be performed after the debugger has finished executing the next statement or group of statements.</p>
        <p id="p_18615" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18615"> 5. The  s  command steps into a subroutine when it encounters one; the  n  command executes the subroutine without stepping into it, stopping at the statement following the subroutine.</p>
        <p id="p_18616" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18616"> 6. The answers are as follows:</p>
        <p id="p_18617" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18617"> 1. This displays the next window of statements, continuing where the last 1 command left off.</p>
        <p id="p_18618" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18618"> 2. This displays just line 26.</p>
        <p id="p_18619" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18619"> 3. This displays lines 5-7.</p>
        <p id="p_18620" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18620"> 4. This displays lines 5-12.</p>
        <p id="p_18621" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18621"> 5. This displays the window of statements surrounding the current line.</p>
        <p id="p_18622" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18622"> Appendix B</p>
        <p id="p_18623" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18623"> ASCII Character Set</p>
        <p id="p_18624" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18624"> •  Page 1</p>
        <p id="p_18625" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18625"> •  Page 2</p>
        <p id="p_18626" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18626"> •  Page 3</p>
        <p id="p_18627" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18627"> •  Page 4</p>
        <p id="p_18628" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18628"> •  Page 5</p>
        <p id="p_18629" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18629"> Dec Hex Char</p>
        <p id="p_18630" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18630"> Dec Hex Char</p>
        <p id="p_18631" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18631"> Dec Hex Char</p>
        <p id="p_18632" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18632"> Dec Hex Char</p>
        <p id="p_18633" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18633"> Dec Hex Char</p>
        <p id="p_18634" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18634"> Dec Hex Char</p>
        <p id="p_18635" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18635"> Dec Hs Char</p>
        <p id="p_18636" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18636"> Dec Hcc Char</p>
      </level1>
    </bodymatter>
    <rearmatter id="rearmatter_18637">
      <level1 id="level1_18638">
        <p id="p_18639" smilref="ebookpdfteachyourselfperlin21days_daisy.smil#p_18639">End of book</p>
      </level1>
    </rearmatter>
  </book>
</dtbook>
