Student Workbook for Learning Perl by brian d foy

Student Workbook for Learning Perl by brian d foy

Author:brian d foy [brian d foy]
Language: eng
Format: epub, pdf
ISBN: 9781449328061
Publisher: O'Reilly Media
Published: 2012-01-29T16:00:00+00:00


Appendix B. Answers to Chapter 2 Exercises

Answer 2.1: Although the exact warning message differs between versions of Perl, each of the programs should output a warning. To turn these into a full program, insert the given lines into a short program such as this one then run the program to see the warning:

#!/usr/bin/perl use warnings; print;

Program 1 uses the print function, but doesn’t print anything. You should get a warning that complains about an uninitialized value. The warning message also tells you the program name (I called mine test) and the line number of the warning:

Use of uninitialized value in print at test line 3.

Program 2 adds two numbers, but doesn’t do anything with the result. That’s a pretty silly thing. Why do the work if you don’t use the result for anything?

Useless use of a constant in void context at test line 3.

Program 3 adds a number to a variable that has no value then prints the result. The warning message complains about the uninitialized value in the addition. You should also get the result of the print too; that’s the 1 after the warning:

Use of uninitialized value in addition (+) at test line 3. 1

Answer 2.2: You can use the same program you created for the Answer to Exercise 2.1. Program 1 complained about an uninitialized value. With diagnostics on, you get the same warning, but also the entry from the perldiag for that warning. The longer warning tries to explain what you might have done to cause the warning. The entry may not always talk about your exact error, but it’s usually close. Notice that the start of the longer warning has a W, meaning it’s a warning (rather than a fatal error or something else), and that the class of the warning is uninitialized:

Use of uninitialized value in print at test line 3 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.

In program 2, you added two numbers but discarded the result. Here’s part of the warning you should get:

Useless use of a constant in void context at test line 3 (#1) (W void) You did something without a side effect in a context that does nothing with the return value, such as a statement that doesn't return a value from a block, or the left side of a scalar comma operator. Very often this points not to stupidity on your part, but a failure of Perl to parse your program the way you thought it would.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.