Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Perl 5 By Example

Appendix A -- Answers to Review Questions

Appendix A

Answers to Review Questions

Chapter 1

  1. What is the address of Perl's home page?
  2. Who was the creator of Perl?
    Larry Wall
  3. How much does Perl cost?
  4. Why are comments important to Programming?
    Comments will enable you to figure out the intent behind the mechanics of your program.

Chapter 2

  1. What are the four types of data that can be used in literals?
    Literals can be Numeric, Strings, Arrays, or Associative Arrays.
  2. What are numeric literals?
    They represent a number that your program will need to work with.
  3. How many types of string literals are there?
    Three-Single-quoted, double-quoted, and back-quoted.
  4. What is the major difference between single- and double-quoted strings?
    Double-quoted strings allow the use of variables.
  5. What are three escape sequences and what do they mean?
    Any value in this table would be correct:

Table A.1  Escape Sequences

Escape Sequences
Description or Character
Form Feed
Carriage Return
Vertical Tab
Dollar Sign
Percent Sign
Any octal byte
Any hexadecimal byte
Any Control character
Change the next character to lowercase
Change the next character to uppercase
Change the following characters to lowercase until an \E sequence is encountered
Change the following characters to uppercase until an \E sequence is encountered
Terminate the \L or \U sequence

  1. What would the following one-line program display?
    print 'dir c:\*.log';
    This would display all files with the extension of LOG on a DOS system.
  2. What is scientific notation?
    A means of displaying very large or very small numbers in a base-10 notation.
  3. How can you represent the number 64 in hexadecimal inside a double-quoted string?
  4. What is the easiest way to represent an array that includes the numbers 56 to 87?

Chapter 3

  1. What are the three basic data types that Perl uses?
    Scalar, array, and associative array.
  2. How can you determine the number of elements in an array?
    You can assign the array variable to a scalar variable.
  3. What is a namespace?
    A namespace is a way to segregate one type of name from another. The scalar variables use one namespace and array variables use another.
  4. What is the special variable $[ used for?
    The $[ variable can tell you the base array subscript.
  5. What is the special variable $" used for?
    The $" variable determines what character or string is used to separate array elements when they are printed using a double-quoted string and variable interpolation.
  6. What is the value of a variable when it is first used?
    In a numeric context, zero. In a string context, an empty string.
  7. What is an associative array?
    An associative array is an array that can use non-numbers as array subscripts. A scalar key is used as the subscript to retrieve an associated value.
  8. How can you access associative array elements?
    You use curly braces around the subscript and a $ to begin the variable name.

Chapter 4

  1. What are three arithmetic operators?
    + Addition
    - Subtraction
    * Multiplication
    / Division
  2. What does the x operator do?
    Returns the string to the left of x and repeats it the number of times listed to the right of the x.
  3. What does it mean to pre-decrement a variable?
    To reduce the value of the variable by one before it is used.
  4. What is the value of 1 ^ 1?
  5. What is the value of 1 << 3?
  6. What is the ternary operator used for?
    The ternary operator is actually a sequence of operators designed to choose between two options.
  7. Can the x operator be used with arrays?
  8. What is the precedence level of the range operator?
  9. What is the value of 2 ¥ 5 + 10?
  10. What is the value of 65 >> 1?
  11. What is the spaceship operator used for?
    Compares two values and returns a -1 for less than and a +1 for greater than.
  12. If an array was defined with ("fy".."gb"), how many elements would it have?

Chapter 5

  1. What is a parameter?
    Values that get passed to a function.
  2. What two functions are used to create variables with local scope?
    local() and my()
  3. What does parameter passing by reference mean?
    When parameters are called by reference, changing their value in the function also changes their value in the main program.
  4. What is the @_ array used for?
    To store arguments used by Perl.
  5. Do Perl variables have global or local scope by default?
  6. Why is it hard to pass two arrays to a function?
    Because passing an array will change the values in the main program as well as the function.
  7. What is the difference between variables created with local() and variables created with my()?
    The my() function creates variables that only the current procedure or code block can see. The local() function, however, creates variables that procedures called from the current procedure can see.
  8. What does the map() function do?
    This function will evaluate an expression for every element of a given array. The special variable $_ is assigned each element of the specified array before the expression is evaluated.

Chapter 6

  1. What is an expression?
    It is a sequence of literals, variables, or functions connected by one or more operators that evaluate to a single value-scalar or array.
  2. What is a statement?
    Statements are a complete unit of instruction for the computer to process.
  3. What are the four statement modifiers?
    if, unless, until, and while
  4. What are two uses for statement blocks?
    As a visual device to mark sections of code and a way to create local variables and when you temporarily need to send debugging output to a file.
  5. What can non-action statements be used for?
    These statements evaluate a value, but perform no actions.
  6. How is the if modifier different from the unless modifier?
    The if modifier will tell Perl that the expression should only be evaluated if a given condition is true. The unless modifier will evaluate an expression unless a condition is true.

Chapter 7

  1. What are the four loop keywords?
    While, until, for, and foreach.
  2. What are the four jump keywords?
    Last, next, redo, and goto.
  3. Which form of the until statement is used when the statement block needs to be executed at least once?
    The do..until loop.
  4. What will be displayed when this program executes?

    $firstVar = 5;
        if ($firstVar > 10) {
    This program will display:
  5. What is the default name of the local variable in the foreach loop?

  6. How is the next keyword different from the redo keyword?
    The next keyword is used to skip the rest of the statement block and continue to the next iteration of the loop. The redo keyword is used to restart the statement block.
  7. Why is the comma operator useful in the initialization expression of a for loop?
    Because you can use the comma operator to evaluate two expressions at once in the initialization and the increment/decrement expressions.
  8. What is the shift() function used for?
    To value a local variable and remove the first element of the parameter array from the array at the same time. If you use shift() all by itself, the value of the first element is lost.

Chapter 8

  1. What is a reference?
    A reference is a scalar value that points to a memory location that holds some type of data.
  2. How many types of references are there?
  3. What does the ref() function return if passed a non-reference as a
    The ref() function will return a string that contains the words "Non-reference."
  4. What notation is used to dereference a reference value?
    Curly braces.
  5. What is an anonymous array?
    Anonymous arrays are associative arrays that are not being assigned directly to a variable.
  6. What is a nested data structure?
    A nested data structure is a data structure that contains another data structure. Frequently, an object needs to contain another object-just like a bag can contain groceries.
  7. What will the following line of code display?

    This line of code displays:

Chapter 9

  1. What is a file handle?
    File handles are variables used to manipulate files.
  2. What is binary mode?
    In binary mode on DOS systems, line endings are read as two characters-the line feed and the carriage return. On both DOS and UNIX systems, binary mode lets you read the end of file character as regular characters with no special meaning.
  3. What is a fully qualified file name?
    The fully qualified name includes the name of the disk, the directory, and the file name.
  4. Are variables in the computer's memory considered persistent storage?
  5. What is the <> operator used for?
    The <> characters, when used together, are called the diamond operator. They tell Perl to read a line of input from the file handle inside the operators.
  6. What is the default file handle for the printf() function?
    The default file handle is STDOUT.
  7. What is the difference between the following two open statements?
    open(FILE_ONE, ">FILE_ONE.DAT");
        open(FILE_TWO, ">>FILE_TWO.DAT");
    The > character causes the file to be opened for writing and causes any existing data in the file to be lost, whereas the >> character sequence will open the file for appending-preserving the existing data.
  8. What value will the following expression return?
    This expression returns the size of the file.
  9. What is globbing?
    Using wildcard characters to find filenames.
  10. What will the following statement display?

         printf("%x", 16);
    This statement displays:


Chapter 10

  1. Can you use variable interpolation with the translation operator?
  2. What happens if the pattern is empty?
    The last pattern is used.
  3. What variable does the substitution operator use as its default?
    All regular expression operators use $_ as the default search space.
  4. Will the following line of code work?
    No. When using curly braces as alternative delimiters, the end delimiter must be }.
  5. What is the /g option of the substitution operator used for?
    The /g option forces all instances of the pattern to be substituted, not just the first.
  6. What does the \d meta-character sequence mean?
    The \d sequence is a symbolic character class that matches digits.
  7. What is the meaning of the dollar sign in the following pattern?
    The dollar sign is the beginning of the special variable $].
  8. What is a word boundary?
    A word boundary is that imaginary point between the end of a word and the next character-which is usually a space character or a punctuation mark.
  9. What will be displayed by the following program?
    $_ = 'AB AB AC';
    print m/c$/i;
    This program displays:


Chapter 11

  1. What is the syntax of the format statement?
    format FORMATNAME =
  2. What is a footer?
    Footers are used at the bottom of each page and can only consist of static text.
  3. What function is used to invoke the format statement?
    The write() function.
  4. How can you change a detail format line into a header format line?
    By changing the field lines and value lines.
  5. What is the > format character used for?
    This character indicates that the field should be right-justified.
  6. What is the $^L variable used for?
    The $^L variable holds the string that Perl writes before every report page except for the first.
  7. Can associative array variables be used in value lines?
  8. What will the following line of code do?
    select((select(ANNUAL_RPT), $^ = "REGIONAL_SALES")[0]);
    First ANNUAL RPT will be selected as the default file handle for the print and write statements and then the $~ variable will be changed to the new format name. By enclosing the two statements inside parentheses their return values will be used in an array context. Since the select function returns the value of the previous default file handle, after executing the second select() the default file handle will be restored to its previous value.

Chapter 12

  1. What is the $/ variable used for?
    This variable holds the input record separator.
  2. What file handle is used to avoid a second system call when doing two or more file tests?
    The underscore.
  3. What will the following program display?
    $_ = "The big red shoe";
    This program displays:
    The Big
  4. What variable holds the value of the last match string?
  5. What will the following program display?
    @array = (1..5);
    $" = "+";
    This program displays:
  6. What does the following program display?

    @array = ('A'..'E');

    foreach (@array) {

    $\ = "\n";
    foreach (@array) {
    This program displays:


Chapter 13

  1. Why is it important to check for errors?
    There is only one way to check for errors in any programming language. You need to test the return values of the functions that you call. Most functions return zero or false when something goes wrong. So when using a critical function like open() or sysread(), checking the return value helps to ensure that your program will work properly.
  2. How is the die() function different from the warn() function?
    The die() function is used to quit your script and display a message for the user to read. The warn() function has the same functionality that die() does except the script is not exited. This function is better suited for non-fatal messages like low memory or disk space conditions.
  3. What is the meaning of the $! special variable?
    It helps to find out what happened after an error has occurred. The $! variable can be used in either a numeric or a string context. In a numeric context it holds the current value of errno. If used in a string context, it will hold the error string associated with errno.
  4. What does the eval() function do?
    The eval() function executes its arguments as semi-isolated Perl code. First the Perl code in $code is executed and then, if an error arises, the Perl code in $code is displayed as text by the die() function.
  5. What is a signal?
    Signals are messages sent to the process running your Perl script by the operating system.
  6. What will the statement $SIG{'ABRT'} = 'IGNORE' do?
    You can cause Perl to ignore the Ctrl+c key sequence by placing this line of code near the beginning of your program.
  7. Which signal is used to trap floating point exceptions?

Chapter 14

  1. What is an object?
    Actually, "What are objects?" is a silly question because you already know what an object is. Trust your instincts. The book you are reading is an object. The knife and fork that you eat with are objects. In short, your life is filled with them.
  2. What is a class?
    The question that really needs to be asked is, "What are classes?" You see, all object oriented techniques use classes to do the real work. A class is a combination of variables and functions designed to emulate an object. However, when referring to variables in a class, object-oriented folks use the term properties. And when referring to functions in a class, the term method is used.
  3. What is a property?
    Variables in a class.
  4. What does the term polymorphism mean?
    A child class can redefine a method already defined in the parent class.
  5. Is the bless() function used to create classes?
    No. The bless() function is used to change the data type of the anonymous hash to $class or Inventory_item.
  6. What does the package keyword do?
    The package keyword is used to introduce new classes and namespaces.
  7. What can a static variable be used for?
    Static variables can be used to emulate constants-values that don't change.
  8. Why is it good to use anonymous hashes to represent objects instead of regular arrays?
    Child classes, in Perl, will not automatically inherit properties from its parents. However, using anonymous hashes totally avoids this issue because the parent constructor can be explicitly called to create the object. Then, the child can simply add entries to the anonymous hash.
  9. How can you create a function that is available to all classes in your script?
    Start a definition of the UNIVERSAL class.

Chapter 15

  1. What is a module?
    A module is a namespace defined in a file. For example, the English module is defined in the file and the Find module is defined in the file.
  2. What is the correct file extension for a module?
  3. What is a pragma?
    It turns other compiler directives on and off.
  4. What is the most important pragma and why?
    The most important pragma is strict. This pragma generates compiler errors if unsafe programming is detected.
  5. What does the END block do?
    END blocks write a message to a log file about end times for the program.
  6. What is a symbol table and how are they named?
    A symbol table, in Perl, is a hash that holds all of the names defined in a namespace. All of the variable and function names can be found there. The hash for each namespace is named after the namespace with two colons.
  7. How can you create a variable that is local to a package?
    Fully qualify your variable name in the declaration or initialization statement.

Chapter 16

  1. What is a logic error?
    Logic errors are insidious and difficult to find. For example, you might place an assignment statement inside an if statement block that belongs outside the block. Or you might have a loop that runs from 0 to 100 when it should run from 10 to 100. Accidentally deleting the 1 or not entering it in the first place is very easy.
  2. What is a compile-time error?
    Compile-time errors are also known as syntax errors and are made as you type your script into an editor.
  3. What will the D debugger command do?
    Deletes all breakpoints.
  4. What is a conditional breakpoint?
    A breakpoint that occurs only when certain criteria are met.
  5. What will the c debugger command do?
    Executes the rest of the statements in your script unless a breakpoint is found before the script ends. You can optionally use this command to create a temporary break by specifying a line number after the c.
  6. Can you invoke any function directly from the debugger command line?
  7. What is an alias?
    An alias is a way of assigning a name to a long command used over and over again.
  8. What is a common error associated with conditional expressions?
    One of the most common logic problems is using the assignment operator (=) when you should use the equality operator (==). If you are creating a conditional expression, you'll almost always use the equality operator (==).

Chapter 17

  1. What is a command-line option?
    Command-line options (also called switches) turn on and off different behaviors of the Perl interpreter.
  2. What are the two places that the switches can be specified?
    Switches can be placed either on the command line that invokes Perl or in the first line of the program.
  3. What switch should always be used?
    You should always use the -w switch to turn on the extended warning option.
  4. Which switch lets you read records that end with the ~ character instead of the newline?
    The -o switch lets you specify the record delimiter character.
  5. What two options can be used with the -n option?
    The -a and -F options can be used with the -n.
  6. How can you execute a script that someone sent you via e-mail?
    The -x option can be used to execute a script from the middle of the file.
  7. What happens if you specify both the -v and the -c options?
    The -v option displays the Perl version number and then the -c option checks the syntax of any Perl script specified on the command line.

Chapter 18

  1. What is a protocol?
    A protocol is a set of commands and responses.
  2. What is a socket?
    Sockets are the low-level links that enable Internet conversations.
  3. What is the POP?
    Post Office Protocol-incoming mail.
  4. Will client programs use the bind() function?
  5. Do newly created sockets have an address?
  6. What is a port?
    A Port is an imaginary place where incoming packets of information can arrive (just like a ship arrives at a sea port).
  7. When sending the body or text of an e-mail message, will the server respond after each line?
  8. Why shouldn't the echo service be used by Windows operating systems?
    Windows 95 (and perhaps other operating systems) can't use the SIGALRM interrupt signal. This may cause problems if you use this script on those systems because the program will wait forever when a server does not respond.
  9. What is the largest NNTP reponse in terms of bytes?

Chapter 19

  1. Is CGI a programming language?
  2. Are CGI and Java the same type of protocol?
  3. Do CGI program files need to have the write turned on?
  4. What is the first line of output for CGI programs?
    An HTTP header.
  5. What information does the HTTP_USER_AGENT contain?
    It provides the type and version of the user's Web browser.
  6. Why is CGItap a useful program?
    It is a CGI debugging aid that can help pinpoint the problem in a troubling CGI application.

Chapter 20

  1. What does the acronym HTML stand for?
    Hypertext Markup Language.
  2. What are the <H1>..</H1> set of tags used for?
    It formats text as a first level heading.
  3. What is the down side of using SSI directives?
    They are very processor intensive.
  4. Can an HTML form have two submit buttons?
  5. Why should all angle brackets be replaced in form information?
    So that the Web browser doesn't think that a new HTML tag is starting.
  6. How much text can be entered into a <TEXTAREA> input field?
  7. Can you debug a CGI script?

Chapter 21

  1. What is the access log used for?
    It records who and what accesses a specific HTML page or graphic.
  2. Does the fullName field in the log file correspond to the user's mail address?
  3. Why is the status code of the log file entry important?
    To determine if unauthorized people are trying to access secured
  4. Can you find log file analysis programs on the Internet?
  5. What good is a customized log file?
    To keep track of who executes your CGI scripts.
  6. What are two popular features of Web sites?
    The "What's New" page and the Feedback Form.
  7. What does recursion mean?
    A recursive function calls itself in order to get work done.